1 /* Copyright (C) 2000-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 2000.
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.
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.
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/>. */
26 static const char lower
[] = "abcdefghijklmnopqrstuvwxyz";
27 static const char upper
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
28 static const char digits
[] = "0123456789";
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 ";
40 #define ENTRY(name) { #name, _IS##name }
54 #define nclasses (sizeof (classes) / sizeof (classes[0]))
57 #define FAIL(str, args...) \
59 printf (" " str "\n", ##args); \
71 size_t inplinelen
= 0;
73 size_t reslinelen
= 0;
75 const unsigned short int *__ctype_b
;
77 setlocale (LC_ALL
, "");
79 printf ("Testing the ctype data of the `%s' locale\n",
80 setlocale (LC_CTYPE
, NULL
));
82 __ctype_b
= ((const unsigned short *) nl_langinfo (_NL_CTYPE_CLASS
)) + 128;
85 /* Just for debugging. */
87 /* Contents of the class array. */
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
,
100 printf ("%04x%s", __ctype_b
[n
], (n
+ 1) % 8 == 0 ? "\n" : " ");
105 puts (" Test of ASCII character range\n special NUL byte handling");
107 FAIL ("isupper ('\\0') is true");
109 FAIL ("islower ('\\0') is true");
111 FAIL ("isalpha ('\\0') is true");
113 FAIL ("isdigit ('\\0') is true");
115 FAIL ("isxdigit ('\\0') is true");
117 FAIL ("isspace ('\\0') is true");
119 FAIL ("isprint ('\\0') is true");
121 FAIL ("isgraph ('\\0') is true");
123 FAIL ("isblank ('\\0') is true");
124 if (! iscntrl ('\0'))
125 FAIL ("iscntrl ('\\0') not true");
127 FAIL ("ispunct ('\\0') is true");
129 FAIL ("isalnum ('\\0') is true");
132 for (cp
= lower
; *cp
!= '\0'; ++cp
)
134 FAIL ("islower ('%c') not true", *cp
);
135 for (cp
= upper
; *cp
!= '\0'; ++cp
)
137 FAIL ("islower ('%c') is true", *cp
);
138 for (cp
= digits
; *cp
!= '\0'; ++cp
)
140 FAIL ("islower ('%c') is true", *cp
);
141 for (cp
= cntrl
; *cp
!= '\0'; ++cp
)
143 FAIL ("islower ('\\x%02x') is true", *cp
);
146 for (cp
= lower
; *cp
!= '\0'; ++cp
)
148 FAIL ("isupper ('%c') is true", *cp
);
149 for (cp
= upper
; *cp
!= '\0'; ++cp
)
151 FAIL ("isupper ('%c') not true", *cp
);
152 for (cp
= digits
; *cp
!= '\0'; ++cp
)
154 FAIL ("isupper ('%c') is true", *cp
);
155 for (cp
= cntrl
; *cp
!= '\0'; ++cp
)
157 FAIL ("isupper ('\\x%02x') is true", *cp
);
160 for (cp
= lower
; *cp
!= '\0'; ++cp
)
162 FAIL ("isalpha ('%c') not true", *cp
);
163 for (cp
= upper
; *cp
!= '\0'; ++cp
)
165 FAIL ("isalpha ('%c') not true", *cp
);
166 for (cp
= digits
; *cp
!= '\0'; ++cp
)
168 FAIL ("isalpha ('%c') is true", *cp
);
169 for (cp
= cntrl
; *cp
!= '\0'; ++cp
)
171 FAIL ("isalpha ('\\x%02x') is true", *cp
);
174 for (cp
= lower
; *cp
!= '\0'; ++cp
)
176 FAIL ("isdigit ('%c') is true", *cp
);
177 for (cp
= upper
; *cp
!= '\0'; ++cp
)
179 FAIL ("isdigit ('%c') is true", *cp
);
180 for (cp
= digits
; *cp
!= '\0'; ++cp
)
182 FAIL ("isdigit ('%c') not true", *cp
);
183 for (cp
= cntrl
; *cp
!= '\0'; ++cp
)
185 FAIL ("isdigit ('\\x%02x') is true", *cp
);
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
);
199 for (cp
= cntrl
; *cp
!= '\0'; ++cp
)
201 FAIL ("isxdigit ('\\x%02x') is true", *cp
);
204 for (cp
= lower
; *cp
!= '\0'; ++cp
)
206 FAIL ("isspace ('%c') is true", *cp
);
207 for (cp
= upper
; *cp
!= '\0'; ++cp
)
209 FAIL ("isspace ('%c') is true", *cp
);
210 for (cp
= digits
; *cp
!= '\0'; ++cp
)
212 FAIL ("isspace ('%c') is true", *cp
);
213 for (cp
= cntrl
; *cp
!= '\0'; ++cp
)
214 if ((isspace (*cp
) && ((*cp
< '\x09' || *cp
> '\x0d') && *cp
!= ' '))
216 && ((*cp
>= '\x09' && *cp
<= '\x0d') || *cp
== ' ')))
217 FAIL ("isspace ('\\x%02x') %s true", *cp
,
218 (*cp
< '\x09' || *cp
> '\x0d') ? "is" : "not");
221 for (cp
= lower
; *cp
!= '\0'; ++cp
)
223 FAIL ("isprint ('%c') not true", *cp
);
224 for (cp
= upper
; *cp
!= '\0'; ++cp
)
226 FAIL ("isprint ('%c') not true", *cp
);
227 for (cp
= digits
; *cp
!= '\0'; ++cp
)
229 FAIL ("isprint ('%c') not true", *cp
);
230 for (cp
= cntrl
; *cp
!= '\0'; ++cp
)
231 if ((isprint (*cp
) && *cp
!= ' ')
232 || (! isprint (*cp
) && *cp
== ' '))
233 FAIL ("isprint ('\\x%02x') is true", *cp
);
236 for (cp
= lower
; *cp
!= '\0'; ++cp
)
238 FAIL ("isgraph ('%c') not true", *cp
);
239 for (cp
= upper
; *cp
!= '\0'; ++cp
)
241 FAIL ("isgraph ('%c') not true", *cp
);
242 for (cp
= digits
; *cp
!= '\0'; ++cp
)
244 FAIL ("isgraph ('%c') not true", *cp
);
245 for (cp
= cntrl
; *cp
!= '\0'; ++cp
)
247 FAIL ("isgraph ('\\x%02x') is true", *cp
);
250 for (cp
= lower
; *cp
!= '\0'; ++cp
)
252 FAIL ("isblank ('%c') is true", *cp
);
253 for (cp
= upper
; *cp
!= '\0'; ++cp
)
255 FAIL ("isblank ('%c') is true", *cp
);
256 for (cp
= digits
; *cp
!= '\0'; ++cp
)
258 FAIL ("isblank ('%c') is true", *cp
);
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");
265 for (cp
= lower
; *cp
!= '\0'; ++cp
)
267 FAIL ("iscntrl ('%c') is true", *cp
);
268 for (cp
= upper
; *cp
!= '\0'; ++cp
)
270 FAIL ("iscntrl ('%c') is true", *cp
);
271 for (cp
= digits
; *cp
!= '\0'; ++cp
)
273 FAIL ("iscntrl ('%c') is true", *cp
);
274 for (cp
= cntrl
; *cp
!= '\0'; ++cp
)
275 if ((iscntrl (*cp
) && *cp
== ' ')
276 || (! iscntrl (*cp
) && *cp
!= ' '))
277 FAIL ("iscntrl ('\\x%02x') not true", *cp
);
280 for (cp
= lower
; *cp
!= '\0'; ++cp
)
282 FAIL ("ispunct ('%c') is true", *cp
);
283 for (cp
= upper
; *cp
!= '\0'; ++cp
)
285 FAIL ("ispunct ('%c') is true", *cp
);
286 for (cp
= digits
; *cp
!= '\0'; ++cp
)
288 FAIL ("ispunct ('%c') is true", *cp
);
289 for (cp
= cntrl
; *cp
!= '\0'; ++cp
)
291 FAIL ("ispunct ('\\x%02x') is true", *cp
);
294 for (cp
= lower
; *cp
!= '\0'; ++cp
)
296 FAIL ("isalnum ('%c') not true", *cp
);
297 for (cp
= upper
; *cp
!= '\0'; ++cp
)
299 FAIL ("isalnum ('%c') not true", *cp
);
300 for (cp
= digits
; *cp
!= '\0'; ++cp
)
302 FAIL ("isalnum ('%c') not true", *cp
);
303 for (cp
= cntrl
; *cp
!= '\0'; ++cp
)
305 FAIL ("isalnum ('\\x%02x') is true", *cp
);
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
);
318 for (cp
= cntrl
; *cp
!= '\0'; ++cp
)
319 if (tolower (*cp
) != *cp
)
320 FAIL ("tolower ('\\x%02x') != '\\x%02x'", *cp
, *cp
);
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
);
332 for (cp
= cntrl
; *cp
!= '\0'; ++cp
)
333 if (toupper (*cp
) != *cp
)
334 FAIL ("toupper ('\\x%02x') != '\\x%02x'", *cp
, *cp
);
337 /* Now some locale specific tests. */
338 while (! feof (stdin
))
343 if (getline (&inpline
, &inplinelen
, stdin
) <= 0
344 || getline (&resline
, &reslinelen
, stdin
) <= 0)
347 inp
= (unsigned char *) strchr (inpline
, '\n');
350 resp
= (unsigned char *) strchr (resline
, '\n');
354 inp
= (unsigned char *) inpline
;
355 while (*inp
!= ' ' && *inp
!= '\t' && *inp
&& *inp
!= '\n'
361 printf ("line \"%s\" is without content\n", inpline
);
365 while (*inp
== ' ' || *inp
== '\t')
368 /* Try all classes. */
369 for (n
= 0; n
< nclasses
; ++n
)
370 if (strcmp (inpline
, classes
[n
].name
) == 0)
373 resp
= (unsigned char *) resline
;
374 while (*resp
== ' ' || *resp
== '\t')
377 if (strlen ((char *) inp
) != strlen ((char *) resp
))
379 printf ("lines \"%.20s\"... and \"%.20s\" have not the same length\n",
386 if (strspn ((char *) resp
, "01") != strlen ((char *) resp
))
388 printf ("result string \"%s\" malformed\n", resp
);
392 printf (" Locale-specific tests for `%s'\n", inpline
);
394 while (*inp
!= '\0' && *inp
!= '\n')
396 if (((__ctype_b
[(unsigned int) *inp
] & classes
[n
].mask
) != 0)
399 printf (" is%s('%c' = '\\x%02x') %s true\n", inpline
,
400 *inp
, *inp
, *resp
== '1' ? "not" : "is");
407 else if (strcmp (inpline
, "tolower") == 0)
411 if (tolower (*inp
) != *resp
)
413 printf (" tolower('%c' = '\\x%02x') != '%c'\n",
421 else if (strcmp (inpline
, "toupper") == 0)
425 if (toupper (*inp
) != *resp
)
427 printf (" toupper('%c' = '\\x%02x') != '%c'\n",
436 printf ("\"%s\": unknown class or map\n", inpline
);
442 printf (" %d error%s for `%s' locale\n\n\n", errors
,
443 errors
== 1 ? "" : "s", setlocale (LC_ALL
, NULL
));
447 printf (" No errors for `%s' locale\n\n\n", setlocale (LC_ALL
, NULL
));