string.c 30 KB

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