string.c 43 KB

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