string.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /* Tester for string functions.
  2. Copyright (C) 1995-2000, 2001 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 Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _GNU_SOURCE
  17. #define _GNU_SOURCE
  18. #endif
  19. /* Make sure we don't test the optimized inline functions if we want to
  20. test the real implementation. */
  21. #if !defined DO_STRING_INLINES
  22. #undef __USE_STRING_INLINES
  23. #endif
  24. #include <errno.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <strings.h>
  29. #include <fcntl.h>
  30. #define STREQ(a, b) (strcmp((a), (b)) == 0)
  31. const char *it = "<UNSET>"; /* Routine name for message routines. */
  32. size_t errors = 0;
  33. /* Complain if condition is not true. */
  34. static void
  35. check (int thing, int number)
  36. {
  37. if (!thing)
  38. {
  39. printf("%s flunked test %d\n", it, number);
  40. ++errors;
  41. }
  42. }
  43. /* Complain if first two args don't strcmp as equal. */
  44. static void
  45. equal (const char *a, const char *b, int number)
  46. {
  47. check(a != NULL && b != NULL && STREQ (a, b), number);
  48. }
  49. char one[50];
  50. char two[50];
  51. char *cp;
  52. static void
  53. test_strcmp (void)
  54. {
  55. it = "strcmp";
  56. check (strcmp ("", "") == 0, 1); /* Trivial case. */
  57. check (strcmp ("a", "a") == 0, 2); /* Identity. */
  58. check (strcmp ("abc", "abc") == 0, 3); /* Multicharacter. */
  59. check (strcmp ("abc", "abcd") < 0, 4); /* Length mismatches. */
  60. check (strcmp ("abcd", "abc") > 0, 5);
  61. check (strcmp ("abcd", "abce") < 0, 6); /* Honest miscompares. */
  62. check (strcmp ("abce", "abcd") > 0, 7);
  63. check (strcmp ("a\203", "a") > 0, 8); /* Tricky if char signed. */
  64. check (strcmp ("a\203", "a\003") > 0, 9);
  65. {
  66. char buf1[0x40], buf2[0x40];
  67. int i, j;
  68. for (i=0; i < 0x10; i++)
  69. for (j = 0; j < 0x10; j++)
  70. {
  71. int k;
  72. for (k = 0; k < 0x3f; k++)
  73. {
  74. buf1[j] = '0' ^ (k & 4);
  75. buf2[j] = '4' ^ (k & 4);
  76. }
  77. buf1[i] = buf1[0x3f] = 0;
  78. buf2[j] = buf2[0x3f] = 0;
  79. for (k = 0; k < 0xf; k++)
  80. {
  81. int cnum = 0x10+0x10*k+0x100*j+0x1000*i;
  82. check (strcmp (buf1+i,buf2+j) == 0, cnum);
  83. buf1[i+k] = 'A' + i + k;
  84. buf1[i+k+1] = 0;
  85. check (strcmp (buf1+i,buf2+j) > 0, cnum+1);
  86. check (strcmp (buf2+j,buf1+i) < 0, cnum+2);
  87. buf2[j+k] = 'B' + i + k;
  88. buf2[j+k+1] = 0;
  89. check (strcmp (buf1+i,buf2+j) < 0, cnum+3);
  90. check (strcmp (buf2+j,buf1+i) > 0, cnum+4);
  91. buf2[j+k] = 'A' + i + k;
  92. buf1[i] = 'A' + i + 0x80;
  93. check (strcmp (buf1+i,buf2+j) > 0, cnum+5);
  94. check (strcmp (buf2+j,buf1+i) < 0, cnum+6);
  95. buf1[i] = 'A' + i;
  96. }
  97. }
  98. }
  99. }
  100. #define SIMPLE_COPY(fn, n, str, ntest) \
  101. do { \
  102. int __n; \
  103. char *cp; \
  104. for (__n = 0; __n < (int) sizeof (one); ++__n) \
  105. one[__n] = 'Z'; \
  106. fn (one, str); \
  107. for (cp = one, __n = 0; __n < n; ++__n, ++cp) \
  108. check (*cp == '0' + (n % 10), ntest); \
  109. check (*cp == '\0', ntest); \
  110. } while (0)
  111. static void
  112. test_strcpy (void)
  113. {
  114. int i;
  115. it = "strcpy";
  116. check (strcpy (one, "abcd") == one, 1); /* Returned value. */
  117. equal (one, "abcd", 2); /* Basic test. */
  118. (void) strcpy (one, "x");
  119. equal (one, "x", 3); /* Writeover. */
  120. equal (one+2, "cd", 4); /* Wrote too much? */
  121. (void) strcpy (two, "hi there");
  122. (void) strcpy (one, two);
  123. equal (one, "hi there", 5); /* Basic test encore. */
  124. equal (two, "hi there", 6); /* Stomped on source? */
  125. (void) strcpy (one, "");
  126. equal (one, "", 7); /* Boundary condition. */
  127. for (i = 0; i < 16; i++)
  128. {
  129. (void) strcpy (one + i, "hi there"); /* Unaligned destination. */
  130. equal (one + i, "hi there", 8 + (i * 2));
  131. (void) strcpy (two, one + i); /* Unaligned source. */
  132. equal (two, "hi there", 9 + (i * 2));
  133. }
  134. SIMPLE_COPY(strcpy, 0, "", 41);
  135. SIMPLE_COPY(strcpy, 1, "1", 42);
  136. SIMPLE_COPY(strcpy, 2, "22", 43);
  137. SIMPLE_COPY(strcpy, 3, "333", 44);
  138. SIMPLE_COPY(strcpy, 4, "4444", 45);
  139. SIMPLE_COPY(strcpy, 5, "55555", 46);
  140. SIMPLE_COPY(strcpy, 6, "666666", 47);
  141. SIMPLE_COPY(strcpy, 7, "7777777", 48);
  142. SIMPLE_COPY(strcpy, 8, "88888888", 49);
  143. SIMPLE_COPY(strcpy, 9, "999999999", 50);
  144. SIMPLE_COPY(strcpy, 10, "0000000000", 51);
  145. SIMPLE_COPY(strcpy, 11, "11111111111", 52);
  146. SIMPLE_COPY(strcpy, 12, "222222222222", 53);
  147. SIMPLE_COPY(strcpy, 13, "3333333333333", 54);
  148. SIMPLE_COPY(strcpy, 14, "44444444444444", 55);
  149. SIMPLE_COPY(strcpy, 15, "555555555555555", 56);
  150. SIMPLE_COPY(strcpy, 16, "6666666666666666", 57);
  151. }
  152. static void
  153. test_stpcpy (void)
  154. {
  155. it = "stpcpy";
  156. check ((stpcpy (one, "a") - one) == 1, 1);
  157. equal (one, "a", 2);
  158. check ((stpcpy (one, "ab") - one) == 2, 3);
  159. equal (one, "ab", 4);
  160. check ((stpcpy (one, "abc") - one) == 3, 5);
  161. equal (one, "abc", 6);
  162. check ((stpcpy (one, "abcd") - one) == 4, 7);
  163. equal (one, "abcd", 8);
  164. check ((stpcpy (one, "abcde") - one) == 5, 9);
  165. equal (one, "abcde", 10);
  166. check ((stpcpy (one, "abcdef") - one) == 6, 11);
  167. equal (one, "abcdef", 12);
  168. check ((stpcpy (one, "abcdefg") - one) == 7, 13);
  169. equal (one, "abcdefg", 14);
  170. check ((stpcpy (one, "abcdefgh") - one) == 8, 15);
  171. equal (one, "abcdefgh", 16);
  172. check ((stpcpy (one, "abcdefghi") - one) == 9, 17);
  173. equal (one, "abcdefghi", 18);
  174. check ((stpcpy (one, "x") - one) == 1, 19);
  175. equal (one, "x", 20); /* Writeover. */
  176. equal (one+2, "cdefghi", 21); /* Wrote too much? */
  177. check ((stpcpy (one, "xx") - one) == 2, 22);
  178. equal (one, "xx", 23); /* Writeover. */
  179. equal (one+3, "defghi", 24); /* Wrote too much? */
  180. check ((stpcpy (one, "xxx") - one) == 3, 25);
  181. equal (one, "xxx", 26); /* Writeover. */
  182. equal (one+4, "efghi", 27); /* Wrote too much? */
  183. check ((stpcpy (one, "xxxx") - one) == 4, 28);
  184. equal (one, "xxxx", 29); /* Writeover. */
  185. equal (one+5, "fghi", 30); /* Wrote too much? */
  186. check ((stpcpy (one, "xxxxx") - one) == 5, 31);
  187. equal (one, "xxxxx", 32); /* Writeover. */
  188. equal (one+6, "ghi", 33); /* Wrote too much? */
  189. check ((stpcpy (one, "xxxxxx") - one) == 6, 34);
  190. equal (one, "xxxxxx", 35); /* Writeover. */
  191. equal (one+7, "hi", 36); /* Wrote too much? */
  192. check ((stpcpy (one, "xxxxxxx") - one) == 7, 37);
  193. equal (one, "xxxxxxx", 38); /* Writeover. */
  194. equal (one+8, "i", 39); /* Wrote too much? */
  195. check ((stpcpy (stpcpy (stpcpy (one, "a"), "b"), "c") - one) == 3, 40);
  196. equal (one, "abc", 41);
  197. equal (one + 4, "xxx", 42);
  198. SIMPLE_COPY(stpcpy, 0, "", 43);
  199. SIMPLE_COPY(stpcpy, 1, "1", 44);
  200. SIMPLE_COPY(stpcpy, 2, "22", 45);
  201. SIMPLE_COPY(stpcpy, 3, "333", 46);
  202. SIMPLE_COPY(stpcpy, 4, "4444", 47);
  203. SIMPLE_COPY(stpcpy, 5, "55555", 48);
  204. SIMPLE_COPY(stpcpy, 6, "666666", 49);
  205. SIMPLE_COPY(stpcpy, 7, "7777777", 50);
  206. SIMPLE_COPY(stpcpy, 8, "88888888", 51);
  207. SIMPLE_COPY(stpcpy, 9, "999999999", 52);
  208. SIMPLE_COPY(stpcpy, 10, "0000000000", 53);
  209. SIMPLE_COPY(stpcpy, 11, "11111111111", 54);
  210. SIMPLE_COPY(stpcpy, 12, "222222222222", 55);
  211. SIMPLE_COPY(stpcpy, 13, "3333333333333", 56);
  212. SIMPLE_COPY(stpcpy, 14, "44444444444444", 57);
  213. SIMPLE_COPY(stpcpy, 15, "555555555555555", 58);
  214. SIMPLE_COPY(stpcpy, 16, "6666666666666666", 59);
  215. }
  216. static void
  217. test_stpncpy (void)
  218. {
  219. it = "stpncpy";
  220. memset (one, 'x', sizeof (one));
  221. check (stpncpy (one, "abc", 2) == one + 2, 1);
  222. check (stpncpy (one, "abc", 3) == one + 3, 2);
  223. check (stpncpy (one, "abc", 4) == one + 3, 3);
  224. check (one[3] == '\0' && one[4] == 'x', 4);
  225. check (stpncpy (one, "abcd", 5) == one + 4, 5);
  226. check (one[4] == '\0' && one[5] == 'x', 6);
  227. check (stpncpy (one, "abcd", 6) == one + 4, 7);
  228. check (one[4] == '\0' && one[5] == '\0' && one[6] == 'x', 8);
  229. }
  230. static void
  231. test_strcat (void)
  232. {
  233. it = "strcat";
  234. (void) strcpy (one, "ijk");
  235. check (strcat (one, "lmn") == one, 1); /* Returned value. */
  236. equal (one, "ijklmn", 2); /* Basic test. */
  237. (void) strcpy (one, "x");
  238. (void) strcat (one, "yz");
  239. equal (one, "xyz", 3); /* Writeover. */
  240. equal (one+4, "mn", 4); /* Wrote too much? */
  241. (void) strcpy (one, "gh");
  242. (void) strcpy (two, "ef");
  243. (void) strcat (one, two);
  244. equal (one, "ghef", 5); /* Basic test encore. */
  245. equal (two, "ef", 6); /* Stomped on source? */
  246. (void) strcpy (one, "");
  247. (void) strcat (one, "");
  248. equal (one, "", 7); /* Boundary conditions. */
  249. (void) strcpy (one, "ab");
  250. (void) strcat (one, "");
  251. equal (one, "ab", 8);
  252. (void) strcpy (one, "");
  253. (void) strcat (one, "cd");
  254. equal (one, "cd", 9);
  255. }
  256. static void
  257. test_strncat (void)
  258. {
  259. /* First test it as strcat, with big counts, then test the count
  260. mechanism. */
  261. it = "strncat";
  262. (void) strcpy (one, "ijk");
  263. check (strncat (one, "lmn", 99) == one, 1); /* Returned value. */
  264. equal (one, "ijklmn", 2); /* Basic test. */
  265. (void) strcpy (one, "x");
  266. (void) strncat (one, "yz", 99);
  267. equal (one, "xyz", 3); /* Writeover. */
  268. equal (one+4, "mn", 4); /* Wrote too much? */
  269. (void) strcpy (one, "gh");
  270. (void) strcpy (two, "ef");
  271. (void) strncat (one, two, 99);
  272. equal (one, "ghef", 5); /* Basic test encore. */
  273. equal (two, "ef", 6); /* Stomped on source? */
  274. (void) strcpy (one, "");
  275. (void) strncat (one, "", 99);
  276. equal (one, "", 7); /* Boundary conditions. */
  277. (void) strcpy (one, "ab");
  278. (void) strncat (one, "", 99);
  279. equal (one, "ab", 8);
  280. (void) strcpy (one, "");
  281. (void) strncat (one, "cd", 99);
  282. equal (one, "cd", 9);
  283. (void) strcpy (one, "ab");
  284. (void) strncat (one, "cdef", 2);
  285. equal (one, "abcd", 10); /* Count-limited. */
  286. (void) strncat (one, "gh", 0);
  287. equal (one, "abcd", 11); /* Zero count. */
  288. (void) strncat (one, "gh", 2);
  289. equal (one, "abcdgh", 12); /* Count and length equal. */
  290. }
  291. static void
  292. test_strncmp (void)
  293. {
  294. /* First test as strcmp with big counts, then test count code. */
  295. it = "strncmp";
  296. check (strncmp ("", "", 99) == 0, 1); /* Trivial case. */
  297. check (strncmp ("a", "a", 99) == 0, 2); /* Identity. */
  298. check (strncmp ("abc", "abc", 99) == 0, 3); /* Multicharacter. */
  299. check (strncmp ("abc", "abcd", 99) < 0, 4); /* Length unequal. */
  300. check (strncmp ("abcd", "abc", 99) > 0, 5);
  301. check (strncmp ("abcd", "abce", 99) < 0, 6); /* Honestly unequal. */
  302. check (strncmp ("abce", "abcd", 99) > 0, 7);
  303. check (strncmp ("a\203", "a", 2) > 0, 8); /* Tricky if '\203' < 0 */
  304. check (strncmp ("a\203", "a\003", 2) > 0, 9);
  305. check (strncmp ("abce", "abcd", 3) == 0, 10); /* Count limited. */
  306. check (strncmp ("abce", "abc", 3) == 0, 11); /* Count == length. */
  307. check (strncmp ("abcd", "abce", 4) < 0, 12); /* Nudging limit. */
  308. check (strncmp ("abc", "def", 0) == 0, 13); /* Zero count. */
  309. }
  310. static void
  311. test_strncpy (void)
  312. {
  313. /* Testing is a bit different because of odd semantics. */
  314. it = "strncpy";
  315. check (strncpy (one, "abc", 4) == one, 1); /* Returned value. */
  316. equal (one, "abc", 2); /* Did the copy go right? */
  317. (void) strcpy (one, "abcdefgh");
  318. (void) strncpy (one, "xyz", 2);
  319. equal (one, "xycdefgh", 3); /* Copy cut by count. */
  320. (void) strcpy (one, "abcdefgh");
  321. (void) strncpy (one, "xyz", 3); /* Copy cut just before NUL. */
  322. equal (one, "xyzdefgh", 4);
  323. (void) strcpy (one, "abcdefgh");
  324. (void) strncpy (one, "xyz", 4); /* Copy just includes NUL. */
  325. equal (one, "xyz", 5);
  326. equal (one+4, "efgh", 6); /* Wrote too much? */
  327. (void) strcpy (one, "abcdefgh");
  328. (void) strncpy (one, "xyz", 5); /* Copy includes padding. */
  329. equal (one, "xyz", 7);
  330. equal (one+4, "", 8);
  331. equal (one+5, "fgh", 9);
  332. (void) strcpy (one, "abc");
  333. (void) strncpy (one, "xyz", 0); /* Zero-length copy. */
  334. equal (one, "abc", 10);
  335. (void) strncpy (one, "", 2); /* Zero-length source. */
  336. equal (one, "", 11);
  337. equal (one+1, "", 12);
  338. equal (one+2, "c", 13);
  339. (void) strcpy (one, "hi there");
  340. (void) strncpy (two, one, 9);
  341. equal (two, "hi there", 14); /* Just paranoia. */
  342. equal (one, "hi there", 15); /* Stomped on source? */
  343. }
  344. static void
  345. test_strlen (void)
  346. {
  347. it = "strlen";
  348. check (strlen ("") == 0, 1); /* Empty. */
  349. check (strlen ("a") == 1, 2); /* Single char. */
  350. check (strlen ("abcd") == 4, 3); /* Multiple chars. */
  351. {
  352. char buf[4096];
  353. int i;
  354. char *p;
  355. for (i=0; i < 0x100; i++)
  356. {
  357. p = (char *) ((unsigned long int)(buf + 0xff) & ~0xff) + i;
  358. strcpy (p, "OK");
  359. strcpy (p+3, "BAD/WRONG");
  360. check (strlen (p) == 2, 4+i);
  361. }
  362. }
  363. }
  364. static void
  365. test_strchr (void)
  366. {
  367. it = "strchr";
  368. check (strchr ("abcd", 'z') == NULL, 1); /* Not found. */
  369. (void) strcpy (one, "abcd");
  370. check (strchr (one, 'c') == one+2, 2); /* Basic test. */
  371. check (strchr (one, 'd') == one+3, 3); /* End of string. */
  372. check (strchr (one, 'a') == one, 4); /* Beginning. */
  373. check (strchr (one, '\0') == one+4, 5); /* Finding NUL. */
  374. (void) strcpy (one, "ababa");
  375. check (strchr (one, 'b') == one+1, 6); /* Finding first. */
  376. (void) strcpy (one, "");
  377. check (strchr (one, 'b') == NULL, 7); /* Empty string. */
  378. check (strchr (one, '\0') == one, 8); /* NUL in empty string. */
  379. {
  380. char buf[4096];
  381. int i;
  382. char *p;
  383. for (i=0; i < 0x100; i++)
  384. {
  385. p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
  386. strcpy (p, "OK");
  387. strcpy (p+3, "BAD/WRONG");
  388. check (strchr (p, '/') == NULL, 9+i);
  389. }
  390. }
  391. }
  392. static void
  393. test_strchrnul (void)
  394. {
  395. const char *os;
  396. it = "strchrnul";
  397. cp = strchrnul ((os = "abcd"), 'z');
  398. check (*cp == '\0', 1); /* Not found. */
  399. check (cp == os + 4, 2);
  400. (void) strcpy (one, "abcd");
  401. check (strchrnul (one, 'c') == one+2, 3); /* Basic test. */
  402. check (strchrnul (one, 'd') == one+3, 4); /* End of string. */
  403. check (strchrnul (one, 'a') == one, 5); /* Beginning. */
  404. check (strchrnul (one, '\0') == one+4, 6); /* Finding NUL. */
  405. (void) strcpy (one, "ababa");
  406. check (strchrnul (one, 'b') == one+1, 7); /* Finding first. */
  407. (void) strcpy (one, "");
  408. check (strchrnul (one, 'b') == one, 8); /* Empty string. */
  409. check (strchrnul (one, '\0') == one, 9); /* NUL in empty string. */
  410. {
  411. char buf[4096];
  412. int i;
  413. char *p;
  414. for (i=0; i < 0x100; i++)
  415. {
  416. p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
  417. strcpy (p, "OK");
  418. strcpy (p+3, "BAD/WRONG");
  419. cp = strchrnul (p, '/');
  420. check (*cp == '\0', 9+2*i);
  421. check (cp == p+2, 10+2*i);
  422. }
  423. }
  424. }
  425. static void
  426. test_rawmemchr (void)
  427. {
  428. it = "rawmemchr";
  429. (void) strcpy (one, "abcd");
  430. check (rawmemchr (one, 'c') == one+2, 1); /* Basic test. */
  431. check (rawmemchr (one, 'd') == one+3, 2); /* End of string. */
  432. check (rawmemchr (one, 'a') == one, 3); /* Beginning. */
  433. check (rawmemchr (one, '\0') == one+4, 4); /* Finding NUL. */
  434. (void) strcpy (one, "ababa");
  435. check (rawmemchr (one, 'b') == one+1, 5); /* Finding first. */
  436. (void) strcpy (one, "");
  437. check (rawmemchr (one, '\0') == one, 6); /* NUL in empty string. */
  438. {
  439. char buf[4096];
  440. int i;
  441. char *p;
  442. for (i=0; i < 0x100; i++)
  443. {
  444. p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
  445. strcpy (p, "OK");
  446. strcpy (p+3, "BAD/WRONG");
  447. check (rawmemchr (p, 'R') == p+8, 6+i);
  448. }
  449. }
  450. }
  451. static void
  452. test_index (void)
  453. {
  454. it = "index";
  455. check (index ("abcd", 'z') == NULL, 1); /* Not found. */
  456. (void) strcpy (one, "abcd");
  457. check (index (one, 'c') == one+2, 2); /* Basic test. */
  458. check (index (one, 'd') == one+3, 3); /* End of string. */
  459. check (index (one, 'a') == one, 4); /* Beginning. */
  460. check (index (one, '\0') == one+4, 5); /* Finding NUL. */
  461. (void) strcpy (one, "ababa");
  462. check (index (one, 'b') == one+1, 6); /* Finding first. */
  463. (void) strcpy (one, "");
  464. check (index (one, 'b') == NULL, 7); /* Empty string. */
  465. check (index (one, '\0') == one, 8); /* NUL in empty string. */
  466. }
  467. static void
  468. test_strrchr (void)
  469. {
  470. it = "strrchr";
  471. check (strrchr ("abcd", 'z') == NULL, 1); /* Not found. */
  472. (void) strcpy (one, "abcd");
  473. check (strrchr (one, 'c') == one+2, 2); /* Basic test. */
  474. check (strrchr (one, 'd') == one+3, 3); /* End of string. */
  475. check (strrchr (one, 'a') == one, 4); /* Beginning. */
  476. check (strrchr (one, '\0') == one+4, 5); /* Finding NUL. */
  477. (void) strcpy (one, "ababa");
  478. check (strrchr (one, 'b') == one+3, 6); /* Finding last. */
  479. (void) strcpy (one, "");
  480. check (strrchr (one, 'b') == NULL, 7); /* Empty string. */
  481. check (strrchr (one, '\0') == one, 8); /* NUL in empty string. */
  482. {
  483. char buf[4096];
  484. int i;
  485. char *p;
  486. for (i=0; i < 0x100; i++)
  487. {
  488. p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
  489. strcpy (p, "OK");
  490. strcpy (p+3, "BAD/WRONG");
  491. check (strrchr (p, '/') == NULL, 9+i);
  492. }
  493. }
  494. }
  495. static void
  496. test_memrchr (void)
  497. {
  498. size_t l;
  499. it = "memrchr";
  500. check (memrchr ("abcd", 'z', 5) == NULL, 1); /* Not found. */
  501. (void) strcpy (one, "abcd");
  502. l = strlen (one) + 1;
  503. check (memrchr (one, 'c', l) == one+2, 2); /* Basic test. */
  504. check (memrchr (one, 'd', l) == one+3, 3); /* End of string. */
  505. check (memrchr (one, 'a', l) == one, 4); /* Beginning. */
  506. check (memrchr (one, '\0', l) == one+4, 5); /* Finding NUL. */
  507. (void) strcpy (one, "ababa");
  508. l = strlen (one) + 1;
  509. check (memrchr (one, 'b', l) == one+3, 6); /* Finding last. */
  510. (void) strcpy (one, "");
  511. l = strlen (one) + 1;
  512. check (memrchr (one, 'b', l) == NULL, 7); /* Empty string. */
  513. check (memrchr (one, '\0', l) == one, 8); /* NUL in empty string. */
  514. /* now test all possible alignment and length combinations to catch
  515. bugs due to unrolled loops (assuming unrolling is limited to no
  516. more than 128 byte chunks: */
  517. {
  518. char buf[128 + sizeof(long)];
  519. long align, len, i, pos;
  520. for (align = 0; align < (long) sizeof(long); ++align) {
  521. for (len = 0; len < (long) (sizeof(buf) - align); ++len) {
  522. for (i = 0; i < len; ++i)
  523. buf[align + i] = 'x'; /* don't depend on memset... */
  524. for (pos = len - 1; pos >= 0; --pos) {
  525. #if 0
  526. printf("align %d, len %d, pos %d\n", align, len, pos);
  527. #endif
  528. check(memrchr(buf + align, 'x', len) == buf + align + pos, 9);
  529. check(memrchr(buf + align + pos + 1, 'x', len - (pos + 1)) == NULL,
  530. 10);
  531. buf[align + pos] = '-';
  532. }
  533. }
  534. }
  535. }
  536. }
  537. static void
  538. test_rindex (void)
  539. {
  540. it = "rindex";
  541. check (rindex ("abcd", 'z') == NULL, 1); /* Not found. */
  542. (void) strcpy (one, "abcd");
  543. check (rindex (one, 'c') == one+2, 2); /* Basic test. */
  544. check (rindex (one, 'd') == one+3, 3); /* End of string. */
  545. check (rindex (one, 'a') == one, 4); /* Beginning. */
  546. check (rindex (one, '\0') == one+4, 5); /* Finding NUL. */
  547. (void) strcpy (one, "ababa");
  548. check (rindex (one, 'b') == one+3, 6); /* Finding last. */
  549. (void) strcpy (one, "");
  550. check (rindex (one, 'b') == NULL, 7); /* Empty string. */
  551. check (rindex (one, '\0') == one, 8); /* NUL in empty string. */
  552. }
  553. static void
  554. test_strpbrk (void)
  555. {
  556. it = "strpbrk";
  557. check(strpbrk("abcd", "z") == NULL, 1); /* Not found. */
  558. (void) strcpy(one, "abcd");
  559. check(strpbrk(one, "c") == one+2, 2); /* Basic test. */
  560. check(strpbrk(one, "d") == one+3, 3); /* End of string. */
  561. check(strpbrk(one, "a") == one, 4); /* Beginning. */
  562. check(strpbrk(one, "") == NULL, 5); /* Empty search list. */
  563. check(strpbrk(one, "cb") == one+1, 6); /* Multiple search. */
  564. (void) strcpy(one, "abcabdea");
  565. check(strpbrk(one, "b") == one+1, 7); /* Finding first. */
  566. check(strpbrk(one, "cb") == one+1, 8); /* With multiple search. */
  567. check(strpbrk(one, "db") == one+1, 9); /* Another variant. */
  568. (void) strcpy(one, "");
  569. check(strpbrk(one, "bc") == NULL, 10); /* Empty string. */
  570. (void) strcpy(one, "");
  571. check(strpbrk(one, "bcd") == NULL, 11); /* Empty string. */
  572. (void) strcpy(one, "");
  573. check(strpbrk(one, "bcde") == NULL, 12); /* Empty string. */
  574. check(strpbrk(one, "") == NULL, 13); /* Both strings empty. */
  575. (void) strcpy(one, "abcabdea");
  576. check(strpbrk(one, "befg") == one+1, 14); /* Finding first. */
  577. check(strpbrk(one, "cbr") == one+1, 15); /* With multiple search. */
  578. check(strpbrk(one, "db") == one+1, 16); /* Another variant. */
  579. check(strpbrk(one, "efgh") == one+6, 17); /* And yet another. */
  580. }
  581. static void
  582. test_strstr (void)
  583. {
  584. it = "strstr";
  585. check(strstr("abcd", "z") == NULL, 1); /* Not found. */
  586. check(strstr("abcd", "abx") == NULL, 2); /* Dead end. */
  587. (void) strcpy(one, "abcd");
  588. check(strstr(one, "c") == one+2, 3); /* Basic test. */
  589. check(strstr(one, "bc") == one+1, 4); /* Multichar. */
  590. check(strstr(one, "d") == one+3, 5); /* End of string. */
  591. check(strstr(one, "cd") == one+2, 6); /* Tail of string. */
  592. check(strstr(one, "abc") == one, 7); /* Beginning. */
  593. check(strstr(one, "abcd") == one, 8); /* Exact match. */
  594. check(strstr(one, "abcde") == NULL, 9); /* Too long. */
  595. check(strstr(one, "de") == NULL, 10); /* Past end. */
  596. check(strstr(one, "") == one, 11); /* Finding empty. */
  597. (void) strcpy(one, "ababa");
  598. check(strstr(one, "ba") == one+1, 12); /* Finding first. */
  599. (void) strcpy(one, "");
  600. check(strstr(one, "b") == NULL, 13); /* Empty string. */
  601. check(strstr(one, "") == one, 14); /* Empty in empty string. */
  602. (void) strcpy(one, "bcbca");
  603. check(strstr(one, "bca") == one+2, 15); /* False start. */
  604. (void) strcpy(one, "bbbcabbca");
  605. check(strstr(one, "bbca") == one+1, 16); /* With overlap. */
  606. }
  607. static void
  608. test_strspn (void)
  609. {
  610. it = "strspn";
  611. check(strspn("abcba", "abc") == 5, 1); /* Whole string. */
  612. check(strspn("abcba", "ab") == 2, 2); /* Partial. */
  613. check(strspn("abc", "qx") == 0, 3); /* None. */
  614. check(strspn("", "ab") == 0, 4); /* Null string. */
  615. check(strspn("abc", "") == 0, 5); /* Null search list. */
  616. }
  617. static void
  618. test_strcspn (void)
  619. {
  620. it = "strcspn";
  621. check(strcspn("abcba", "qx") == 5, 1); /* Whole string. */
  622. check(strcspn("abcba", "cx") == 2, 2); /* Partial. */
  623. check(strcspn("abc", "abc") == 0, 3); /* None. */
  624. check(strcspn("", "ab") == 0, 4); /* Null string. */
  625. check(strcspn("abc", "") == 3, 5); /* Null search list. */
  626. }
  627. static void
  628. test_strtok (void)
  629. {
  630. it = "strtok";
  631. (void) strcpy(one, "first, second, third");
  632. equal(strtok(one, ", "), "first", 1); /* Basic test. */
  633. equal(one, "first", 2);
  634. equal(strtok((char *)NULL, ", "), "second", 3);
  635. equal(strtok((char *)NULL, ", "), "third", 4);
  636. check(strtok((char *)NULL, ", ") == NULL, 5);
  637. (void) strcpy(one, ", first, ");
  638. equal(strtok(one, ", "), "first", 6); /* Extra delims, 1 tok. */
  639. check(strtok((char *)NULL, ", ") == NULL, 7);
  640. (void) strcpy(one, "1a, 1b; 2a, 2b");
  641. equal(strtok(one, ", "), "1a", 8); /* Changing delim lists. */
  642. equal(strtok((char *)NULL, "; "), "1b", 9);
  643. equal(strtok((char *)NULL, ", "), "2a", 10);
  644. (void) strcpy(two, "x-y");
  645. equal(strtok(two, "-"), "x", 11); /* New string before done. */
  646. equal(strtok((char *)NULL, "-"), "y", 12);
  647. check(strtok((char *)NULL, "-") == NULL, 13);
  648. (void) strcpy(one, "a,b, c,, ,d");
  649. equal(strtok(one, ", "), "a", 14); /* Different separators. */
  650. equal(strtok((char *)NULL, ", "), "b", 15);
  651. equal(strtok((char *)NULL, " ,"), "c", 16); /* Permute list too. */
  652. equal(strtok((char *)NULL, " ,"), "d", 17);
  653. check(strtok((char *)NULL, ", ") == NULL, 18);
  654. check(strtok((char *)NULL, ", ") == NULL, 19); /* Persistence. */
  655. (void) strcpy(one, ", ");
  656. check(strtok(one, ", ") == NULL, 20); /* No tokens. */
  657. (void) strcpy(one, "");
  658. check(strtok(one, ", ") == NULL, 21); /* Empty string. */
  659. (void) strcpy(one, "abc");
  660. equal(strtok(one, ", "), "abc", 22); /* No delimiters. */
  661. check(strtok((char *)NULL, ", ") == NULL, 23);
  662. (void) strcpy(one, "abc");
  663. equal(strtok(one, ""), "abc", 24); /* Empty delimiter list. */
  664. check(strtok((char *)NULL, "") == NULL, 25);
  665. (void) strcpy(one, "abcdefgh");
  666. (void) strcpy(one, "a,b,c");
  667. equal(strtok(one, ","), "a", 26); /* Basics again... */
  668. equal(strtok((char *)NULL, ","), "b", 27);
  669. equal(strtok((char *)NULL, ","), "c", 28);
  670. check(strtok((char *)NULL, ",") == NULL, 29);
  671. equal(one+6, "gh", 30); /* Stomped past end? */
  672. equal(one, "a", 31); /* Stomped old tokens? */
  673. equal(one+2, "b", 32);
  674. equal(one+4, "c", 33);
  675. }
  676. static void
  677. test_strtok_r (void)
  678. {
  679. it = "strtok_r";
  680. (void) strcpy(one, "first, second, third");
  681. cp = NULL; /* Always initialize cp to make sure it doesn't point to some old data. */
  682. equal(strtok_r(one, ", ", &cp), "first", 1); /* Basic test. */
  683. equal(one, "first", 2);
  684. equal(strtok_r((char *)NULL, ", ", &cp), "second", 3);
  685. equal(strtok_r((char *)NULL, ", ", &cp), "third", 4);
  686. check(strtok_r((char *)NULL, ", ", &cp) == NULL, 5);
  687. (void) strcpy(one, ", first, ");
  688. cp = NULL;
  689. equal(strtok_r(one, ", ", &cp), "first", 6); /* Extra delims, 1 tok. */
  690. check(strtok_r((char *)NULL, ", ", &cp) == NULL, 7);
  691. (void) strcpy(one, "1a, 1b; 2a, 2b");
  692. cp = NULL;
  693. equal(strtok_r(one, ", ", &cp), "1a", 8); /* Changing delim lists. */
  694. equal(strtok_r((char *)NULL, "; ", &cp), "1b", 9);
  695. equal(strtok_r((char *)NULL, ", ", &cp), "2a", 10);
  696. (void) strcpy(two, "x-y");
  697. cp = NULL;
  698. equal(strtok_r(two, "-", &cp), "x", 11); /* New string before done. */
  699. equal(strtok_r((char *)NULL, "-", &cp), "y", 12);
  700. check(strtok_r((char *)NULL, "-", &cp) == NULL, 13);
  701. (void) strcpy(one, "a,b, c,, ,d");
  702. cp = NULL;
  703. equal(strtok_r(one, ", ", &cp), "a", 14); /* Different separators. */
  704. equal(strtok_r((char *)NULL, ", ", &cp), "b", 15);
  705. equal(strtok_r((char *)NULL, " ,", &cp), "c", 16); /* Permute list too. */
  706. equal(strtok_r((char *)NULL, " ,", &cp), "d", 17);
  707. check(strtok_r((char *)NULL, ", ", &cp) == NULL, 18);
  708. check(strtok_r((char *)NULL, ", ", &cp) == NULL, 19); /* Persistence. */
  709. (void) strcpy(one, ", ");
  710. cp = NULL;
  711. check(strtok_r(one, ", ", &cp) == NULL, 20); /* No tokens. */
  712. (void) strcpy(one, "");
  713. cp = NULL;
  714. check(strtok_r(one, ", ", &cp) == NULL, 21); /* Empty string. */
  715. check(strtok_r((char *)NULL, ", ", &cp) == NULL, 22); /* Persistence. */
  716. (void) strcpy(one, "abc");
  717. cp = NULL;
  718. equal(strtok_r(one, ", ", &cp), "abc", 23); /* No delimiters. */
  719. check(strtok_r((char *)NULL, ", ", &cp) == NULL, 24);
  720. (void) strcpy(one, "abc");
  721. cp = NULL;
  722. equal(strtok_r(one, "", &cp), "abc", 25); /* Empty delimiter list. */
  723. check(strtok_r((char *)NULL, "", &cp) == NULL, 26);
  724. (void) strcpy(one, "abcdefgh");
  725. (void) strcpy(one, "a,b,c");
  726. cp = NULL;
  727. equal(strtok_r(one, ",", &cp), "a", 27); /* Basics again... */
  728. equal(strtok_r((char *)NULL, ",", &cp), "b", 28);
  729. equal(strtok_r((char *)NULL, ",", &cp), "c", 29);
  730. check(strtok_r((char *)NULL, ",", &cp) == NULL, 30);
  731. equal(one+6, "gh", 31); /* Stomped past end? */
  732. equal(one, "a", 32); /* Stomped old tokens? */
  733. equal(one+2, "b", 33);
  734. equal(one+4, "c", 34);
  735. }
  736. static void
  737. test_strsep (void)
  738. {
  739. char *ptr;
  740. it = "strsep";
  741. cp = strcpy(one, "first, second, third");
  742. equal(strsep(&cp, ", "), "first", 1); /* Basic test. */
  743. equal(one, "first", 2);
  744. equal(strsep(&cp, ", "), "", 3);
  745. equal(strsep(&cp, ", "), "second", 4);
  746. equal(strsep(&cp, ", "), "", 5);
  747. equal(strsep(&cp, ", "), "third", 6);
  748. check(strsep(&cp, ", ") == NULL, 7);
  749. cp = strcpy(one, ", first, ");
  750. equal(strsep(&cp, ", "), "", 8);
  751. equal(strsep(&cp, ", "), "", 9);
  752. equal(strsep(&cp, ", "), "first", 10); /* Extra delims, 1 tok. */
  753. equal(strsep(&cp, ", "), "", 11);
  754. equal(strsep(&cp, ", "), "", 12);
  755. check(strsep(&cp, ", ") == NULL, 13);
  756. cp = strcpy(one, "1a, 1b; 2a, 2b");
  757. equal(strsep(&cp, ", "), "1a", 14); /* Changing delim lists. */
  758. equal(strsep(&cp, ", "), "", 15);
  759. equal(strsep(&cp, "; "), "1b", 16);
  760. equal(strsep(&cp, ", "), "", 17);
  761. equal(strsep(&cp, ", "), "2a", 18);
  762. cp = strcpy(two, "x-y");
  763. equal(strsep(&cp, "-"), "x", 19); /* New string before done. */
  764. equal(strsep(&cp, "-"), "y", 20);
  765. check(strsep(&cp, "-") == NULL, 21);
  766. cp = strcpy(one, "a,b, c,, ,d ");
  767. equal(strsep(&cp, ", "), "a", 22); /* Different separators. */
  768. equal(strsep(&cp, ", "), "b", 23);
  769. equal(strsep(&cp, " ,"), "", 24);
  770. equal(strsep(&cp, " ,"), "c", 25); /* Permute list too. */
  771. equal(strsep(&cp, " ,"), "", 26);
  772. equal(strsep(&cp, " ,"), "", 27);
  773. equal(strsep(&cp, " ,"), "", 28);
  774. equal(strsep(&cp, " ,"), "d", 29);
  775. equal(strsep(&cp, " ,"), "", 30);
  776. check(strsep(&cp, ", ") == NULL, 31);
  777. check(strsep(&cp, ", ") == NULL, 32); /* Persistence. */
  778. cp = strcpy(one, ", ");
  779. equal(strsep(&cp, ", "), "", 33);
  780. equal(strsep(&cp, ", "), "", 34);
  781. equal(strsep(&cp, ", "), "", 35);
  782. check(strsep(&cp, ", ") == NULL, 36); /* No tokens. */
  783. cp = strcpy(one, "");
  784. equal(strsep(&cp, ", "), "", 37);
  785. check(strsep(&cp, ", ") == NULL, 38); /* Empty string. */
  786. cp = strcpy(one, "abc");
  787. equal(strsep(&cp, ", "), "abc", 39); /* No delimiters. */
  788. check(strsep(&cp, ", ") == NULL, 40);
  789. cp = strcpy(one, "abc");
  790. equal(strsep(&cp, ""), "abc", 41); /* Empty delimiter list. */
  791. check(strsep(&cp, "") == NULL, 42);
  792. (void) strcpy(one, "abcdefgh");
  793. cp = strcpy(one, "a,b,c");
  794. equal(strsep(&cp, ","), "a", 43); /* Basics again... */
  795. equal(strsep(&cp, ","), "b", 44);
  796. equal(strsep(&cp, ","), "c", 45);
  797. check(strsep(&cp, ",") == NULL, 46);
  798. equal(one+6, "gh", 47); /* Stomped past end? */
  799. equal(one, "a", 48); /* Stomped old tokens? */
  800. equal(one+2, "b", 49);
  801. equal(one+4, "c", 50);
  802. {
  803. char text[] = "This,is,a,test";
  804. char *list = strdupa (text);
  805. equal (strsep (&list, ","), "This", 51);
  806. equal (strsep (&list, ","), "is", 52);
  807. equal (strsep (&list, ","), "a", 53);
  808. equal (strsep (&list, ","), "test", 54);
  809. check (strsep (&list, ",") == NULL, 55);
  810. }
  811. cp = strcpy(one, "a,b, c,, ,d,");
  812. equal(strsep(&cp, ","), "a", 56); /* Different separators. */
  813. equal(strsep(&cp, ","), "b", 57);
  814. equal(strsep(&cp, ","), " c", 58); /* Permute list too. */
  815. equal(strsep(&cp, ","), "", 59);
  816. equal(strsep(&cp, ","), " ", 60);
  817. equal(strsep(&cp, ","), "d", 61);
  818. equal(strsep(&cp, ","), "", 62);
  819. check(strsep(&cp, ",") == NULL, 63);
  820. check(strsep(&cp, ",") == NULL, 64); /* Persistence. */
  821. cp = strcpy(one, "a,b, c,, ,d,");
  822. equal(strsep(&cp, "xy,"), "a", 65); /* Different separators. */
  823. equal(strsep(&cp, "x,y"), "b", 66);
  824. equal(strsep(&cp, ",xy"), " c", 67); /* Permute list too. */
  825. equal(strsep(&cp, "xy,"), "", 68);
  826. equal(strsep(&cp, "x,y"), " ", 69);
  827. equal(strsep(&cp, ",xy"), "d", 70);
  828. equal(strsep(&cp, "xy,"), "", 71);
  829. check(strsep(&cp, "x,y") == NULL, 72);
  830. check(strsep(&cp, ",xy") == NULL, 73); /* Persistence. */
  831. cp = strcpy(one, "ABC");
  832. one[4] = ':';
  833. equal(strsep(&cp, "C"), "AB", 74); /* Access beyond NUL. */
  834. ptr = strsep(&cp, ":");
  835. equal(ptr, "", 75);
  836. check(ptr == one + 3, 76);
  837. check(cp == NULL, 77);
  838. cp = strcpy(one, "ABC");
  839. one[4] = ':';
  840. equal(strsep(&cp, "CD"), "AB", 78); /* Access beyond NUL. */
  841. ptr = strsep(&cp, ":.");
  842. equal(ptr, "", 79);
  843. check(ptr == one + 3, 80);
  844. cp = strcpy(one, "ABC"); /* No token in string. */
  845. equal(strsep(&cp, ","), "ABC", 81);
  846. check(cp == NULL, 82);
  847. *one = '\0'; /* Empty string. */
  848. cp = one;
  849. ptr = strsep(&cp, ",");
  850. equal(ptr, "", 83);
  851. check(ptr == one, 84);
  852. check(cp == NULL, 85);
  853. *one = '\0'; /* Empty string and no token. */
  854. cp = one;
  855. ptr = strsep(&cp, "");
  856. equal(ptr, "", 86);
  857. check(ptr == one , 87);
  858. check(cp == NULL, 88);
  859. }
  860. static void
  861. test_memcmp (void)
  862. {
  863. it = "memcmp";
  864. check(memcmp("a", "a", 1) == 0, 1); /* Identity. */
  865. check(memcmp("abc", "abc", 3) == 0, 2); /* Multicharacter. */
  866. check(memcmp("abcd", "abce", 4) < 0, 3); /* Honestly unequal. */
  867. check(memcmp("abce", "abcd", 4) > 0, 4);
  868. check(memcmp("alph", "beta", 4) < 0, 5);
  869. check(memcmp("a\203", "a\003", 2) > 0, 6);
  870. check(memcmp("abce", "abcd", 3) == 0, 7); /* Count limited. */
  871. check(memcmp("abc", "def", 0) == 0, 8); /* Zero count. */
  872. }
  873. static void
  874. test_memchr (void)
  875. {
  876. it = "memchr";
  877. check(memchr("abcd", 'z', 4) == NULL, 1); /* Not found. */
  878. (void) strcpy(one, "abcd");
  879. check(memchr(one, 'c', 4) == one+2, 2); /* Basic test. */
  880. check(memchr(one, ~0xff|'c', 4) == one+2, 2); /* ignore highorder bits. */
  881. check(memchr(one, 'd', 4) == one+3, 3); /* End of string. */
  882. check(memchr(one, 'a', 4) == one, 4); /* Beginning. */
  883. check(memchr(one, '\0', 5) == one+4, 5); /* Finding NUL. */
  884. (void) strcpy(one, "ababa");
  885. check(memchr(one, 'b', 5) == one+1, 6); /* Finding first. */
  886. check(memchr(one, 'b', 0) == NULL, 7); /* Zero count. */
  887. check(memchr(one, 'a', 1) == one, 8); /* Singleton case. */
  888. (void) strcpy(one, "a\203b");
  889. check(memchr(one, 0203, 3) == one+1, 9); /* Unsignedness. */
  890. /* now test all possible alignment and length combinations to catch
  891. bugs due to unrolled loops (assuming unrolling is limited to no
  892. more than 128 byte chunks: */
  893. {
  894. char buf[128 + sizeof(long)];
  895. long align, len, i, pos;
  896. for (align = 0; align < (long) sizeof(long); ++align) {
  897. for (len = 0; len < (long) (sizeof(buf) - align); ++len) {
  898. for (i = 0; i < len; ++i) {
  899. buf[align + i] = 'x'; /* don't depend on memset... */
  900. }
  901. for (pos = 0; pos < len; ++pos) {
  902. #if 0
  903. printf("align %d, len %d, pos %d\n", align, len, pos);
  904. #endif
  905. check(memchr(buf + align, 'x', len) == buf + align + pos, 10);
  906. check(memchr(buf + align, 'x', pos) == NULL, 11);
  907. buf[align + pos] = '-';
  908. }
  909. }
  910. }
  911. }
  912. }
  913. static void
  914. test_memcpy (void)
  915. {
  916. int i;
  917. it = "memcpy";
  918. check(memcpy(one, "abc", 4) == one, 1); /* Returned value. */
  919. equal(one, "abc", 2); /* Did the copy go right? */
  920. (void) strcpy(one, "abcdefgh");
  921. (void) memcpy(one+1, "xyz", 2);
  922. equal(one, "axydefgh", 3); /* Basic test. */
  923. (void) strcpy(one, "abc");
  924. (void) memcpy(one, "xyz", 0);
  925. equal(one, "abc", 4); /* Zero-length copy. */
  926. (void) strcpy(one, "hi there");
  927. (void) strcpy(two, "foo");
  928. (void) memcpy(two, one, 9);
  929. equal(two, "hi there", 5); /* Just paranoia. */
  930. equal(one, "hi there", 6); /* Stomped on source? */
  931. for (i = 0; i < 16; i++)
  932. {
  933. const char *x = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  934. strcpy (one, x);
  935. check (memcpy (one + i, "hi there", 9) == one + i,
  936. 7 + (i * 6)); /* Unaligned destination. */
  937. check (memcmp (one, x, i) == 0, 8 + (i * 6)); /* Wrote under? */
  938. equal (one + i, "hi there", 9 + (i * 6));
  939. check (one[i + 9] == 'x', 10 + (i * 6)); /* Wrote over? */
  940. check (memcpy (two, one + i, 9) == two,
  941. 11 + (i * 6)); /* Unaligned source. */
  942. equal (two, "hi there", 12 + (i * 6));
  943. }
  944. }
  945. static void
  946. test_mempcpy (void)
  947. {
  948. int i;
  949. it = "mempcpy";
  950. check(mempcpy(one, "abc", 4) == one + 4, 1); /* Returned value. */
  951. equal(one, "abc", 2); /* Did the copy go right? */
  952. (void) strcpy(one, "abcdefgh");
  953. (void) mempcpy(one+1, "xyz", 2);
  954. equal(one, "axydefgh", 3); /* Basic test. */
  955. (void) strcpy(one, "abc");
  956. (void) mempcpy(one, "xyz", 0);
  957. equal(one, "abc", 4); /* Zero-length copy. */
  958. (void) strcpy(one, "hi there");
  959. (void) strcpy(two, "foo");
  960. (void) mempcpy(two, one, 9);
  961. equal(two, "hi there", 5); /* Just paranoia. */
  962. equal(one, "hi there", 6); /* Stomped on source? */
  963. for (i = 0; i < 16; i++)
  964. {
  965. const char *x = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  966. strcpy (one, x);
  967. check (mempcpy (one + i, "hi there", 9) == one + i + 9,
  968. 7 + (i * 6)); /* Unaligned destination. */
  969. check (memcmp (one, x, i) == 0, 8 + (i * 6)); /* Wrote under? */
  970. equal (one + i, "hi there", 9 + (i * 6));
  971. check (one[i + 9] == 'x', 10 + (i * 6)); /* Wrote over? */
  972. check (mempcpy (two, one + i, 9) == two + 9,
  973. 11 + (i * 6)); /* Unaligned source. */
  974. equal (two, "hi there", 12 + (i * 6));
  975. }
  976. }
  977. static void
  978. test_memmove (void)
  979. {
  980. it = "memmove";
  981. check(memmove(one, "abc", 4) == one, 1); /* Returned value. */
  982. equal(one, "abc", 2); /* Did the copy go right? */
  983. (void) strcpy(one, "abcdefgh");
  984. (void) memmove(one+1, "xyz", 2);
  985. equal(one, "axydefgh", 3); /* Basic test. */
  986. (void) strcpy(one, "abc");
  987. (void) memmove(one, "xyz", 0);
  988. equal(one, "abc", 4); /* Zero-length copy. */
  989. (void) strcpy(one, "hi there");
  990. (void) strcpy(two, "foo");
  991. (void) memmove(two, one, 9);
  992. equal(two, "hi there", 5); /* Just paranoia. */
  993. equal(one, "hi there", 6); /* Stomped on source? */
  994. (void) strcpy(one, "abcdefgh");
  995. (void) memmove(one+1, one, 9);
  996. equal(one, "aabcdefgh", 7); /* Overlap, right-to-left. */
  997. (void) strcpy(one, "abcdefgh");
  998. (void) memmove(one+1, one+2, 7);
  999. equal(one, "acdefgh", 8); /* Overlap, left-to-right. */
  1000. (void) strcpy(one, "abcdefgh");
  1001. (void) memmove(one, one, 9);
  1002. equal(one, "abcdefgh", 9); /* 100% overlap. */
  1003. }
  1004. static void
  1005. test_memccpy (void)
  1006. {
  1007. /* First test like memcpy, then the search part The SVID, the only
  1008. place where memccpy is mentioned, says overlap might fail, so we
  1009. don't try it. Besides, it's hard to see the rationale for a
  1010. non-left-to-right memccpy. */
  1011. it = "memccpy";
  1012. check(memccpy(one, "abc", 'q', 4) == NULL, 1); /* Returned value. */
  1013. equal(one, "abc", 2); /* Did the copy go right? */
  1014. (void) strcpy(one, "abcdefgh");
  1015. (void) memccpy(one+1, "xyz", 'q', 2);
  1016. equal(one, "axydefgh", 3); /* Basic test. */
  1017. (void) strcpy(one, "abc");
  1018. (void) memccpy(one, "xyz", 'q', 0);
  1019. equal(one, "abc", 4); /* Zero-length copy. */
  1020. (void) strcpy(one, "hi there");
  1021. (void) strcpy(two, "foo");
  1022. (void) memccpy(two, one, 'q', 9);
  1023. equal(two, "hi there", 5); /* Just paranoia. */
  1024. equal(one, "hi there", 6); /* Stomped on source? */
  1025. (void) strcpy(one, "abcdefgh");
  1026. (void) strcpy(two, "horsefeathers");
  1027. check(memccpy(two, one, 'f', 9) == two+6, 7); /* Returned value. */
  1028. equal(one, "abcdefgh", 8); /* Source intact? */
  1029. equal(two, "abcdefeathers", 9); /* Copy correct? */
  1030. (void) strcpy(one, "abcd");
  1031. (void) strcpy(two, "bumblebee");
  1032. check(memccpy(two, one, 'a', 4) == two+1, 10); /* First char. */
  1033. equal(two, "aumblebee", 11);
  1034. check(memccpy(two, one, 'd', 4) == two+4, 12); /* Last char. */
  1035. equal(two, "abcdlebee", 13);
  1036. (void) strcpy(one, "xyz");
  1037. check(memccpy(two, one, 'x', 1) == two+1, 14); /* Singleton. */
  1038. equal(two, "xbcdlebee", 15);
  1039. }
  1040. static void
  1041. test_memset (void)
  1042. {
  1043. int i;
  1044. it = "memset";
  1045. (void) strcpy(one, "abcdefgh");
  1046. check(memset(one+1, 'x', 3) == one+1, 1); /* Return value. */
  1047. equal(one, "axxxefgh", 2); /* Basic test. */
  1048. (void) memset(one+2, 'y', 0);
  1049. equal(one, "axxxefgh", 3); /* Zero-length set. */
  1050. (void) memset(one+5, 0, 1);
  1051. equal(one, "axxxe", 4); /* Zero fill. */
  1052. equal(one+6, "gh", 5); /* And the leftover. */
  1053. (void) memset(one+2, 010045, 1);
  1054. equal(one, "ax\045xe", 6); /* Unsigned char convert. */
  1055. /* Non-8bit fill character. */
  1056. memset (one, 0x101, sizeof (one));
  1057. for (i = 0; i < (int) sizeof (one); ++i)
  1058. check (one[i] == '\01', 7);
  1059. /* Test for more complex versions of memset, for all alignments and
  1060. lengths up to 256. This test takes a little while, perhaps it should
  1061. be made weaker? */
  1062. {
  1063. char data[512];
  1064. int j;
  1065. int k;
  1066. int c;
  1067. for (i = 0; i < 512; i++)
  1068. data[i] = 'x';
  1069. for (c = 0; c <= 'y'; c += 'y') /* check for memset(,0,) and
  1070. memset(,'y',) */
  1071. for (j = 0; j < 256; j++)
  1072. for (i = 0; i < 256; i++)
  1073. {
  1074. memset (data + i, c, j);
  1075. for (k = 0; k < i; k++)
  1076. if (data[k] != 'x')
  1077. goto fail;
  1078. for (k = i; k < i+j; k++)
  1079. {
  1080. if (data[k] != c)
  1081. goto fail;
  1082. data[k] = 'x';
  1083. }
  1084. for (k = i+j; k < 512; k++)
  1085. if (data[k] != 'x')
  1086. goto fail;
  1087. continue;
  1088. fail:
  1089. check (0, 8 + i + j * 256 + (c != 0) * 256 * 256);
  1090. }
  1091. }
  1092. }
  1093. static void
  1094. test_bcopy (void)
  1095. {
  1096. /* Much like memcpy. Berklix manual is silent about overlap, so
  1097. don't test it. */
  1098. it = "bcopy";
  1099. (void) bcopy("abc", one, 4);
  1100. equal(one, "abc", 1); /* Simple copy. */
  1101. (void) strcpy(one, "abcdefgh");
  1102. (void) bcopy("xyz", one+1, 2);
  1103. equal(one, "axydefgh", 2); /* Basic test. */
  1104. (void) strcpy(one, "abc");
  1105. (void) bcopy("xyz", one, 0);
  1106. equal(one, "abc", 3); /* Zero-length copy. */
  1107. (void) strcpy(one, "hi there");
  1108. (void) strcpy(two, "foo");
  1109. (void) bcopy(one, two, 9);
  1110. equal(two, "hi there", 4); /* Just paranoia. */
  1111. equal(one, "hi there", 5); /* Stomped on source? */
  1112. }
  1113. static void
  1114. test_bzero (void)
  1115. {
  1116. it = "bzero";
  1117. (void) strcpy(one, "abcdef");
  1118. bzero(one+2, 2);
  1119. equal(one, "ab", 1); /* Basic test. */
  1120. equal(one+3, "", 2);
  1121. equal(one+4, "ef", 3);
  1122. (void) strcpy(one, "abcdef");
  1123. bzero(one+2, 0);
  1124. equal(one, "abcdef", 4); /* Zero-length copy. */
  1125. }
  1126. static void
  1127. test_strndup (void)
  1128. {
  1129. char *p, *q;
  1130. it = "strndup";
  1131. p = strndup("abcdef", 12);
  1132. check(p != NULL, 1);
  1133. if (p != NULL)
  1134. {
  1135. equal(p, "abcdef", 2);
  1136. q = strndup(p + 1, 2);
  1137. check(q != NULL, 3);
  1138. if (q != NULL)
  1139. equal(q, "bc", 4);
  1140. free (q);
  1141. }
  1142. free (p);
  1143. p = strndup("abc def", 3);
  1144. check(p != NULL, 5);
  1145. if (p != NULL)
  1146. equal(p, "abc", 6);
  1147. free (p);
  1148. }
  1149. static void
  1150. test_bcmp (void)
  1151. {
  1152. it = "bcmp";
  1153. check(bcmp("a", "a", 1) == 0, 1); /* Identity. */
  1154. check(bcmp("abc", "abc", 3) == 0, 2); /* Multicharacter. */
  1155. check(bcmp("abcd", "abce", 4) != 0, 3); /* Honestly unequal. */
  1156. check(bcmp("abce", "abcd", 4) != 0, 4);
  1157. check(bcmp("alph", "beta", 4) != 0, 5);
  1158. check(bcmp("abce", "abcd", 3) == 0, 6); /* Count limited. */
  1159. check(bcmp("abc", "def", 0) == 0, 8); /* Zero count. */
  1160. }
  1161. static void
  1162. test_strerror (void)
  1163. {
  1164. it = "strerror";
  1165. check(strerror(EDOM) != 0, 1);
  1166. check(strerror(ERANGE) != 0, 2);
  1167. check(strerror(ENOENT) != 0, 3);
  1168. }
  1169. int
  1170. main (void)
  1171. {
  1172. int status;
  1173. /* Test strcmp first because we use it to test other things. */
  1174. test_strcmp ();
  1175. /* Test strcpy next because we need it to set up other tests. */
  1176. test_strcpy ();
  1177. /* A closely related function is stpcpy. */
  1178. test_stpcpy ();
  1179. /* stpncpy. */
  1180. test_stpncpy ();
  1181. /* strcat. */
  1182. test_strcat ();
  1183. /* strncat. */
  1184. test_strncat ();
  1185. /* strncmp. */
  1186. test_strncmp ();
  1187. /* strncpy. */
  1188. test_strncpy ();
  1189. /* strlen. */
  1190. test_strlen ();
  1191. /* strchr. */
  1192. test_strchr ();
  1193. /* strchrnul. */
  1194. test_strchrnul ();
  1195. /* rawmemchr. */
  1196. test_rawmemchr ();
  1197. /* index - just like strchr. */
  1198. test_index ();
  1199. /* strrchr. */
  1200. test_strrchr ();
  1201. /* memrchr. */
  1202. test_memrchr ();
  1203. /* rindex - just like strrchr. */
  1204. test_rindex ();
  1205. /* strpbrk - somewhat like strchr. */
  1206. test_strpbrk ();
  1207. /* strstr - somewhat like strchr. */
  1208. test_strstr ();
  1209. /* strspn. */
  1210. test_strspn ();
  1211. /* strcspn. */
  1212. test_strcspn ();
  1213. /* strtok - the hard one. */
  1214. test_strtok ();
  1215. /* strtok_r. */
  1216. test_strtok_r ();
  1217. /* strsep. */
  1218. test_strsep ();
  1219. /* memcmp. */
  1220. test_memcmp ();
  1221. /* memchr. */
  1222. test_memchr ();
  1223. /* memcpy - need not work for overlap. */
  1224. test_memcpy ();
  1225. /* memmove - must work on overlap. */
  1226. test_memmove ();
  1227. /* mempcpy */
  1228. test_mempcpy ();
  1229. /* memccpy. */
  1230. test_memccpy ();
  1231. /* memset. */
  1232. test_memset ();
  1233. /* bcopy. */
  1234. test_bcopy ();
  1235. /* bzero. */
  1236. test_bzero ();
  1237. /* bcmp - somewhat like memcmp. */
  1238. test_bcmp ();
  1239. /* strndup. */
  1240. test_strndup ();
  1241. /* strerror - VERY system-dependent. */
  1242. test_strerror ();
  1243. if (errors == 0)
  1244. {
  1245. status = EXIT_SUCCESS;
  1246. puts("No errors.");
  1247. }
  1248. else
  1249. {
  1250. status = EXIT_FAILURE;
  1251. printf("%d errors.\n", errors);
  1252. }
  1253. return status;
  1254. }