]>
Commit | Line | Data |
---|---|---|
d614a753 | 1 | /* Copyright (C) 2000-2020 Free Software Foundation, Inc. |
9bbc1ecc UD |
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 | |
41bdb6e2 AJ |
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. | |
9bbc1ecc UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
9bbc1ecc | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
9bbc1ecc UD |
18 | |
19 | #include <ctype.h> | |
20 | #include <locale.h> | |
0f283ffc | 21 | #include <langinfo.h> |
9bbc1ecc | 22 | #include <stdio.h> |
4edc30e4 | 23 | #include <string.h> |
9bbc1ecc UD |
24 | |
25 | ||
26 | static const char lower[] = "abcdefghijklmnopqrstuvwxyz"; | |
27 | static const char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
28 | static const char digits[] = "0123456789"; | |
15396608 UD |
29 | static const char cntrl[] = "\ |
30 | \x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\ | |
31 | \x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f "; | |
9bbc1ecc UD |
32 | |
33 | ||
4edc30e4 UD |
34 | static struct classes |
35 | { | |
36 | const char *name; | |
37 | int mask; | |
38 | } classes[] = | |
39 | { | |
40 | #define ENTRY(name) { #name, _IS##name } | |
41 | ENTRY (upper), | |
42 | ENTRY (lower), | |
43 | ENTRY (alpha), | |
44 | ENTRY (digit), | |
45 | ENTRY (xdigit), | |
46 | ENTRY (space), | |
47 | ENTRY (print), | |
48 | ENTRY (graph), | |
49 | ENTRY (blank), | |
50 | ENTRY (cntrl), | |
51 | ENTRY (punct), | |
52 | ENTRY (alnum) | |
53 | }; | |
54 | #define nclasses (sizeof (classes) / sizeof (classes[0])) | |
55 | ||
56 | ||
9bbc1ecc UD |
57 | #define FAIL(str, args...) \ |
58 | { \ | |
59 | printf (" " str "\n", ##args); \ | |
60 | ++errors; \ | |
61 | } | |
62 | ||
63 | ||
29955b5d AS |
64 | static int |
65 | do_test (void) | |
9bbc1ecc UD |
66 | { |
67 | const char *cp; | |
68 | const char *cp2; | |
69 | int errors = 0; | |
4edc30e4 UD |
70 | char *inpline = NULL; |
71 | size_t inplinelen = 0; | |
72 | char *resline = NULL; | |
73 | size_t reslinelen = 0; | |
29514aad | 74 | size_t n; |
0f283ffc | 75 | const unsigned short int *__ctype_b; |
9bbc1ecc UD |
76 | |
77 | setlocale (LC_ALL, ""); | |
78 | ||
79 | printf ("Testing the ctype data of the `%s' locale\n", | |
80 | setlocale (LC_CTYPE, NULL)); | |
81 | ||
0f283ffc RM |
82 | __ctype_b = ((const unsigned short *) nl_langinfo (_NL_CTYPE_CLASS)) + 128; |
83 | ||
9bbc1ecc UD |
84 | #if 0 |
85 | /* Just for debugging. */ | |
4edc30e4 UD |
86 | |
87 | /* Contents of the class array. */ | |
88 | printf ("\ | |
89 | upper = %04x lower = %04x alpha = %04x digit = %04x xdigit = %04x\n\ | |
90 | space = %04x print = %04x graph = %04x blank = %04x cntrl = %04x\n\ | |
91 | punct = %04x alnum = %04x\n", | |
92 | _ISupper, _ISlower, _ISalpha, _ISdigit, _ISxdigit, | |
93 | _ISspace, _ISprint, _ISgraph, _ISblank, _IScntrl, | |
94 | _ISpunct, _ISalnum); | |
95 | ||
96 | while (n < 256) | |
97 | { | |
98 | if (n % 8 == 0) | |
99 | printf ("%02x: ", n); | |
100 | printf ("%04x%s", __ctype_b[n], (n + 1) % 8 == 0 ? "\n" : " "); | |
101 | ++n; | |
102 | } | |
9bbc1ecc UD |
103 | #endif |
104 | ||
105 | puts (" Test of ASCII character range\n special NUL byte handling"); | |
106 | if (isupper ('\0')) | |
107 | FAIL ("isupper ('\\0') is true"); | |
108 | if (islower ('\0')) | |
109 | FAIL ("islower ('\\0') is true"); | |
110 | if (isalpha ('\0')) | |
111 | FAIL ("isalpha ('\\0') is true"); | |
112 | if (isdigit ('\0')) | |
113 | FAIL ("isdigit ('\\0') is true"); | |
114 | if (isxdigit ('\0')) | |
115 | FAIL ("isxdigit ('\\0') is true"); | |
116 | if (isspace ('\0')) | |
117 | FAIL ("isspace ('\\0') is true"); | |
118 | if (isprint ('\0')) | |
119 | FAIL ("isprint ('\\0') is true"); | |
120 | if (isgraph ('\0')) | |
121 | FAIL ("isgraph ('\\0') is true"); | |
122 | if (isblank ('\0')) | |
123 | FAIL ("isblank ('\\0') is true"); | |
74238b8e UD |
124 | if (! iscntrl ('\0')) |
125 | FAIL ("iscntrl ('\\0') not true"); | |
9bbc1ecc UD |
126 | if (ispunct ('\0')) |
127 | FAIL ("ispunct ('\\0') is true"); | |
128 | if (isalnum ('\0')) | |
129 | FAIL ("isalnum ('\\0') is true"); | |
130 | ||
131 | puts (" islower()"); | |
132 | for (cp = lower; *cp != '\0'; ++cp) | |
133 | if (! islower (*cp)) | |
134 | FAIL ("islower ('%c') not true", *cp); | |
135 | for (cp = upper; *cp != '\0'; ++cp) | |
136 | if (islower (*cp)) | |
137 | FAIL ("islower ('%c') is true", *cp); | |
138 | for (cp = digits; *cp != '\0'; ++cp) | |
139 | if (islower (*cp)) | |
140 | FAIL ("islower ('%c') is true", *cp); | |
15396608 UD |
141 | for (cp = cntrl; *cp != '\0'; ++cp) |
142 | if (islower (*cp)) | |
143 | FAIL ("islower ('\\x%02x') is true", *cp); | |
9bbc1ecc UD |
144 | |
145 | puts (" isupper()"); | |
146 | for (cp = lower; *cp != '\0'; ++cp) | |
147 | if (isupper (*cp)) | |
148 | FAIL ("isupper ('%c') is true", *cp); | |
149 | for (cp = upper; *cp != '\0'; ++cp) | |
150 | if (! isupper (*cp)) | |
151 | FAIL ("isupper ('%c') not true", *cp); | |
152 | for (cp = digits; *cp != '\0'; ++cp) | |
153 | if (isupper (*cp)) | |
154 | FAIL ("isupper ('%c') is true", *cp); | |
15396608 UD |
155 | for (cp = cntrl; *cp != '\0'; ++cp) |
156 | if (isupper (*cp)) | |
157 | FAIL ("isupper ('\\x%02x') is true", *cp); | |
9bbc1ecc UD |
158 | |
159 | puts (" isalpha()"); | |
160 | for (cp = lower; *cp != '\0'; ++cp) | |
161 | if (! isalpha (*cp)) | |
162 | FAIL ("isalpha ('%c') not true", *cp); | |
163 | for (cp = upper; *cp != '\0'; ++cp) | |
164 | if (! isalpha (*cp)) | |
165 | FAIL ("isalpha ('%c') not true", *cp); | |
166 | for (cp = digits; *cp != '\0'; ++cp) | |
167 | if (isalpha (*cp)) | |
168 | FAIL ("isalpha ('%c') is true", *cp); | |
15396608 UD |
169 | for (cp = cntrl; *cp != '\0'; ++cp) |
170 | if (isalpha (*cp)) | |
171 | FAIL ("isalpha ('\\x%02x') is true", *cp); | |
9bbc1ecc UD |
172 | |
173 | puts (" isdigit()"); | |
174 | for (cp = lower; *cp != '\0'; ++cp) | |
175 | if (isdigit (*cp)) | |
176 | FAIL ("isdigit ('%c') is true", *cp); | |
177 | for (cp = upper; *cp != '\0'; ++cp) | |
178 | if (isdigit (*cp)) | |
179 | FAIL ("isdigit ('%c') is true", *cp); | |
180 | for (cp = digits; *cp != '\0'; ++cp) | |
181 | if (! isdigit (*cp)) | |
182 | FAIL ("isdigit ('%c') not true", *cp); | |
15396608 UD |
183 | for (cp = cntrl; *cp != '\0'; ++cp) |
184 | if (isdigit (*cp)) | |
185 | FAIL ("isdigit ('\\x%02x') is true", *cp); | |
9bbc1ecc UD |
186 | |
187 | puts (" isxdigit()"); | |
188 | for (cp = lower; *cp != '\0'; ++cp) | |
189 | if ((! isxdigit (*cp) && cp - lower < 6) | |
190 | || (isxdigit (*cp) && cp - lower >= 6)) | |
191 | FAIL ("isxdigit ('%c') %s true", *cp, cp - upper < 6 ? "not" : "is"); | |
192 | for (cp = upper; *cp != '\0'; ++cp) | |
193 | if ((! isxdigit (*cp) && cp - upper < 6) | |
194 | || (isxdigit (*cp) && cp - upper >= 6)) | |
195 | FAIL ("isxdigit ('%c') %s true", *cp, cp - upper < 6 ? "not" : "is"); | |
196 | for (cp = digits; *cp != '\0'; ++cp) | |
197 | if (! isxdigit (*cp)) | |
198 | FAIL ("isxdigit ('%c') not true", *cp); | |
15396608 UD |
199 | for (cp = cntrl; *cp != '\0'; ++cp) |
200 | if (isxdigit (*cp)) | |
201 | FAIL ("isxdigit ('\\x%02x') is true", *cp); | |
9bbc1ecc UD |
202 | |
203 | puts (" isspace()"); | |
204 | for (cp = lower; *cp != '\0'; ++cp) | |
205 | if (isspace (*cp)) | |
206 | FAIL ("isspace ('%c') is true", *cp); | |
207 | for (cp = upper; *cp != '\0'; ++cp) | |
208 | if (isspace (*cp)) | |
209 | FAIL ("isspace ('%c') is true", *cp); | |
210 | for (cp = digits; *cp != '\0'; ++cp) | |
211 | if (isspace (*cp)) | |
212 | FAIL ("isspace ('%c') is true", *cp); | |
15396608 UD |
213 | for (cp = cntrl; *cp != '\0'; ++cp) |
214 | if ((isspace (*cp) && ((*cp < '\x09' || *cp > '\x0d') && *cp != ' ')) | |
215 | || (! isspace (*cp) | |
216 | && ((*cp >= '\x09' && *cp <= '\x0d') || *cp == ' '))) | |
217 | FAIL ("isspace ('\\x%02x') %s true", *cp, | |
218 | (*cp < '\x09' || *cp > '\x0d') ? "is" : "not"); | |
9bbc1ecc UD |
219 | |
220 | puts (" isprint()"); | |
221 | for (cp = lower; *cp != '\0'; ++cp) | |
222 | if (! isprint (*cp)) | |
223 | FAIL ("isprint ('%c') not true", *cp); | |
224 | for (cp = upper; *cp != '\0'; ++cp) | |
225 | if (! isprint (*cp)) | |
226 | FAIL ("isprint ('%c') not true", *cp); | |
227 | for (cp = digits; *cp != '\0'; ++cp) | |
228 | if (! isprint (*cp)) | |
229 | FAIL ("isprint ('%c') not true", *cp); | |
15396608 UD |
230 | for (cp = cntrl; *cp != '\0'; ++cp) |
231 | if ((isprint (*cp) && *cp != ' ') | |
232 | || (! isprint (*cp) && *cp == ' ')) | |
233 | FAIL ("isprint ('\\x%02x') is true", *cp); | |
9bbc1ecc UD |
234 | |
235 | puts (" isgraph()"); | |
236 | for (cp = lower; *cp != '\0'; ++cp) | |
237 | if (! isgraph (*cp)) | |
238 | FAIL ("isgraph ('%c') not true", *cp); | |
239 | for (cp = upper; *cp != '\0'; ++cp) | |
240 | if (! isgraph (*cp)) | |
241 | FAIL ("isgraph ('%c') not true", *cp); | |
242 | for (cp = digits; *cp != '\0'; ++cp) | |
243 | if (! isgraph (*cp)) | |
244 | FAIL ("isgraph ('%c') not true", *cp); | |
15396608 UD |
245 | for (cp = cntrl; *cp != '\0'; ++cp) |
246 | if (isgraph (*cp)) | |
247 | FAIL ("isgraph ('\\x%02x') is true", *cp); | |
9bbc1ecc UD |
248 | |
249 | puts (" isblank()"); | |
250 | for (cp = lower; *cp != '\0'; ++cp) | |
251 | if (isblank (*cp)) | |
252 | FAIL ("isblank ('%c') is true", *cp); | |
253 | for (cp = upper; *cp != '\0'; ++cp) | |
254 | if (isblank (*cp)) | |
255 | FAIL ("isblank ('%c') is true", *cp); | |
256 | for (cp = digits; *cp != '\0'; ++cp) | |
257 | if (isblank (*cp)) | |
258 | FAIL ("isblank ('%c') is true", *cp); | |
15396608 UD |
259 | for (cp = cntrl; *cp != '\0'; ++cp) |
260 | if ((isblank (*cp) && *cp != '\x09' && *cp != ' ') | |
261 | || (! isblank (*cp) && (*cp == '\x09' || *cp == ' '))) | |
262 | FAIL ("isblank ('\\x%02x') %s true", *cp, *cp != '\x09' ? "is" : "not"); | |
9bbc1ecc UD |
263 | |
264 | puts (" iscntrl()"); | |
265 | for (cp = lower; *cp != '\0'; ++cp) | |
266 | if (iscntrl (*cp)) | |
267 | FAIL ("iscntrl ('%c') is true", *cp); | |
268 | for (cp = upper; *cp != '\0'; ++cp) | |
269 | if (iscntrl (*cp)) | |
270 | FAIL ("iscntrl ('%c') is true", *cp); | |
271 | for (cp = digits; *cp != '\0'; ++cp) | |
272 | if (iscntrl (*cp)) | |
273 | FAIL ("iscntrl ('%c') is true", *cp); | |
15396608 UD |
274 | for (cp = cntrl; *cp != '\0'; ++cp) |
275 | if ((iscntrl (*cp) && *cp == ' ') | |
276 | || (! iscntrl (*cp) && *cp != ' ')) | |
277 | FAIL ("iscntrl ('\\x%02x') not true", *cp); | |
9bbc1ecc UD |
278 | |
279 | puts (" ispunct()"); | |
280 | for (cp = lower; *cp != '\0'; ++cp) | |
281 | if (ispunct (*cp)) | |
282 | FAIL ("ispunct ('%c') is true", *cp); | |
283 | for (cp = upper; *cp != '\0'; ++cp) | |
284 | if (ispunct (*cp)) | |
285 | FAIL ("ispunct ('%c') is true", *cp); | |
286 | for (cp = digits; *cp != '\0'; ++cp) | |
287 | if (ispunct (*cp)) | |
288 | FAIL ("ispunct ('%c') is true", *cp); | |
15396608 UD |
289 | for (cp = cntrl; *cp != '\0'; ++cp) |
290 | if (ispunct (*cp)) | |
291 | FAIL ("ispunct ('\\x%02x') is true", *cp); | |
9bbc1ecc UD |
292 | |
293 | puts (" isalnum()"); | |
294 | for (cp = lower; *cp != '\0'; ++cp) | |
295 | if (! isalnum (*cp)) | |
296 | FAIL ("isalnum ('%c') not true", *cp); | |
297 | for (cp = upper; *cp != '\0'; ++cp) | |
298 | if (! isalnum (*cp)) | |
299 | FAIL ("isalnum ('%c') not true", *cp); | |
300 | for (cp = digits; *cp != '\0'; ++cp) | |
301 | if (! isalnum (*cp)) | |
302 | FAIL ("isalnum ('%c') not true", *cp); | |
15396608 UD |
303 | for (cp = cntrl; *cp != '\0'; ++cp) |
304 | if (isalnum (*cp)) | |
305 | FAIL ("isalnum ('\\x%02x') is true", *cp); | |
9bbc1ecc UD |
306 | |
307 | ||
308 | puts (" tolower()"); | |
309 | for (cp = lower; *cp != '\0'; ++cp) | |
310 | if (tolower (*cp) != *cp) | |
311 | FAIL ("tolower ('%c') != '%c'", *cp, *cp); | |
312 | for (cp = upper, cp2 = lower; *cp != '\0'; ++cp, ++cp2) | |
313 | if (tolower (*cp) != *cp2) | |
314 | FAIL ("tolower ('%c') != '%c'", *cp, *cp2); | |
315 | for (cp = digits; *cp != '\0'; ++cp) | |
316 | if (tolower (*cp) != *cp) | |
317 | FAIL ("tolower ('%c') != '%c'", *cp, *cp); | |
15396608 UD |
318 | for (cp = cntrl; *cp != '\0'; ++cp) |
319 | if (tolower (*cp) != *cp) | |
320 | FAIL ("tolower ('\\x%02x') != '\\x%02x'", *cp, *cp); | |
9bbc1ecc UD |
321 | |
322 | puts (" toupper()"); | |
323 | for (cp = lower, cp2 = upper; *cp != '\0'; ++cp, ++cp2) | |
324 | if (toupper (*cp) != *cp2) | |
325 | FAIL ("toupper ('%c') != '%c'", *cp, *cp2); | |
326 | for (cp = upper; *cp != '\0'; ++cp) | |
327 | if (toupper (*cp) != *cp) | |
328 | FAIL ("toupper ('%c') != '%c'", *cp, *cp); | |
329 | for (cp = digits; *cp != '\0'; ++cp) | |
330 | if (toupper (*cp) != *cp) | |
331 | FAIL ("toupper ('%c') != '%c'", *cp, *cp); | |
15396608 UD |
332 | for (cp = cntrl; *cp != '\0'; ++cp) |
333 | if (toupper (*cp) != *cp) | |
334 | FAIL ("toupper ('\\x%02x') != '\\x%02x'", *cp, *cp); | |
9bbc1ecc UD |
335 | |
336 | ||
4edc30e4 UD |
337 | /* Now some locale specific tests. */ |
338 | while (! feof (stdin)) | |
339 | { | |
340 | unsigned char *inp; | |
341 | unsigned char *resp; | |
342 | ||
343 | if (getline (&inpline, &inplinelen, stdin) <= 0 | |
344 | || getline (&resline, &reslinelen, stdin) <= 0) | |
345 | break; | |
346 | ||
e7cbb3a8 | 347 | inp = (unsigned char *) strchr (inpline, '\n'); |
4edc30e4 UD |
348 | if (inp != NULL) |
349 | *inp = '\0'; | |
e7cbb3a8 | 350 | resp = (unsigned char *) strchr (resline, '\n'); |
4edc30e4 UD |
351 | if (resp != NULL) |
352 | *resp = '\0'; | |
353 | ||
e7cbb3a8 | 354 | inp = (unsigned char *) inpline; |
4edc30e4 UD |
355 | while (*inp != ' ' && *inp != '\t' && *inp && *inp != '\n' |
356 | && *inp != '\0') | |
357 | ++inp; | |
358 | ||
359 | if (*inp == '\0') | |
360 | { | |
361 | printf ("line \"%s\" is without content\n", inpline); | |
362 | continue; | |
363 | } | |
364 | *inp++ = '\0'; | |
365 | while (*inp == ' ' || *inp == '\t') | |
366 | ++inp; | |
367 | ||
368 | /* Try all classes. */ | |
369 | for (n = 0; n < nclasses; ++n) | |
370 | if (strcmp (inpline, classes[n].name) == 0) | |
371 | break; | |
372 | ||
e7cbb3a8 | 373 | resp = (unsigned char *) resline; |
4edc30e4 UD |
374 | while (*resp == ' ' || *resp == '\t') |
375 | ++resp; | |
376 | ||
e7cbb3a8 | 377 | if (strlen ((char *) inp) != strlen ((char *) resp)) |
4edc30e4 UD |
378 | { |
379 | printf ("lines \"%.20s\"... and \"%.20s\" have not the same length\n", | |
380 | inp, resp); | |
381 | continue; | |
382 | } | |
383 | ||
384 | if (n < nclasses) | |
385 | { | |
e7cbb3a8 | 386 | if (strspn ((char *) resp, "01") != strlen ((char *) resp)) |
4edc30e4 UD |
387 | { |
388 | printf ("result string \"%s\" malformed\n", resp); | |
389 | continue; | |
390 | } | |
391 | ||
392 | printf (" Locale-specific tests for `%s'\n", inpline); | |
393 | ||
394 | while (*inp != '\0' && *inp != '\n') | |
395 | { | |
396 | if (((__ctype_b[(unsigned int) *inp] & classes[n].mask) != 0) | |
397 | != (*resp != '0')) | |
398 | { | |
399 | printf (" is%s('%c' = '\\x%02x') %s true\n", inpline, | |
400 | *inp, *inp, *resp == '1' ? "not" : "is"); | |
401 | ++errors; | |
402 | } | |
403 | ++inp; | |
404 | ++resp; | |
405 | } | |
406 | } | |
407 | else if (strcmp (inpline, "tolower") == 0) | |
408 | { | |
409 | while (*inp != '\0') | |
410 | { | |
411 | if (tolower (*inp) != *resp) | |
412 | { | |
413 | printf (" tolower('%c' = '\\x%02x') != '%c'\n", | |
414 | *inp, *inp, *resp); | |
415 | ++errors; | |
416 | } | |
417 | ++inp; | |
418 | ++resp; | |
419 | } | |
420 | } | |
421 | else if (strcmp (inpline, "toupper") == 0) | |
422 | { | |
423 | while (*inp != '\0') | |
424 | { | |
425 | if (toupper (*inp) != *resp) | |
426 | { | |
427 | printf (" toupper('%c' = '\\x%02x') != '%c'\n", | |
428 | *inp, *inp, *resp); | |
429 | ++errors; | |
430 | } | |
431 | ++inp; | |
432 | ++resp; | |
433 | } | |
434 | } | |
435 | else | |
436 | printf ("\"%s\": unknown class or map\n", inpline); | |
437 | } | |
438 | ||
439 | ||
9bbc1ecc UD |
440 | if (errors != 0) |
441 | { | |
4edc30e4 UD |
442 | printf (" %d error%s for `%s' locale\n\n\n", errors, |
443 | errors == 1 ? "" : "s", setlocale (LC_ALL, NULL)); | |
9bbc1ecc UD |
444 | return 1; |
445 | } | |
446 | ||
447 | printf (" No errors for `%s' locale\n\n\n", setlocale (LC_ALL, NULL)); | |
448 | return 0; | |
449 | } | |
29955b5d AS |
450 | |
451 | #define TEST_FUNCTION do_test () | |
452 | #include "../test-skeleton.c" |