Update.
[platform/upstream/glibc.git] / localedata / tst-digits.c
1 /* Copyright (C) 2000 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@gnu.org>, 2000.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <ctype.h>
21 #include <langinfo.h>
22 #include <locale.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <wchar.h>
28 #include <wctype.h>
29 #include <sys/types.h>
30
31
32 #define ZERO  "\xe2\x82\x80"
33 #define ONE   "\xe2\x82\x81"
34 #define TWO   "\xe2\x82\x82"
35 #define THREE "\xe2\x82\x83"
36 #define FOUR  "\xe2\x82\x84"
37 #define FIVE  "\xe2\x82\x85"
38 #define SIX   "\xe2\x82\x86"
39 #define SEVEN "\xe2\x82\x87"
40 #define EIGHT "\xe2\x82\x88"
41 #define NINE  "\xe2\x82\x89"
42
43 static struct printf_int_test
44 {
45   int n;
46   const char *format;
47   const char *expected;
48 } printf_int_tests[] =
49 {
50   {       0, "%I'10d", "       " ZERO },
51   {       1, "%I'10d", "       " ONE },
52   {       2, "%I'10d", "       " TWO },
53   {       3, "%I'10d", "       " THREE },
54   {       4, "%I'10d", "       " FOUR },
55   {       5, "%I'10d", "       " FIVE },
56   {       6, "%I'10d", "       " SIX },
57   {       7, "%I'10d", "       " SEVEN },
58   {       8, "%I'10d", "       " EIGHT },
59   {       9, "%I'10d", "       " NINE },
60   {      11, "%I'10d", "    " ONE ONE },
61   {      12, "%I'10d", "    " ONE TWO },
62   {     123, "%I10d",  " " ONE TWO THREE },
63   {     123, "%I'10d", " " ONE TWO THREE },
64   {    1234, "%I10d",  ONE TWO THREE FOUR },
65   {    1234, "%I'10d", ONE "," TWO THREE FOUR },
66   {   12345, "%I'10d", ONE TWO "," THREE FOUR FIVE },
67   {  123456, "%I'10d", ONE TWO THREE "," FOUR FIVE SIX },
68   { 1234567, "%I'10d", ONE "," TWO THREE FOUR "," FIVE SIX SEVEN }
69 };
70
71 #define WZERO  L"\x2080"
72 #define WONE   L"\x2081"
73 #define WTWO   L"\x2082"
74 #define WTHREE L"\x2083"
75 #define WFOUR  L"\x2084"
76 #define WFIVE  L"\x2085"
77 #define WSIX   L"\x2086"
78 #define WSEVEN L"\x2087"
79 #define WEIGHT L"\x2088"
80 #define WNINE  L"\x2089"
81
82 static struct wprintf_int_test
83 {
84   int n;
85   const wchar_t *format;
86   const wchar_t *expected;
87 } wprintf_int_tests[] =
88 {
89   {       0, L"%I'10d", L"         " WZERO },
90   {       1, L"%I'10d", L"         " WONE },
91   {       2, L"%I'10d", L"         " WTWO },
92   {       3, L"%I'10d", L"         " WTHREE },
93   {       4, L"%I'10d", L"         " WFOUR },
94   {       5, L"%I'10d", L"         " WFIVE },
95   {       6, L"%I'10d", L"         " WSIX },
96   {       7, L"%I'10d", L"         " WSEVEN },
97   {       8, L"%I'10d", L"         " WEIGHT },
98   {       9, L"%I'10d", L"         " WNINE },
99   {      11, L"%I'10d", L"        " WONE WONE },
100   {      12, L"%I'10d", L"        " WONE WTWO },
101   {     123, L"%I10d",  L"       " WONE WTWO WTHREE },
102   {     123, L"%I'10d", L"       " WONE WTWO WTHREE },
103   {    1234, L"%I10d",  L"      " WONE WTWO WTHREE WFOUR },
104   {    1234, L"%I'10d", L"     " WONE L"," WTWO WTHREE WFOUR },
105   {   12345, L"%I'10d", L"    " WONE WTWO L"," WTHREE WFOUR WFIVE },
106   {  123456, L"%I'10d", L"   " WONE WTWO WTHREE L"," WFOUR WFIVE WSIX },
107   { 1234567, L"%I'10d", L" " WONE L"," WTWO WTHREE WFOUR L"," WFIVE WSIX WSEVEN }
108 };
109
110
111
112 int
113 main (void)
114 {
115   int cnt;
116   int failures;
117   int status;
118
119   if (setlocale (LC_ALL, "test7") == NULL)
120     {
121       puts ("cannot set locale `test7'");
122       exit (1);
123     }
124   printf ("CODESET = \"%s\"\n", nl_langinfo (CODESET));
125
126   /* First: printf tests.  */
127   failures = 0;
128   for (cnt = 0; cnt < sizeof (printf_int_tests) / sizeof (printf_int_tests[0]);
129        ++cnt)
130     {
131       char buf[100];
132       ssize_t n;
133
134       n = snprintf (buf, sizeof buf, printf_int_tests[cnt].format,
135                     printf_int_tests[cnt].n);
136
137       printf ("%3d: got \"%s\", expected \"%s\"",
138               cnt, buf, printf_int_tests[cnt].expected);
139
140       if (n != strlen (printf_int_tests[cnt].expected)
141           || strcmp (buf, printf_int_tests[cnt].expected) != 0)
142         {
143           puts ("  -> FAILED");
144           ++failures;
145         }
146       else
147         puts ("  -> OK");
148     }
149
150   printf ("%d failures in printf tests\n", failures);
151   status = failures != 0;
152
153   /* wprintf tests.  */
154   failures = 0;
155   for (cnt = 0;
156        cnt < sizeof (wprintf_int_tests) / sizeof (wprintf_int_tests[0]);
157        ++cnt)
158     {
159       wchar_t buf[100];
160       ssize_t n;
161
162       n = swprintf (buf, sizeof buf / sizeof (buf[0]),
163                     wprintf_int_tests[cnt].format,
164                     wprintf_int_tests[cnt].n);
165
166       printf ("%3d: got \"%ls\", expected \"%ls\"",
167               cnt, buf, wprintf_int_tests[cnt].expected);
168
169       if (n != wcslen (wprintf_int_tests[cnt].expected)
170           || wcscmp (buf, wprintf_int_tests[cnt].expected) != 0)
171         {
172           puts ("  -> FAILED");
173           ++failures;
174         }
175       else
176         puts ("  -> OK");
177     }
178
179   printf ("%d failures in wprintf tests\n", failures);
180   status = failures != 0;
181
182   /* ctype tests.  This makes sure that the multibyte chracter digit
183      representations are not handle in this table.  */
184   failures = 0;
185   for (cnt = 0; cnt < 256; ++cnt)
186     if (cnt >= '0' && cnt <= '9')
187       {
188         if (! isdigit (cnt))
189           {
190             printf ("isdigit ('%c') == 0\n", cnt);
191             ++failures;
192           }
193       }
194     else
195       {
196         if (isdigit (cnt))
197           {
198             printf ("isdigit (%d) != 0\n", cnt);
199             ++failures;
200           }
201       }
202
203   printf ("%d failures in ctype tests\n", failures);
204   status = failures != 0;
205
206   /* wctype tests.  This makes sure the second set of digits is also
207      recorded.  */
208   failures = 0;
209   for (cnt = 0; cnt < 256; ++cnt)
210     if (cnt >= '0' && cnt <= '9')
211       {
212         if (! iswdigit (cnt))
213           {
214             printf ("iswdigit (L'%c') == 0\n", cnt);
215             ++failures;
216           }
217       }
218     else
219       {
220         if (iswdigit (cnt))
221           {
222             printf ("iswdigit (%d) != 0\n", cnt);
223             ++failures;
224           }
225       }
226
227   for (cnt = 0x2070; cnt < 0x2090; ++cnt)
228     if (cnt >= 0x2080 && cnt <= 0x2089)
229       {
230         if (! iswdigit (cnt))
231           {
232             printf ("iswdigit (U%04X) == 0\n", cnt);
233             ++failures;
234           }
235       }
236     else
237       {
238         if (iswdigit (cnt))
239           {
240             printf ("iswdigit (U%04X) != 0\n", cnt);
241             ++failures;
242           }
243       }
244
245   printf ("%d failures in wctype tests\n", failures);
246   status = failures != 0;
247
248   return status;
249 }