tester.c 51 KB

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