string.c 43 KB

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