Tests for module 'wcwidth'.
[gnulib.git] / tests / test-wcwidth.c
1 /* Test of wcwidth() function.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #include <config.h>
21
22 #include <wchar.h>
23
24 #include <locale.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #define ASSERT(expr) \
29   do                                                                         \
30     {                                                                        \
31       if (!(expr))                                                           \
32         {                                                                    \
33           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
34           abort ();                                                          \
35         }                                                                    \
36     }                                                                        \
37   while (0)
38
39 int
40 main ()
41 {
42   wchar_t wc;
43
44   /* Test width of ASCII characters.  */
45   for (wc = 0x20; wc < 0x7F; wc++)
46     ASSERT (wcwidth (wc) == 1);
47
48   /* Switch to an UTF-8 locale.  */
49   if (setlocale (LC_ALL, "fr_FR.UTF-8") != NULL)
50     {
51       /* Test width of ASCII characters.  */
52       for (wc = 0x20; wc < 0x7F; wc++)
53         ASSERT (wcwidth (wc) == 1);
54
55       /* Test width of some non-spacing characters.  */
56       ASSERT (wcwidth (0x0301) == 0);
57       ASSERT (wcwidth (0x05B0) == 0);
58
59       /* Test width of some format control characters.  */
60       ASSERT (wcwidth (0x200E) == 0);
61       ASSERT (wcwidth (0x2060) == 0);
62 #if 0  /* wchar_t may be only 16 bits.  */
63       ASSERT (wcwidth (0xE0001) == 0);
64       ASSERT (wcwidth (0xE0044) == 0);
65 #endif
66
67       /* Test width of some zero width characters.  */
68       ASSERT (wcwidth (0x200B) == 0);
69       ASSERT (wcwidth (0xFEFF) == 0);
70
71       /* Test width of some CJK characters.  */
72       ASSERT (wcwidth (0x3000) == 2);
73       ASSERT (wcwidth (0xB250) == 2);
74       ASSERT (wcwidth (0xFF1A) == 2);
75 #if 0  /* wchar_t may be only 16 bits.  */
76       ASSERT (wcwidth (0x20369) == 2);
77       ASSERT (wcwidth (0x2F876) == 2);
78 #endif
79     }
80
81   return 0;
82 }