string.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /* Tester for string functions.
  2. Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. 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. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. /* Make sure we don't test the optimized inline functions if we want to
  17. test the real implementation. */
  18. #undef __USE_STRING_INLINES
  19. #include <errno.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <strings.h>
  24. #include <fcntl.h>
  25. #ifndef HAVE_GNU_LD
  26. #define _sys_nerr sys_nerr
  27. #define _sys_errlist sys_errlist
  28. #endif
  29. #define STREQ(a, b) (strcmp((a), (b)) == 0)
  30. const char *it = "<UNSET>"; /* Routine name for message routines. */
  31. size_t errors = 0;
  32. /* Complain if condition is not true. */
  33. void
  34. check (int thing, int number)
  35. {
  36. if (!thing)
  37. {
  38. printf("%s flunked test %d\n", it, number);
  39. ++errors;
  40. }
  41. }
  42. /* Complain if first two args don't strcmp as equal. */
  43. void
  44. equal (const char *a, const char *b, int number)
  45. {
  46. check(a != NULL && b != NULL && (strcmp(a, b)==0), number);
  47. }
  48. char one[50];
  49. char two[50];
  50. char *cp;
  51. void
  52. test_strcmp (void)
  53. {
  54. it = "strcmp";
  55. check (strcmp ("", "") == 0, 1); /* Trivial case. */
  56. check (strcmp ("a", "a") == 0, 2); /* Identity. */
  57. check (strcmp ("abc", "abc") == 0, 3); /* Multicharacter. */
  58. check (strcmp ("abc", "abcd") < 0, 4); /* Length mismatches. */
  59. check (strcmp ("abcd", "abc") > 0, 5);
  60. check (strcmp ("abcd", "abce") < 0, 6); /* Honest miscompares. */
  61. check (strcmp ("abce", "abcd") > 0, 7);
  62. check (strcmp ("a\203", "a") > 0, 8); /* Tricky if char signed. */
  63. check (strcmp ("a\203", "a\003") > 0, 9);
  64. {
  65. char buf1[0x40], buf2[0x40];
  66. int i, j;
  67. for (i=0; i < 0x10; i++)
  68. for (j = 0; j < 0x10; j++)
  69. {
  70. int k;
  71. for (k = 0; k < 0x3f; k++)
  72. {
  73. buf1[j] = '0' ^ (k & 4);
  74. buf2[j] = '4' ^ (k & 4);
  75. }
  76. buf1[i] = buf1[0x3f] = 0;
  77. buf2[j] = buf2[0x3f] = 0;
  78. for (k = 0; k < 0xf; k++)
  79. {
  80. int cnum = 0x10+0x10*k+0x100*j+0x1000*i;
  81. check (strcmp (buf1+i,buf2+j) == 0, cnum);
  82. buf1[i+k] = 'A' + i + k;
  83. buf1[i+k+1] = 0;
  84. check (strcmp (buf1+i,buf2+j) > 0, cnum+1);
  85. check (strcmp (buf2+j,buf1+i) < 0, cnum+2);
  86. buf2[j+k] = 'B' + i + k;
  87. buf2[j+k+1] = 0;
  88. check (strcmp (buf1+i,buf2+j) < 0, cnum+3);
  89. check (strcmp (buf2+j,buf1+i) > 0, cnum+4);
  90. buf2[j+k] = 'A' + i + k;
  91. buf1[i] = 'A' + i + 0x80;
  92. check (strcmp (buf1+i,buf2+j) > 0, cnum+5);
  93. check (strcmp (buf2+j,buf1+i) < 0, cnum+6);
  94. buf1[i] = 'A' + i;
  95. }
  96. }
  97. }
  98. }
  99. void
  100. test_strcpy (void)
  101. {
  102. int i;
  103. it = "strcpy";
  104. check (strcpy (one, "abcd") == one, 1); /* Returned value. */
  105. equal (one, "abcd", 2); /* Basic test. */
  106. (void) strcpy (one, "x");
  107. equal (one, "x", 3); /* Writeover. */
  108. equal (one+2, "cd", 4); /* Wrote too much? */
  109. (void) strcpy (two, "hi there");
  110. (void) strcpy (one, two);
  111. equal (one, "hi there", 5); /* Basic test encore. */
  112. equal (two, "hi there", 6); /* Stomped on source? */
  113. (void) strcpy (one, "");
  114. equal (one, "", 7); /* Boundary condition. */
  115. for (i = 0; i < 16; i++)
  116. {
  117. (void) strcpy (one + i, "hi there"); /* Unaligned destination. */
  118. equal (one + i, "hi there", 8 + (i * 2));
  119. (void) strcpy (two, one + i); /* Unaligned source. */
  120. equal (two, "hi there", 9 + (i * 2));
  121. }
  122. }
  123. void
  124. test_stpcpy (void)
  125. {
  126. int i;
  127. it = "stpcpy";
  128. check (stpcpy (one, "abcd") == one+4, 1); /* Returned value. */
  129. equal (one, "abcd", 2); /* Basic test. */
  130. (void) stpcpy (one, "x");
  131. equal (one, "x", 3); /* Writeover. */
  132. equal (one+2, "cd", 4); /* Wrote too much? */
  133. (void) stpcpy (two, "hi there");
  134. (void) stpcpy (one, two);
  135. equal (one, "hi there", 5); /* Basic test encore. */
  136. equal (two, "hi there", 6); /* Stomped on source? */
  137. (void) stpcpy (one, "");
  138. equal (one, "", 7); /* Boundary condition. */
  139. for (i = 0; i < 16; i++)
  140. {
  141. (void) stpcpy (one + i, "hi there"); /* Unaligned destination. */
  142. equal (one + i, "hi there", 8 + (i * 2));
  143. (void) stpcpy (two, one + i); /* Unaligned source. */
  144. equal (two, "hi there", 9 + (i * 2));
  145. }
  146. }
  147. void
  148. test_strcat (void)
  149. {
  150. it = "strcat";
  151. (void) strcpy (one, "ijk");
  152. check (strcat (one, "lmn") == one, 1); /* Returned value. */
  153. equal (one, "ijklmn", 2); /* Basic test. */
  154. (void) strcpy (one, "x");
  155. (void) strcat (one, "yz");
  156. equal (one, "xyz", 3); /* Writeover. */
  157. equal (one+4, "mn", 4); /* Wrote too much? */
  158. (void) strcpy (one, "gh");
  159. (void) strcpy (two, "ef");
  160. (void) strcat (one, two);
  161. equal (one, "ghef", 5); /* Basic test encore. */
  162. equal (two, "ef", 6); /* Stomped on source? */
  163. (void) strcpy (one, "");
  164. (void) strcat (one, "");
  165. equal (one, "", 7); /* Boundary conditions. */
  166. (void) strcpy (one, "ab");
  167. (void) strcat (one, "");
  168. equal (one, "ab", 8);
  169. (void) strcpy (one, "");
  170. (void) strcat (one, "cd");
  171. equal (one, "cd", 9);
  172. }
  173. void
  174. test_strncat (void)
  175. {
  176. /* First test it as strcat, with big counts, then test the count
  177. mechanism. */
  178. it = "strncat";
  179. (void) strcpy (one, "ijk");
  180. check (strncat (one, "lmn", 99) == one, 1); /* Returned value. */
  181. equal (one, "ijklmn", 2); /* Basic test. */
  182. (void) strcpy (one, "x");
  183. (void) strncat (one, "yz", 99);
  184. equal (one, "xyz", 3); /* Writeover. */
  185. equal (one+4, "mn", 4); /* Wrote too much? */
  186. (void) strcpy (one, "gh");
  187. (void) strcpy (two, "ef");
  188. (void) strncat (one, two, 99);
  189. equal (one, "ghef", 5); /* Basic test encore. */
  190. equal (two, "ef", 6); /* Stomped on source? */
  191. (void) strcpy (one, "");
  192. (void) strncat (one, "", 99);
  193. equal (one, "", 7); /* Boundary conditions. */
  194. (void) strcpy (one, "ab");
  195. (void) strncat (one, "", 99);
  196. equal (one, "ab", 8);
  197. (void) strcpy (one, "");
  198. (void) strncat (one, "cd", 99);
  199. equal (one, "cd", 9);
  200. (void) strcpy (one, "ab");
  201. (void) strncat (one, "cdef", 2);
  202. equal (one, "abcd", 10); /* Count-limited. */
  203. (void) strncat (one, "gh", 0);
  204. equal (one, "abcd", 11); /* Zero count. */
  205. (void) strncat (one, "gh", 2);
  206. equal (one, "abcdgh", 12); /* Count and length equal. */
  207. }
  208. void
  209. test_strncmp (void)
  210. {
  211. /* First test as strcmp with big counts, then test count code. */
  212. it = "strncmp";
  213. check (strncmp ("", "", 99) == 0, 1); /* Trivial case. */
  214. check (strncmp ("a", "a", 99) == 0, 2); /* Identity. */
  215. check (strncmp ("abc", "abc", 99) == 0, 3); /* Multicharacter. */
  216. check (strncmp ("abc", "abcd", 99) < 0, 4); /* Length unequal. */
  217. check (strncmp ("abcd", "abc", 99) > 0, 5);
  218. check (strncmp ("abcd", "abce", 99) < 0, 6); /* Honestly unequal. */
  219. check (strncmp ("abce", "abcd", 99) > 0, 7);
  220. check (strncmp ("a\203", "a", 2) > 0, 8); /* Tricky if '\203' < 0 */
  221. check (strncmp ("a\203", "a\003", 2) > 0, 9);
  222. check (strncmp ("abce", "abcd", 3) == 0, 10); /* Count limited. */
  223. check (strncmp ("abce", "abc", 3) == 0, 11); /* Count == length. */
  224. check (strncmp ("abcd", "abce", 4) < 0, 12); /* Nudging limit. */
  225. check (strncmp ("abc", "def", 0) == 0, 13); /* Zero count. */
  226. }
  227. void
  228. test_strncpy (void)
  229. {
  230. /* Testing is a bit different because of odd semantics. */
  231. it = "strncpy";
  232. check (strncpy (one, "abc", 4) == one, 1); /* Returned value. */
  233. equal (one, "abc", 2); /* Did the copy go right? */
  234. (void) strcpy (one, "abcdefgh");
  235. (void) strncpy (one, "xyz", 2);
  236. equal (one, "xycdefgh", 3); /* Copy cut by count. */
  237. (void) strcpy (one, "abcdefgh");
  238. (void) strncpy (one, "xyz", 3); /* Copy cut just before NUL. */
  239. equal (one, "xyzdefgh", 4);
  240. (void) strcpy (one, "abcdefgh");
  241. (void) strncpy (one, "xyz", 4); /* Copy just includes NUL. */
  242. equal (one, "xyz", 5);
  243. equal (one+4, "efgh", 6); /* Wrote too much? */
  244. (void) strcpy (one, "abcdefgh");
  245. (void) strncpy (one, "xyz", 5); /* Copy includes padding. */
  246. equal (one, "xyz", 7);
  247. equal (one+4, "", 8);
  248. equal (one+5, "fgh", 9);
  249. (void) strcpy (one, "abc");
  250. (void) strncpy (one, "xyz", 0); /* Zero-length copy. */
  251. equal (one, "abc", 10);
  252. (void) strncpy (one, "", 2); /* Zero-length source. */
  253. equal (one, "", 11);
  254. equal (one+1, "", 12);
  255. equal (one+2, "c", 13);
  256. (void) strcpy (one, "hi there");
  257. (void) strncpy (two, one, 9);
  258. equal (two, "hi there", 14); /* Just paranoia. */
  259. equal (one, "hi there", 15); /* Stomped on source? */
  260. }
  261. void
  262. test_strlen (void)
  263. {
  264. it = "strlen";
  265. check (strlen ("") == 0, 1); /* Empty. */
  266. check (strlen ("a") == 1, 2); /* Single char. */
  267. check (strlen ("abcd") == 4, 3); /* Multiple chars. */
  268. {
  269. char buf[4096];
  270. int i;
  271. char *p;
  272. for (i=0; i < 0x100; i++)
  273. {
  274. p = (char *) ((unsigned long int)(buf + 0xff) & ~0xff) + i;
  275. strcpy (p, "OK");
  276. strcpy (p+3, "BAD/WRONG");
  277. check (strlen (p) == 2, 4+i);
  278. }
  279. }
  280. }
  281. void
  282. test_strchr (void)
  283. {
  284. it = "strchr";
  285. check (strchr ("abcd", 'z') == NULL, 1); /* Not found. */
  286. (void) strcpy (one, "abcd");
  287. check (strchr (one, 'c') == one+2, 2); /* Basic test. */
  288. check (strchr (one, 'd') == one+3, 3); /* End of string. */
  289. check (strchr (one, 'a') == one, 4); /* Beginning. */
  290. check (strchr (one, '\0') == one+4, 5); /* Finding NUL. */
  291. (void) strcpy (one, "ababa");
  292. check (strchr (one, 'b') == one+1, 6); /* Finding first. */
  293. (void) strcpy (one, "");
  294. check (strchr (one, 'b') == NULL, 7); /* Empty string. */
  295. check (strchr (one, '\0') == one, 8); /* NUL in empty string. */
  296. {
  297. char buf[4096];
  298. int i;
  299. char *p;
  300. for (i=0; i < 0x100; i++)
  301. {
  302. p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
  303. strcpy (p, "OK");
  304. strcpy (p+3, "BAD/WRONG");
  305. check (strchr (p, '/') == NULL, 9+i);
  306. }
  307. }
  308. }
  309. void
  310. test_index (void)
  311. {
  312. it = "index";
  313. check (index ("abcd", 'z') == NULL, 1); /* Not found. */
  314. (void) strcpy (one, "abcd");
  315. check (index (one, 'c') == one+2, 2); /* Basic test. */
  316. check (index (one, 'd') == one+3, 3); /* End of string. */
  317. check (index (one, 'a') == one, 4); /* Beginning. */
  318. check (index (one, '\0') == one+4, 5); /* Finding NUL. */
  319. (void) strcpy (one, "ababa");
  320. check (index (one, 'b') == one+1, 6); /* Finding first. */
  321. (void) strcpy (one, "");
  322. check (index (one, 'b') == NULL, 7); /* Empty string. */
  323. check (index (one, '\0') == one, 8); /* NUL in empty string. */
  324. }
  325. void
  326. test_strrchr (void)
  327. {
  328. it = "strrchr";
  329. check (strrchr ("abcd", 'z') == NULL, 1); /* Not found. */
  330. (void) strcpy (one, "abcd");
  331. check (strrchr (one, 'c') == one+2, 2); /* Basic test. */
  332. check (strrchr (one, 'd') == one+3, 3); /* End of string. */
  333. check (strrchr (one, 'a') == one, 4); /* Beginning. */
  334. check (strrchr (one, '\0') == one+4, 5); /* Finding NUL. */
  335. (void) strcpy (one, "ababa");
  336. check (strrchr (one, 'b') == one+3, 6); /* Finding last. */
  337. (void) strcpy (one, "");
  338. check (strrchr (one, 'b') == NULL, 7); /* Empty string. */
  339. check (strrchr (one, '\0') == one, 8); /* NUL in empty string. */
  340. {
  341. char buf[4096];
  342. int i;
  343. char *p;
  344. for (i=0; i < 0x100; i++)
  345. {
  346. p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
  347. strcpy (p, "OK");
  348. strcpy (p+3, "BAD/WRONG");
  349. check (strrchr (p, '/') == NULL, 9+i);
  350. }
  351. }
  352. }
  353. void
  354. test_rindex (void)
  355. {
  356. it = "rindex";
  357. check (rindex ("abcd", 'z') == NULL, 1); /* Not found. */
  358. (void) strcpy (one, "abcd");
  359. check (rindex (one, 'c') == one+2, 2); /* Basic test. */
  360. check (rindex (one, 'd') == one+3, 3); /* End of string. */
  361. check (rindex (one, 'a') == one, 4); /* Beginning. */
  362. check (rindex (one, '\0') == one+4, 5); /* Finding NUL. */
  363. (void) strcpy (one, "ababa");
  364. check (rindex (one, 'b') == one+3, 6); /* Finding last. */
  365. (void) strcpy (one, "");
  366. check (rindex (one, 'b') == NULL, 7); /* Empty string. */
  367. check (rindex (one, '\0') == one, 8); /* NUL in empty string. */
  368. }
  369. void
  370. test_strpbrk (void)
  371. {
  372. it = "strpbrk";
  373. check(strpbrk("abcd", "z") == NULL, 1); /* Not found. */
  374. (void) strcpy(one, "abcd");
  375. check(strpbrk(one, "c") == one+2, 2); /* Basic test. */
  376. check(strpbrk(one, "d") == one+3, 3); /* End of string. */
  377. check(strpbrk(one, "a") == one, 4); /* Beginning. */
  378. check(strpbrk(one, "") == NULL, 5); /* Empty search list. */
  379. check(strpbrk(one, "cb") == one+1, 6); /* Multiple search. */
  380. (void) strcpy(one, "abcabdea");
  381. check(strpbrk(one, "b") == one+1, 7); /* Finding first. */
  382. check(strpbrk(one, "cb") == one+1, 8); /* With multiple search. */
  383. check(strpbrk(one, "db") == one+1, 9); /* Another variant. */
  384. (void) strcpy(one, "");
  385. check(strpbrk(one, "bc") == NULL, 10); /* Empty string. */
  386. (void) strcpy(one, "");
  387. check(strpbrk(one, "bcd") == NULL, 11); /* Empty string. */
  388. (void) strcpy(one, "");
  389. check(strpbrk(one, "bcde") == NULL, 12); /* Empty string. */
  390. check(strpbrk(one, "") == NULL, 13); /* Both strings empty. */
  391. (void) strcpy(one, "abcabdea");
  392. check(strpbrk(one, "befg") == one+1, 14); /* Finding first. */
  393. check(strpbrk(one, "cbr") == one+1, 15); /* With multiple search. */
  394. check(strpbrk(one, "db") == one+1, 16); /* Another variant. */
  395. check(strpbrk(one, "efgh") == one+6, 17); /* And yet another. */
  396. }
  397. void
  398. test_strstr (void)
  399. {
  400. it = "strstr";
  401. check(strstr("abcd", "z") == NULL, 1); /* Not found. */
  402. check(strstr("abcd", "abx") == NULL, 2); /* Dead end. */
  403. (void) strcpy(one, "abcd");
  404. check(strstr(one, "c") == one+2, 3); /* Basic test. */
  405. check(strstr(one, "bc") == one+1, 4); /* Multichar. */
  406. check(strstr(one, "d") == one+3, 5); /* End of string. */
  407. check(strstr(one, "cd") == one+2, 6); /* Tail of string. */
  408. check(strstr(one, "abc") == one, 7); /* Beginning. */
  409. check(strstr(one, "abcd") == one, 8); /* Exact match. */
  410. check(strstr(one, "abcde") == NULL, 9); /* Too long. */
  411. check(strstr(one, "de") == NULL, 10); /* Past end. */
  412. check(strstr(one, "") == one, 11); /* Finding empty. */
  413. (void) strcpy(one, "ababa");
  414. check(strstr(one, "ba") == one+1, 12); /* Finding first. */
  415. (void) strcpy(one, "");
  416. check(strstr(one, "b") == NULL, 13); /* Empty string. */
  417. check(strstr(one, "") == one, 14); /* Empty in empty string. */
  418. (void) strcpy(one, "bcbca");
  419. check(strstr(one, "bca") == one+2, 15); /* False start. */
  420. (void) strcpy(one, "bbbcabbca");
  421. check(strstr(one, "bbca") == one+1, 16); /* With overlap. */
  422. }
  423. void
  424. test_strcasestr (void)
  425. {
  426. it = "strcasestr";
  427. check(strcasestr("abcd", "z") == NULL, 1); /* Not found. */
  428. check(strcasestr("abcd", "abx") == NULL, 2); /* Dead end. */
  429. (void) strcpy(one, "aBcD");
  430. check(strcasestr(one, "c") == one+2, 3); /* Basic test. */
  431. check(strcasestr(one, "bc") == one+1, 4); /* Multichar. */
  432. check(strcasestr(one, "d") == one+3, 5); /* End of string. */
  433. check(strcasestr(one, "cd") == one+2, 6); /* Tail of string. */
  434. check(strcasestr(one, "abc") == one, 7); /* Beginning. */
  435. check(strcasestr(one, "abcd") == one, 8); /* Exact match. */
  436. check(strcasestr(one, "abcde") == NULL, 9); /* Too long. */
  437. check(strcasestr(one, "de") == NULL, 10); /* Past end. */
  438. check(strcasestr(one, "") == one, 11); /* Finding empty. */
  439. (void) strcpy(one, "aBaBa");
  440. check(strcasestr(one, "ba") == one+1, 12); /* Finding first. */
  441. (void) strcpy(one, "");
  442. check(strcasestr(one, "b") == NULL, 13); /* Empty string. */
  443. check(strcasestr(one, "") == one, 14); /* Empty in empty string. */
  444. (void) strcpy(one, "BcBcA");
  445. check(strcasestr(one, "bca") == one+2, 15); /* False start. */
  446. (void) strcpy(one, "BbBcABBcA");
  447. check(strcasestr(one, "bbca") == one+1, 16); /* With overlap. */
  448. }
  449. void
  450. test_strspn (void)
  451. {
  452. it = "strspn";
  453. check(strspn("abcba", "abc") == 5, 1); /* Whole string. */
  454. check(strspn("abcba", "ab") == 2, 2); /* Partial. */
  455. check(strspn("abc", "qx") == 0, 3); /* None. */
  456. check(strspn("", "ab") == 0, 4); /* Null string. */
  457. check(strspn("abc", "") == 0, 5); /* Null search list. */
  458. }
  459. void
  460. test_strcspn (void)
  461. {
  462. it = "strcspn";
  463. check(strcspn("abcba", "qx") == 5, 1); /* Whole string. */
  464. check(strcspn("abcba", "cx") == 2, 2); /* Partial. */
  465. check(strcspn("abc", "abc") == 0, 3); /* None. */
  466. check(strcspn("", "ab") == 0, 4); /* Null string. */
  467. check(strcspn("abc", "") == 3, 5); /* Null search list. */
  468. }
  469. void
  470. test_strtok (void)
  471. {
  472. it = "strtok";
  473. (void) strcpy(one, "first, second, third");
  474. equal(strtok(one, ", "), "first", 1); /* Basic test. */
  475. equal(one, "first", 2);
  476. equal(strtok((char *)NULL, ", "), "second", 3);
  477. equal(strtok((char *)NULL, ", "), "third", 4);
  478. check(strtok((char *)NULL, ", ") == NULL, 5);
  479. (void) strcpy(one, ", first, ");
  480. equal(strtok(one, ", "), "first", 6); /* Extra delims, 1 tok. */
  481. check(strtok((char *)NULL, ", ") == NULL, 7);
  482. (void) strcpy(one, "1a, 1b; 2a, 2b");
  483. equal(strtok(one, ", "), "1a", 8); /* Changing delim lists. */
  484. equal(strtok((char *)NULL, "; "), "1b", 9);
  485. equal(strtok((char *)NULL, ", "), "2a", 10);
  486. (void) strcpy(two, "x-y");
  487. equal(strtok(two, "-"), "x", 11); /* New string before done. */
  488. equal(strtok((char *)NULL, "-"), "y", 12);
  489. check(strtok((char *)NULL, "-") == NULL, 13);
  490. (void) strcpy(one, "a,b, c,, ,d");
  491. equal(strtok(one, ", "), "a", 14); /* Different separators. */
  492. equal(strtok((char *)NULL, ", "), "b", 15);
  493. equal(strtok((char *)NULL, " ,"), "c", 16); /* Permute list too. */
  494. equal(strtok((char *)NULL, " ,"), "d", 17);
  495. check(strtok((char *)NULL, ", ") == NULL, 18);
  496. check(strtok((char *)NULL, ", ") == NULL, 19); /* Persistence. */
  497. (void) strcpy(one, ", ");
  498. check(strtok(one, ", ") == NULL, 20); /* No tokens. */
  499. (void) strcpy(one, "");
  500. check(strtok(one, ", ") == NULL, 21); /* Empty string. */
  501. (void) strcpy(one, "abc");
  502. equal(strtok(one, ", "), "abc", 22); /* No delimiters. */
  503. check(strtok((char *)NULL, ", ") == NULL, 23);
  504. (void) strcpy(one, "abc");
  505. equal(strtok(one, ""), "abc", 24); /* Empty delimiter list. */
  506. check(strtok((char *)NULL, "") == NULL, 25);
  507. (void) strcpy(one, "abcdefgh");
  508. (void) strcpy(one, "a,b,c");
  509. equal(strtok(one, ","), "a", 26); /* Basics again... */
  510. equal(strtok((char *)NULL, ","), "b", 27);
  511. equal(strtok((char *)NULL, ","), "c", 28);
  512. check(strtok((char *)NULL, ",") == NULL, 29);
  513. equal(one+6, "gh", 30); /* Stomped past end? */
  514. equal(one, "a", 31); /* Stomped old tokens? */
  515. equal(one+2, "b", 32);
  516. equal(one+4, "c", 33);
  517. }
  518. void
  519. test_strsep (void)
  520. {
  521. it = "strsep";
  522. cp = strcpy(one, "first, second, third");
  523. equal(strsep(&cp, ", "), "first", 1); /* Basic test. */
  524. equal(one, "first", 2);
  525. equal(strsep(&cp, ", "), "", 3);
  526. equal(strsep(&cp, ", "), "second", 4);
  527. equal(strsep(&cp, ", "), "", 5);
  528. equal(strsep(&cp, ", "), "third", 6);
  529. check(strsep(&cp, ", ") == NULL, 7);
  530. cp = strcpy(one, ", first, ");
  531. equal(strsep(&cp, ", "), "", 8);
  532. equal(strsep(&cp, ", "), "", 9);
  533. equal(strsep(&cp, ", "), "first", 10); /* Extra delims, 1 tok. */
  534. equal(strsep(&cp, ", "), "", 11);
  535. equal(strsep(&cp, ", "), "", 12);
  536. check(strsep(&cp, ", ") == NULL, 13);
  537. cp = strcpy(one, "1a, 1b; 2a, 2b");
  538. equal(strsep(&cp, ", "), "1a", 14); /* Changing delim lists. */
  539. equal(strsep(&cp, ", "), "", 15);
  540. equal(strsep(&cp, "; "), "1b", 16);
  541. equal(strsep(&cp, ", "), "", 17);
  542. equal(strsep(&cp, ", "), "2a", 18);
  543. cp = strcpy(two, "x-y");
  544. equal(strsep(&cp, "-"), "x", 19); /* New string before done. */
  545. equal(strsep(&cp, "-"), "y", 20);
  546. check(strsep(&cp, "-") == NULL, 21);
  547. cp = strcpy(one, "a,b, c,, ,d ");
  548. equal(strsep(&cp, ", "), "a", 22); /* Different separators. */
  549. equal(strsep(&cp, ", "), "b", 23);
  550. equal(strsep(&cp, " ,"), "", 24);
  551. equal(strsep(&cp, " ,"), "c", 25); /* Permute list too. */
  552. equal(strsep(&cp, " ,"), "", 26);
  553. equal(strsep(&cp, " ,"), "", 27);
  554. equal(strsep(&cp, " ,"), "", 28);
  555. equal(strsep(&cp, " ,"), "d", 29);
  556. equal(strsep(&cp, " ,"), "", 30);
  557. check(strsep(&cp, ", ") == NULL, 31);
  558. check(strsep(&cp, ", ") == NULL, 32); /* Persistence. */
  559. cp = strcpy(one, ", ");
  560. equal(strsep(&cp, ", "), "", 33);
  561. equal(strsep(&cp, ", "), "", 34);
  562. equal(strsep(&cp, ", "), "", 35);
  563. check(strsep(&cp, ", ") == NULL, 36); /* No tokens. */
  564. cp = strcpy(one, "");
  565. equal(strsep(&cp, ", "), "", 37);
  566. check(strsep(&cp, ", ") == NULL, 38); /* Empty string. */
  567. cp = strcpy(one, "abc");
  568. equal(strsep(&cp, ", "), "abc", 39); /* No delimiters. */
  569. check(strsep(&cp, ", ") == NULL, 40);
  570. cp = strcpy(one, "abc");
  571. equal(strsep(&cp, ""), "abc", 41); /* Empty delimiter list. */
  572. check(strsep(&cp, "") == NULL, 42);
  573. (void) strcpy(one, "abcdefgh");
  574. cp = strcpy(one, "a,b,c");
  575. equal(strsep(&cp, ","), "a", 43); /* Basics again... */
  576. equal(strsep(&cp, ","), "b", 44);
  577. equal(strsep(&cp, ","), "c", 45);
  578. check(strsep(&cp, ",") == NULL, 46);
  579. equal(one+6, "gh", 47); /* Stomped past end? */
  580. equal(one, "a", 48); /* Stomped old tokens? */
  581. equal(one+2, "b", 49);
  582. equal(one+4, "c", 50);
  583. {
  584. char text[] = "This,is,a,test";
  585. char *list = strdup (text);
  586. equal (strsep (&list, ","), "This", 51);
  587. equal (strsep (&list, ","), "is", 52);
  588. equal (strsep (&list, ","), "a", 53);
  589. equal (strsep (&list, ","), "test", 54);
  590. check (strsep (&list, ",") == NULL, 55);
  591. }
  592. cp = strcpy(one, "a,b, c,, ,d,");
  593. equal(strsep(&cp, ","), "a", 56); /* Different separators. */
  594. equal(strsep(&cp, ","), "b", 57);
  595. equal(strsep(&cp, ","), " c", 58); /* Permute list too. */
  596. equal(strsep(&cp, ","), "", 59);
  597. equal(strsep(&cp, ","), " ", 60);
  598. equal(strsep(&cp, ","), "d", 61);
  599. equal(strsep(&cp, ","), "", 62);
  600. check(strsep(&cp, ",") == NULL, 63);
  601. check(strsep(&cp, ",") == NULL, 64); /* Persistence. */
  602. cp = strcpy(one, "a,b, c,, ,d,");
  603. equal(strsep(&cp, "xy,"), "a", 65); /* Different separators. */
  604. equal(strsep(&cp, "x,y"), "b", 66);
  605. equal(strsep(&cp, ",xy"), " c", 67); /* Permute list too. */
  606. equal(strsep(&cp, "xy,"), "", 68);
  607. equal(strsep(&cp, "x,y"), " ", 69);
  608. equal(strsep(&cp, ",xy"), "d", 70);
  609. equal(strsep(&cp, "xy,"), "", 71);
  610. check(strsep(&cp, "x,y") == NULL, 72);
  611. check(strsep(&cp, ",xy") == NULL, 73); /* Persistence. */
  612. }
  613. void
  614. test_memcmp (void)
  615. {
  616. int foo[6] = {'\0','0','1','2','3','4'};
  617. int bar[6] = {'\0','0','1','2','3','5'};
  618. it = "memcmp";
  619. check(memcmp("a", "a", 1) == 0, 1); /* Identity. */
  620. check(memcmp("abc", "abc", 3) == 0, 2); /* Multicharacter. */
  621. check(memcmp("abcd", "abce", 4) < 0, 3); /* Honestly unequal. */
  622. check(memcmp("abce", "abcd", 4) > 0, 4);
  623. check(memcmp("alph", "beta", 4) < 0, 5);
  624. check(memcmp("a\203", "a\003", 2) > 0, 6);
  625. check(memcmp("abce", "abcd", 3) == 0, 7); /* Count limited. */
  626. check(memcmp("abc", "def", 0) == 0, 8); /* Zero count. */
  627. check(memcmp(foo, bar, 5*(sizeof(int))) == 0, 9); /* non-string test
  628. case (both begin
  629. with a NULL) */
  630. check(memcmp(foo, bar, 6*(sizeof(int))) < 0, 10); /* non-string test
  631. case (both begin
  632. with a NULL) */
  633. }
  634. void
  635. test_memchr (void)
  636. {
  637. it = "memchr";
  638. check(memchr("abcd", 'z', 4) == NULL, 1); /* Not found. */
  639. (void) strcpy(one, "abcd");
  640. check(memchr(one, 'c', 4) == one+2, 2); /* Basic test. */
  641. check(memchr(one, ~0xff|'c', 4) == one+2, 2); /* ignore highorder bits. */
  642. check(memchr(one, 'd', 4) == one+3, 3); /* End of string. */
  643. check(memchr(one, 'a', 4) == one, 4); /* Beginning. */
  644. check(memchr(one, '\0', 5) == one+4, 5); /* Finding NUL. */
  645. (void) strcpy(one, "ababa");
  646. check(memchr(one, 'b', 5) == one+1, 6); /* Finding first. */
  647. check(memchr(one, 'b', 0) == NULL, 7); /* Zero count. */
  648. check(memchr(one, 'a', 1) == one, 8); /* Singleton case. */
  649. (void) strcpy(one, "a\203b");
  650. check(memchr(one, 0203, 3) == one+1, 9); /* Unsignedness. */
  651. /* now test all possible alignment and length combinations to catch
  652. bugs due to unrolled loops (assuming unrolling is limited to no
  653. more than 128 byte chunks: */
  654. {
  655. char buf[128 + sizeof(long)];
  656. long align, len, i, pos;
  657. for (align = 0; align < (long) sizeof(long); ++align) {
  658. for (len = 0; len < (long) (sizeof(buf) - align); ++len) {
  659. for (i = 0; i < len; ++i) {
  660. buf[align + i] = 'x'; /* don't depend on memset... */
  661. }
  662. for (pos = 0; pos < len; ++pos) {
  663. #if 0
  664. printf("align %d, len %d, pos %d\n", align, len, pos);
  665. #endif
  666. check(memchr(buf + align, 'x', len) == buf + align + pos, 10);
  667. check(memchr(buf + align, 'x', pos) == NULL, 11);
  668. buf[align + pos] = '-';
  669. }
  670. }
  671. }
  672. }
  673. }
  674. void
  675. test_memcpy (void)
  676. {
  677. it = "memcpy";
  678. check(memcpy(one, "abc", 4) == one, 1); /* Returned value. */
  679. equal(one, "abc", 2); /* Did the copy go right? */
  680. (void) strcpy(one, "abcdefgh");
  681. (void) memcpy(one+1, "xyz", 2);
  682. equal(one, "axydefgh", 3); /* Basic test. */
  683. (void) strcpy(one, "abc");
  684. (void) memcpy(one, "xyz", 0);
  685. equal(one, "abc", 4); /* Zero-length copy. */
  686. (void) strcpy(one, "hi there");
  687. (void) strcpy(two, "foo");
  688. (void) memcpy(two, one, 9);
  689. equal(two, "hi there", 5); /* Just paranoia. */
  690. equal(one, "hi there", 6); /* Stomped on source? */
  691. }
  692. void
  693. test_memmove (void)
  694. {
  695. it = "memmove";
  696. check(memmove(one, "abc", 4) == one, 1); /* Returned value. */
  697. equal(one, "abc", 2); /* Did the copy go right? */
  698. (void) strcpy(one, "abcdefgh");
  699. (void) memmove(one+1, "xyz", 2);
  700. equal(one, "axydefgh", 3); /* Basic test. */
  701. (void) strcpy(one, "abc");
  702. (void) memmove(one, "xyz", 0);
  703. equal(one, "abc", 4); /* Zero-length copy. */
  704. (void) strcpy(one, "hi there");
  705. (void) strcpy(two, "foo");
  706. (void) memmove(two, one, 9);
  707. equal(two, "hi there", 5); /* Just paranoia. */
  708. equal(one, "hi there", 6); /* Stomped on source? */
  709. (void) strcpy(one, "abcdefgh");
  710. (void) memmove(one+1, one, 9);
  711. equal(one, "aabcdefgh", 7); /* Overlap, right-to-left. */
  712. (void) strcpy(one, "abcdefgh");
  713. (void) memmove(one+1, one+2, 7);
  714. equal(one, "acdefgh", 8); /* Overlap, left-to-right. */
  715. (void) strcpy(one, "abcdefgh");
  716. (void) memmove(one, one, 9);
  717. equal(one, "abcdefgh", 9); /* 100% overlap. */
  718. }
  719. void
  720. test_memccpy (void)
  721. {
  722. /* First test like memcpy, then the search part The SVID, the only
  723. place where memccpy is mentioned, says overlap might fail, so we
  724. don't try it. Besides, it's hard to see the rationale for a
  725. non-left-to-right memccpy. */
  726. it = "memccpy";
  727. check(memccpy(one, "abc", 'q', 4) == NULL, 1); /* Returned value. */
  728. equal(one, "abc", 2); /* Did the copy go right? */
  729. (void) strcpy(one, "abcdefgh");
  730. (void) memccpy(one+1, "xyz", 'q', 2);
  731. equal(one, "axydefgh", 3); /* Basic test. */
  732. (void) strcpy(one, "abc");
  733. (void) memccpy(one, "xyz", 'q', 0);
  734. equal(one, "abc", 4); /* Zero-length copy. */
  735. (void) strcpy(one, "hi there");
  736. (void) strcpy(two, "foo");
  737. (void) memccpy(two, one, 'q', 9);
  738. equal(two, "hi there", 5); /* Just paranoia. */
  739. equal(one, "hi there", 6); /* Stomped on source? */
  740. (void) strcpy(one, "abcdefgh");
  741. (void) strcpy(two, "horsefeathers");
  742. check(memccpy(two, one, 'f', 9) == two+6, 7); /* Returned value. */
  743. equal(one, "abcdefgh", 8); /* Source intact? */
  744. equal(two, "abcdefeathers", 9); /* Copy correct? */
  745. (void) strcpy(one, "abcd");
  746. (void) strcpy(two, "bumblebee");
  747. check(memccpy(two, one, 'a', 4) == two+1, 10); /* First char. */
  748. equal(two, "aumblebee", 11);
  749. check(memccpy(two, one, 'd', 4) == two+4, 12); /* Last char. */
  750. equal(two, "abcdlebee", 13);
  751. (void) strcpy(one, "xyz");
  752. check(memccpy(two, one, 'x', 1) == two+1, 14); /* Singleton. */
  753. equal(two, "xbcdlebee", 15);
  754. }
  755. void
  756. test_memset (void)
  757. {
  758. int i;
  759. it = "memset";
  760. (void) strcpy(one, "abcdefgh");
  761. check(memset(one+1, 'x', 3) == one+1, 1); /* Return value. */
  762. equal(one, "axxxefgh", 2); /* Basic test. */
  763. (void) memset(one+2, 'y', 0);
  764. equal(one, "axxxefgh", 3); /* Zero-length set. */
  765. (void) memset(one+5, 0, 1);
  766. equal(one, "axxxe", 4); /* Zero fill. */
  767. equal(one+6, "gh", 5); /* And the leftover. */
  768. (void) memset(one+2, 010045, 1);
  769. equal(one, "ax\045xe", 6); /* Unsigned char convert. */
  770. /* Non-8bit fill character. */
  771. memset (one, 0x101, sizeof (one));
  772. for (i = 0; i < sizeof (one); ++i)
  773. check (one[i] == '\01', 7);
  774. /* Test for more complex versions of memset, for all alignments and
  775. lengths up to 256. This test takes a little while, perhaps it should
  776. be made weaker? */
  777. {
  778. char data[512];
  779. int j;
  780. int k;
  781. int c;
  782. for (i = 0; i < 512; i++)
  783. data[i] = 'x';
  784. for (c = 0; c <= 'y'; c += 'y') /* check for memset(,0,) and
  785. memset(,'y',) */
  786. for (j = 0; j < 256; j++)
  787. for (i = 0; i < 256; i++)
  788. {
  789. memset (data + i, c, j);
  790. for (k = 0; k < i; k++)
  791. if (data[k] != 'x')
  792. goto fail;
  793. for (k = i; k < i+j; k++)
  794. {
  795. if (data[k] != c)
  796. goto fail;
  797. data[k] = 'x';
  798. }
  799. for (k = i+j; k < 512; k++)
  800. if (data[k] != 'x')
  801. goto fail;
  802. continue;
  803. fail:
  804. check (0, 8 + i + j * 256 + (c != 0) * 256 * 256);
  805. }
  806. }
  807. }
  808. void
  809. test_bcopy (void)
  810. {
  811. /* Much like memcpy. Berklix manual is silent about overlap, so
  812. don't test it. */
  813. it = "bcopy";
  814. (void) bcopy("abc", one, 4);
  815. equal(one, "abc", 1); /* Simple copy. */
  816. (void) strcpy(one, "abcdefgh");
  817. (void) bcopy("xyz", one+1, 2);
  818. equal(one, "axydefgh", 2); /* Basic test. */
  819. (void) strcpy(one, "abc");
  820. (void) bcopy("xyz", one, 0);
  821. equal(one, "abc", 3); /* Zero-length copy. */
  822. (void) strcpy(one, "hi there");
  823. (void) strcpy(two, "foo");
  824. (void) bcopy(one, two, 9);
  825. equal(two, "hi there", 4); /* Just paranoia. */
  826. equal(one, "hi there", 5); /* Stomped on source? */
  827. }
  828. void
  829. test_bzero (void)
  830. {
  831. it = "bzero";
  832. (void) strcpy(one, "abcdef");
  833. bzero(one+2, 2);
  834. equal(one, "ab", 1); /* Basic test. */
  835. equal(one+3, "", 2);
  836. equal(one+4, "ef", 3);
  837. (void) strcpy(one, "abcdef");
  838. bzero(one+2, 0);
  839. equal(one, "abcdef", 4); /* Zero-length copy. */
  840. }
  841. void
  842. test_bcmp (void)
  843. {
  844. it = "bcmp";
  845. check(bcmp("a", "a", 1) == 0, 1); /* Identity. */
  846. check(bcmp("abc", "abc", 3) == 0, 2); /* Multicharacter. */
  847. check(bcmp("abcd", "abce", 4) != 0, 3); /* Honestly unequal. */
  848. check(bcmp("abce", "abcd", 4) != 0, 4);
  849. check(bcmp("alph", "beta", 4) != 0, 5);
  850. check(bcmp("abce", "abcd", 3) == 0, 6); /* Count limited. */
  851. check(bcmp("abc", "def", 0) == 0, 8); /* Zero count. */
  852. }
  853. void
  854. test_strerror (void)
  855. {
  856. int f;
  857. it = "strerror";
  858. f = open("/", O_WRONLY); /* Should always fail. */
  859. check(f < 0 && errno > 0 && errno < _sys_nerr, 1);
  860. equal(strerror(errno), _sys_errlist[errno], 2);
  861. }
  862. int
  863. main (void)
  864. {
  865. int status;
  866. /* Test strcmp first because we use it to test other things. */
  867. test_strcmp ();
  868. /* Test strcpy next because we need it to set up other tests. */
  869. test_strcpy ();
  870. /* stpcpy */
  871. test_stpcpy ();
  872. /* strcat. */
  873. test_strcat ();
  874. /* strncat. */
  875. test_strncat ();
  876. /* strncmp. */
  877. test_strncmp ();
  878. /* strncpy. */
  879. test_strncpy ();
  880. /* strlen. */
  881. test_strlen ();
  882. /* strchr. */
  883. test_strchr ();
  884. /* index - just like strchr. */
  885. test_index ();
  886. /* strrchr. */
  887. test_strrchr ();
  888. /* rindex - just like strrchr. */
  889. test_rindex ();
  890. /* strpbrk - somewhat like strchr. */
  891. test_strpbrk ();
  892. /* strstr - somewhat like strchr. */
  893. test_strstr ();
  894. test_strcasestr ();
  895. /* strspn. */
  896. test_strspn ();
  897. /* strcspn. */
  898. test_strcspn ();
  899. /* strtok - the hard one. */
  900. test_strtok ();
  901. /* strsep. */
  902. test_strsep ();
  903. /* memcmp. */
  904. test_memcmp ();
  905. /* memchr. */
  906. test_memchr ();
  907. /* memcpy - need not work for overlap. */
  908. test_memcpy ();
  909. /* memmove - must work on overlap. */
  910. test_memmove ();
  911. /* memccpy. */
  912. test_memccpy ();
  913. /* memset. */
  914. test_memset ();
  915. /* bcopy. */
  916. test_bcopy ();
  917. /* bzero. */
  918. test_bzero ();
  919. /* bcmp - somewhat like memcmp. */
  920. test_bcmp ();
  921. /* strerror - VERY system-dependent. */
  922. test_strerror ();
  923. if (errors == 0)
  924. {
  925. status = EXIT_SUCCESS;
  926. puts("No errors.");
  927. }
  928. else
  929. {
  930. status = EXIT_FAILURE;
  931. printf("%d errors.\n", errors);
  932. }
  933. exit(status);
  934. }