78f71e0eb1d32a439ae83427687e0e3f2d49df6c
[gnulib.git] / tests / uniwidth / test-uc_width2.c
1 /* Test of uc_width() 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>, 2008.  */
18
19 #include <config.h>
20
21 #include "uniwidth.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #define ASSERT(expr) \
27   do                                                                         \
28     {                                                                        \
29       if (!(expr))                                                           \
30         {                                                                    \
31           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
32           fflush (stderr);                                                   \
33           abort ();                                                          \
34         }                                                                    \
35     }                                                                        \
36   while (0)
37
38 /* One of 0, '0', '1', 'A', '2'.  */
39 static char current_width;
40 /* The interval for which the current_width holds.  */
41 static ucs4_t current_start;
42 static ucs4_t current_end;
43
44 static void
45 finish_interval (void)
46 {
47   if (current_width != 0)
48     {
49       if (current_start == current_end)
50         printf ("%04X\t\t%c\n", (unsigned) current_start, current_width);
51       else
52         printf ("%04X..%04X\t%c\n", (unsigned) current_start,
53                 (unsigned) current_end, current_width);
54       current_width = 0;
55     }
56 }
57
58 static void
59 add_to_interval (ucs4_t uc, char width)
60 {
61   if (current_width == width && uc == current_end + 1)
62     current_end = uc;
63   else
64     {
65       finish_interval ();
66       current_width = width;
67       current_start = current_end = uc;
68     }
69 }
70
71 int
72 main ()
73 {
74   ucs4_t uc;
75
76   for (uc = 0; uc < 0x110000; uc++)
77     {
78       int w1 = uc_width (uc, "UTF-8");
79       int w2 = uc_width (uc, "GBK");
80       char width =
81         (w1 == 0 && w2 == 0 ? '0' :
82          w1 == 1 && w2 == 1 ? '1' :
83          w1 == 1 && w2 == 2 ? 'A' :
84          w1 == 2 && w2 == 2 ? '2' :
85          0);
86       if (width == 0)
87         {
88           /* uc must be a control character.  */
89           ASSERT (w1 < 0 && w2 < 0);
90         }
91       else
92         add_to_interval (uc, width);
93     }
94   finish_interval ();
95
96   return 0;
97 }