tester.c 49 KB

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