home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sources / misc / 3906 < prev    next >
Encoding:
Text File  |  1992-09-04  |  27.0 KB  |  815 lines

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