tst-ctype.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* Copyright (C) 2000,02, 05 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@gnu.org>, 2000.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <ctype.h>
  17. #include <locale.h>
  18. #include <langinfo.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. static const char lower[] = "abcdefghijklmnopqrstuvwxyz";
  22. static const char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  23. static const char digits[] = "0123456789";
  24. static const char cntrl[] = "\
  25. \x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\
  26. \x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f ";
  27. static struct classes
  28. {
  29. const char *name;
  30. int mask;
  31. } classes[] =
  32. {
  33. #define ENTRY(name) { #name, _IS##name }
  34. ENTRY (upper),
  35. ENTRY (lower),
  36. ENTRY (alpha),
  37. ENTRY (digit),
  38. ENTRY (xdigit),
  39. ENTRY (space),
  40. ENTRY (print),
  41. ENTRY (graph),
  42. ENTRY (blank),
  43. ENTRY (cntrl),
  44. ENTRY (punct),
  45. ENTRY (alnum)
  46. };
  47. #define nclasses (sizeof (classes) / sizeof (classes[0]))
  48. #define FAIL(str, args...) \
  49. { \
  50. printf (" " str "\n", ##args); \
  51. ++errors; \
  52. }
  53. int
  54. main (void)
  55. {
  56. const char *cp;
  57. const char *cp2;
  58. int errors = 0;
  59. char *inpline = NULL;
  60. size_t inplinelen = 0;
  61. char *resline = NULL;
  62. size_t reslinelen = 0;
  63. size_t n;
  64. setlocale (LC_ALL, "");
  65. printf ("Testing the ctype data of the `%s' locale\n",
  66. setlocale (LC_CTYPE, NULL));
  67. #if 0
  68. /* Just for debugging. */
  69. /* Contents of the class array. */
  70. printf ("\
  71. upper = %04x lower = %04x alpha = %04x digit = %04x xdigit = %04x\n\
  72. space = %04x print = %04x graph = %04x blank = %04x cntrl = %04x\n\
  73. punct = %04x alnum = %04x\n",
  74. _ISupper, _ISlower, _ISalpha, _ISdigit, _ISxdigit,
  75. _ISspace, _ISprint, _ISgraph, _ISblank, _IScntrl,
  76. _ISpunct, _ISalnum);
  77. while (n < 256)
  78. {
  79. if (n % 8 == 0)
  80. printf ("%02x: ", n);
  81. printf ("%04x%s", __ctype_b[n], (n + 1) % 8 == 0 ? "\n" : " ");
  82. ++n;
  83. }
  84. #endif
  85. puts (" Test of ASCII character range\n special NUL byte handling");
  86. if (isupper ('\0'))
  87. FAIL ("isupper ('\\0') is true");
  88. if (islower ('\0'))
  89. FAIL ("islower ('\\0') is true");
  90. if (isalpha ('\0'))
  91. FAIL ("isalpha ('\\0') is true");
  92. if (isdigit ('\0'))
  93. FAIL ("isdigit ('\\0') is true");
  94. if (isxdigit ('\0'))
  95. FAIL ("isxdigit ('\\0') is true");
  96. if (isspace ('\0'))
  97. FAIL ("isspace ('\\0') is true");
  98. if (isprint ('\0'))
  99. FAIL ("isprint ('\\0') is true");
  100. if (isgraph ('\0'))
  101. FAIL ("isgraph ('\\0') is true");
  102. if (isblank ('\0'))
  103. FAIL ("isblank ('\\0') is true");
  104. if (! iscntrl ('\0'))
  105. FAIL ("iscntrl ('\\0') not true");
  106. if (ispunct ('\0'))
  107. FAIL ("ispunct ('\\0') is true");
  108. if (isalnum ('\0'))
  109. FAIL ("isalnum ('\\0') is true");
  110. puts (" islower()");
  111. for (cp = lower; *cp != '\0'; ++cp)
  112. if (! islower (*cp))
  113. FAIL ("islower ('%c') not true", *cp);
  114. for (cp = upper; *cp != '\0'; ++cp)
  115. if (islower (*cp))
  116. FAIL ("islower ('%c') is true", *cp);
  117. for (cp = digits; *cp != '\0'; ++cp)
  118. if (islower (*cp))
  119. FAIL ("islower ('%c') is true", *cp);
  120. for (cp = cntrl; *cp != '\0'; ++cp)
  121. if (islower (*cp))
  122. FAIL ("islower ('\\x%02x') is true", *cp);
  123. puts (" isupper()");
  124. for (cp = lower; *cp != '\0'; ++cp)
  125. if (isupper (*cp))
  126. FAIL ("isupper ('%c') is true", *cp);
  127. for (cp = upper; *cp != '\0'; ++cp)
  128. if (! isupper (*cp))
  129. FAIL ("isupper ('%c') not true", *cp);
  130. for (cp = digits; *cp != '\0'; ++cp)
  131. if (isupper (*cp))
  132. FAIL ("isupper ('%c') is true", *cp);
  133. for (cp = cntrl; *cp != '\0'; ++cp)
  134. if (isupper (*cp))
  135. FAIL ("isupper ('\\x%02x') is true", *cp);
  136. puts (" isalpha()");
  137. for (cp = lower; *cp != '\0'; ++cp)
  138. if (! isalpha (*cp))
  139. FAIL ("isalpha ('%c') not true", *cp);
  140. for (cp = upper; *cp != '\0'; ++cp)
  141. if (! isalpha (*cp))
  142. FAIL ("isalpha ('%c') not true", *cp);
  143. for (cp = digits; *cp != '\0'; ++cp)
  144. if (isalpha (*cp))
  145. FAIL ("isalpha ('%c') is true", *cp);
  146. for (cp = cntrl; *cp != '\0'; ++cp)
  147. if (isalpha (*cp))
  148. FAIL ("isalpha ('\\x%02x') is true", *cp);
  149. puts (" isdigit()");
  150. for (cp = lower; *cp != '\0'; ++cp)
  151. if (isdigit (*cp))
  152. FAIL ("isdigit ('%c') is true", *cp);
  153. for (cp = upper; *cp != '\0'; ++cp)
  154. if (isdigit (*cp))
  155. FAIL ("isdigit ('%c') is true", *cp);
  156. for (cp = digits; *cp != '\0'; ++cp)
  157. if (! isdigit (*cp))
  158. FAIL ("isdigit ('%c') not true", *cp);
  159. for (cp = cntrl; *cp != '\0'; ++cp)
  160. if (isdigit (*cp))
  161. FAIL ("isdigit ('\\x%02x') is true", *cp);
  162. puts (" isxdigit()");
  163. for (cp = lower; *cp != '\0'; ++cp)
  164. if ((! isxdigit (*cp) && cp - lower < 6)
  165. || (isxdigit (*cp) && cp - lower >= 6))
  166. FAIL ("isxdigit ('%c') %s true", *cp, cp - upper < 6 ? "not" : "is");
  167. for (cp = upper; *cp != '\0'; ++cp)
  168. if ((! isxdigit (*cp) && cp - upper < 6)
  169. || (isxdigit (*cp) && cp - upper >= 6))
  170. FAIL ("isxdigit ('%c') %s true", *cp, cp - upper < 6 ? "not" : "is");
  171. for (cp = digits; *cp != '\0'; ++cp)
  172. if (! isxdigit (*cp))
  173. FAIL ("isxdigit ('%c') not true", *cp);
  174. for (cp = cntrl; *cp != '\0'; ++cp)
  175. if (isxdigit (*cp))
  176. FAIL ("isxdigit ('\\x%02x') is true", *cp);
  177. puts (" isspace()");
  178. for (cp = lower; *cp != '\0'; ++cp)
  179. if (isspace (*cp))
  180. FAIL ("isspace ('%c') is true", *cp);
  181. for (cp = upper; *cp != '\0'; ++cp)
  182. if (isspace (*cp))
  183. FAIL ("isspace ('%c') is true", *cp);
  184. for (cp = digits; *cp != '\0'; ++cp)
  185. if (isspace (*cp))
  186. FAIL ("isspace ('%c') is true", *cp);
  187. for (cp = cntrl; *cp != '\0'; ++cp)
  188. if ((isspace (*cp) && ((*cp < '\x09' || *cp > '\x0d') && *cp != ' '))
  189. || (! isspace (*cp)
  190. && ((*cp >= '\x09' && *cp <= '\x0d') || *cp == ' ')))
  191. FAIL ("isspace ('\\x%02x') %s true", *cp,
  192. (*cp < '\x09' || *cp > '\x0d') ? "is" : "not");
  193. puts (" isprint()");
  194. for (cp = lower; *cp != '\0'; ++cp)
  195. if (! isprint (*cp))
  196. FAIL ("isprint ('%c') not true", *cp);
  197. for (cp = upper; *cp != '\0'; ++cp)
  198. if (! isprint (*cp))
  199. FAIL ("isprint ('%c') not true", *cp);
  200. for (cp = digits; *cp != '\0'; ++cp)
  201. if (! isprint (*cp))
  202. FAIL ("isprint ('%c') not true", *cp);
  203. for (cp = cntrl; *cp != '\0'; ++cp)
  204. if ((isprint (*cp) && *cp != ' ')
  205. || (! isprint (*cp) && *cp == ' '))
  206. FAIL ("isprint ('\\x%02x') is true", *cp);
  207. puts (" isgraph()");
  208. for (cp = lower; *cp != '\0'; ++cp)
  209. if (! isgraph (*cp))
  210. FAIL ("isgraph ('%c') not true", *cp);
  211. for (cp = upper; *cp != '\0'; ++cp)
  212. if (! isgraph (*cp))
  213. FAIL ("isgraph ('%c') not true", *cp);
  214. for (cp = digits; *cp != '\0'; ++cp)
  215. if (! isgraph (*cp))
  216. FAIL ("isgraph ('%c') not true", *cp);
  217. for (cp = cntrl; *cp != '\0'; ++cp)
  218. if (isgraph (*cp))
  219. FAIL ("isgraph ('\\x%02x') is true", *cp);
  220. puts (" isblank()");
  221. for (cp = lower; *cp != '\0'; ++cp)
  222. if (isblank (*cp))
  223. FAIL ("isblank ('%c') is true", *cp);
  224. for (cp = upper; *cp != '\0'; ++cp)
  225. if (isblank (*cp))
  226. FAIL ("isblank ('%c') is true", *cp);
  227. for (cp = digits; *cp != '\0'; ++cp)
  228. if (isblank (*cp))
  229. FAIL ("isblank ('%c') is true", *cp);
  230. for (cp = cntrl; *cp != '\0'; ++cp)
  231. if ((isblank (*cp) && *cp != '\x09' && *cp != ' ')
  232. || (! isblank (*cp) && (*cp == '\x09' || *cp == ' ')))
  233. FAIL ("isblank ('\\x%02x') %s true", *cp, *cp != '\x09' ? "is" : "not");
  234. puts (" iscntrl()");
  235. for (cp = lower; *cp != '\0'; ++cp)
  236. if (iscntrl (*cp))
  237. FAIL ("iscntrl ('%c') is true", *cp);
  238. for (cp = upper; *cp != '\0'; ++cp)
  239. if (iscntrl (*cp))
  240. FAIL ("iscntrl ('%c') is true", *cp);
  241. for (cp = digits; *cp != '\0'; ++cp)
  242. if (iscntrl (*cp))
  243. FAIL ("iscntrl ('%c') is true", *cp);
  244. for (cp = cntrl; *cp != '\0'; ++cp)
  245. if ((iscntrl (*cp) && *cp == ' ')
  246. || (! iscntrl (*cp) && *cp != ' '))
  247. FAIL ("iscntrl ('\\x%02x') not true", *cp);
  248. puts (" ispunct()");
  249. for (cp = lower; *cp != '\0'; ++cp)
  250. if (ispunct (*cp))
  251. FAIL ("ispunct ('%c') is true", *cp);
  252. for (cp = upper; *cp != '\0'; ++cp)
  253. if (ispunct (*cp))
  254. FAIL ("ispunct ('%c') is true", *cp);
  255. for (cp = digits; *cp != '\0'; ++cp)
  256. if (ispunct (*cp))
  257. FAIL ("ispunct ('%c') is true", *cp);
  258. for (cp = cntrl; *cp != '\0'; ++cp)
  259. if (ispunct (*cp))
  260. FAIL ("ispunct ('\\x%02x') is true", *cp);
  261. puts (" isalnum()");
  262. for (cp = lower; *cp != '\0'; ++cp)
  263. if (! isalnum (*cp))
  264. FAIL ("isalnum ('%c') not true", *cp);
  265. for (cp = upper; *cp != '\0'; ++cp)
  266. if (! isalnum (*cp))
  267. FAIL ("isalnum ('%c') not true", *cp);
  268. for (cp = digits; *cp != '\0'; ++cp)
  269. if (! isalnum (*cp))
  270. FAIL ("isalnum ('%c') not true", *cp);
  271. for (cp = cntrl; *cp != '\0'; ++cp)
  272. if (isalnum (*cp))
  273. FAIL ("isalnum ('\\x%02x') is true", *cp);
  274. puts (" tolower()");
  275. for (cp = lower; *cp != '\0'; ++cp)
  276. if (tolower (*cp) != *cp)
  277. FAIL ("tolower ('%c') != '%c'", *cp, *cp);
  278. for (cp = upper, cp2 = lower; *cp != '\0'; ++cp, ++cp2)
  279. if (tolower (*cp) != *cp2)
  280. FAIL ("tolower ('%c') != '%c'", *cp, *cp2);
  281. for (cp = digits; *cp != '\0'; ++cp)
  282. if (tolower (*cp) != *cp)
  283. FAIL ("tolower ('%c') != '%c'", *cp, *cp);
  284. for (cp = cntrl; *cp != '\0'; ++cp)
  285. if (tolower (*cp) != *cp)
  286. FAIL ("tolower ('\\x%02x') != '\\x%02x'", *cp, *cp);
  287. puts (" toupper()");
  288. for (cp = lower, cp2 = upper; *cp != '\0'; ++cp, ++cp2)
  289. if (toupper (*cp) != *cp2)
  290. FAIL ("toupper ('%c') != '%c'", *cp, *cp2);
  291. for (cp = upper; *cp != '\0'; ++cp)
  292. if (toupper (*cp) != *cp)
  293. FAIL ("toupper ('%c') != '%c'", *cp, *cp);
  294. for (cp = digits; *cp != '\0'; ++cp)
  295. if (toupper (*cp) != *cp)
  296. FAIL ("toupper ('%c') != '%c'", *cp, *cp);
  297. for (cp = cntrl; *cp != '\0'; ++cp)
  298. if (toupper (*cp) != *cp)
  299. FAIL ("toupper ('\\x%02x') != '\\x%02x'", *cp, *cp);
  300. /* Now some locale specific tests. */
  301. while (! feof (stdin))
  302. {
  303. unsigned char *inp;
  304. unsigned char *resp;
  305. if (getline (&inpline, &inplinelen, stdin) <= 0
  306. || getline (&resline, &reslinelen, stdin) <= 0)
  307. break;
  308. inp = (unsigned char *) strchr (inpline, '\n');
  309. if (inp != NULL)
  310. *inp = '\0';
  311. resp = (unsigned char *) strchr (resline, '\n');
  312. if (resp != NULL)
  313. *resp = '\0';
  314. inp = (unsigned char *) inpline;
  315. while (*inp != ' ' && *inp != '\t' && *inp && *inp != '\n'
  316. && *inp != '\0')
  317. ++inp;
  318. if (*inp == '\0')
  319. {
  320. printf ("line \"%s\" is without content\n", inpline);
  321. continue;
  322. }
  323. *inp++ = '\0';
  324. while (*inp == ' ' || *inp == '\t')
  325. ++inp;
  326. /* Try all classes. */
  327. for (n = 0; n < nclasses; ++n)
  328. if (strcmp (inpline, classes[n].name) == 0)
  329. break;
  330. resp = (unsigned char *) resline;
  331. while (*resp == ' ' || *resp == '\t')
  332. ++resp;
  333. if (strlen ((char *) inp) != strlen ((char *) resp))
  334. {
  335. printf ("lines \"%.20s\"... and \"%.20s\" have not the same length\n",
  336. inp, resp);
  337. continue;
  338. }
  339. if (n < nclasses)
  340. {
  341. if (strspn ((char *) resp, "01") != strlen ((char *) resp))
  342. {
  343. printf ("result string \"%s\" malformed\n", resp);
  344. continue;
  345. }
  346. printf (" Locale-specific tests for `%s'\n", inpline);
  347. while (*inp != '\0' && *inp != '\n')
  348. {
  349. if (((__ctype_b[(unsigned int) *inp] & classes[n].mask) != 0)
  350. != (*resp != '0'))
  351. {
  352. printf (" is%s('%c' = '\\x%02x') %s true\n", inpline,
  353. *inp, *inp, *resp == '1' ? "not" : "is");
  354. ++errors;
  355. }
  356. ++inp;
  357. ++resp;
  358. }
  359. }
  360. else if (strcmp (inpline, "tolower") == 0)
  361. {
  362. while (*inp != '\0')
  363. {
  364. if (tolower (*inp) != *resp)
  365. {
  366. printf (" tolower('%c' = '\\x%02x') != '%c'\n",
  367. *inp, *inp, *resp);
  368. ++errors;
  369. }
  370. ++inp;
  371. ++resp;
  372. }
  373. }
  374. else if (strcmp (inpline, "toupper") == 0)
  375. {
  376. while (*inp != '\0')
  377. {
  378. if (toupper (*inp) != *resp)
  379. {
  380. printf (" toupper('%c' = '\\x%02x') != '%c'\n",
  381. *inp, *inp, *resp);
  382. ++errors;
  383. }
  384. ++inp;
  385. ++resp;
  386. }
  387. }
  388. else
  389. printf ("\"%s\": unknown class or map\n", inpline);
  390. }
  391. if (errors != 0)
  392. {
  393. printf (" %d error%s for `%s' locale\n\n\n", errors,
  394. errors == 1 ? "" : "s", setlocale (LC_ALL, NULL));
  395. return 1;
  396. }
  397. printf (" No errors for `%s' locale\n\n\n", setlocale (LC_ALL, NULL));
  398. return 0;
  399. }