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