tester.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  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. #if !defined (__UCLIBC__) || !defined(__GLIBC__)
  541. #define rawmemchr(s,c) memchr((s),(size_t)-1,(c))
  542. #endif
  543. static void
  544. test_rawmemchr (void)
  545. {
  546. it = "rawmemchr";
  547. (void) strcpy (one, "abcd");
  548. check (rawmemchr (one, 'c') == one+2, 1); /* Basic test. */
  549. check (rawmemchr (one, 'd') == one+3, 2); /* End of string. */
  550. check (rawmemchr (one, 'a') == one, 3); /* Beginning. */
  551. check (rawmemchr (one, '\0') == one+4, 4); /* Finding NUL. */
  552. (void) strcpy (one, "ababa");
  553. check (rawmemchr (one, 'b') == one+1, 5); /* Finding first. */
  554. (void) strcpy (one, "");
  555. check (rawmemchr (one, '\0') == one, 6); /* NUL in empty string. */
  556. {
  557. char buf[4096];
  558. int i;
  559. char *p;
  560. for (i=0; i < 0x100; i++)
  561. {
  562. p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
  563. strcpy (p, "OK");
  564. strcpy (p+3, "BAD/WRONG");
  565. check (rawmemchr (p, 'R') == p+8, 6+i);
  566. }
  567. }
  568. }
  569. static void
  570. test_index (void)
  571. {
  572. #ifdef __TEST_SUSV3_LEGACY__
  573. it = "index";
  574. check (index ("abcd", 'z') == NULL, 1); /* Not found. */
  575. (void) strcpy (one, "abcd");
  576. check (index (one, 'c') == one+2, 2); /* Basic test. */
  577. check (index (one, 'd') == one+3, 3); /* End of string. */
  578. check (index (one, 'a') == one, 4); /* Beginning. */
  579. check (index (one, '\0') == one+4, 5); /* Finding NUL. */
  580. (void) strcpy (one, "ababa");
  581. check (index (one, 'b') == one+1, 6); /* Finding first. */
  582. (void) strcpy (one, "");
  583. check (index (one, 'b') == NULL, 7); /* Empty string. */
  584. check (index (one, '\0') == one, 8); /* NUL in empty string. */
  585. #endif
  586. }
  587. static void
  588. test_strrchr (void)
  589. {
  590. it = "strrchr";
  591. check (strrchr ("abcd", 'z') == NULL, 1); /* Not found. */
  592. (void) strcpy (one, "abcd");
  593. check (strrchr (one, 'c') == one+2, 2); /* Basic test. */
  594. check (strrchr (one, 'd') == one+3, 3); /* End of string. */
  595. check (strrchr (one, 'a') == one, 4); /* Beginning. */
  596. check (strrchr (one, '\0') == one+4, 5); /* Finding NUL. */
  597. (void) strcpy (one, "ababa");
  598. check (strrchr (one, 'b') == one+3, 6); /* Finding last. */
  599. (void) strcpy (one, "");
  600. check (strrchr (one, 'b') == NULL, 7); /* Empty string. */
  601. check (strrchr (one, '\0') == one, 8); /* NUL in empty string. */
  602. {
  603. char buf[4096];
  604. int i;
  605. char *p;
  606. for (i=0; i < 0x100; i++)
  607. {
  608. p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
  609. strcpy (p, "OK");
  610. strcpy (p+3, "BAD/WRONG");
  611. check (strrchr (p, '/') == NULL, 9+i);
  612. }
  613. }
  614. }
  615. static void
  616. test_memrchr (void)
  617. {
  618. size_t l;
  619. it = "memrchr";
  620. check (memrchr ("abcd", 'z', 5) == NULL, 1); /* Not found. */
  621. (void) strcpy (one, "abcd");
  622. l = strlen (one) + 1;
  623. check (memrchr (one, 'c', l) == one+2, 2); /* Basic test. */
  624. check (memrchr (one, 'd', l) == one+3, 3); /* End of string. */
  625. check (memrchr (one, 'a', l) == one, 4); /* Beginning. */
  626. check (memrchr (one, '\0', l) == one+4, 5); /* Finding NUL. */
  627. (void) strcpy (one, "ababa");
  628. l = strlen (one) + 1;
  629. check (memrchr (one, 'b', l) == one+3, 6); /* Finding last. */
  630. (void) strcpy (one, "");
  631. l = strlen (one) + 1;
  632. check (memrchr (one, 'b', l) == NULL, 7); /* Empty string. */
  633. check (memrchr (one, '\0', l) == one, 8); /* NUL in empty string. */
  634. /* now test all possible alignment and length combinations to catch
  635. bugs due to unrolled loops (assuming unrolling is limited to no
  636. more than 128 byte chunks: */
  637. {
  638. char buf[128 + sizeof(long)];
  639. long align, len, i, pos;
  640. for (align = 0; align < (long) sizeof(long); ++align) {
  641. for (len = 0; len < (long) (sizeof(buf) - align); ++len) {
  642. for (i = 0; i < len; ++i)
  643. buf[align + i] = 'x'; /* don't depend on memset... */
  644. for (pos = len - 1; pos >= 0; --pos) {
  645. #if 0
  646. printf("align %d, len %d, pos %d\n", align, len, pos);
  647. #endif
  648. check(memrchr(buf + align, 'x', len) == buf + align + pos, 9);
  649. check(memrchr(buf + align + pos + 1, 'x', len - (pos + 1)) == NULL,
  650. 10);
  651. buf[align + pos] = '-';
  652. }
  653. }
  654. }
  655. }
  656. }
  657. static void
  658. test_rindex (void)
  659. {
  660. #ifdef __TEST_SUSV3_LEGACY__
  661. it = "rindex";
  662. check (rindex ("abcd", 'z') == NULL, 1); /* Not found. */
  663. (void) strcpy (one, "abcd");
  664. check (rindex (one, 'c') == one+2, 2); /* Basic test. */
  665. check (rindex (one, 'd') == one+3, 3); /* End of string. */
  666. check (rindex (one, 'a') == one, 4); /* Beginning. */
  667. check (rindex (one, '\0') == one+4, 5); /* Finding NUL. */
  668. (void) strcpy (one, "ababa");
  669. check (rindex (one, 'b') == one+3, 6); /* Finding last. */
  670. (void) strcpy (one, "");
  671. check (rindex (one, 'b') == NULL, 7); /* Empty string. */
  672. check (rindex (one, '\0') == one, 8); /* NUL in empty string. */
  673. #endif
  674. }
  675. static void
  676. test_strpbrk (void)
  677. {
  678. it = "strpbrk";
  679. check(strpbrk("abcd", "z") == NULL, 1); /* Not found. */
  680. (void) strcpy(one, "abcd");
  681. check(strpbrk(one, "c") == one+2, 2); /* Basic test. */
  682. check(strpbrk(one, "d") == one+3, 3); /* End of string. */
  683. check(strpbrk(one, "a") == one, 4); /* Beginning. */
  684. check(strpbrk(one, "") == NULL, 5); /* Empty search list. */
  685. check(strpbrk(one, "cb") == one+1, 6); /* Multiple search. */
  686. (void) strcpy(one, "abcabdea");
  687. check(strpbrk(one, "b") == one+1, 7); /* Finding first. */
  688. check(strpbrk(one, "cb") == one+1, 8); /* With multiple search. */
  689. check(strpbrk(one, "db") == one+1, 9); /* Another variant. */
  690. (void) strcpy(one, "");
  691. check(strpbrk(one, "bc") == NULL, 10); /* Empty string. */
  692. (void) strcpy(one, "");
  693. check(strpbrk(one, "bcd") == NULL, 11); /* Empty string. */
  694. (void) strcpy(one, "");
  695. check(strpbrk(one, "bcde") == NULL, 12); /* Empty string. */
  696. check(strpbrk(one, "") == NULL, 13); /* Both strings empty. */
  697. (void) strcpy(one, "abcabdea");
  698. check(strpbrk(one, "befg") == one+1, 14); /* Finding first. */
  699. check(strpbrk(one, "cbr") == one+1, 15); /* With multiple search. */
  700. check(strpbrk(one, "db") == one+1, 16); /* Another variant. */
  701. check(strpbrk(one, "efgh") == one+6, 17); /* And yet another. */
  702. }
  703. static void
  704. test_strstr (void)
  705. {
  706. it = "strstr";
  707. check(strstr("abcd", "z") == NULL, 1); /* Not found. */
  708. check(strstr("abcd", "abx") == NULL, 2); /* Dead end. */
  709. (void) strcpy(one, "abcd");
  710. check(strstr(one, "c") == one+2, 3); /* Basic test. */
  711. check(strstr(one, "bc") == one+1, 4); /* Multichar. */
  712. check(strstr(one, "d") == one+3, 5); /* End of string. */
  713. check(strstr(one, "cd") == one+2, 6); /* Tail of string. */
  714. check(strstr(one, "abc") == one, 7); /* Beginning. */
  715. check(strstr(one, "abcd") == one, 8); /* Exact match. */
  716. check(strstr(one, "abcde") == NULL, 9); /* Too long. */
  717. check(strstr(one, "de") == NULL, 10); /* Past end. */
  718. check(strstr(one, "") == one, 11); /* Finding empty. */
  719. (void) strcpy(one, "ababa");
  720. check(strstr(one, "ba") == one+1, 12); /* Finding first. */
  721. (void) strcpy(one, "");
  722. check(strstr(one, "b") == NULL, 13); /* Empty string. */
  723. check(strstr(one, "") == one, 14); /* Empty in empty string. */
  724. (void) strcpy(one, "bcbca");
  725. check(strstr(one, "bca") == one+2, 15); /* False start. */
  726. (void) strcpy(one, "bbbcabbca");
  727. check(strstr(one, "bbca") == one+1, 16); /* With overlap. */
  728. }
  729. static void
  730. test_strspn (void)
  731. {
  732. it = "strspn";
  733. check(strspn("abcba", "abc") == 5, 1); /* Whole string. */
  734. check(strspn("abcba", "ab") == 2, 2); /* Partial. */
  735. check(strspn("abc", "qx") == 0, 3); /* None. */
  736. check(strspn("", "ab") == 0, 4); /* Null string. */
  737. check(strspn("abc", "") == 0, 5); /* Null search list. */
  738. }
  739. static void
  740. test_strcspn (void)
  741. {
  742. it = "strcspn";
  743. check(strcspn("abcba", "qx") == 5, 1); /* Whole string. */
  744. check(strcspn("abcba", "cx") == 2, 2); /* Partial. */
  745. check(strcspn("abc", "abc") == 0, 3); /* None. */
  746. check(strcspn("", "ab") == 0, 4); /* Null string. */
  747. check(strcspn("abc", "") == 3, 5); /* Null search list. */
  748. }
  749. static void
  750. test_strtok (void)
  751. {
  752. it = "strtok";
  753. (void) strcpy(one, "first, second, third");
  754. equal(strtok(one, ", "), "first", 1); /* Basic test. */
  755. equal(one, "first", 2);
  756. equal(strtok((char *)NULL, ", "), "second", 3);
  757. equal(strtok((char *)NULL, ", "), "third", 4);
  758. check(strtok((char *)NULL, ", ") == NULL, 5);
  759. (void) strcpy(one, ", first, ");
  760. equal(strtok(one, ", "), "first", 6); /* Extra delims, 1 tok. */
  761. check(strtok((char *)NULL, ", ") == NULL, 7);
  762. (void) strcpy(one, "1a, 1b; 2a, 2b");
  763. equal(strtok(one, ", "), "1a", 8); /* Changing delim lists. */
  764. equal(strtok((char *)NULL, "; "), "1b", 9);
  765. equal(strtok((char *)NULL, ", "), "2a", 10);
  766. (void) strcpy(two, "x-y");
  767. equal(strtok(two, "-"), "x", 11); /* New string before done. */
  768. equal(strtok((char *)NULL, "-"), "y", 12);
  769. check(strtok((char *)NULL, "-") == NULL, 13);
  770. (void) strcpy(one, "a,b, c,, ,d");
  771. equal(strtok(one, ", "), "a", 14); /* Different separators. */
  772. equal(strtok((char *)NULL, ", "), "b", 15);
  773. equal(strtok((char *)NULL, " ,"), "c", 16); /* Permute list too. */
  774. equal(strtok((char *)NULL, " ,"), "d", 17);
  775. check(strtok((char *)NULL, ", ") == NULL, 18);
  776. check(strtok((char *)NULL, ", ") == NULL, 19); /* Persistence. */
  777. (void) strcpy(one, ", ");
  778. check(strtok(one, ", ") == NULL, 20); /* No tokens. */
  779. (void) strcpy(one, "");
  780. check(strtok(one, ", ") == NULL, 21); /* Empty string. */
  781. (void) strcpy(one, "abc");
  782. equal(strtok(one, ", "), "abc", 22); /* No delimiters. */
  783. check(strtok((char *)NULL, ", ") == NULL, 23);
  784. (void) strcpy(one, "abc");
  785. equal(strtok(one, ""), "abc", 24); /* Empty delimiter list. */
  786. check(strtok((char *)NULL, "") == NULL, 25);
  787. (void) strcpy(one, "abcdefgh");
  788. (void) strcpy(one, "a,b,c");
  789. equal(strtok(one, ","), "a", 26); /* Basics again... */
  790. equal(strtok((char *)NULL, ","), "b", 27);
  791. equal(strtok((char *)NULL, ","), "c", 28);
  792. check(strtok((char *)NULL, ",") == NULL, 29);
  793. equal(one+6, "gh", 30); /* Stomped past end? */
  794. equal(one, "a", 31); /* Stomped old tokens? */
  795. equal(one+2, "b", 32);
  796. equal(one+4, "c", 33);
  797. }
  798. static void
  799. test_strtok_r (void)
  800. {
  801. it = "strtok_r";
  802. (void) strcpy(one, "first, second, third");
  803. cp = NULL; /* Always initialize cp to make sure it doesn't point to some old data. */
  804. equal(strtok_r(one, ", ", &cp), "first", 1); /* Basic test. */
  805. equal(one, "first", 2);
  806. equal(strtok_r((char *)NULL, ", ", &cp), "second", 3);
  807. equal(strtok_r((char *)NULL, ", ", &cp), "third", 4);
  808. check(strtok_r((char *)NULL, ", ", &cp) == NULL, 5);
  809. (void) strcpy(one, ", first, ");
  810. cp = NULL;
  811. equal(strtok_r(one, ", ", &cp), "first", 6); /* Extra delims, 1 tok. */
  812. check(strtok_r((char *)NULL, ", ", &cp) == NULL, 7);
  813. (void) strcpy(one, "1a, 1b; 2a, 2b");
  814. cp = NULL;
  815. equal(strtok_r(one, ", ", &cp), "1a", 8); /* Changing delim lists. */
  816. equal(strtok_r((char *)NULL, "; ", &cp), "1b", 9);
  817. equal(strtok_r((char *)NULL, ", ", &cp), "2a", 10);
  818. (void) strcpy(two, "x-y");
  819. cp = NULL;
  820. equal(strtok_r(two, "-", &cp), "x", 11); /* New string before done. */
  821. equal(strtok_r((char *)NULL, "-", &cp), "y", 12);
  822. check(strtok_r((char *)NULL, "-", &cp) == NULL, 13);
  823. (void) strcpy(one, "a,b, c,, ,d");
  824. cp = NULL;
  825. equal(strtok_r(one, ", ", &cp), "a", 14); /* Different separators. */
  826. equal(strtok_r((char *)NULL, ", ", &cp), "b", 15);
  827. equal(strtok_r((char *)NULL, " ,", &cp), "c", 16); /* Permute list too. */
  828. equal(strtok_r((char *)NULL, " ,", &cp), "d", 17);
  829. check(strtok_r((char *)NULL, ", ", &cp) == NULL, 18);
  830. check(strtok_r((char *)NULL, ", ", &cp) == NULL, 19); /* Persistence. */
  831. (void) strcpy(one, ", ");
  832. cp = NULL;
  833. check(strtok_r(one, ", ", &cp) == NULL, 20); /* No tokens. */
  834. (void) strcpy(one, "");
  835. cp = NULL;
  836. check(strtok_r(one, ", ", &cp) == NULL, 21); /* Empty string. */
  837. check(strtok_r((char *)NULL, ", ", &cp) == NULL, 22); /* Persistence. */
  838. (void) strcpy(one, "abc");
  839. cp = NULL;
  840. equal(strtok_r(one, ", ", &cp), "abc", 23); /* No delimiters. */
  841. check(strtok_r((char *)NULL, ", ", &cp) == NULL, 24);
  842. (void) strcpy(one, "abc");
  843. cp = NULL;
  844. equal(strtok_r(one, "", &cp), "abc", 25); /* Empty delimiter list. */
  845. check(strtok_r((char *)NULL, "", &cp) == NULL, 26);
  846. (void) strcpy(one, "abcdefgh");
  847. (void) strcpy(one, "a,b,c");
  848. cp = NULL;
  849. equal(strtok_r(one, ",", &cp), "a", 27); /* Basics again... */
  850. equal(strtok_r((char *)NULL, ",", &cp), "b", 28);
  851. equal(strtok_r((char *)NULL, ",", &cp), "c", 29);
  852. check(strtok_r((char *)NULL, ",", &cp) == NULL, 30);
  853. equal(one+6, "gh", 31); /* Stomped past end? */
  854. equal(one, "a", 32); /* Stomped old tokens? */
  855. equal(one+2, "b", 33);
  856. equal(one+4, "c", 34);
  857. }
  858. static void
  859. test_strsep (void)
  860. {
  861. char *ptr;
  862. it = "strsep";
  863. cp = strcpy(one, "first, second, third");
  864. equal(strsep(&cp, ", "), "first", 1); /* Basic test. */
  865. equal(one, "first", 2);
  866. equal(strsep(&cp, ", "), "", 3);
  867. equal(strsep(&cp, ", "), "second", 4);
  868. equal(strsep(&cp, ", "), "", 5);
  869. equal(strsep(&cp, ", "), "third", 6);
  870. check(strsep(&cp, ", ") == NULL, 7);
  871. cp = strcpy(one, ", first, ");
  872. equal(strsep(&cp, ", "), "", 8);
  873. equal(strsep(&cp, ", "), "", 9);
  874. equal(strsep(&cp, ", "), "first", 10); /* Extra delims, 1 tok. */
  875. equal(strsep(&cp, ", "), "", 11);
  876. equal(strsep(&cp, ", "), "", 12);
  877. check(strsep(&cp, ", ") == NULL, 13);
  878. cp = strcpy(one, "1a, 1b; 2a, 2b");
  879. equal(strsep(&cp, ", "), "1a", 14); /* Changing delim lists. */
  880. equal(strsep(&cp, ", "), "", 15);
  881. equal(strsep(&cp, "; "), "1b", 16);
  882. equal(strsep(&cp, ", "), "", 17);
  883. equal(strsep(&cp, ", "), "2a", 18);
  884. cp = strcpy(two, "x-y");
  885. equal(strsep(&cp, "-"), "x", 19); /* New string before done. */
  886. equal(strsep(&cp, "-"), "y", 20);
  887. check(strsep(&cp, "-") == NULL, 21);
  888. cp = strcpy(one, "a,b, c,, ,d ");
  889. equal(strsep(&cp, ", "), "a", 22); /* Different separators. */
  890. equal(strsep(&cp, ", "), "b", 23);
  891. equal(strsep(&cp, " ,"), "", 24);
  892. equal(strsep(&cp, " ,"), "c", 25); /* Permute list too. */
  893. equal(strsep(&cp, " ,"), "", 26);
  894. equal(strsep(&cp, " ,"), "", 27);
  895. equal(strsep(&cp, " ,"), "", 28);
  896. equal(strsep(&cp, " ,"), "d", 29);
  897. equal(strsep(&cp, " ,"), "", 30);
  898. check(strsep(&cp, ", ") == NULL, 31);
  899. check(strsep(&cp, ", ") == NULL, 32); /* Persistence. */
  900. cp = strcpy(one, ", ");
  901. equal(strsep(&cp, ", "), "", 33);
  902. equal(strsep(&cp, ", "), "", 34);
  903. equal(strsep(&cp, ", "), "", 35);
  904. check(strsep(&cp, ", ") == NULL, 36); /* No tokens. */
  905. cp = strcpy(one, "");
  906. equal(strsep(&cp, ", "), "", 37);
  907. check(strsep(&cp, ", ") == NULL, 38); /* Empty string. */
  908. cp = strcpy(one, "abc");
  909. equal(strsep(&cp, ", "), "abc", 39); /* No delimiters. */
  910. check(strsep(&cp, ", ") == NULL, 40);
  911. cp = strcpy(one, "abc");
  912. equal(strsep(&cp, ""), "abc", 41); /* Empty delimiter list. */
  913. check(strsep(&cp, "") == NULL, 42);
  914. (void) strcpy(one, "abcdefgh");
  915. cp = strcpy(one, "a,b,c");
  916. equal(strsep(&cp, ","), "a", 43); /* Basics again... */
  917. equal(strsep(&cp, ","), "b", 44);
  918. equal(strsep(&cp, ","), "c", 45);
  919. check(strsep(&cp, ",") == NULL, 46);
  920. equal(one+6, "gh", 47); /* Stomped past end? */
  921. equal(one, "a", 48); /* Stomped old tokens? */
  922. equal(one+2, "b", 49);
  923. equal(one+4, "c", 50);
  924. {
  925. char text[] = "This,is,a,test";
  926. char *list = strdupa (text);
  927. equal (strsep (&list, ","), "This", 51);
  928. equal (strsep (&list, ","), "is", 52);
  929. equal (strsep (&list, ","), "a", 53);
  930. equal (strsep (&list, ","), "test", 54);
  931. check (strsep (&list, ",") == NULL, 55);
  932. }
  933. cp = strcpy(one, "a,b, c,, ,d,");
  934. equal(strsep(&cp, ","), "a", 56); /* Different separators. */
  935. equal(strsep(&cp, ","), "b", 57);
  936. equal(strsep(&cp, ","), " c", 58); /* Permute list too. */
  937. equal(strsep(&cp, ","), "", 59);
  938. equal(strsep(&cp, ","), " ", 60);
  939. equal(strsep(&cp, ","), "d", 61);
  940. equal(strsep(&cp, ","), "", 62);
  941. check(strsep(&cp, ",") == NULL, 63);
  942. check(strsep(&cp, ",") == NULL, 64); /* Persistence. */
  943. cp = strcpy(one, "a,b, c,, ,d,");
  944. equal(strsep(&cp, "xy,"), "a", 65); /* Different separators. */
  945. equal(strsep(&cp, "x,y"), "b", 66);
  946. equal(strsep(&cp, ",xy"), " c", 67); /* Permute list too. */
  947. equal(strsep(&cp, "xy,"), "", 68);
  948. equal(strsep(&cp, "x,y"), " ", 69);
  949. equal(strsep(&cp, ",xy"), "d", 70);
  950. equal(strsep(&cp, "xy,"), "", 71);
  951. check(strsep(&cp, "x,y") == NULL, 72);
  952. check(strsep(&cp, ",xy") == NULL, 73); /* Persistence. */
  953. cp = strcpy(one, "ABC");
  954. one[4] = ':';
  955. equal(strsep(&cp, "C"), "AB", 74); /* Access beyond NUL. */
  956. ptr = strsep(&cp, ":");
  957. equal(ptr, "", 75);
  958. check(ptr == one + 3, 76);
  959. check(cp == NULL, 77);
  960. cp = strcpy(one, "ABC");
  961. one[4] = ':';
  962. equal(strsep(&cp, "CD"), "AB", 78); /* Access beyond NUL. */
  963. ptr = strsep(&cp, ":.");
  964. equal(ptr, "", 79);
  965. check(ptr == one + 3, 80);
  966. cp = strcpy(one, "ABC"); /* No token in string. */
  967. equal(strsep(&cp, ","), "ABC", 81);
  968. check(cp == NULL, 82);
  969. *one = '\0'; /* Empty string. */
  970. cp = one;
  971. ptr = strsep(&cp, ",");
  972. equal(ptr, "", 83);
  973. check(ptr == one, 84);
  974. check(cp == NULL, 85);
  975. *one = '\0'; /* Empty string and no token. */
  976. cp = one;
  977. ptr = strsep(&cp, "");
  978. equal(ptr, "", 86);
  979. check(ptr == one , 87);
  980. check(cp == NULL, 88);
  981. }
  982. static void
  983. test_memcmp (void)
  984. {
  985. int i, cnt = 1;
  986. char one[21], two[21];
  987. it = "memcmp";
  988. check(memcmp("a", "a", 1) == 0, cnt++); /* Identity. */
  989. check(memcmp("abc", "abc", 3) == 0, cnt++); /* Multicharacter. */
  990. check(memcmp("abcd", "abcf", 4) < 0, cnt++); /* Honestly unequal. */
  991. check(memcmp("abcf", "abcd", 4) > 0, cnt++);
  992. check(memcmp("alph", "cold", 4) < 0, cnt++);
  993. check(memcmp("a\203", "a\003", 2) > 0, cnt++);
  994. check(memcmp("a\003", "a\203", 2) < 0, cnt++);
  995. check(memcmp("a\003bc", "a\203bc", 2) < 0, cnt++);
  996. check(memcmp("abc\203", "abc\003", 4) > 0, cnt++);
  997. check(memcmp("abc\003", "abc\203", 4) < 0, cnt++);
  998. check(memcmp("abcf", "abcd", 3) == 0, cnt++); /* Count limited. */
  999. check(memcmp("abc", "def", 0) == 0, cnt++); /* Zero count. */
  1000. /* Comparisons with shifting 4-byte boundaries. */
  1001. for (i=0; i<4; i++)
  1002. {
  1003. char *a = one + i, *b = two + i;
  1004. strncpy( a, "--------11112222", 16);
  1005. strncpy( b, "--------33334444", 16);
  1006. check( memcmp(b, a, 16) > 0, cnt++);
  1007. check( memcmp(a, b, 16) < 0, cnt++);
  1008. }
  1009. }
  1010. static void
  1011. test_memchr (void)
  1012. {
  1013. it = "memchr";
  1014. check(memchr("abcd", 'z', 4) == NULL, 1); /* Not found. */
  1015. (void) strcpy(one, "abcd");
  1016. check(memchr(one, 'c', 4) == one+2, 2); /* Basic test. */
  1017. check(memchr(one, ~0xff|'c', 4) == one+2, 2); /* ignore highorder bits. */
  1018. check(memchr(one, 'd', 4) == one+3, 3); /* End of string. */
  1019. check(memchr(one, 'a', 4) == one, 4); /* Beginning. */
  1020. check(memchr(one, '\0', 5) == one+4, 5); /* Finding NUL. */
  1021. (void) strcpy(one, "ababa");
  1022. check(memchr(one, 'b', 5) == one+1, 6); /* Finding first. */
  1023. check(memchr(one, 'b', 0) == NULL, 7); /* Zero count. */
  1024. check(memchr(one, 'a', 1) == one, 8); /* Singleton case. */
  1025. (void) strcpy(one, "a\203b");
  1026. check(memchr(one, 0203, 3) == one+1, 9); /* Unsignedness. */
  1027. /* now test all possible alignment and length combinations to catch
  1028. bugs due to unrolled loops (assuming unrolling is limited to no
  1029. more than 128 byte chunks: */
  1030. {
  1031. char buf[128 + sizeof(long)];
  1032. long align, len, i, pos;
  1033. for (align = 0; align < (long) sizeof(long); ++align) {
  1034. for (len = 0; len < (long) (sizeof(buf) - align); ++len) {
  1035. for (i = 0; i < len; ++i) {
  1036. buf[align + i] = 'x'; /* don't depend on memset... */
  1037. }
  1038. for (pos = 0; pos < len; ++pos) {
  1039. #if 0
  1040. printf("align %d, len %d, pos %d\n", align, len, pos);
  1041. #endif
  1042. check(memchr(buf + align, 'x', len) == buf + align + pos, 10);
  1043. check(memchr(buf + align, 'x', pos) == NULL, 11);
  1044. buf[align + pos] = '-';
  1045. }
  1046. }
  1047. }
  1048. }
  1049. }
  1050. static void
  1051. test_memcpy (void)
  1052. {
  1053. int i;
  1054. it = "memcpy";
  1055. check(memcpy(one, "abc", 4) == one, 1); /* Returned value. */
  1056. equal(one, "abc", 2); /* Did the copy go right? */
  1057. (void) strcpy(one, "abcdefgh");
  1058. (void) memcpy(one+1, "xyz", 2);
  1059. equal(one, "axydefgh", 3); /* Basic test. */
  1060. (void) strcpy(one, "abc");
  1061. (void) memcpy(one, "xyz", 0);
  1062. equal(one, "abc", 4); /* Zero-length copy. */
  1063. (void) strcpy(one, "hi there");
  1064. (void) strcpy(two, "foo");
  1065. (void) memcpy(two, one, 9);
  1066. equal(two, "hi there", 5); /* Just paranoia. */
  1067. equal(one, "hi there", 6); /* Stomped on source? */
  1068. for (i = 0; i < 16; i++)
  1069. {
  1070. const char *x = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  1071. strcpy (one, x);
  1072. check (memcpy (one + i, "hi there", 9) == one + i,
  1073. 7 + (i * 6)); /* Unaligned destination. */
  1074. check (memcmp (one, x, i) == 0, 8 + (i * 6)); /* Wrote under? */
  1075. equal (one + i, "hi there", 9 + (i * 6));
  1076. check (one[i + 9] == 'x', 10 + (i * 6)); /* Wrote over? */
  1077. check (memcpy (two, one + i, 9) == two,
  1078. 11 + (i * 6)); /* Unaligned source. */
  1079. equal (two, "hi there", 12 + (i * 6));
  1080. }
  1081. }
  1082. static void
  1083. test_mempcpy (void)
  1084. {
  1085. int i;
  1086. it = "mempcpy";
  1087. check(mempcpy(one, "abc", 4) == one + 4, 1); /* Returned value. */
  1088. equal(one, "abc", 2); /* Did the copy go right? */
  1089. (void) strcpy(one, "abcdefgh");
  1090. (void) mempcpy(one+1, "xyz", 2);
  1091. equal(one, "axydefgh", 3); /* Basic test. */
  1092. (void) strcpy(one, "abc");
  1093. (void) mempcpy(one, "xyz", 0);
  1094. equal(one, "abc", 4); /* Zero-length copy. */
  1095. (void) strcpy(one, "hi there");
  1096. (void) strcpy(two, "foo");
  1097. (void) mempcpy(two, one, 9);
  1098. equal(two, "hi there", 5); /* Just paranoia. */
  1099. equal(one, "hi there", 6); /* Stomped on source? */
  1100. for (i = 0; i < 16; i++)
  1101. {
  1102. const char *x = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  1103. strcpy (one, x);
  1104. check (mempcpy (one + i, "hi there", 9) == one + i + 9,
  1105. 7 + (i * 6)); /* Unaligned destination. */
  1106. check (memcmp (one, x, i) == 0, 8 + (i * 6)); /* Wrote under? */
  1107. equal (one + i, "hi there", 9 + (i * 6));
  1108. check (one[i + 9] == 'x', 10 + (i * 6)); /* Wrote over? */
  1109. check (mempcpy (two, one + i, 9) == two + 9,
  1110. 11 + (i * 6)); /* Unaligned source. */
  1111. equal (two, "hi there", 12 + (i * 6));
  1112. }
  1113. }
  1114. static void
  1115. test_memmove (void)
  1116. {
  1117. it = "memmove";
  1118. check(memmove(one, "abc", 4) == one, 1); /* Returned value. */
  1119. equal(one, "abc", 2); /* Did the copy go right? */
  1120. (void) strcpy(one, "abcdefgh");
  1121. (void) memmove(one+1, "xyz", 2);
  1122. equal(one, "axydefgh", 3); /* Basic test. */
  1123. (void) strcpy(one, "abc");
  1124. (void) memmove(one, "xyz", 0);
  1125. equal(one, "abc", 4); /* Zero-length copy. */
  1126. (void) strcpy(one, "hi there");
  1127. (void) strcpy(two, "foo");
  1128. (void) memmove(two, one, 9);
  1129. equal(two, "hi there", 5); /* Just paranoia. */
  1130. equal(one, "hi there", 6); /* Stomped on source? */
  1131. (void) strcpy(one, "abcdefgh");
  1132. (void) memmove(one+1, one, 9);
  1133. equal(one, "aabcdefgh", 7); /* Overlap, right-to-left. */
  1134. (void) strcpy(one, "abcdefgh");
  1135. (void) memmove(one+1, one+2, 7);
  1136. equal(one, "acdefgh", 8); /* Overlap, left-to-right. */
  1137. (void) strcpy(one, "abcdefgh");
  1138. (void) memmove(one, one, 9);
  1139. equal(one, "abcdefgh", 9); /* 100% overlap. */
  1140. }
  1141. static void
  1142. test_memccpy (void)
  1143. {
  1144. /* First test like memcpy, then the search part The SVID, the only
  1145. place where memccpy is mentioned, says overlap might fail, so we
  1146. don't try it. Besides, it's hard to see the rationale for a
  1147. non-left-to-right memccpy. */
  1148. it = "memccpy";
  1149. check(memccpy(one, "abc", 'q', 4) == NULL, 1); /* Returned value. */
  1150. equal(one, "abc", 2); /* Did the copy go right? */
  1151. (void) strcpy(one, "abcdefgh");
  1152. (void) memccpy(one+1, "xyz", 'q', 2);
  1153. equal(one, "axydefgh", 3); /* Basic test. */
  1154. (void) strcpy(one, "abc");
  1155. (void) memccpy(one, "xyz", 'q', 0);
  1156. equal(one, "abc", 4); /* Zero-length copy. */
  1157. (void) strcpy(one, "hi there");
  1158. (void) strcpy(two, "foo");
  1159. (void) memccpy(two, one, 'q', 9);
  1160. equal(two, "hi there", 5); /* Just paranoia. */
  1161. equal(one, "hi there", 6); /* Stomped on source? */
  1162. (void) strcpy(one, "abcdefgh");
  1163. (void) strcpy(two, "horsefeathers");
  1164. check(memccpy(two, one, 'f', 9) == two+6, 7); /* Returned value. */
  1165. equal(one, "abcdefgh", 8); /* Source intact? */
  1166. equal(two, "abcdefeathers", 9); /* Copy correct? */
  1167. (void) strcpy(one, "abcd");
  1168. (void) strcpy(two, "bumblebee");
  1169. check(memccpy(two, one, 'a', 4) == two+1, 10); /* First char. */
  1170. equal(two, "aumblebee", 11);
  1171. check(memccpy(two, one, 'd', 4) == two+4, 12); /* Last char. */
  1172. equal(two, "abcdlebee", 13);
  1173. (void) strcpy(one, "xyz");
  1174. check(memccpy(two, one, 'x', 1) == two+1, 14); /* Singleton. */
  1175. equal(two, "xbcdlebee", 15);
  1176. }
  1177. static void
  1178. test_memset (void)
  1179. {
  1180. int i;
  1181. it = "memset";
  1182. (void) strcpy(one, "abcdefgh");
  1183. check(memset(one+1, 'x', 3) == one+1, 1); /* Return value. */
  1184. equal(one, "axxxefgh", 2); /* Basic test. */
  1185. (void) memset(one+2, 'y', 0);
  1186. equal(one, "axxxefgh", 3); /* Zero-length set. */
  1187. (void) memset(one+5, 0, 1);
  1188. equal(one, "axxxe", 4); /* Zero fill. */
  1189. equal(one+6, "gh", 5); /* And the leftover. */
  1190. (void) memset(one+2, 010045, 1);
  1191. equal(one, "ax\045xe", 6); /* Unsigned char convert. */
  1192. /* Non-8bit fill character. */
  1193. memset (one, 0x101, sizeof (one));
  1194. for (i = 0; i < (int) sizeof (one); ++i)
  1195. check (one[i] == '\01', 7);
  1196. /* Test for more complex versions of memset, for all alignments and
  1197. lengths up to 256. This test takes a little while, perhaps it should
  1198. be made weaker? */
  1199. {
  1200. char data[512];
  1201. int j;
  1202. int k;
  1203. int c;
  1204. for (i = 0; i < 512; i++)
  1205. data[i] = 'x';
  1206. for (c = 0; c <= 'y'; c += 'y') /* check for memset(,0,) and
  1207. memset(,'y',) */
  1208. for (j = 0; j < 256; j++)
  1209. for (i = 0; i < 256; i++)
  1210. {
  1211. memset (data + i, c, j);
  1212. for (k = 0; k < i; k++)
  1213. if (data[k] != 'x')
  1214. goto fail;
  1215. for (k = i; k < i+j; k++)
  1216. {
  1217. if (data[k] != c)
  1218. goto fail;
  1219. data[k] = 'x';
  1220. }
  1221. for (k = i+j; k < 512; k++)
  1222. if (data[k] != 'x')
  1223. goto fail;
  1224. continue;
  1225. fail:
  1226. check (0, 8 + i + j * 256 + (c != 0) * 256 * 256);
  1227. }
  1228. }
  1229. }
  1230. static void
  1231. test_bcopy (void)
  1232. {
  1233. #ifdef __TEST_SUSV3_LEGACY__
  1234. /* Much like memcpy. Berklix manual is silent about overlap, so
  1235. don't test it. */
  1236. it = "bcopy";
  1237. (void) bcopy("abc", one, 4);
  1238. equal(one, "abc", 1); /* Simple copy. */
  1239. (void) strcpy(one, "abcdefgh");
  1240. (void) bcopy("xyz", one+1, 2);
  1241. equal(one, "axydefgh", 2); /* Basic test. */
  1242. (void) strcpy(one, "abc");
  1243. (void) bcopy("xyz", one, 0);
  1244. equal(one, "abc", 3); /* Zero-length copy. */
  1245. (void) strcpy(one, "hi there");
  1246. (void) strcpy(two, "foo");
  1247. (void) bcopy(one, two, 9);
  1248. equal(two, "hi there", 4); /* Just paranoia. */
  1249. equal(one, "hi there", 5); /* Stomped on source? */
  1250. #endif
  1251. }
  1252. static void
  1253. test_bzero (void)
  1254. {
  1255. #ifdef __TEST_SUSV3_LEGACY__
  1256. it = "bzero";
  1257. (void) strcpy(one, "abcdef");
  1258. bzero(one+2, 2);
  1259. equal(one, "ab", 1); /* Basic test. */
  1260. equal(one+3, "", 2);
  1261. equal(one+4, "ef", 3);
  1262. (void) strcpy(one, "abcdef");
  1263. bzero(one+2, 0);
  1264. equal(one, "abcdef", 4); /* Zero-length copy. */
  1265. #endif
  1266. }
  1267. static void
  1268. test_strndup (void)
  1269. {
  1270. char *p, *q;
  1271. it = "strndup";
  1272. p = strndup("abcdef", 12);
  1273. check(p != NULL, 1);
  1274. if (p != NULL)
  1275. {
  1276. equal(p, "abcdef", 2);
  1277. q = strndup(p + 1, 2);
  1278. check(q != NULL, 3);
  1279. if (q != NULL)
  1280. equal(q, "bc", 4);
  1281. free (q);
  1282. }
  1283. free (p);
  1284. p = strndup("abc def", 3);
  1285. check(p != NULL, 5);
  1286. if (p != NULL)
  1287. equal(p, "abc", 6);
  1288. free (p);
  1289. }
  1290. static void
  1291. test_bcmp (void)
  1292. {
  1293. #ifdef __TEST_SUSV3_LEGACY__
  1294. it = "bcmp";
  1295. check(bcmp("a", "a", 1) == 0, 1); /* Identity. */
  1296. check(bcmp("abc", "abc", 3) == 0, 2); /* Multicharacter. */
  1297. check(bcmp("abcd", "abce", 4) != 0, 3); /* Honestly unequal. */
  1298. check(bcmp("abce", "abcd", 4) != 0, 4);
  1299. check(bcmp("alph", "beta", 4) != 0, 5);
  1300. check(bcmp("abce", "abcd", 3) == 0, 6); /* Count limited. */
  1301. check(bcmp("abc", "def", 0) == 0, 8); /* Zero count. */
  1302. #endif
  1303. }
  1304. static void
  1305. test_strerror (void)
  1306. {
  1307. it = "strerror";
  1308. check(strerror(EDOM) != 0, 1);
  1309. check(strerror(ERANGE) != 0, 2);
  1310. check(strerror(ENOENT) != 0, 3);
  1311. }
  1312. static void
  1313. test_strcasecmp (void)
  1314. {
  1315. it = "strcasecmp";
  1316. /* Note that the locale is "C". */
  1317. check(strcasecmp("a", "a") == 0, 1);
  1318. check(strcasecmp("a", "A") == 0, 2);
  1319. check(strcasecmp("A", "a") == 0, 3);
  1320. check(strcasecmp("a", "b") < 0, 4);
  1321. check(strcasecmp("c", "b") > 0, 5);
  1322. check(strcasecmp("abc", "AbC") == 0, 6);
  1323. check(strcasecmp("0123456789", "0123456789") == 0, 7);
  1324. check(strcasecmp("", "0123456789") < 0, 8);
  1325. check(strcasecmp("AbC", "") > 0, 9);
  1326. check(strcasecmp("AbC", "A") > 0, 10);
  1327. check(strcasecmp("AbC", "Ab") > 0, 11);
  1328. check(strcasecmp("AbC", "ab") > 0, 12);
  1329. }
  1330. static void
  1331. test_strncasecmp (void)
  1332. {
  1333. it = "strncasecmp";
  1334. /* Note that the locale is "C". */
  1335. check(strncasecmp("a", "a", 5) == 0, 1);
  1336. check(strncasecmp("a", "A", 5) == 0, 2);
  1337. check(strncasecmp("A", "a", 5) == 0, 3);
  1338. check(strncasecmp("a", "b", 5) < 0, 4);
  1339. check(strncasecmp("c", "b", 5) > 0, 5);
  1340. check(strncasecmp("abc", "AbC", 5) == 0, 6);
  1341. check(strncasecmp("0123456789", "0123456789", 10) == 0, 7);
  1342. check(strncasecmp("", "0123456789", 10) < 0, 8);
  1343. check(strncasecmp("AbC", "", 5) > 0, 9);
  1344. check(strncasecmp("AbC", "A", 5) > 0, 10);
  1345. check(strncasecmp("AbC", "Ab", 5) > 0, 11);
  1346. check(strncasecmp("AbC", "ab", 5) > 0, 12);
  1347. check(strncasecmp("0123456789", "AbC", 0) == 0, 13);
  1348. check(strncasecmp("AbC", "abc", 1) == 0, 14);
  1349. check(strncasecmp("AbC", "abc", 2) == 0, 15);
  1350. check(strncasecmp("AbC", "abc", 3) == 0, 16);
  1351. check(strncasecmp("AbC", "abcd", 3) == 0, 17);
  1352. check(strncasecmp("AbC", "abcd", 4) < 0, 18);
  1353. check(strncasecmp("ADC", "abcd", 1) == 0, 19);
  1354. check(strncasecmp("ADC", "abcd", 2) > 0, 20);
  1355. }
  1356. int
  1357. main (void)
  1358. {
  1359. int status;
  1360. /* Test strcmp first because we use it to test other things. */
  1361. test_strcmp ();
  1362. /* Test strcpy next because we need it to set up other tests. */
  1363. test_strcpy ();
  1364. /* A closely related function is stpcpy. */
  1365. test_stpcpy ();
  1366. /* stpncpy. */
  1367. test_stpncpy ();
  1368. /* strcat. */
  1369. test_strcat ();
  1370. /* strncat. */
  1371. test_strncat ();
  1372. /* strlcat. */
  1373. test_strlcat ();
  1374. /* strncmp. */
  1375. test_strncmp ();
  1376. /* strncpy. */
  1377. test_strncpy ();
  1378. /* strlcpy. */
  1379. test_strlcpy ();
  1380. /* strlen. */
  1381. test_strlen ();
  1382. /* strnlen. */
  1383. test_strnlen ();
  1384. /* strchr. */
  1385. test_strchr ();
  1386. /* strchrnul. */
  1387. test_strchrnul ();
  1388. /* rawmemchr. */
  1389. #if defined(__GLIBC__) || defined(__UCLIBC__)
  1390. test_rawmemchr ();
  1391. #endif
  1392. /* index - just like strchr. */
  1393. test_index ();
  1394. /* strrchr. */
  1395. test_strrchr ();
  1396. /* memrchr. */
  1397. test_memrchr ();
  1398. /* rindex - just like strrchr. */
  1399. test_rindex ();
  1400. /* strpbrk - somewhat like strchr. */
  1401. test_strpbrk ();
  1402. /* strstr - somewhat like strchr. */
  1403. test_strstr ();
  1404. /* strspn. */
  1405. test_strspn ();
  1406. /* strcspn. */
  1407. test_strcspn ();
  1408. /* strtok - the hard one. */
  1409. test_strtok ();
  1410. /* strtok_r. */
  1411. test_strtok_r ();
  1412. /* strsep. */
  1413. test_strsep ();
  1414. /* memcmp. */
  1415. test_memcmp ();
  1416. /* memchr. */
  1417. test_memchr ();
  1418. /* memcpy - need not work for overlap. */
  1419. test_memcpy ();
  1420. /* memmove - must work on overlap. */
  1421. test_memmove ();
  1422. /* mempcpy */
  1423. test_mempcpy ();
  1424. /* memccpy. */
  1425. test_memccpy ();
  1426. /* memset. */
  1427. test_memset ();
  1428. /* bcopy. */
  1429. test_bcopy ();
  1430. /* bzero. */
  1431. test_bzero ();
  1432. /* bcmp - somewhat like memcmp. */
  1433. test_bcmp ();
  1434. /* strndup. */
  1435. test_strndup ();
  1436. /* strerror - VERY system-dependent. */
  1437. test_strerror ();
  1438. /* strcasecmp. Without locale dependencies. */
  1439. test_strcasecmp ();
  1440. /* strncasecmp. Without locale dependencies. */
  1441. test_strncasecmp ();
  1442. if (errors == 0)
  1443. {
  1444. status = EXIT_SUCCESS;
  1445. puts("No errors.");
  1446. }
  1447. else
  1448. {
  1449. status = EXIT_FAILURE;
  1450. printf("%Zd errors.\n", errors);
  1451. }
  1452. return status;
  1453. }