tst-ctype.c 13 KB

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