Prefer <config.h> over "config.h". See autoconf doc for explanation.
[gnulib.git] / tests / test-wcwidth.c
1 /* Test of wcwidth() function.
2    Copyright (C) 2007-2008 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 3 of the License, or
7    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include <wchar.h>
22
23 #include <locale.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "localcharset.h"
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 int
42 main ()
43 {
44   wchar_t wc;
45
46   /* Test width of ASCII characters.  */
47   for (wc = 0x20; wc < 0x7F; wc++)
48     ASSERT (wcwidth (wc) == 1);
49
50   /* Switch to an UTF-8 locale.  */
51   if (setlocale (LC_ALL, "fr_FR.UTF-8") != NULL
52       /* Check whether it's really an UTF-8 locale.
53          On OpenBSD 4.0, the setlocale call succeeds only for the LC_CTYPE
54          category and therefore returns "C/fr_FR.UTF-8/C/C/C/C", but the
55          LC_CTYPE category is effectively set to an ASCII LC_CTYPE category;
56          in particular, locale_charset() returns "ASCII".  */
57       && strcmp (locale_charset (), "UTF-8") == 0)
58     {
59       /* Test width of ASCII characters.  */
60       for (wc = 0x20; wc < 0x7F; wc++)
61         ASSERT (wcwidth (wc) == 1);
62
63       /* Test width of some non-spacing characters.  */
64       ASSERT (wcwidth (0x0301) == 0);
65       ASSERT (wcwidth (0x05B0) == 0);
66
67       /* Test width of some format control characters.  */
68       ASSERT (wcwidth (0x200E) <= 0);
69       ASSERT (wcwidth (0x2060) <= 0);
70 #if 0  /* wchar_t may be only 16 bits.  */
71       ASSERT (wcwidth (0xE0001) <= 0);
72       ASSERT (wcwidth (0xE0044) <= 0);
73 #endif
74
75       /* Test width of some zero width characters.  */
76       ASSERT (wcwidth (0x200B) == 0);
77       ASSERT (wcwidth (0xFEFF) <= 0);
78
79       /* Test width of some CJK characters.  */
80       ASSERT (wcwidth (0x3000) == 2);
81       ASSERT (wcwidth (0xB250) == 2);
82       ASSERT (wcwidth (0xFF1A) == 2);
83 #if 0  /* wchar_t may be only 16 bits.  */
84       ASSERT (wcwidth (0x20369) == 2);
85       ASSERT (wcwidth (0x2F876) == 2);
86 #endif
87     }
88
89   return 0;
90 }