home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume32 / dbmalloc / part10 < prev    next >
Encoding:
Text File  |  1992-09-03  |  26.7 KB  |  809 lines

  1. Newsgroups: comp.sources.misc
  2. From: cpcahil@vti.com (Conor P. Cahill)
  3. Subject:  v32i015:  dbmalloc - Debug Malloc Library PL14, Part10/10
  4. Message-ID: <1992Sep4.152431.13635@sparky.imd.sterling.com>
  5. X-Md4-Signature: 292b637028fb8f72ad1ac5da5c4a4bd3
  6. Date: Fri, 4 Sep 1992 15:24:31 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: cpcahil@vti.com (Conor P. Cahill)
  10. Posting-number: Volume 32, Issue 15
  11. Archive-name: dbmalloc/part10
  12. Environment: C, UNIX
  13.  
  14. #! /bin/sh
  15. # This is a shell archive.  Remove anything before this line, then unpack
  16. # it by saving it into a file and typing "sh file".  To overwrite existing
  17. # files, type "sh file -c".  You can also feed this as standard input via
  18. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  19. # will see the following message at the end:
  20. #        "End of archive 10 (of 10)."
  21. # Contents:  testmem.c
  22. # Wrapped by cpcahil@virtech on Thu Sep  3 18:39:21 1992
  23. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  24. if test -f 'testmem.c' -a "${1}" != "-c" ; then 
  25.   echo shar: Will not clobber existing file \"'testmem.c'\"
  26. else
  27. echo shar: Extracting \"'testmem.c'\" \(24605 characters\)
  28. sed "s/^X//" >'testmem.c' <<'END_OF_FILE'
  29. X/*
  30. X * (c) Copyright 1990, 1991, 1992 Conor P. Cahill (cpcahil@virtech.vti.com)
  31. X *
  32. X * This software may be distributed freely as long as the following conditions
  33. X * are met:
  34. X *         * the distribution, or any derivative thereof, may not be
  35. X *          included as part of a commercial product
  36. X *        * full source code is provided including this copyright
  37. X *        * there is no charge for the software itself (there may be
  38. X *          a minimal charge for the copying or distribution effort)
  39. X *        * this copyright notice is not modified or removed from any
  40. X *          source file
  41. X */
  42. X/* 
  43. X * This stuff is all stolen (with permission, since it was in the public
  44. X * domain) from Henry Spencer's string and memory library.  Thanks Henry.
  45. X */
  46. X
  47. X/*
  48. X * Test program for string(3) routines.
  49. X * 
  50. X * Note that at least one Bell Labs implementation of the string
  51. X * routines flunks a couple of these tests -- the ones which test
  52. X * behavior on "negative" characters.
  53. X */
  54. X
  55. X#include <stdio.h>
  56. X#include <sys/types.h>
  57. X#include "malloc.h"
  58. X
  59. X#ifndef index
  60. Xchar * index();
  61. Xchar * rindex();
  62. X#endif
  63. X
  64. XVOIDTYPE    first();
  65. XVOIDTYPE    second();
  66. X
  67. X#define    STREQ(a, b)    (strcmp((a), (b)) == 0)
  68. X
  69. Xchar *it = "<UNSET>";        /* Routine name for message routines. */
  70. Xint waserror = 0;        /* For exit status. */
  71. X
  72. XSTRCMPTYPE structest[] = "\004\203";    /* For testing signedness of chars. */
  73. XMEMCMPTYPE memuctest[] = "\004\203";    /* For testing signedness of chars. */
  74. Xint strcharsigned;            /* Result. */
  75. Xint memcharsigned;            /* Result. */
  76. X
  77. X/*
  78. X - check - complain if condition is not true
  79. X */
  80. XVOIDTYPE
  81. Xcheck(thing, number)
  82. Xint thing;
  83. Xint number;            /* Test number for error message. */
  84. X{
  85. X    if (!thing) {
  86. X        printf("%s flunked test %d\n", it, number);
  87. X        waserror = 1;
  88. X    }
  89. X}
  90. X
  91. X/*
  92. X - equal - complain if first two args don't strcmp as equal
  93. X */
  94. XVOIDTYPE
  95. Xequal(a, b, number)
  96. Xchar *a;
  97. Xchar *b;
  98. Xint number;            /* Test number for error message. */
  99. X{
  100. X    check(a != NULL && b != NULL && STREQ(a, b), number);
  101. X}
  102. X
  103. Xchar one[50];
  104. Xchar two[50];
  105. X
  106. X#ifdef UNIXERR
  107. X#define ERR 1
  108. X#endif
  109. X#ifdef BERKERR
  110. X#define ERR 1
  111. X#endif
  112. X#ifdef ERR
  113. Xint f;
  114. Xextern char *sys_errlist[];
  115. Xextern int sys_nerr;
  116. Xextern int errno;
  117. X#endif
  118. X
  119. X/* ARGSUSED */
  120. Xint
  121. Xmain(argc, argv)
  122. Xint argc;
  123. Xchar *argv[];
  124. X{
  125. X    /*
  126. X     * First, establish whether string chars are signed.
  127. X     */
  128. X    if (structest[0] < structest[1])
  129. X        strcharsigned = 0;
  130. X    else
  131. X        strcharsigned = 1;
  132. X
  133. X    /*
  134. X     * then establish whether memory chars are signed.
  135. X     */
  136. X    if (memuctest[0] < memuctest[1])
  137. X        memcharsigned = 0;
  138. X    else
  139. X        memcharsigned = 1;
  140. X
  141. X    /*
  142. X     * Then, do the rest of the work.  Split into two functions because
  143. X     * some compilers get unhappy about a single immense function.
  144. X     */
  145. X    first();
  146. X    second();
  147. X
  148. X    exit((waserror) ? 1 : 0);
  149. X    /*NOTREACHED*/
  150. X}
  151. X
  152. XVOIDTYPE
  153. Xfirst()
  154. X{
  155. X    /*
  156. X     * Test strcmp first because we use it to test other things.
  157. X     */
  158. X    it = "strcmp";
  159. X    check(strcmp("", "") == 0, 1);        /* Trivial case. */
  160. X    check(strcmp("a", "a") == 0, 2);    /* Identity. */
  161. X    check(strcmp("abc", "abc") == 0, 3);    /* Multicharacter. */
  162. X    check(strcmp("abc", "abcd") < 0, 4);    /* Length mismatches. */
  163. X    check(strcmp("abcd", "abc") > 0, 5);
  164. X    check(strcmp("abcd", "abce") < 0, 6);    /* Honest miscompares. */
  165. X    check(strcmp("abce", "abcd") > 0, 7);
  166. X    check(strcmp("a\203", "a") > 0, 8);    /* Tricky if char signed. */
  167. X    if (strcharsigned)            /* Sign-bit comparison. */
  168. X        check(strcmp("a\203", "a\003") < 0, 9);
  169. X    else
  170. X        check(strcmp("a\203", "a\003") > 0, 9);
  171. X    check(strcmp("a", "a\203") < 0, 10);    /* Tricky if char signed. */
  172. X
  173. X    /*
  174. X     * now test stricmp (make sure it does everything of strcmp and then
  175. X     * some)
  176. X     */
  177. X    it = "stricmp";
  178. X    check(stricmp("", "") == 0, 1);        /* Trivial case. */
  179. X    check(stricmp("a", "a") == 0, 2);    /* Identity. */
  180. X    check(stricmp("abc", "abc") == 0, 3);    /* Multicharacter. */
  181. X    check(stricmp("abc", "abcd") < 0, 4);    /* Length mismatches. */
  182. X    check(stricmp("abcd", "abc") > 0, 5);
  183. X    check(stricmp("abcd", "abce") < 0, 6);    /* Honest miscompares. */
  184. X    check(stricmp("abce", "abcd") > 0, 7);
  185. X    check(stricmp("a\203", "a") > 0, 8);    /* Tricky if char signed. */
  186. X    if (strcharsigned)            /* Sign-bit comparison. */
  187. X        check(stricmp("a\203", "a\003") < 0, 9);
  188. X    else
  189. X        check(stricmp("a\203", "a\003") > 0, 9);
  190. X    check(stricmp("a", "a\203") < 0, 10);    /* Tricky if char signed. */
  191. X    check(stricmp("a", "A") == 0, 11);    /* Identity. */
  192. X    check(stricmp("aBc", "abc") == 0, 12);    /* Multicharacter. */
  193. X    check(stricmp("abC", "abcd") < 0, 13);    /* Length mismatches. */
  194. X    check(stricmp("abcD", "abc") > 0, 14);
  195. X    check(stricmp("abcD", "abce") < 0, 15);    /* Honest miscompares. */
  196. X    check(stricmp("abcd", "abcE") < 0, 16);    /* Honest miscompares. */
  197. X
  198. X    /*
  199. X     * Test strcpy next because we need it to set up other tests.
  200. X     */
  201. X    it = "strcpy";
  202. X    check(strcpy(one, "abcd") == one, 1);    /* Returned value. */
  203. X    equal(one, "abcd", 2);            /* Basic test. */
  204. X
  205. X    VOIDCAST strcpy(one, "x");
  206. X    equal(one, "x", 3);            /* Writeover. */
  207. X    equal(one+2, "cd", 4);            /* Wrote too much? */
  208. X
  209. X    VOIDCAST strcpy(two, "hi there");
  210. X    VOIDCAST strcpy(one, two);
  211. X    equal(one, "hi there", 5);        /* Basic test encore. */
  212. X    equal(two, "hi there", 6);        /* Stomped on source? */
  213. X
  214. X    VOIDCAST strcpy(one, "");
  215. X    equal(one, "", 7);            /* Boundary condition. */
  216. X
  217. X    /*
  218. X     * strcat
  219. X     */
  220. X    it = "strcat";
  221. X    VOIDCAST strcpy(one, "ijk");
  222. X    check(strcat(one, "lmn") == one, 1);    /* Returned value. */
  223. X    equal(one, "ijklmn", 2);        /* Basic test. */
  224. X
  225. X    VOIDCAST strcpy(one, "x");
  226. X    VOIDCAST strcat(one, "yz");
  227. X    equal(one, "xyz", 3);            /* Writeover. */
  228. X    equal(one+4, "mn", 4);            /* Wrote too much? */
  229. X
  230. X    VOIDCAST strcpy(one, "gh");
  231. X    VOIDCAST strcpy(two, "ef");
  232. X    VOIDCAST strcat(one, two);
  233. X    equal(one, "ghef", 5);            /* Basic test encore. */
  234. X    equal(two, "ef", 6);            /* Stomped on source? */
  235. X
  236. X    VOIDCAST strcpy(one, "");
  237. X    VOIDCAST strcat(one, "");
  238. X    equal(one, "", 7);            /* Boundary conditions. */
  239. X    VOIDCAST strcpy(one, "ab");
  240. X    VOIDCAST strcat(one, "");
  241. X    equal(one, "ab", 8);
  242. X    VOIDCAST strcpy(one, "");
  243. X    VOIDCAST strcat(one, "cd");
  244. X    equal(one, "cd", 9);
  245. X
  246. X    /*
  247. X     * strncat - first test it as strcat, with big counts, then
  248. X     * test the count mechanism.
  249. X     */
  250. X    it = "strncat";
  251. X    VOIDCAST strcpy(one, "ijk");
  252. X    check(strncat(one, "lmn", 99) == one, 1);    /* Returned value. */
  253. X    equal(one, "ijklmn", 2);        /* Basic test. */
  254. X
  255. X    VOIDCAST strcpy(one, "x");
  256. X    VOIDCAST strncat(one, "yz", 99);
  257. X    equal(one, "xyz", 3);            /* Writeover. */
  258. X    equal(one+4, "mn", 4);            /* Wrote too much? */
  259. X
  260. X    VOIDCAST strcpy(one, "gh");
  261. X    VOIDCAST strcpy(two, "ef");
  262. X    VOIDCAST strncat(one, two, 99);
  263. X    equal(one, "ghef", 5);            /* Basic test encore. */
  264. X    equal(two, "ef", 6);            /* Stomped on source? */
  265. X
  266. X    VOIDCAST strcpy(one, "");
  267. X    VOIDCAST strncat(one, "", 99);
  268. X    equal(one, "", 7);            /* Boundary conditions. */
  269. X    VOIDCAST strcpy(one, "ab");
  270. X    VOIDCAST strncat(one, "", 99);
  271. X    equal(one, "ab", 8);
  272. X    VOIDCAST strcpy(one, "");
  273. X    VOIDCAST strncat(one, "cd", 99);
  274. X    equal(one, "cd", 9);
  275. X
  276. X    VOIDCAST strcpy(one, "ab");
  277. X    VOIDCAST strncat(one, "cdef", 2);
  278. X    equal(one, "abcd", 10);            /* Count-limited. */
  279. X
  280. X    VOIDCAST strncat(one, "gh", 0);
  281. X    equal(one, "abcd", 11);            /* Zero count. */
  282. X
  283. X    VOIDCAST strncat(one, "gh", 2);
  284. X    equal(one, "abcdgh", 12);        /* Count and length equal. */
  285. X
  286. X    /*
  287. X     * strncmp - first test as strcmp with big counts, then test
  288. X     * count code.
  289. X     */
  290. X    it = "strncmp";
  291. X    check(strncmp("", "", 99) == 0, 1);    /* Trivial case. */
  292. X    check(strncmp("a", "a", 99) == 0, 2);    /* Identity. */
  293. X    check(strncmp("abc", "abc", 99) == 0, 3);    /* Multicharacter. */
  294. X    check(strncmp("abc", "abcd", 99) < 0, 4);    /* Length unequal. */
  295. X    check(strncmp("abcd", "abc", 99) > 0, 5);
  296. X    check(strncmp("abcd", "abce", 99) < 0, 6);    /* Honestly unequal. */
  297. X    check(strncmp("abce", "abcd", 99) > 0, 7);
  298. X    check(strncmp("a\203", "a", 2) > 0, 8);    /* Tricky if '\203' < 0 */
  299. X    if (strcharsigned)            /* Sign-bit comparison. */
  300. X        check(strncmp("a\203", "a\003", 2) < 0, 9);
  301. X    else
  302. X        check(strncmp("a\203", "a\003", 2) > 0, 9);
  303. X    check(strncmp("abce", "abcd", 3) == 0, 10);    /* Count limited. */
  304. X    check(strncmp("abce", "abc", 3) == 0, 11);    /* Count == length. */
  305. X    check(strncmp("abcd", "abce", 4) < 0, 12);    /* Nudging limit. */
  306. X    check(strncmp("abc", "def", 0) == 0, 13);    /* Zero count. */
  307. X
  308. X    /*
  309. X     * strincmp - first test as strincmp with big counts, then test
  310. X     * count code, then test with different cases
  311. X     */
  312. X    it = "strincmp";
  313. X    check(strincmp("", "", 99) == 0, 1);    /* Trivial case. */
  314. X    check(strincmp("a", "a", 99) == 0, 2);    /* Identity. */
  315. X    check(strincmp("abc", "abc", 99) == 0, 3);    /* Multicharacter. */
  316. X    check(strincmp("abc", "abcd", 99) < 0, 4);    /* Length unequal. */
  317. X    check(strincmp("abcd", "abc", 99) > 0, 5);
  318. X    check(strincmp("abcd", "abce", 99) < 0, 6);    /* Honestly unequal. */
  319. X    check(strincmp("abce", "abcd", 99) > 0, 7);
  320. X    check(strincmp("a\203", "a", 2) > 0, 8);/* Tricky if '\203' < 0 */
  321. X    if (strcharsigned)            /* Sign-bit comparison. */
  322. X        check(strincmp("a\203", "a\003", 2) < 0, 9);
  323. X    else
  324. X        check(strincmp("a\203", "a\003", 2) > 0, 9);
  325. X    check(strincmp("abce", "abcd", 3) == 0, 10);    /* Count limited. */
  326. X    check(strincmp("abce", "abc", 3) == 0, 11);    /* Count == length. */
  327. X    check(strincmp("abcd", "abce", 4) < 0, 12);    /* Nudging limit. */
  328. X    check(strincmp("abc", "def", 0) == 0, 13);    /* Zero count. */
  329. X
  330. X    check(strincmp("a", "A", 99) == 0, 14);    /* Identity. */
  331. X    check(strincmp("abC", "abc", 99) == 0, 15);    /* Multicharacter. */
  332. X    check(strincmp("abC", "abcd", 99) < 0, 16);    /* Length unequal. */
  333. X    check(strincmp("abcd", "Abc", 99) > 0, 17);
  334. X    check(strincmp("abcD", "abce", 99) < 0, 18);    /* Honestly unequal. */
  335. X    check(strincmp("abcE", "abcd", 99) > 0, 19);
  336. X    check(strincmp("abce", "abcd", 99) > 0, 20);
  337. X
  338. X    /*
  339. X     * strncpy - testing is a bit different because of odd semantics
  340. X     */
  341. X    it = "strncpy";
  342. X    check(strncpy(one, "abc", 4) == one, 1);    /* Returned value. */
  343. X    equal(one, "abc", 2);            /* Did the copy go right? */
  344. X
  345. X    VOIDCAST strcpy(one, "abcdefgh");
  346. X    VOIDCAST strncpy(one, "xyz", 2);
  347. X    equal(one, "xycdefgh", 3);        /* Copy cut by count. */
  348. X
  349. X    VOIDCAST strcpy(one, "abcdefgh");
  350. X    VOIDCAST strncpy(one, "xyz", 3);    /* Copy cut just before NUL. */
  351. X    equal(one, "xyzdefgh", 4);
  352. X
  353. X    VOIDCAST strcpy(one, "abcdefgh");
  354. X    VOIDCAST strncpy(one, "xyz", 4);    /* Copy just includes NUL. */
  355. X    equal(one, "xyz", 5);
  356. X    equal(one+4, "efgh", 6);        /* Wrote too much? */
  357. X
  358. X    VOIDCAST strcpy(one, "abcdefgh");
  359. X    VOIDCAST strncpy(one, "xyz", 5);    /* Copy includes padding. */
  360. X    equal(one, "xyz", 7);
  361. X    equal(one+4, "", 8);
  362. X    equal(one+5, "fgh", 9);
  363. X
  364. X    VOIDCAST strcpy(one, "abc");
  365. X    VOIDCAST strncpy(one, "xyz", 0);    /* Zero-length copy. */
  366. X    equal(one, "abc", 10);    
  367. X
  368. X    VOIDCAST strncpy(one, "", 2);        /* Zero-length source. */
  369. X    equal(one, "", 11);
  370. X    equal(one+1, "", 12);    
  371. X    equal(one+2, "c", 13);
  372. X
  373. X    VOIDCAST strcpy(one, "hi there");
  374. X    VOIDCAST strncpy(two, one, 9);
  375. X    equal(two, "hi there", 14);        /* Just paranoia. */
  376. X    equal(one, "hi there", 15);        /* Stomped on source? */
  377. X
  378. X    /*
  379. X     * strlen
  380. X     */
  381. X    it = "strlen";
  382. X    check(strlen("") == 0, 1);        /* Empty. */
  383. X    check(strlen("a") == 1, 2);        /* Single char. */
  384. X    check(strlen("abcd") == 4, 3);        /* Multiple chars. */
  385. X
  386. X    /*
  387. X     * strchr
  388. X     */
  389. X    it = "strchr";
  390. X    check(strchr("abcd", 'z') == NULL, 1);    /* Not found. */
  391. X    VOIDCAST strcpy(one, "abcd");
  392. X    check(strchr(one, 'c') == one+2, 2);    /* Basic test. */
  393. X    check(strchr(one, 'd') == one+3, 3);    /* End of string. */
  394. X    check(strchr(one, 'a') == one, 4);    /* Beginning. */
  395. X    check(strchr(one, '\0') == one+4, 5);    /* Finding NUL. */
  396. X    VOIDCAST strcpy(one, "ababa");
  397. X    check(strchr(one, 'b') == one+1, 6);    /* Finding first. */
  398. X    VOIDCAST strcpy(one, "");
  399. X    check(strchr(one, 'b') == NULL, 7);    /* Empty string. */
  400. X    check(strchr(one, '\0') == one, 8);    /* NUL in empty string. */
  401. X
  402. X    /*
  403. X     * strstr (minimal testing added by cpc)
  404. X     */
  405. X    it = "strstr";
  406. X    check(strstr("abcd", "z") == NULL, 1);    /* Not found. */
  407. X    VOIDCAST strcpy(one, "abcd");
  408. X    check(strstr(one, "c") == one+2, 2);    /* Basic test. */
  409. X    check(strstr(one, "cd") == one+2, 3);    /* Basic test. */
  410. X    check(strstr(one, "d") == one+3, 4);    /* End of string. */
  411. X    check(strstr(one, "a") == one, 5);    /* Beginning. */
  412. X    check(strstr(one, "") == one, 6);    /* Finding NUL. */
  413. X    VOIDCAST strcpy(one, "ababa");
  414. X    check(strstr(one, "b") == one+1, 7);    /* Finding first. */
  415. X    VOIDCAST strcpy(one, "");
  416. X    check(strstr(one, "b") == NULL, 8);    /* Empty string. */
  417. X    check(strstr(one, "") == one, 9);    /* NUL in empty string. */
  418. X
  419. X    /*
  420. X     * index - just like strchr
  421. X     */
  422. X    it = "index";
  423. X    check(index("abcd", 'z') == NULL, 1);    /* Not found. */
  424. X    VOIDCAST strcpy(one, "abcd");
  425. X    check(index(one, 'c') == one+2, 2);    /* Basic test. */
  426. X    check(index(one, 'd') == one+3, 3);    /* End of string. */
  427. X    check(index(one, 'a') == one, 4);    /* Beginning. */
  428. X    check(index(one, '\0') == one+4, 5);    /* Finding NUL. */
  429. X    VOIDCAST strcpy(one, "ababa");
  430. X    check(index(one, 'b') == one+1, 6);    /* Finding first. */
  431. X    VOIDCAST strcpy(one, "");
  432. X    check(index(one, 'b') == NULL, 7);    /* Empty string. */
  433. X    check(index(one, '\0') == one, 8);    /* NUL in empty string. */
  434. X
  435. X    /*
  436. X     * strrchr
  437. X     */
  438. X    it = "strrchr";
  439. X    check(strrchr("abcd", 'z') == NULL, 1);    /* Not found. */
  440. X    VOIDCAST strcpy(one, "abcd");
  441. X    check(strrchr(one, 'c') == one+2, 2);    /* Basic test. */
  442. X    check(strrchr(one, 'd') == one+3, 3);    /* End of string. */
  443. X    check(strrchr(one, 'a') == one, 4);    /* Beginning. */
  444. X    check(strrchr(one, '\0') == one+4, 5);    /* Finding NUL. */
  445. X    VOIDCAST strcpy(one, "ababa");
  446. X    check(strrchr(one, 'b') == one+3, 6);    /* Finding last. */
  447. X    VOIDCAST strcpy(one, "");
  448. X    check(strrchr(one, 'b') == NULL, 7);    /* Empty string. */
  449. X    check(strrchr(one, '\0') == one, 8);    /* NUL in empty string. */
  450. X
  451. X    /*
  452. X     * rindex - just like strrchr
  453. X     */
  454. X    it = "rindex";
  455. X    check(rindex("abcd", 'z') == NULL, 1);    /* Not found. */
  456. X    VOIDCAST strcpy(one, "abcd");
  457. X    check(rindex(one, 'c') == one+2, 2);    /* Basic test. */
  458. X    check(rindex(one, 'd') == one+3, 3);    /* End of string. */
  459. X    check(rindex(one, 'a') == one, 4);    /* Beginning. */
  460. X    check(rindex(one, '\0') == one+4, 5);    /* Finding NUL. */
  461. X    VOIDCAST strcpy(one, "ababa");
  462. X    check(rindex(one, 'b') == one+3, 6);    /* Finding last. */
  463. X    VOIDCAST strcpy(one, "");
  464. X    check(rindex(one, 'b') == NULL, 7);    /* Empty string. */
  465. X    check(rindex(one, '\0') == one, 8);    /* NUL in empty string. */
  466. X}
  467. X
  468. XVOIDTYPE
  469. Xsecond()
  470. X{
  471. X    /*
  472. X     * strpbrk - somewhat like strchr
  473. X     */
  474. X    it = "strpbrk";
  475. X    check(strpbrk("abcd", "z") == NULL, 1);    /* Not found. */
  476. X    VOIDCAST strcpy(one, "abcd");
  477. X    check(strpbrk(one, "c") == one+2, 2);    /* Basic test. */
  478. X    check(strpbrk(one, "d") == one+3, 3);    /* End of string. */
  479. X    check(strpbrk(one, "a") == one, 4);    /* Beginning. */
  480. X    check(strpbrk(one, "") == NULL, 5);    /* Empty search list. */
  481. X    check(strpbrk(one, "cb") == one+1, 6);    /* Multiple search. */
  482. X    VOIDCAST strcpy(one, "abcabdea");
  483. X    check(strpbrk(one, "b") == one+1, 7);    /* Finding first. */
  484. X    check(strpbrk(one, "cb") == one+1, 8);    /* With multiple search. */
  485. X    check(strpbrk(one, "db") == one+1, 9);    /* Another variant. */
  486. X    VOIDCAST strcpy(one, "");
  487. X    check(strpbrk(one, "bc") == NULL, 10);    /* Empty string. */
  488. X    check(strpbrk(one, "") == NULL, 11);    /* Both strings empty. */
  489. X
  490. X#if 0
  491. X    /*
  492. X     * strstr - somewhat like strchr
  493. X     */
  494. X    it = "strstr";
  495. X    check(strstr("abcd", "z") == NULL, 1);    /* Not found. */
  496. X    check(strstr("abcd", "abx") == NULL, 2);    /* Dead end. */
  497. X    VOIDCAST strcpy(one, "abcd");
  498. X    check(strstr(one, "c") == one+2, 3);    /* Basic test. */
  499. X    check(strstr(one, "bc") == one+1, 4);    /* Multichar. */
  500. X    check(strstr(one, "d") == one+3, 5);    /* End of string. */
  501. X    check(strstr(one, "cd") == one+2, 6);    /* Tail of string. */
  502. X    check(strstr(one, "abc") == one, 7);    /* Beginning. */
  503. X    check(strstr(one, "abcd") == one, 8);    /* Exact match. */
  504. X    check(strstr(one, "abcde") == NULL, 9);    /* Too long. */
  505. X    check(strstr(one, "de") == NULL, 10);    /* Past end. */
  506. X    check(strstr(one, "") == one+4, 11);    /* Finding empty. */
  507. X    VOIDCAST strcpy(one, "ababa");
  508. X    check(strstr(one, "ba") == one+1, 12);    /* Finding first. */
  509. X    VOIDCAST strcpy(one, "");
  510. X    check(strstr(one, "b") == NULL, 13);    /* Empty string. */
  511. X    check(strstr(one, "") == one, 14);    /* Empty in empty string. */
  512. X    VOIDCAST strcpy(one, "bcbca");
  513. X    check(strstr(one, "bca") == one+2, 15);    /* False start. */
  514. X    VOIDCAST strcpy(one, "bbbcabbca");
  515. X    check(strstr(one, "bbca") == one+1, 16);    /* With overlap. */
  516. X#endif
  517. X
  518. X    /*
  519. X     * strspn
  520. X     */
  521. X    it = "strspn";
  522. X    check(strspn("abcba", "abc") == 5, 1);    /* Whole string. */
  523. X    check(strspn("abcba", "ab") == 2, 2);    /* Partial. */
  524. X    check(strspn("abc", "qx") == 0, 3);    /* None. */
  525. X    check(strspn("", "ab") == 0, 4);    /* Null string. */
  526. X    check(strspn("abc", "") == 0, 5);    /* Null search list. */
  527. X
  528. X    /*
  529. X     * strcspn
  530. X     */
  531. X    it = "strcspn";
  532. X    check(strcspn("abcba", "qx") == 5, 1);    /* Whole string. */
  533. X    check(strcspn("abcba", "cx") == 2, 2);    /* Partial. */
  534. X    check(strcspn("abc", "abc") == 0, 3);    /* None. */
  535. X    check(strcspn("", "ab") == 0, 4);    /* Null string. */
  536. X    check(strcspn("abc", "") == 3, 5);    /* Null search list. */
  537. X
  538. X    /*
  539. X     * strtok - the hard one
  540. X     */
  541. X    it = "strtok";
  542. X    VOIDCAST strcpy(one, "first, second, third");
  543. X    equal(strtok(one, ", "), "first", 1);    /* Basic test. */
  544. X    equal(one, "first", 2);
  545. X    equal(strtok((char *)NULL, ", "), "second", 3);
  546. X    equal(strtok((char *)NULL, ", "), "third", 4);
  547. X    check(strtok((char *)NULL, ", ") == NULL, 5);
  548. X    VOIDCAST strcpy(one, ", first, ");
  549. X    equal(strtok(one, ", "), "first", 6);    /* Extra delims, 1 tok. */
  550. X    check(strtok((char *)NULL, ", ") == NULL, 7);
  551. X    VOIDCAST strcpy(one, "1a, 1b; 2a, 2b");
  552. X    equal(strtok(one, ", "), "1a", 8);    /* Changing delim lists. */
  553. X    equal(strtok((char *)NULL, "; "), "1b", 9);
  554. X    equal(strtok((char *)NULL, ", "), "2a", 10);
  555. X    VOIDCAST strcpy(two, "x-y");
  556. X    equal(strtok(two, "-"), "x", 11);    /* New string before done. */
  557. X    equal(strtok((char *)NULL, "-"), "y", 12);
  558. X    check(strtok((char *)NULL, "-") == NULL, 13);
  559. X    VOIDCAST strcpy(one, "a,b, c,, ,d");
  560. X    equal(strtok(one, ", "), "a", 14);    /* Different separators. */
  561. X    equal(strtok((char *)NULL, ", "), "b", 15);
  562. X    equal(strtok((char *)NULL, " ,"), "c", 16);    /* Permute list too. */
  563. X    equal(strtok((char *)NULL, " ,"), "d", 17);
  564. X    check(strtok((char *)NULL, ", ") == NULL, 18);
  565. X    check(strtok((char *)NULL, ", ") == NULL, 19);    /* Persistence. */
  566. X    VOIDCAST strcpy(one, ", ");
  567. X    check(strtok(one, ", ") == NULL, 20);    /* No tokens. */
  568. X    VOIDCAST strcpy(one, "");
  569. X    check(strtok(one, ", ") == NULL, 21);    /* Empty string. */
  570. X    VOIDCAST strcpy(one, "abc");
  571. X    equal(strtok(one, ", "), "abc", 22);    /* No delimiters. */
  572. X    check(strtok((char *)NULL, ", ") == NULL, 23);
  573. X    VOIDCAST strcpy(one, "abc");
  574. X    equal(strtok(one, ""), "abc", 24);    /* Empty delimiter list. */
  575. X    check(strtok((char *)NULL, "") == NULL, 25);
  576. X    VOIDCAST strcpy(one, "abcdefgh");
  577. X    VOIDCAST strcpy(one, "a,b,c");
  578. X    equal(strtok(one, ","), "a", 26);    /* Basics again... */
  579. X    equal(strtok((char *)NULL, ","), "b", 27);
  580. X    equal(strtok((char *)NULL, ","), "c", 28);
  581. X    check(strtok((char *)NULL, ",") == NULL, 29);
  582. X    equal(one+6, "gh", 30);            /* Stomped past end? */
  583. X    equal(one, "a", 31);            /* Stomped old tokens? */
  584. X    equal(one+2, "b", 32);
  585. X    equal(one+4, "c", 33);
  586. X
  587. X    /*
  588. X     * memcmp
  589. X     */
  590. X    it = "memcmp";
  591. X    check(memcmp("a", "a", 1) == 0, 1);    /* Identity. */
  592. X    check(memcmp("abc", "abc", 3) == 0, 2);    /* Multicharacter. */
  593. X    check(memcmp("abcd", "abce", 4) < 0, 3);    /* Honestly unequal. */
  594. X    check(memcmp("abce", "abcd", 4) > 0, 4);
  595. X    check(memcmp("alph", "beta", 4) < 0, 5);
  596. X    if (memcharsigned)            /* Sign-bit comparison. */
  597. X        check(memcmp("a\203", "a\003", 2) < 0, 6);
  598. X    else
  599. X        check(memcmp("a\203", "a\003", 2) > 0, 6);
  600. X    check(memcmp("abce", "abcd", 3) == 0, 7);    /* Count limited. */
  601. X    check(memcmp("abc", "def", 0) == 0, 8);    /* Zero count. */
  602. X
  603. X    /*
  604. X     * memchr
  605. X     */
  606. X    it = "memchr";
  607. X    check(memchr("abcd", 'z', 4) == NULL, 1);    /* Not found. */
  608. X    VOIDCAST strcpy(one, "abcd");
  609. X    check(memchr(one, 'c', 4) == one+2, 2);    /* Basic test. */
  610. X    check(memchr(one, 'd', 4) == one+3, 3);    /* End of string. */
  611. X    check(memchr(one, 'a', 4) == one, 4);    /* Beginning. */
  612. X    check(memchr(one, '\0', 5) == one+4, 5);    /* Finding NUL. */
  613. X    VOIDCAST strcpy(one, "ababa");
  614. X    check(memchr(one, 'b', 5) == one+1, 6);    /* Finding first. */
  615. X    check(memchr(one, 'b', 0) == NULL, 7);    /* Zero count. */
  616. X    check(memchr(one, 'a', 1) == one, 8);    /* Singleton case. */
  617. X    VOIDCAST strcpy(one, "a\203b");
  618. X    check(memchr(one, 0203, 3) == one+1, 9);    /* Unsignedness. */
  619. X
  620. X    /*
  621. X     * memcpy
  622. X     *
  623. X     * Note that X3J11 says memcpy must work regardless of overlap.
  624. X     * The SVID says it might fail.
  625. X     */
  626. X    it = "memcpy";
  627. X    check(memcpy(one, "abc", 4) == one, 1);    /* Returned value. */
  628. X    equal(one, "abc", 2);            /* Did the copy go right? */
  629. X
  630. X    VOIDCAST strcpy(one, "abcdefgh");
  631. X    VOIDCAST memcpy(one+1, "xyz", 2);
  632. X    equal(one, "axydefgh", 3);        /* Basic test. */
  633. X
  634. X    VOIDCAST strcpy(one, "abc");
  635. X    VOIDCAST memcpy(one, "xyz", 0);
  636. X    equal(one, "abc", 4);            /* Zero-length copy. */
  637. X
  638. X    VOIDCAST strcpy(one, "hi there");
  639. X    VOIDCAST strcpy(two, "foo");
  640. X    VOIDCAST memcpy(two, one, 9);
  641. X    equal(two, "hi there", 5);        /* Just paranoia. */
  642. X    equal(one, "hi there", 6);        /* Stomped on source? */
  643. X
  644. X    VOIDCAST strcpy(one, "abcdefgh");
  645. X    VOIDCAST memcpy(one+1, one, 9);
  646. X    equal(one, "aabcdefgh", 7);        /* Overlap, right-to-left. */
  647. X
  648. X    VOIDCAST strcpy(one, "abcdefgh");
  649. X    VOIDCAST memcpy(one+1, one+2, 7);
  650. X    equal(one, "acdefgh", 8);        /* Overlap, left-to-right. */
  651. X
  652. X    VOIDCAST strcpy(one, "abcdefgh");
  653. X    VOIDCAST memcpy(one, one, 9);
  654. X    equal(one, "abcdefgh", 9);        /* 100% overlap. */
  655. X
  656. X    /*
  657. X     * memccpy - first test like memcpy, then the search part
  658. X     *
  659. X     * The SVID, the only place where memccpy is mentioned, says
  660. X     * overlap might fail, so we don't try it.  Besides, it's hard
  661. X     * to see the rationale for a non-left-to-right memccpy.
  662. X     */
  663. X    it = "memccpy";
  664. X    check(memccpy(one, "abc", 'q', 4) == NULL, 1);    /* Returned value. */
  665. X    equal(one, "abc", 2);            /* Did the copy go right? */
  666. X
  667. X    VOIDCAST strcpy(one, "abcdefgh");
  668. X    VOIDCAST memccpy(one+1, "xyz", 'q', 2);
  669. X    equal(one, "axydefgh", 3);        /* Basic test. */
  670. X
  671. X    VOIDCAST strcpy(one, "abc");
  672. X    VOIDCAST memccpy(one, "xyz", 'q', 0);
  673. X    equal(one, "abc", 4);            /* Zero-length copy. */
  674. X
  675. X    VOIDCAST strcpy(one, "hi there");
  676. X    VOIDCAST strcpy(two, "foo");
  677. X    VOIDCAST memccpy(two, one, 'q', 9);
  678. X    equal(two, "hi there", 5);        /* Just paranoia. */
  679. X    equal(one, "hi there", 6);        /* Stomped on source? */
  680. X
  681. X    VOIDCAST strcpy(one, "abcdefgh");
  682. X    VOIDCAST strcpy(two, "horsefeathers");
  683. X    check(memccpy(two, one, 'f', 9) == two+6, 7);    /* Returned value. */
  684. X    equal(one, "abcdefgh", 8);        /* Source intact? */
  685. X    equal(two, "abcdefeathers", 9);        /* Copy correct? */
  686. X
  687. X    VOIDCAST strcpy(one, "abcd");
  688. X    VOIDCAST strcpy(two, "bumblebee");
  689. X    check(memccpy(two, one, 'a', 4) == two+1, 10);    /* First char. */
  690. X    equal(two, "aumblebee", 11);
  691. X    check(memccpy(two, one, 'd', 4) == two+4, 12);    /* Last char. */
  692. X    equal(two, "abcdlebee", 13);
  693. X    VOIDCAST strcpy(one, "xyz");
  694. X    check(memccpy(two, one, 'x', 1) == two+1, 14);    /* Singleton. */
  695. X    equal(two, "xbcdlebee", 15);
  696. X
  697. X    /*
  698. X     * memset
  699. X     */
  700. X    it = "memset";
  701. X    VOIDCAST strcpy(one, "abcdefgh");
  702. X    check(memset(one+1, 'x', 3) == one+1, 1);    /* Return value. */
  703. X    equal(one, "axxxefgh", 2);        /* Basic test. */
  704. X
  705. X    VOIDCAST memset(one+2, 'y', 0);
  706. X    equal(one, "axxxefgh", 3);        /* Zero-length set. */
  707. X
  708. X    VOIDCAST memset(one+5, 0, 1);
  709. X    equal(one, "axxxe", 4);            /* Zero fill. */
  710. X    equal(one+6, "gh", 5);            /* And the leftover. */
  711. X
  712. X    VOIDCAST memset(one+2, 010045, 1);
  713. X    equal(one, "ax\045xe", 6);        /* Unsigned char convert. */
  714. X
  715. X    /*
  716. X     * bcopy - much like memcpy
  717. X     *
  718. X     * Berklix manual is silent about overlap, so don't test it.
  719. X     */
  720. X    it = "bcopy";
  721. X    VOIDCAST bcopy("abc", one, 4);
  722. X    equal(one, "abc", 1);            /* Simple copy. */
  723. X
  724. X    VOIDCAST strcpy(one, "abcdefgh");
  725. X    VOIDCAST bcopy("xyz", one+1, 2);
  726. X    equal(one, "axydefgh", 2);        /* Basic test. */
  727. X
  728. X    VOIDCAST strcpy(one, "abc");
  729. X    VOIDCAST bcopy("xyz", one, 0);
  730. X    equal(one, "abc", 3);            /* Zero-length copy. */
  731. X
  732. X    VOIDCAST strcpy(one, "hi there");
  733. X    VOIDCAST strcpy(two, "foo");
  734. X    VOIDCAST bcopy(one, two, 9);
  735. X    equal(two, "hi there", 4);        /* Just paranoia. */
  736. X    equal(one, "hi there", 5);        /* Stomped on source? */
  737. X
  738. X    /*
  739. X     * bzero
  740. X     */
  741. X    it = "bzero";
  742. X    VOIDCAST strcpy(one, "abcdef");
  743. X    bzero(one+2, 2);
  744. X    equal(one, "ab", 1);            /* Basic test. */
  745. X    equal(one+3, "", 2);
  746. X    equal(one+4, "ef", 3);
  747. X
  748. X    VOIDCAST strcpy(one, "abcdef");
  749. X    bzero(one+2, 0);
  750. X    equal(one, "abcdef", 4);        /* Zero-length copy. */
  751. X
  752. X    /*
  753. X     * bcmp - somewhat like memcmp
  754. X     */
  755. X    it = "bcmp";
  756. X    check(bcmp("a", "a", 1) == 0, 1);    /* Identity. */
  757. X    check(bcmp("abc", "abc", 3) == 0, 2);    /* Multicharacter. */
  758. X    check(bcmp("abcd", "abce", 4) != 0, 3);    /* Honestly unequal. */
  759. X    check(bcmp("abce", "abcd", 4) != 0, 4);
  760. X    check(bcmp("alph", "beta", 4) != 0, 5);
  761. X    check(bcmp("abce", "abcd", 3) == 0, 6);    /* Count limited. */
  762. X    check(bcmp("abc", "def", 0) == 0, 8);    /* Zero count. */
  763. X
  764. X#ifdef ERR
  765. X    /*
  766. X     * strerror - VERY system-dependent
  767. X     */
  768. X    it = "strerror";
  769. X    f = open("/", 1);    /* Should always fail. */
  770. X    check(f < 0 && errno > 0 && errno < sys_nerr, 1);
  771. X    equal(strerror(errno), sys_errlist[errno], 2);
  772. X#ifdef UNIXERR
  773. X    equal(strerror(errno), "Is a directory", 3);
  774. X#endif
  775. X#ifdef BERKERR
  776. X    equal(strerror(errno), "Permission denied", 3);
  777. X#endif
  778. X#endif
  779. X}
  780. END_OF_FILE
  781. if test 24605 -ne `wc -c <'testmem.c'`; then
  782.     echo shar: \"'testmem.c'\" unpacked with wrong size!
  783. fi
  784. # end of 'testmem.c'
  785. fi
  786. echo shar: End of archive 10 \(of 10\).
  787. cp /dev/null ark10isdone
  788. MISSING=""
  789. for I in 1 2 3 4 5 6 7 8 9 10 ; do
  790.     if test ! -f ark${I}isdone ; then
  791.     MISSING="${MISSING} ${I}"
  792.     fi
  793. done
  794. if test "${MISSING}" = "" ; then
  795.     echo You have unpacked all 10 archives.
  796.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  797. else
  798.     echo You still need to unpack the following archives:
  799.     echo "        " ${MISSING}
  800. fi
  801. ##  End of shell archive.
  802. exit 0
  803. *** SENTINEL(tm) The ultimate Debugging Environment - email for more info ***
  804.  
  805. Conor P. Cahill              (703)430-9247            cpcahil@virtech.vti.com
  806. Virtual Technologies, Inc.  46030 Manekin Plaza          Dulles, VA 21066 
  807.  
  808. exit 0 # Just in case...
  809.