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