home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d029 / stringlib.lha / StringLib / tester.c < prev   
Encoding:
C/C++ Source or Header  |  1986-07-21  |  19.6 KB  |  642 lines

  1. /*
  2.  * Test program for string(3) routines.
  3.  * 
  4.  * Note that at least one Bell Labs implementation of the string
  5.  * routines flunks a couple of these tests -- the ones which test
  6.  * behavior on "negative" characters.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include "string.h"
  11. #include "config.h"
  12.  
  13. #define    STREQ(a, b)    (strcmp((a), (b)) == 0)
  14.  
  15. char *it = "<UNSET>";        /* Routine name for message routines. */
  16. int waserror = 0;        /* For exit status. */
  17.  
  18. char uctest[] = "\004\203";    /* For testing signedness of chars. */
  19. int charsigned;            /* Result. */
  20.  
  21. /*
  22.  - check - complain if condition is not true
  23.  */
  24. void
  25. check(thing, number)
  26. int thing;
  27. int number;            /* Test number for error message. */
  28. {
  29.     if (!thing) {
  30.         printf("%s flunked test %d\n", it, number);
  31.         waserror = 1;
  32.     }
  33. }
  34.  
  35. /*
  36.  - equal - complain if first two args don't strcmp as equal
  37.  */
  38. void
  39. equal(a, b, number)
  40. char *a;
  41. char *b;
  42. int number;            /* Test number for error message. */
  43. {
  44.     check(a != NULL && b != NULL && STREQ(a, b), number);
  45. }
  46.  
  47. char one[50];
  48. char two[50];
  49.  
  50. #ifdef UNIXERR
  51. #define ERR 1
  52. #endif
  53. #ifdef BERKERR
  54. #define ERR 1
  55. #endif
  56. #ifdef ERR
  57. int f;
  58. #ifdef unix
  59. extern char *sys_errlist[];
  60. extern int sys_nerr;
  61. #else
  62. char *sys_errlist[1] = {"dummy entry to keep compilers happy"};
  63. int sys_nerr = 1;
  64. #endif
  65. extern int errno;
  66. #endif
  67.  
  68. /* ARGSUSED */
  69. main(argc, argv)
  70. int argc;
  71. char *argv[];
  72. {
  73.     /*
  74.      * First, establish whether chars are signed.
  75.      */
  76.     if (uctest[0] < uctest[1])
  77.         charsigned = 0;
  78.     else
  79.         charsigned = 1;
  80.  
  81.     /*
  82.      * Then, do the rest of the work.  Split into two functions because
  83.      * some compilers get unhappy about a single immense function.
  84.      */
  85.     first();
  86.     second();
  87.  
  88.     exit((waserror) ? 1 : 0);
  89. }
  90.  
  91. first()
  92. {
  93.     /*
  94.      * Test strcmp first because we use it to test other things.
  95.      */
  96.     it = "strcmp";
  97.     check(strcmp("", "") == 0, 1);        /* Trivial case. */
  98.     check(strcmp("a", "a") == 0, 2);    /* Identity. */
  99.     check(strcmp("abc", "abc") == 0, 3);    /* Multicharacter. */
  100.     check(strcmp("abc", "abcd") < 0, 4);    /* Length mismatches. */
  101.     check(strcmp("abcd", "abc") > 0, 5);
  102.     check(strcmp("abcd", "abce") < 0, 6);    /* Honest miscompares. */
  103.     check(strcmp("abce", "abcd") > 0, 7);
  104.     check(strcmp("a\203", "a") > 0, 8);    /* Tricky if char signed. */
  105.     if (charsigned)                /* Sign-bit comparison. */
  106.         check(strcmp("a\203", "a\003") < 0, 9);
  107.     else
  108.         check(strcmp("a\203", "a\003") > 0, 9);
  109.  
  110.     /*
  111.      * Test strcpy next because we need it to set up other tests.
  112.      */
  113.     it = "strcpy";
  114.     check(strcpy(one, "abcd") == one, 1);    /* Returned value. */
  115.     equal(one, "abcd", 2);            /* Basic test. */
  116.  
  117.     (void) strcpy(one, "x");
  118.     equal(one, "x", 3);            /* Writeover. */
  119.     equal(one+2, "cd", 4);            /* Wrote too much? */
  120.  
  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.  
  126.     (void) strcpy(one, "");
  127.     equal(one, "", 7);            /* Boundary condition. */
  128.  
  129.     /*
  130.      * strcat
  131.      */
  132.     it = "strcat";
  133.     (void) strcpy(one, "ijk");
  134.     check(strcat(one, "lmn") == one, 1);    /* Returned value. */
  135.     equal(one, "ijklmn", 2);        /* Basic test. */
  136.  
  137.     (void) strcpy(one, "x");
  138.     (void) strcat(one, "yz");
  139.     equal(one, "xyz", 3);            /* Writeover. */
  140.     equal(one+4, "mn", 4);            /* Wrote too much? */
  141.  
  142.     (void) strcpy(one, "gh");
  143.     (void) strcpy(two, "ef");
  144.     (void) strcat(one, two);
  145.     equal(one, "ghef", 5);            /* Basic test encore. */
  146.     equal(two, "ef", 6);            /* Stomped on source? */
  147.  
  148.     (void) strcpy(one, "");
  149.     (void) strcat(one, "");
  150.     equal(one, "", 7);            /* Boundary conditions. */
  151.     (void) strcpy(one, "ab");
  152.     (void) strcat(one, "");
  153.     equal(one, "ab", 8);
  154.     (void) strcpy(one, "");
  155.     (void) strcat(one, "cd");
  156.     equal(one, "cd", 9);
  157.  
  158.     /*
  159.      * strncat - first test it as strcat, with big counts, then
  160.      * test the count mechanism.
  161.      */
  162.     it = "strncat";
  163.     (void) strcpy(one, "ijk");
  164.     check(strncat(one, "lmn", 99) == one, 1);    /* Returned value. */
  165.     equal(one, "ijklmn", 2);        /* Basic test. */
  166.  
  167.     (void) strcpy(one, "x");
  168.     (void) strncat(one, "yz", 99);
  169.     equal(one, "xyz", 3);            /* Writeover. */
  170.     equal(one+4, "mn", 4);            /* Wrote too much? */
  171.  
  172.     (void) strcpy(one, "gh");
  173.     (void) strcpy(two, "ef");
  174.     (void) strncat(one, two, 99);
  175.     equal(one, "ghef", 5);            /* Basic test encore. */
  176.     equal(two, "ef", 6);            /* Stomped on source? */
  177.  
  178.     (void) strcpy(one, "");
  179.     (void) strncat(one, "", 99);
  180.     equal(one, "", 7);            /* Boundary conditions. */
  181.     (void) strcpy(one, "ab");
  182.     (void) strncat(one, "", 99);
  183.     equal(one, "ab", 8);
  184.     (void) strcpy(one, "");
  185.     (void) strncat(one, "cd", 99);
  186.     equal(one, "cd", 9);
  187.  
  188.     (void) strcpy(one, "ab");
  189.     (void) strncat(one, "cdef", 2);
  190.     equal(one, "abcd", 10);            /* Count-limited. */
  191.  
  192.     (void) strncat(one, "gh", 0);
  193.     equal(one, "abcd", 11);            /* Zero count. */
  194.  
  195.     (void) strncat(one, "gh", 2);
  196.     equal(one, "abcdgh", 12);        /* Count and length equal. */
  197.  
  198.     /*
  199.      * strncmp - first test as strcmp with big counts, then test
  200.      * count code.
  201.      */
  202.     it = "strncmp";
  203.     check(strncmp("", "", 99) == 0, 1);    /* Trivial case. */
  204.     check(strncmp("a", "a", 99) == 0, 2);    /* Identity. */
  205.     check(strncmp("abc", "abc", 99) == 0, 3);    /* Multicharacter. */
  206.     check(strncmp("abc", "abcd", 99) < 0, 4);    /* Length unequal. */
  207.     check(strncmp("abcd", "abc", 99) > 0, 5);
  208.     check(strncmp("abcd", "abce", 99) < 0, 6);    /* Honestly unequal. */
  209.     check(strncmp("abce", "abcd", 99) > 0, 7);
  210.     check(strncmp("a\203", "a", 2) > 0, 8);    /* Tricky if '\203' < 0 */
  211.     if (charsigned)                /* Sign-bit comparison. */
  212.         check(strncmp("a\203", "a\003", 2) < 0, 9);
  213.     else
  214.         check(strncmp("a\203", "a\003", 2) > 0, 9);
  215.     check(strncmp("abce", "abcd", 3) == 0, 10);    /* Count limited. */
  216.     check(strncmp("abce", "abc", 3) == 0, 11);    /* Count == length. */
  217.     check(strncmp("abcd", "abce", 4) < 0, 12);    /* Nudging limit. */
  218.     check(strncmp("abc", "def", 0) == 0, 13);    /* Zero count. */
  219.  
  220.     /*
  221.      * strncpy - testing is a bit different because of odd semantics
  222.      */
  223.     it = "strncpy";
  224.     check(strncpy(one, "abc", 4) == one, 1);    /* Returned value. */
  225.     equal(one, "abc", 2);            /* Did the copy go right? */
  226.  
  227.     (void) strcpy(one, "abcdefgh");
  228.     (void) strncpy(one, "xyz", 2);
  229.     equal(one, "xycdefgh", 3);        /* Copy cut by count. */
  230.  
  231.     (void) strcpy(one, "abcdefgh");
  232.     (void) strncpy(one, "xyz", 3);        /* Copy cut just before NUL. */
  233.     equal(one, "xyzdefgh", 4);
  234.  
  235.     (void) strcpy(one, "abcdefgh");
  236.     (void) strncpy(one, "xyz", 4);        /* Copy just includes NUL. */
  237.     equal(one, "xyz", 5);
  238.     equal(one+4, "efgh", 6);        /* Wrote too much? */
  239.  
  240.     (void) strcpy(one, "abcdefgh");
  241.     (void) strncpy(one, "xyz", 5);        /* Copy includes padding. */
  242.     equal(one, "xyz", 7);
  243.     equal(one+4, "", 8);
  244.     equal(one+5, "fgh", 9);
  245.  
  246.     (void) strcpy(one, "abc");
  247.     (void) strncpy(one, "xyz", 0);        /* Zero-length copy. */
  248.     equal(one, "abc", 10);    
  249.  
  250.     (void) strncpy(one, "", 2);        /* Zero-length source. */
  251.     equal(one, "", 11);
  252.     equal(one+1, "", 12);    
  253.     equal(one+2, "c", 13);
  254.  
  255.     (void) strcpy(one, "hi there");
  256.     (void) strncpy(two, one, 9);
  257.     equal(two, "hi there", 14);        /* Just paranoia. */
  258.     equal(one, "hi there", 15);        /* Stomped on source? */
  259.  
  260.     /*
  261.      * strlen
  262.      */
  263.     it = "strlen";
  264.     check(strlen("") == 0, 1);        /* Empty. */
  265.     check(strlen("a") == 1, 2);        /* Single char. */
  266.     check(strlen("abcd") == 4, 3);        /* Multiple chars. */
  267.  
  268.     /*
  269.      * strchr
  270.      */
  271.     it = "strchr";
  272.     check(strchr("abcd", 'z') == NULL, 1);    /* Not found. */
  273.     (void) strcpy(one, "abcd");
  274.     check(strchr(one, 'c') == one+2, 2);    /* Basic test. */
  275.     check(strchr(one, 'd') == one+3, 3);    /* End of string. */
  276.     check(strchr(one, 'a') == one, 4);    /* Beginning. */
  277.     check(strchr(one, '\0') == one+4, 5);    /* Finding NUL. */
  278.     (void) strcpy(one, "ababa");
  279.     check(strchr(one, 'b') == one+1, 6);    /* Finding first. */
  280.     (void) strcpy(one, "");
  281.     check(strchr(one, 'b') == NULL, 7);    /* Empty string. */
  282.     check(strchr(one, '\0') == one, 8);    /* NUL in empty string. */
  283.  
  284.     /*
  285.      * index - just like strchr
  286.      */
  287.     it = "index";
  288.     check(index("abcd", 'z') == NULL, 1);    /* Not found. */
  289.     (void) strcpy(one, "abcd");
  290.     check(index(one, 'c') == one+2, 2);    /* Basic test. */
  291.     check(index(one, 'd') == one+3, 3);    /* End of string. */
  292.     check(index(one, 'a') == one, 4);    /* Beginning. */
  293.     check(index(one, '\0') == one+4, 5);    /* Finding NUL. */
  294.     (void) strcpy(one, "ababa");
  295.     check(index(one, 'b') == one+1, 6);    /* Finding first. */
  296.     (void) strcpy(one, "");
  297.     check(index(one, 'b') == NULL, 7);    /* Empty string. */
  298.     check(index(one, '\0') == one, 8);    /* NUL in empty string. */
  299.  
  300.     /*
  301.      * strrchr
  302.      */
  303.     it = "strrchr";
  304.     check(strrchr("abcd", 'z') == NULL, 1);    /* Not found. */
  305.     (void) strcpy(one, "abcd");
  306.     check(strrchr(one, 'c') == one+2, 2);    /* Basic test. */
  307.     check(strrchr(one, 'd') == one+3, 3);    /* End of string. */
  308.     check(strrchr(one, 'a') == one, 4);    /* Beginning. */
  309.     check(strrchr(one, '\0') == one+4, 5);    /* Finding NUL. */
  310.     (void) strcpy(one, "ababa");
  311.     check(strrchr(one, 'b') == one+3, 6);    /* Finding last. */
  312.     (void) strcpy(one, "");
  313.     check(strrchr(one, 'b') == NULL, 7);    /* Empty string. */
  314.     check(strrchr(one, '\0') == one, 8);    /* NUL in empty string. */
  315.  
  316.     /*
  317.      * rindex - just like strrchr
  318.      */
  319.     it = "rindex";
  320.     check(rindex("abcd", 'z') == NULL, 1);    /* Not found. */
  321.     (void) strcpy(one, "abcd");
  322.     check(rindex(one, 'c') == one+2, 2);    /* Basic test. */
  323.     check(rindex(one, 'd') == one+3, 3);    /* End of string. */
  324.     check(rindex(one, 'a') == one, 4);    /* Beginning. */
  325.     check(rindex(one, '\0') == one+4, 5);    /* Finding NUL. */
  326.     (void) strcpy(one, "ababa");
  327.     check(rindex(one, 'b') == one+3, 6);    /* Finding last. */
  328.     (void) strcpy(one, "");
  329.     check(rindex(one, 'b') == NULL, 7);    /* Empty string. */
  330.     check(rindex(one, '\0') == one, 8);    /* NUL in empty string. */
  331. }
  332.  
  333. second()
  334. {
  335.     /*
  336.      * strpbrk - somewhat like strchr
  337.      */
  338.     it = "strpbrk";
  339.     check(strpbrk("abcd", "z") == NULL, 1);    /* Not found. */
  340.     (void) strcpy(one, "abcd");
  341.     check(strpbrk(one, "c") == one+2, 2);    /* Basic test. */
  342.     check(strpbrk(one, "d") == one+3, 3);    /* End of string. */
  343.     check(strpbrk(one, "a") == one, 4);    /* Beginning. */
  344.     check(strpbrk(one, "") == NULL, 5);    /* Empty search list. */
  345.     check(strpbrk(one, "cb") == one+1, 6);    /* Multiple search. */
  346.     (void) strcpy(one, "abcabdea");
  347.     check(strpbrk(one, "b") == one+1, 7);    /* Finding first. */
  348.     check(strpbrk(one, "cb") == one+1, 8);    /* With multiple search. */
  349.     check(strpbrk(one, "db") == one+1, 9);    /* Another variant. */
  350.     (void) strcpy(one, "");
  351.     check(strpbrk(one, "bc") == NULL, 10);    /* Empty string. */
  352.     check(strpbrk(one, "") == NULL, 11);    /* Both strings empty. */
  353.  
  354.     /*
  355.      * strstr - somewhat like strchr
  356.      */
  357.     it = "strstr";
  358.     check(strstr("abcd", "z") == NULL, 1);    /* Not found. */
  359.     check(strstr("abcd", "abx") == NULL, 2);    /* Dead end. */
  360.     (void) strcpy(one, "abcd");
  361.     check(strstr(one, "c") == one+2, 3);    /* Basic test. */
  362.     check(strstr(one, "bc") == one+1, 4);    /* Multichar. */
  363.     check(strstr(one, "d") == one+3, 5);    /* End of string. */
  364.     check(strstr(one, "cd") == one+2, 6);    /* Tail of string. */
  365.     check(strstr(one, "abc") == one, 7);    /* Beginning. */
  366.     check(strstr(one, "abcd") == one, 8);    /* Exact match. */
  367.     check(strstr(one, "abcde") == NULL, 9);    /* Too long. */
  368.     check(strstr(one, "de") == NULL, 10);    /* Past end. */
  369.     check(strstr(one, "") == one+4, 11);    /* Finding empty. */
  370.     (void) strcpy(one, "ababa");
  371.     check(strstr(one, "ba") == one+1, 12);    /* Finding first. */
  372.     (void) strcpy(one, "");
  373.     check(strstr(one, "b") == NULL, 13);    /* Empty string. */
  374.     check(strstr(one, "") == one, 14);    /* Empty in empty string. */
  375.     (void) strcpy(one, "bcbca");
  376.     check(strstr(one, "bca") == one+2, 15);    /* False start. */
  377.     (void) strcpy(one, "bbbcabbca");
  378.     check(strstr(one, "bbca") == one+1, 16);    /* With overlap. */
  379.  
  380.     /*
  381.      * strspn
  382.      */
  383.     it = "strspn";
  384.     check(strspn("abcba", "abc") == 5, 1);    /* Whole string. */
  385.     check(strspn("abcba", "ab") == 2, 2);    /* Partial. */
  386.     check(strspn("abc", "qx") == 0, 3);    /* None. */
  387.     check(strspn("", "ab") == 0, 4);    /* Null string. */
  388.     check(strspn("abc", "") == 0, 5);    /* Null search list. */
  389.  
  390.     /*
  391.      * strcspn
  392.      */
  393.     it = "strcspn";
  394.     check(strcspn("abcba", "qx") == 5, 1);    /* Whole string. */
  395.     check(strcspn("abcba", "cx") == 2, 2);    /* Partial. */
  396.     check(strcspn("abc", "abc") == 0, 3);    /* None. */
  397.     check(strcspn("", "ab") == 0, 4);    /* Null string. */
  398.     check(strcspn("abc", "") == 3, 5);    /* Null search list. */
  399.  
  400.     /*
  401.      * strtok - the hard one
  402.      */
  403.     it = "strtok";
  404.     (void) strcpy(one, "first, second, third");
  405.     equal(strtok(one, ", "), "first", 1);    /* Basic test. */
  406.     equal(one, "first", 2);
  407.     equal(strtok((char *)NULL, ", "), "second", 3);
  408.     equal(strtok((char *)NULL, ", "), "third", 4);
  409.     check(strtok((char *)NULL, ", ") == NULL, 5);
  410.     (void) strcpy(one, ", first, ");
  411.     equal(strtok(one, ", "), "first", 6);    /* Extra delims, 1 tok. */
  412.     check(strtok((char *)NULL, ", ") == NULL, 7);
  413.     (void) strcpy(one, "1a, 1b; 2a, 2b");
  414.     equal(strtok(one, ", "), "1a", 8);    /* Changing delim lists. */
  415.     equal(strtok((char *)NULL, "; "), "1b", 9);
  416.     equal(strtok((char *)NULL, ", "), "2a", 10);
  417.     (void) strcpy(two, "x-y");
  418.     equal(strtok(two, "-"), "x", 11);    /* New string before done. */
  419.     equal(strtok((char *)NULL, "-"), "y", 12);
  420.     check(strtok((char *)NULL, "-") == NULL, 13);
  421.     (void) strcpy(one, "a,b, c,, ,d");
  422.     equal(strtok(one, ", "), "a", 14);    /* Different separators. */
  423.     equal(strtok((char *)NULL, ", "), "b", 15);
  424.     equal(strtok((char *)NULL, " ,"), "c", 16);    /* Permute list too. */
  425.     equal(strtok((char *)NULL, " ,"), "d", 17);
  426.     check(strtok((char *)NULL, ", ") == NULL, 18);
  427.     check(strtok((char *)NULL, ", ") == NULL, 19);    /* Persistence. */
  428.     (void) strcpy(one, ", ");
  429.     check(strtok(one, ", ") == NULL, 20);    /* No tokens. */
  430.     (void) strcpy(one, "");
  431.     check(strtok(one, ", ") == NULL, 21);    /* Empty string. */
  432.     (void) strcpy(one, "abc");
  433.     equal(strtok(one, ", "), "abc", 22);    /* No delimiters. */
  434.     check(strtok((char *)NULL, ", ") == NULL, 23);
  435.     (void) strcpy(one, "abc");
  436.     equal(strtok(one, ""), "abc", 24);    /* Empty delimiter list. */
  437.     check(strtok((char *)NULL, "") == NULL, 25);
  438.     (void) strcpy(one, "abcdefgh");
  439.     (void) strcpy(one, "a,b,c");
  440.     equal(strtok(one, ","), "a", 26);    /* Basics again... */
  441.     equal(strtok((char *)NULL, ","), "b", 27);
  442.     equal(strtok((char *)NULL, ","), "c", 28);
  443.     check(strtok((char *)NULL, ",") == NULL, 29);
  444.     equal(one+6, "gh", 30);            /* Stomped past end? */
  445.     equal(one, "a", 31);            /* Stomped old tokens? */
  446.     equal(one+2, "b", 32);
  447.     equal(one+4, "c", 33);
  448.  
  449.     /*
  450.      * memcmp
  451.      */
  452.     it = "memcmp";
  453.     check(memcmp("a", "a", 1) == 0, 1);    /* Identity. */
  454.     check(memcmp("abc", "abc", 3) == 0, 2);    /* Multicharacter. */
  455.     check(memcmp("abcd", "abce", 4) < 0, 3);    /* Honestly unequal. */
  456.     check(memcmp("abce", "abcd", 4) > 0, 4);
  457.     check(memcmp("alph", "beta", 4) < 0, 5);
  458.     if (charsigned)                /* Sign-bit comparison. */
  459.         check(memcmp("a\203", "a\003", 2) < 0, 6);
  460.     else
  461.         check(memcmp("a\203", "a\003", 2) > 0, 6);
  462.     check(memcmp("abce", "abcd", 3) == 0, 7);    /* Count limited. */
  463.     check(memcmp("abc", "def", 0) == 0, 8);    /* Zero count. */
  464.  
  465.     /*
  466.      * memchr
  467.      */
  468.     it = "memchr";
  469.     check(memchr("abcd", 'z', 4) == NULL, 1);    /* Not found. */
  470.     (void) strcpy(one, "abcd");
  471.     check(memchr(one, 'c', 4) == one+2, 2);    /* Basic test. */
  472.     check(memchr(one, 'd', 4) == one+3, 3);    /* End of string. */
  473.     check(memchr(one, 'a', 4) == one, 4);    /* Beginning. */
  474.     check(memchr(one, '\0', 5) == one+4, 5);    /* Finding NUL. */
  475.     (void) strcpy(one, "ababa");
  476.     check(memchr(one, 'b', 5) == one+1, 6);    /* Finding first. */
  477.     check(memchr(one, 'b', 0) == NULL, 7);    /* Zero count. */
  478.     check(memchr(one, 'a', 1) == one, 8);    /* Singleton case. */
  479.     (void) strcpy(one, "a\203b");
  480.     check(memchr(one, 0203, 3) == one+1, 9);    /* Unsignedness. */
  481.  
  482.     /*
  483.      * memcpy
  484.      *
  485.      * Note that X3J11 says memcpy must work regardless of overlap.
  486.      * The SVID says it might fail.
  487.      */
  488.     it = "memcpy";
  489.     check(memcpy(one, "abc", 4) == one, 1);    /* Returned value. */
  490.     equal(one, "abc", 2);            /* Did the copy go right? */
  491.  
  492.     (void) strcpy(one, "abcdefgh");
  493.     (void) memcpy(one+1, "xyz", 2);
  494.     equal(one, "axydefgh", 3);        /* Basic test. */
  495.  
  496.     (void) strcpy(one, "abc");
  497.     (void) memcpy(one, "xyz", 0);
  498.     equal(one, "abc", 4);            /* Zero-length copy. */
  499.  
  500.     (void) strcpy(one, "hi there");
  501.     (void) strcpy(two, "foo");
  502.     (void) memcpy(two, one, 9);
  503.     equal(two, "hi there", 5);        /* Just paranoia. */
  504.     equal(one, "hi there", 6);        /* Stomped on source? */
  505.  
  506.     (void) strcpy(one, "abcdefgh");
  507.     (void) memcpy(one+1, one, 9);
  508.     equal(one, "aabcdefgh", 7);        /* Overlap, right-to-left. */
  509.  
  510.     (void) strcpy(one, "abcdefgh");
  511.     (void) memcpy(one+1, one+2, 7);
  512.     equal(one, "acdefgh", 8);        /* Overlap, left-to-right. */
  513.  
  514.     (void) strcpy(one, "abcdefgh");
  515.     (void) memcpy(one, one, 9);
  516.     equal(one, "abcdefgh", 9);        /* 100% overlap. */
  517.  
  518.     /*
  519.      * memccpy - first test like memcpy, then the search part
  520.      *
  521.      * The SVID, the only place where memccpy is mentioned, says
  522.      * overlap might fail, so we don't try it.  Besides, it's hard
  523.      * to see the rationale for a non-left-to-right memccpy.
  524.      */
  525.     it = "memccpy";
  526.     check(memccpy(one, "abc", 'q', 4) == NULL, 1);    /* Returned value. */
  527.     equal(one, "abc", 2);            /* Did the copy go right? */
  528.  
  529.     (void) strcpy(one, "abcdefgh");
  530.     (void) memccpy(one+1, "xyz", 'q', 2);
  531.     equal(one, "axydefgh", 3);        /* Basic test. */
  532.  
  533.     (void) strcpy(one, "abc");
  534.     (void) memccpy(one, "xyz", 'q', 0);
  535.     equal(one, "abc", 4);            /* Zero-length copy. */
  536.  
  537.     (void) strcpy(one, "hi there");
  538.     (void) strcpy(two, "foo");
  539.     (void) memccpy(two, one, 'q', 9);
  540.     equal(two, "hi there", 5);        /* Just paranoia. */
  541.     equal(one, "hi there", 6);        /* Stomped on source? */
  542.  
  543.     (void) strcpy(one, "abcdefgh");
  544.     (void) strcpy(two, "horsefeathers");
  545.     check(memccpy(two, one, 'f', 9) == two+6, 7);    /* Returned value. */
  546.     equal(one, "abcdefgh", 8);        /* Source intact? */
  547.     equal(two, "abcdefeathers", 9);        /* Copy correct? */
  548.  
  549.     (void) strcpy(one, "abcd");
  550.     (void) strcpy(two, "bumblebee");
  551.     check(memccpy(two, one, 'a', 4) == two+1, 10);    /* First char. */
  552.     equal(two, "aumblebee", 11);
  553.     check(memccpy(two, one, 'd', 4) == two+4, 12);    /* Last char. */
  554.     equal(two, "abcdlebee", 13);
  555.     (void) strcpy(one, "xyz");
  556.     check(memccpy(two, one, 'x', 1) == two+1, 14);    /* Singleton. */
  557.     equal(two, "xbcdlebee", 15);
  558.  
  559.     /*
  560.      * memset
  561.      */
  562.     it = "memset";
  563.     (void) strcpy(one, "abcdefgh");
  564.     check(memset(one+1, 'x', 3) == one+1, 1);    /* Return value. */
  565.     equal(one, "axxxefgh", 2);        /* Basic test. */
  566.  
  567.     (void) memset(one+2, 'y', 0);
  568.     equal(one, "axxxefgh", 3);        /* Zero-length set. */
  569.  
  570.     (void) memset(one+5, 0, 1);
  571.     equal(one, "axxxe", 4);            /* Zero fill. */
  572.     equal(one+6, "gh", 5);            /* And the leftover. */
  573.  
  574.     (void) memset(one+2, 010045, 1);
  575.     equal(one, "ax\045xe", 6);        /* Unsigned char convert. */
  576.  
  577.     /*
  578.      * bcopy - much like memcpy
  579.      *
  580.      * Berklix manual is silent about overlap, so don't test it.
  581.      */
  582.     it = "bcopy";
  583.     (void) bcopy("abc", one, 4);
  584.     equal(one, "abc", 1);            /* Simple copy. */
  585.  
  586.     (void) strcpy(one, "abcdefgh");
  587.     (void) bcopy("xyz", one+1, 2);
  588.     equal(one, "axydefgh", 2);        /* Basic test. */
  589.  
  590.     (void) strcpy(one, "abc");
  591.     (void) bcopy("xyz", one, 0);
  592.     equal(one, "abc", 3);            /* Zero-length copy. */
  593.  
  594.     (void) strcpy(one, "hi there");
  595.     (void) strcpy(two, "foo");
  596.     (void) bcopy(one, two, 9);
  597.     equal(two, "hi there", 4);        /* Just paranoia. */
  598.     equal(one, "hi there", 5);        /* Stomped on source? */
  599.  
  600.     /*
  601.      * bzero
  602.      */
  603.     it = "bzero";
  604.     (void) strcpy(one, "abcdef");
  605.     bzero(one+2, 2);
  606.     equal(one, "ab", 1);            /* Basic test. */
  607.     equal(one+3, "", 2);
  608.     equal(one+4, "ef", 3);
  609.  
  610.     (void) strcpy(one, "abcdef");
  611.     bzero(one+2, 0);
  612.     equal(one, "abcdef", 4);        /* Zero-length copy. */
  613.  
  614.     /*
  615.      * bcmp - somewhat like memcmp
  616.      */
  617.     it = "bcmp";
  618.     check(bcmp("a", "a", 1) == 0, 1);    /* Identity. */
  619.     check(bcmp("abc", "abc", 3) == 0, 2);    /* Multicharacter. */
  620.     check(bcmp("abcd", "abce", 4) != 0, 3);    /* Honestly unequal. */
  621.     check(bcmp("abce", "abcd", 4) != 0, 4);
  622.     check(bcmp("alph", "beta", 4) != 0, 5);
  623.     check(bcmp("abce", "abcd", 3) == 0, 6);    /* Count limited. */
  624.     check(bcmp("abc", "def", 0) == 0, 8);    /* Zero count. */
  625.  
  626. #ifdef ERR
  627.     /*
  628.      * strerror - VERY system-dependent
  629.      */
  630.     it = "strerror";
  631.     f = open("/", 1);    /* Should always fail. */
  632.     check(f < 0 && errno > 0 && errno < sys_nerr, 1);
  633.     equal(strerror(errno), sys_errlist[errno], 2);
  634. #ifdef UNIXERR
  635.     equal(strerror(errno), "Is a directory", 3);
  636. #endif
  637. #ifdef BERKERR
  638.     equal(strerror(errno), "Permission denied", 3);
  639. #endif
  640. #endif
  641. }
  642.