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