Tests for module 'wcwidth'.
authorBruno Haible <bruno@clisp.org>
Sat, 7 Jul 2007 21:39:09 +0000 (21:39 +0000)
committerBruno Haible <bruno@clisp.org>
Sat, 7 Jul 2007 21:39:09 +0000 (21:39 +0000)
ChangeLog
modules/wcwidth-tests [new file with mode: 0644]
tests/test-wcwidth.c [new file with mode: 0644]

index 03b4ecf..dbce8d2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2007-07-07  Bruno Haible  <bruno@clisp.org>
 
+       * modules/wcwidth-tests: New file.
+       * tests/test-wcwidth.c: New file.
+
        Work around MacOS X wcwidth() bug.
        * m4/wcwidth.m4 (gl_FUNC_WCWIDTH): Test against MacOS X 10.3 bug.
        * lib/wcwidth.c: Include localcharset.h, streq.h, uniwidth.h.
diff --git a/modules/wcwidth-tests b/modules/wcwidth-tests
new file mode 100644 (file)
index 0000000..91ae1b5
--- /dev/null
@@ -0,0 +1,11 @@
+Files:
+tests/test-wcwidth.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-wcwidth
+check_PROGRAMS += test-wcwidth
+
diff --git a/tests/test-wcwidth.c b/tests/test-wcwidth.c
new file mode 100644 (file)
index 0000000..4412ecc
--- /dev/null
@@ -0,0 +1,82 @@
+/* Test of wcwidth() function.
+   Copyright (C) 2007 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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
+
+#include <config.h>
+
+#include <wchar.h>
+
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#define ASSERT(expr) \
+  do                                                                        \
+    {                                                                       \
+      if (!(expr))                                                          \
+        {                                                                   \
+          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
+          abort ();                                                         \
+        }                                                                   \
+    }                                                                       \
+  while (0)
+
+int
+main ()
+{
+  wchar_t wc;
+
+  /* Test width of ASCII characters.  */
+  for (wc = 0x20; wc < 0x7F; wc++)
+    ASSERT (wcwidth (wc) == 1);
+
+  /* Switch to an UTF-8 locale.  */
+  if (setlocale (LC_ALL, "fr_FR.UTF-8") != NULL)
+    {
+      /* Test width of ASCII characters.  */
+      for (wc = 0x20; wc < 0x7F; wc++)
+       ASSERT (wcwidth (wc) == 1);
+
+      /* Test width of some non-spacing characters.  */
+      ASSERT (wcwidth (0x0301) == 0);
+      ASSERT (wcwidth (0x05B0) == 0);
+
+      /* Test width of some format control characters.  */
+      ASSERT (wcwidth (0x200E) == 0);
+      ASSERT (wcwidth (0x2060) == 0);
+#if 0  /* wchar_t may be only 16 bits.  */
+      ASSERT (wcwidth (0xE0001) == 0);
+      ASSERT (wcwidth (0xE0044) == 0);
+#endif
+
+      /* Test width of some zero width characters.  */
+      ASSERT (wcwidth (0x200B) == 0);
+      ASSERT (wcwidth (0xFEFF) == 0);
+
+      /* Test width of some CJK characters.  */
+      ASSERT (wcwidth (0x3000) == 2);
+      ASSERT (wcwidth (0xB250) == 2);
+      ASSERT (wcwidth (0xFF1A) == 2);
+#if 0  /* wchar_t may be only 16 bits.  */
+      ASSERT (wcwidth (0x20369) == 2);
+      ASSERT (wcwidth (0x2F876) == 2);
+#endif
+    }
+
+  return 0;
+}