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