changeset 30633:cef1efbf5725

Tests for module 'wcsrtombs'.
author Bruno Haible <bruno@clisp.org>
date Sun, 21 Dec 2008 23:50:07 +0100
parents f6cc4d478585
children 5fc894fed43e
files ChangeLog modules/wcsrtombs-tests tests/test-wcsrtombs.c tests/test-wcsrtombs1.sh tests/test-wcsrtombs2.sh tests/test-wcsrtombs3.sh tests/test-wcsrtombs4.sh
diffstat 7 files changed, 305 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun Dec 21 23:48:19 2008 +0100
+++ b/ChangeLog	Sun Dec 21 23:50:07 2008 +0100
@@ -1,5 +1,12 @@
 2008-12-21  Bruno Haible  <bruno@clisp.org>
 
+	* modules/wcsrtombs-tests: New file.
+	* tests/test-wcsrtombs1.sh: New file.
+	* tests/test-wcsrtombs2.sh: New file.
+	* tests/test-wcsrtombs3.sh: New file.
+	* tests/test-wcsrtombs4.sh: New file.
+	* tests/test-wcsrtombs.c: New file.
+
 	New module 'wcsrtombs'.
 	* lib/wchar.in.h (wcsrtombs): New declaration.
 	* lib/wcsrtombs.c: New file.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/wcsrtombs-tests	Sun Dec 21 23:50:07 2008 +0100
@@ -0,0 +1,29 @@
+Files:
+tests/test-wcsrtombs1.sh
+tests/test-wcsrtombs2.sh
+tests/test-wcsrtombs3.sh
+tests/test-wcsrtombs4.sh
+tests/test-wcsrtombs.c
+m4/locale-fr.m4
+m4/locale-ja.m4
+m4/locale-zh.m4
+m4/codeset.m4
+
+Depends-on:
+
+configure.ac:
+gt_LOCALE_FR
+gt_LOCALE_FR_UTF8
+gt_LOCALE_JA
+gt_LOCALE_ZH_CN
+
+Makefile.am:
+TESTS += test-wcsrtombs1.sh test-wcsrtombs2.sh test-wcsrtombs3.sh test-wcsrtombs4.sh
+TESTS_ENVIRONMENT += \
+  EXEEXT='@EXEEXT@' \
+  LOCALE_FR='@LOCALE_FR@' \
+  LOCALE_FR_UTF8='@LOCALE_FR_UTF8@' \
+  LOCALE_JA='@LOCALE_JA@' \
+  LOCALE_ZH_CN='@LOCALE_ZH_CN@'
+check_PROGRAMS += test-wcsrtombs
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-wcsrtombs.c	Sun Dec 21 23:50:07 2008 +0100
@@ -0,0 +1,209 @@
+/* Test of conversion of wide string to string.
+   Copyright (C) 2008 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2008.  */
+
+#include <config.h>
+
+#include <wchar.h>
+
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define ASSERT(expr) \
+  do									     \
+    {									     \
+      if (!(expr))							     \
+        {								     \
+          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
+          fflush (stderr);						     \
+          abort ();							     \
+        }								     \
+    }									     \
+  while (0)
+
+int
+main (int argc, char *argv[])
+{
+  /* configure should already have checked that the locale is supported.  */
+  if (setlocale (LC_ALL, "") == NULL)
+    return 1;
+
+  if (argc > 1)
+    {
+      wchar_t input[10];
+      size_t n;
+      const wchar_t *src;
+      #define BUFSIZE 20
+      char buf[BUFSIZE];
+      size_t ret;
+
+      {
+	size_t i;
+	for (i = 0; i < BUFSIZE; i++)
+	  buf[i] = '_';
+      }
+
+      switch (argv[1][0])
+	{
+	case '1':
+	  /* Locale encoding is ISO-8859-1 or ISO-8859-15.  */
+	  {
+	    const char original[] = "B\374\337er"; /* "Büßer" */
+
+	    ret = mbstowcs (input, original, 10);
+	    ASSERT (ret == 5);
+
+	    for (n = 0; n < 10; n++)
+	      {
+		src = input;
+		ret = wcsrtombs (NULL, &src, n, NULL);
+		ASSERT (ret == 5);
+		ASSERT (src == input);
+
+		src = input;
+		ret = wcsrtombs (buf, &src, n, NULL);
+		ASSERT (ret == (n <= 5 ? n : 5));
+		ASSERT (src == (n <= 5 ? input + n : NULL));
+		ASSERT (memcmp (buf, original, ret) == 0);
+		if (src == NULL)
+		  ASSERT (buf[ret] == '\0');
+		ASSERT (buf[ret + (src == NULL) + 0] == '_');
+		ASSERT (buf[ret + (src == NULL) + 1] == '_');
+		ASSERT (buf[ret + (src == NULL) + 2] == '_');
+	      }
+	  }
+	  break;
+
+	case '2':
+	  /* Locale encoding is UTF-8.  */
+	  {
+	    const char original[] = "B\303\274\303\237er"; /* "Büßer" */
+
+	    ret = mbstowcs (input, original, 10);
+	    ASSERT (ret == 5);
+
+	    for (n = 0; n < 10; n++)
+	      {
+		src = input;
+		ret = wcsrtombs (NULL, &src, n, NULL);
+		ASSERT (ret == 7);
+		ASSERT (src == input);
+
+		src = input;
+		ret = wcsrtombs (buf, &src, n, NULL);
+		ASSERT (ret == (n < 1 ? n :
+				n < 3 ? 1 :
+				n < 5 ? 3 :
+				n <= 7 ? n : 7));
+		ASSERT (src == (n < 1 ? input + n :
+				n < 3 ? input + 1 :
+				n < 5 ? input + 2 :
+				n <= 7 ? input + (n - 2) : NULL));
+		ASSERT (memcmp (buf, original, ret) == 0);
+		if (src == NULL)
+		  ASSERT (buf[ret] == '\0');
+		ASSERT (buf[ret + (src == NULL) + 0] == '_');
+		ASSERT (buf[ret + (src == NULL) + 1] == '_');
+		ASSERT (buf[ret + (src == NULL) + 2] == '_');
+	      }
+	  }
+	  break;
+
+	case '3':
+	  /* Locale encoding is EUC-JP.  */
+	  {
+	    const char original[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
+
+	    ret = mbstowcs (input, original, 10);
+	    ASSERT (ret == 5);
+
+	    for (n = 0; n < 10; n++)
+	      {
+		src = input;
+		ret = wcsrtombs (NULL, &src, n, NULL);
+		ASSERT (ret == 8);
+		ASSERT (src == input);
+
+		src = input;
+		ret = wcsrtombs (buf, &src, n, NULL);
+		ASSERT (ret == (n < 1 ? n :
+				n < 3 ? 1 :
+				n < 5 ? 3 :
+				n < 7 ? 5 :
+				n <= 8 ? n : 8));
+		ASSERT (src == (n < 1 ? input + n :
+				n < 3 ? input + 1 :
+				n < 5 ? input + 2 :
+				n < 7 ? input + 3 :
+				n <= 8 ? input + (n - 3) : NULL));
+		ASSERT (memcmp (buf, original, ret) == 0);
+		if (src == NULL)
+		  ASSERT (buf[ret] == '\0');
+		ASSERT (buf[ret + (src == NULL) + 0] == '_');
+		ASSERT (buf[ret + (src == NULL) + 1] == '_');
+		ASSERT (buf[ret + (src == NULL) + 2] == '_');
+	      }
+	  }
+	  break;
+
+
+	case '4':
+	  /* Locale encoding is GB18030.  */
+	  {
+	    const char original[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
+
+	    ret = mbstowcs (input, original, 10);
+	    ASSERT (ret == 5);
+
+	    for (n = 0; n < 10; n++)
+	      {
+		src = input;
+		ret = wcsrtombs (NULL, &src, n, NULL);
+		ASSERT (ret == 9);
+		ASSERT (src == input);
+
+		src = input;
+		ret = wcsrtombs (buf, &src, n, NULL);
+		ASSERT (ret == (n < 1 ? n :
+				n < 3 ? 1 :
+				n < 7 ? 3 :
+				n <= 9 ? n : 9));
+		ASSERT (src == (n < 1 ? input + n :
+				n < 3 ? input + 1 :
+				n < 7 ? input + 2 :
+				n <= 9 ? input + (n - 4) : NULL));
+		ASSERT (memcmp (buf, original, ret) == 0);
+		if (src == NULL)
+		  ASSERT (buf[ret] == '\0');
+		ASSERT (buf[ret + (src == NULL) + 0] == '_');
+		ASSERT (buf[ret + (src == NULL) + 1] == '_');
+		ASSERT (buf[ret + (src == NULL) + 2] == '_');
+	      }
+	  }
+	  break;
+
+	default:
+	  return 1;
+	}
+
+      return 0;
+    }
+
+  return 1;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-wcsrtombs1.sh	Sun Dec 21 23:50:07 2008 +0100
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+# Test in an ISO-8859-1 or ISO-8859-15 locale.
+: ${LOCALE_FR=fr_FR}
+if test $LOCALE_FR = none; then
+  if test -f /usr/bin/localedef; then
+    echo "Skipping test: no traditional french locale is installed"
+  else
+    echo "Skipping test: no traditional french locale is supported"
+  fi
+  exit 77
+fi
+
+LC_ALL=$LOCALE_FR \
+./test-wcsrtombs${EXEEXT} 1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-wcsrtombs2.sh	Sun Dec 21 23:50:07 2008 +0100
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+# Test whether a specific UTF-8 locale is installed.
+: ${LOCALE_FR_UTF8=fr_FR.UTF-8}
+if test $LOCALE_FR_UTF8 = none; then
+  if test -f /usr/bin/localedef; then
+    echo "Skipping test: no french Unicode locale is installed"
+  else
+    echo "Skipping test: no french Unicode locale is supported"
+  fi
+  exit 77
+fi
+
+LC_ALL=$LOCALE_FR_UTF8 \
+./test-wcsrtombs${EXEEXT} 2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-wcsrtombs3.sh	Sun Dec 21 23:50:07 2008 +0100
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+# Test whether a specific EUC-JP locale is installed.
+: ${LOCALE_JA=ja_JP}
+if test $LOCALE_JA = none; then
+  if test -f /usr/bin/localedef; then
+    echo "Skipping test: no traditional japanese locale is installed"
+  else
+    echo "Skipping test: no traditional japanese locale is supported"
+  fi
+  exit 77
+fi
+
+LC_ALL=$LOCALE_JA \
+./test-wcsrtombs${EXEEXT} 3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-wcsrtombs4.sh	Sun Dec 21 23:50:07 2008 +0100
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+# Test whether a specific GB18030 locale is installed.
+: ${LOCALE_ZH_CN=zh_CN}
+if test $LOCALE_ZH_CN = none; then
+  if test -f /usr/bin/localedef; then
+    echo "Skipping test: no transitional chinese locale is installed"
+  else
+    echo "Skipping test: no transitional chinese locale is supported"
+  fi
+  exit 77
+fi
+
+LC_ALL=$LOCALE_ZH_CN \
+./test-wcsrtombs${EXEEXT} 4