tester.c 49 KB

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