1 |
1325 |
phoenix |
/* Tester for string functions.
|
2 |
|
|
Copyright (C) 1995-2000, 2001 Free Software Foundation, Inc.
|
3 |
|
|
This file is part of the GNU C Library.
|
4 |
|
|
|
5 |
|
|
The GNU C Library is free software; you can redistribute it and/or
|
6 |
|
|
modify it under the terms of the GNU Lesser General Public
|
7 |
|
|
License as published by the Free Software Foundation; either
|
8 |
|
|
version 2.1 of the License, or (at your option) any later version.
|
9 |
|
|
|
10 |
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 |
|
|
Lesser General Public License for more details.
|
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU Lesser General Public
|
16 |
|
|
License along with the GNU C Library; if not, write to the Free
|
17 |
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
18 |
|
|
02111-1307 USA. */
|
19 |
|
|
|
20 |
|
|
#ifndef _GNU_SOURCE
|
21 |
|
|
#define _GNU_SOURCE
|
22 |
|
|
#endif
|
23 |
|
|
|
24 |
|
|
/* Make sure we don't test the optimized inline functions if we want to
|
25 |
|
|
test the real implementation. */
|
26 |
|
|
#if !defined DO_STRING_INLINES
|
27 |
|
|
#undef __USE_STRING_INLINES
|
28 |
|
|
#endif
|
29 |
|
|
|
30 |
|
|
#include <errno.h>
|
31 |
|
|
#include <stdio.h>
|
32 |
|
|
#include <stdlib.h>
|
33 |
|
|
#include <string.h>
|
34 |
|
|
#include <strings.h>
|
35 |
|
|
#include <fcntl.h>
|
36 |
|
|
|
37 |
|
|
#define STREQ(a, b) (strcmp((a), (b)) == 0)
|
38 |
|
|
|
39 |
|
|
const char *it = "<UNSET>"; /* Routine name for message routines. */
|
40 |
|
|
size_t errors = 0;
|
41 |
|
|
|
42 |
|
|
/* Complain if condition is not true. */
|
43 |
|
|
static void
|
44 |
|
|
check (int thing, int number)
|
45 |
|
|
{
|
46 |
|
|
if (!thing)
|
47 |
|
|
{
|
48 |
|
|
printf("%s flunked test %d\n", it, number);
|
49 |
|
|
++errors;
|
50 |
|
|
}
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
/* Complain if first two args don't strcmp as equal. */
|
54 |
|
|
static void
|
55 |
|
|
equal (const char *a, const char *b, int number)
|
56 |
|
|
{
|
57 |
|
|
check(a != NULL && b != NULL && STREQ (a, b), number);
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
char one[50];
|
61 |
|
|
char two[50];
|
62 |
|
|
char *cp;
|
63 |
|
|
|
64 |
|
|
static void
|
65 |
|
|
test_strcmp (void)
|
66 |
|
|
{
|
67 |
|
|
it = "strcmp";
|
68 |
|
|
check (strcmp ("", "") == 0, 1); /* Trivial case. */
|
69 |
|
|
check (strcmp ("a", "a") == 0, 2); /* Identity. */
|
70 |
|
|
check (strcmp ("abc", "abc") == 0, 3); /* Multicharacter. */
|
71 |
|
|
check (strcmp ("abc", "abcd") < 0, 4); /* Length mismatches. */
|
72 |
|
|
check (strcmp ("abcd", "abc") > 0, 5);
|
73 |
|
|
check (strcmp ("abcd", "abce") < 0, 6); /* Honest miscompares. */
|
74 |
|
|
check (strcmp ("abce", "abcd") > 0, 7);
|
75 |
|
|
check (strcmp ("a\203", "a") > 0, 8); /* Tricky if char signed. */
|
76 |
|
|
check (strcmp ("a\203", "a\003") > 0, 9);
|
77 |
|
|
|
78 |
|
|
{
|
79 |
|
|
char buf1[0x40], buf2[0x40];
|
80 |
|
|
int i, j;
|
81 |
|
|
for (i=0; i < 0x10; i++)
|
82 |
|
|
for (j = 0; j < 0x10; j++)
|
83 |
|
|
{
|
84 |
|
|
int k;
|
85 |
|
|
for (k = 0; k < 0x3f; k++)
|
86 |
|
|
{
|
87 |
|
|
buf1[j] = '0' ^ (k & 4);
|
88 |
|
|
buf2[j] = '4' ^ (k & 4);
|
89 |
|
|
}
|
90 |
|
|
buf1[i] = buf1[0x3f] = 0;
|
91 |
|
|
buf2[j] = buf2[0x3f] = 0;
|
92 |
|
|
for (k = 0; k < 0xf; k++)
|
93 |
|
|
{
|
94 |
|
|
int cnum = 0x10+0x10*k+0x100*j+0x1000*i;
|
95 |
|
|
check (strcmp (buf1+i,buf2+j) == 0, cnum);
|
96 |
|
|
buf1[i+k] = 'A' + i + k;
|
97 |
|
|
buf1[i+k+1] = 0;
|
98 |
|
|
check (strcmp (buf1+i,buf2+j) > 0, cnum+1);
|
99 |
|
|
check (strcmp (buf2+j,buf1+i) < 0, cnum+2);
|
100 |
|
|
buf2[j+k] = 'B' + i + k;
|
101 |
|
|
buf2[j+k+1] = 0;
|
102 |
|
|
check (strcmp (buf1+i,buf2+j) < 0, cnum+3);
|
103 |
|
|
check (strcmp (buf2+j,buf1+i) > 0, cnum+4);
|
104 |
|
|
buf2[j+k] = 'A' + i + k;
|
105 |
|
|
buf1[i] = 'A' + i + 0x80;
|
106 |
|
|
check (strcmp (buf1+i,buf2+j) > 0, cnum+5);
|
107 |
|
|
check (strcmp (buf2+j,buf1+i) < 0, cnum+6);
|
108 |
|
|
buf1[i] = 'A' + i;
|
109 |
|
|
}
|
110 |
|
|
}
|
111 |
|
|
}
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
#define SIMPLE_COPY(fn, n, str, ntest) \
|
115 |
|
|
do { \
|
116 |
|
|
int __n; \
|
117 |
|
|
char *cp; \
|
118 |
|
|
for (__n = 0; __n < (int) sizeof (one); ++__n) \
|
119 |
|
|
one[__n] = 'Z'; \
|
120 |
|
|
fn (one, str); \
|
121 |
|
|
for (cp = one, __n = 0; __n < n; ++__n, ++cp) \
|
122 |
|
|
check (*cp == '0' + (n % 10), ntest); \
|
123 |
|
|
check (*cp == '\0', ntest); \
|
124 |
|
|
} while (0)
|
125 |
|
|
|
126 |
|
|
static void
|
127 |
|
|
test_strcpy (void)
|
128 |
|
|
{
|
129 |
|
|
int i;
|
130 |
|
|
it = "strcpy";
|
131 |
|
|
check (strcpy (one, "abcd") == one, 1); /* Returned value. */
|
132 |
|
|
equal (one, "abcd", 2); /* Basic test. */
|
133 |
|
|
|
134 |
|
|
(void) strcpy (one, "x");
|
135 |
|
|
equal (one, "x", 3); /* Writeover. */
|
136 |
|
|
equal (one+2, "cd", 4); /* Wrote too much? */
|
137 |
|
|
|
138 |
|
|
(void) strcpy (two, "hi there");
|
139 |
|
|
(void) strcpy (one, two);
|
140 |
|
|
equal (one, "hi there", 5); /* Basic test encore. */
|
141 |
|
|
equal (two, "hi there", 6); /* Stomped on source? */
|
142 |
|
|
|
143 |
|
|
(void) strcpy (one, "");
|
144 |
|
|
equal (one, "", 7); /* Boundary condition. */
|
145 |
|
|
|
146 |
|
|
for (i = 0; i < 16; i++)
|
147 |
|
|
{
|
148 |
|
|
(void) strcpy (one + i, "hi there"); /* Unaligned destination. */
|
149 |
|
|
equal (one + i, "hi there", 8 + (i * 2));
|
150 |
|
|
(void) strcpy (two, one + i); /* Unaligned source. */
|
151 |
|
|
equal (two, "hi there", 9 + (i * 2));
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
SIMPLE_COPY(strcpy, 0, "", 41);
|
155 |
|
|
SIMPLE_COPY(strcpy, 1, "1", 42);
|
156 |
|
|
SIMPLE_COPY(strcpy, 2, "22", 43);
|
157 |
|
|
SIMPLE_COPY(strcpy, 3, "333", 44);
|
158 |
|
|
SIMPLE_COPY(strcpy, 4, "4444", 45);
|
159 |
|
|
SIMPLE_COPY(strcpy, 5, "55555", 46);
|
160 |
|
|
SIMPLE_COPY(strcpy, 6, "666666", 47);
|
161 |
|
|
SIMPLE_COPY(strcpy, 7, "7777777", 48);
|
162 |
|
|
SIMPLE_COPY(strcpy, 8, "88888888", 49);
|
163 |
|
|
SIMPLE_COPY(strcpy, 9, "999999999", 50);
|
164 |
|
|
SIMPLE_COPY(strcpy, 10, "0000000000", 51);
|
165 |
|
|
SIMPLE_COPY(strcpy, 11, "11111111111", 52);
|
166 |
|
|
SIMPLE_COPY(strcpy, 12, "222222222222", 53);
|
167 |
|
|
SIMPLE_COPY(strcpy, 13, "3333333333333", 54);
|
168 |
|
|
SIMPLE_COPY(strcpy, 14, "44444444444444", 55);
|
169 |
|
|
SIMPLE_COPY(strcpy, 15, "555555555555555", 56);
|
170 |
|
|
SIMPLE_COPY(strcpy, 16, "6666666666666666", 57);
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
static void
|
174 |
|
|
test_stpcpy (void)
|
175 |
|
|
{
|
176 |
|
|
it = "stpcpy";
|
177 |
|
|
check ((stpcpy (one, "a") - one) == 1, 1);
|
178 |
|
|
equal (one, "a", 2);
|
179 |
|
|
|
180 |
|
|
check ((stpcpy (one, "ab") - one) == 2, 3);
|
181 |
|
|
equal (one, "ab", 4);
|
182 |
|
|
|
183 |
|
|
check ((stpcpy (one, "abc") - one) == 3, 5);
|
184 |
|
|
equal (one, "abc", 6);
|
185 |
|
|
|
186 |
|
|
check ((stpcpy (one, "abcd") - one) == 4, 7);
|
187 |
|
|
equal (one, "abcd", 8);
|
188 |
|
|
|
189 |
|
|
check ((stpcpy (one, "abcde") - one) == 5, 9);
|
190 |
|
|
equal (one, "abcde", 10);
|
191 |
|
|
|
192 |
|
|
check ((stpcpy (one, "abcdef") - one) == 6, 11);
|
193 |
|
|
equal (one, "abcdef", 12);
|
194 |
|
|
|
195 |
|
|
check ((stpcpy (one, "abcdefg") - one) == 7, 13);
|
196 |
|
|
equal (one, "abcdefg", 14);
|
197 |
|
|
|
198 |
|
|
check ((stpcpy (one, "abcdefgh") - one) == 8, 15);
|
199 |
|
|
equal (one, "abcdefgh", 16);
|
200 |
|
|
|
201 |
|
|
check ((stpcpy (one, "abcdefghi") - one) == 9, 17);
|
202 |
|
|
equal (one, "abcdefghi", 18);
|
203 |
|
|
|
204 |
|
|
check ((stpcpy (one, "x") - one) == 1, 19);
|
205 |
|
|
equal (one, "x", 20); /* Writeover. */
|
206 |
|
|
equal (one+2, "cdefghi", 21); /* Wrote too much? */
|
207 |
|
|
|
208 |
|
|
check ((stpcpy (one, "xx") - one) == 2, 22);
|
209 |
|
|
equal (one, "xx", 23); /* Writeover. */
|
210 |
|
|
equal (one+3, "defghi", 24); /* Wrote too much? */
|
211 |
|
|
|
212 |
|
|
check ((stpcpy (one, "xxx") - one) == 3, 25);
|
213 |
|
|
equal (one, "xxx", 26); /* Writeover. */
|
214 |
|
|
equal (one+4, "efghi", 27); /* Wrote too much? */
|
215 |
|
|
|
216 |
|
|
check ((stpcpy (one, "xxxx") - one) == 4, 28);
|
217 |
|
|
equal (one, "xxxx", 29); /* Writeover. */
|
218 |
|
|
equal (one+5, "fghi", 30); /* Wrote too much? */
|
219 |
|
|
|
220 |
|
|
check ((stpcpy (one, "xxxxx") - one) == 5, 31);
|
221 |
|
|
equal (one, "xxxxx", 32); /* Writeover. */
|
222 |
|
|
equal (one+6, "ghi", 33); /* Wrote too much? */
|
223 |
|
|
|
224 |
|
|
check ((stpcpy (one, "xxxxxx") - one) == 6, 34);
|
225 |
|
|
equal (one, "xxxxxx", 35); /* Writeover. */
|
226 |
|
|
equal (one+7, "hi", 36); /* Wrote too much? */
|
227 |
|
|
|
228 |
|
|
check ((stpcpy (one, "xxxxxxx") - one) == 7, 37);
|
229 |
|
|
equal (one, "xxxxxxx", 38); /* Writeover. */
|
230 |
|
|
equal (one+8, "i", 39); /* Wrote too much? */
|
231 |
|
|
|
232 |
|
|
check ((stpcpy (stpcpy (stpcpy (one, "a"), "b"), "c") - one) == 3, 40);
|
233 |
|
|
equal (one, "abc", 41);
|
234 |
|
|
equal (one + 4, "xxx", 42);
|
235 |
|
|
|
236 |
|
|
SIMPLE_COPY(stpcpy, 0, "", 43);
|
237 |
|
|
SIMPLE_COPY(stpcpy, 1, "1", 44);
|
238 |
|
|
SIMPLE_COPY(stpcpy, 2, "22", 45);
|
239 |
|
|
SIMPLE_COPY(stpcpy, 3, "333", 46);
|
240 |
|
|
SIMPLE_COPY(stpcpy, 4, "4444", 47);
|
241 |
|
|
SIMPLE_COPY(stpcpy, 5, "55555", 48);
|
242 |
|
|
SIMPLE_COPY(stpcpy, 6, "666666", 49);
|
243 |
|
|
SIMPLE_COPY(stpcpy, 7, "7777777", 50);
|
244 |
|
|
SIMPLE_COPY(stpcpy, 8, "88888888", 51);
|
245 |
|
|
SIMPLE_COPY(stpcpy, 9, "999999999", 52);
|
246 |
|
|
SIMPLE_COPY(stpcpy, 10, "0000000000", 53);
|
247 |
|
|
SIMPLE_COPY(stpcpy, 11, "11111111111", 54);
|
248 |
|
|
SIMPLE_COPY(stpcpy, 12, "222222222222", 55);
|
249 |
|
|
SIMPLE_COPY(stpcpy, 13, "3333333333333", 56);
|
250 |
|
|
SIMPLE_COPY(stpcpy, 14, "44444444444444", 57);
|
251 |
|
|
SIMPLE_COPY(stpcpy, 15, "555555555555555", 58);
|
252 |
|
|
SIMPLE_COPY(stpcpy, 16, "6666666666666666", 59);
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
static void
|
256 |
|
|
test_stpncpy (void)
|
257 |
|
|
{
|
258 |
|
|
it = "stpncpy";
|
259 |
|
|
memset (one, 'x', sizeof (one));
|
260 |
|
|
check (stpncpy (one, "abc", 2) == one + 2, 1);
|
261 |
|
|
check (stpncpy (one, "abc", 3) == one + 3, 2);
|
262 |
|
|
check (stpncpy (one, "abc", 4) == one + 3, 3);
|
263 |
|
|
check (one[3] == '\0' && one[4] == 'x', 4);
|
264 |
|
|
check (stpncpy (one, "abcd", 5) == one + 4, 5);
|
265 |
|
|
check (one[4] == '\0' && one[5] == 'x', 6);
|
266 |
|
|
check (stpncpy (one, "abcd", 6) == one + 4, 7);
|
267 |
|
|
check (one[4] == '\0' && one[5] == '\0' && one[6] == 'x', 8);
|
268 |
|
|
}
|
269 |
|
|
|
270 |
|
|
static void
|
271 |
|
|
test_strcat (void)
|
272 |
|
|
{
|
273 |
|
|
it = "strcat";
|
274 |
|
|
(void) strcpy (one, "ijk");
|
275 |
|
|
check (strcat (one, "lmn") == one, 1); /* Returned value. */
|
276 |
|
|
equal (one, "ijklmn", 2); /* Basic test. */
|
277 |
|
|
|
278 |
|
|
(void) strcpy (one, "x");
|
279 |
|
|
(void) strcat (one, "yz");
|
280 |
|
|
equal (one, "xyz", 3); /* Writeover. */
|
281 |
|
|
equal (one+4, "mn", 4); /* Wrote too much? */
|
282 |
|
|
|
283 |
|
|
(void) strcpy (one, "gh");
|
284 |
|
|
(void) strcpy (two, "ef");
|
285 |
|
|
(void) strcat (one, two);
|
286 |
|
|
equal (one, "ghef", 5); /* Basic test encore. */
|
287 |
|
|
equal (two, "ef", 6); /* Stomped on source? */
|
288 |
|
|
|
289 |
|
|
(void) strcpy (one, "");
|
290 |
|
|
(void) strcat (one, "");
|
291 |
|
|
equal (one, "", 7); /* Boundary conditions. */
|
292 |
|
|
(void) strcpy (one, "ab");
|
293 |
|
|
(void) strcat (one, "");
|
294 |
|
|
equal (one, "ab", 8);
|
295 |
|
|
(void) strcpy (one, "");
|
296 |
|
|
(void) strcat (one, "cd");
|
297 |
|
|
equal (one, "cd", 9);
|
298 |
|
|
}
|
299 |
|
|
|
300 |
|
|
static void
|
301 |
|
|
test_strncat (void)
|
302 |
|
|
{
|
303 |
|
|
/* First test it as strcat, with big counts, then test the count
|
304 |
|
|
mechanism. */
|
305 |
|
|
it = "strncat";
|
306 |
|
|
(void) strcpy (one, "ijk");
|
307 |
|
|
check (strncat (one, "lmn", 99) == one, 1); /* Returned value. */
|
308 |
|
|
equal (one, "ijklmn", 2); /* Basic test. */
|
309 |
|
|
|
310 |
|
|
(void) strcpy (one, "x");
|
311 |
|
|
(void) strncat (one, "yz", 99);
|
312 |
|
|
equal (one, "xyz", 3); /* Writeover. */
|
313 |
|
|
equal (one+4, "mn", 4); /* Wrote too much? */
|
314 |
|
|
|
315 |
|
|
(void) strcpy (one, "gh");
|
316 |
|
|
(void) strcpy (two, "ef");
|
317 |
|
|
(void) strncat (one, two, 99);
|
318 |
|
|
equal (one, "ghef", 5); /* Basic test encore. */
|
319 |
|
|
equal (two, "ef", 6); /* Stomped on source? */
|
320 |
|
|
|
321 |
|
|
(void) strcpy (one, "");
|
322 |
|
|
(void) strncat (one, "", 99);
|
323 |
|
|
equal (one, "", 7); /* Boundary conditions. */
|
324 |
|
|
(void) strcpy (one, "ab");
|
325 |
|
|
(void) strncat (one, "", 99);
|
326 |
|
|
equal (one, "ab", 8);
|
327 |
|
|
(void) strcpy (one, "");
|
328 |
|
|
(void) strncat (one, "cd", 99);
|
329 |
|
|
equal (one, "cd", 9);
|
330 |
|
|
|
331 |
|
|
(void) strcpy (one, "ab");
|
332 |
|
|
(void) strncat (one, "cdef", 2);
|
333 |
|
|
equal (one, "abcd", 10); /* Count-limited. */
|
334 |
|
|
|
335 |
|
|
(void) strncat (one, "gh", 0);
|
336 |
|
|
equal (one, "abcd", 11); /* Zero count. */
|
337 |
|
|
|
338 |
|
|
(void) strncat (one, "gh", 2);
|
339 |
|
|
equal (one, "abcdgh", 12); /* Count and length equal. */
|
340 |
|
|
}
|
341 |
|
|
|
342 |
|
|
static void
|
343 |
|
|
test_strncmp (void)
|
344 |
|
|
{
|
345 |
|
|
/* First test as strcmp with big counts, then test count code. */
|
346 |
|
|
it = "strncmp";
|
347 |
|
|
check (strncmp ("", "", 99) == 0, 1); /* Trivial case. */
|
348 |
|
|
check (strncmp ("a", "a", 99) == 0, 2); /* Identity. */
|
349 |
|
|
check (strncmp ("abc", "abc", 99) == 0, 3); /* Multicharacter. */
|
350 |
|
|
check (strncmp ("abc", "abcd", 99) < 0, 4); /* Length unequal. */
|
351 |
|
|
check (strncmp ("abcd", "abc", 99) > 0, 5);
|
352 |
|
|
check (strncmp ("abcd", "abce", 99) < 0, 6); /* Honestly unequal. */
|
353 |
|
|
check (strncmp ("abce", "abcd", 99) > 0, 7);
|
354 |
|
|
check (strncmp ("a\203", "a", 2) > 0, 8); /* Tricky if '\203' < 0 */
|
355 |
|
|
check (strncmp ("a\203", "a\003", 2) > 0, 9);
|
356 |
|
|
check (strncmp ("abce", "abcd", 3) == 0, 10); /* Count limited. */
|
357 |
|
|
check (strncmp ("abce", "abc", 3) == 0, 11); /* Count == length. */
|
358 |
|
|
check (strncmp ("abcd", "abce", 4) < 0, 12); /* Nudging limit. */
|
359 |
|
|
check (strncmp ("abc", "def", 0) == 0, 13); /* Zero count. */
|
360 |
|
|
}
|
361 |
|
|
|
362 |
|
|
static void
|
363 |
|
|
test_strncpy (void)
|
364 |
|
|
{
|
365 |
|
|
/* Testing is a bit different because of odd semantics. */
|
366 |
|
|
it = "strncpy";
|
367 |
|
|
check (strncpy (one, "abc", 4) == one, 1); /* Returned value. */
|
368 |
|
|
equal (one, "abc", 2); /* Did the copy go right? */
|
369 |
|
|
|
370 |
|
|
(void) strcpy (one, "abcdefgh");
|
371 |
|
|
(void) strncpy (one, "xyz", 2);
|
372 |
|
|
equal (one, "xycdefgh", 3); /* Copy cut by count. */
|
373 |
|
|
|
374 |
|
|
(void) strcpy (one, "abcdefgh");
|
375 |
|
|
(void) strncpy (one, "xyz", 3); /* Copy cut just before NUL. */
|
376 |
|
|
equal (one, "xyzdefgh", 4);
|
377 |
|
|
|
378 |
|
|
(void) strcpy (one, "abcdefgh");
|
379 |
|
|
(void) strncpy (one, "xyz", 4); /* Copy just includes NUL. */
|
380 |
|
|
equal (one, "xyz", 5);
|
381 |
|
|
equal (one+4, "efgh", 6); /* Wrote too much? */
|
382 |
|
|
|
383 |
|
|
(void) strcpy (one, "abcdefgh");
|
384 |
|
|
(void) strncpy (one, "xyz", 5); /* Copy includes padding. */
|
385 |
|
|
equal (one, "xyz", 7);
|
386 |
|
|
equal (one+4, "", 8);
|
387 |
|
|
equal (one+5, "fgh", 9);
|
388 |
|
|
|
389 |
|
|
(void) strcpy (one, "abc");
|
390 |
|
|
(void) strncpy (one, "xyz", 0); /* Zero-length copy. */
|
391 |
|
|
equal (one, "abc", 10);
|
392 |
|
|
|
393 |
|
|
(void) strncpy (one, "", 2); /* Zero-length source. */
|
394 |
|
|
equal (one, "", 11);
|
395 |
|
|
equal (one+1, "", 12);
|
396 |
|
|
equal (one+2, "c", 13);
|
397 |
|
|
|
398 |
|
|
(void) strcpy (one, "hi there");
|
399 |
|
|
(void) strncpy (two, one, 9);
|
400 |
|
|
equal (two, "hi there", 14); /* Just paranoia. */
|
401 |
|
|
equal (one, "hi there", 15); /* Stomped on source? */
|
402 |
|
|
}
|
403 |
|
|
|
404 |
|
|
static void
|
405 |
|
|
test_strlen (void)
|
406 |
|
|
{
|
407 |
|
|
it = "strlen";
|
408 |
|
|
check (strlen ("") == 0, 1); /* Empty. */
|
409 |
|
|
check (strlen ("a") == 1, 2); /* Single char. */
|
410 |
|
|
check (strlen ("abcd") == 4, 3); /* Multiple chars. */
|
411 |
|
|
{
|
412 |
|
|
char buf[4096];
|
413 |
|
|
int i;
|
414 |
|
|
char *p;
|
415 |
|
|
for (i=0; i < 0x100; i++)
|
416 |
|
|
{
|
417 |
|
|
p = (char *) ((unsigned long int)(buf + 0xff) & ~0xff) + i;
|
418 |
|
|
strcpy (p, "OK");
|
419 |
|
|
strcpy (p+3, "BAD/WRONG");
|
420 |
|
|
check (strlen (p) == 2, 4+i);
|
421 |
|
|
}
|
422 |
|
|
}
|
423 |
|
|
}
|
424 |
|
|
|
425 |
|
|
static void
|
426 |
|
|
test_strchr (void)
|
427 |
|
|
{
|
428 |
|
|
it = "strchr";
|
429 |
|
|
check (strchr ("abcd", 'z') == NULL, 1); /* Not found. */
|
430 |
|
|
(void) strcpy (one, "abcd");
|
431 |
|
|
check (strchr (one, 'c') == one+2, 2); /* Basic test. */
|
432 |
|
|
check (strchr (one, 'd') == one+3, 3); /* End of string. */
|
433 |
|
|
check (strchr (one, 'a') == one, 4); /* Beginning. */
|
434 |
|
|
check (strchr (one, '\0') == one+4, 5); /* Finding NUL. */
|
435 |
|
|
(void) strcpy (one, "ababa");
|
436 |
|
|
check (strchr (one, 'b') == one+1, 6); /* Finding first. */
|
437 |
|
|
(void) strcpy (one, "");
|
438 |
|
|
check (strchr (one, 'b') == NULL, 7); /* Empty string. */
|
439 |
|
|
check (strchr (one, '\0') == one, 8); /* NUL in empty string. */
|
440 |
|
|
{
|
441 |
|
|
char buf[4096];
|
442 |
|
|
int i;
|
443 |
|
|
char *p;
|
444 |
|
|
for (i=0; i < 0x100; i++)
|
445 |
|
|
{
|
446 |
|
|
p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
|
447 |
|
|
strcpy (p, "OK");
|
448 |
|
|
strcpy (p+3, "BAD/WRONG");
|
449 |
|
|
check (strchr (p, '/') == NULL, 9+i);
|
450 |
|
|
}
|
451 |
|
|
}
|
452 |
|
|
}
|
453 |
|
|
|
454 |
|
|
static void
|
455 |
|
|
test_strchrnul (void)
|
456 |
|
|
{
|
457 |
|
|
const char *os;
|
458 |
|
|
it = "strchrnul";
|
459 |
|
|
cp = strchrnul ((os = "abcd"), 'z');
|
460 |
|
|
check (*cp == '\0', 1); /* Not found. */
|
461 |
|
|
check (cp == os + 4, 2);
|
462 |
|
|
(void) strcpy (one, "abcd");
|
463 |
|
|
check (strchrnul (one, 'c') == one+2, 3); /* Basic test. */
|
464 |
|
|
check (strchrnul (one, 'd') == one+3, 4); /* End of string. */
|
465 |
|
|
check (strchrnul (one, 'a') == one, 5); /* Beginning. */
|
466 |
|
|
check (strchrnul (one, '\0') == one+4, 6); /* Finding NUL. */
|
467 |
|
|
(void) strcpy (one, "ababa");
|
468 |
|
|
check (strchrnul (one, 'b') == one+1, 7); /* Finding first. */
|
469 |
|
|
(void) strcpy (one, "");
|
470 |
|
|
check (strchrnul (one, 'b') == one, 8); /* Empty string. */
|
471 |
|
|
check (strchrnul (one, '\0') == one, 9); /* NUL in empty string. */
|
472 |
|
|
{
|
473 |
|
|
char buf[4096];
|
474 |
|
|
int i;
|
475 |
|
|
char *p;
|
476 |
|
|
for (i=0; i < 0x100; i++)
|
477 |
|
|
{
|
478 |
|
|
p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
|
479 |
|
|
strcpy (p, "OK");
|
480 |
|
|
strcpy (p+3, "BAD/WRONG");
|
481 |
|
|
cp = strchrnul (p, '/');
|
482 |
|
|
check (*cp == '\0', 9+2*i);
|
483 |
|
|
check (cp == p+2, 10+2*i);
|
484 |
|
|
}
|
485 |
|
|
}
|
486 |
|
|
}
|
487 |
|
|
|
488 |
|
|
static void
|
489 |
|
|
test_rawmemchr (void)
|
490 |
|
|
{
|
491 |
|
|
it = "rawmemchr";
|
492 |
|
|
(void) strcpy (one, "abcd");
|
493 |
|
|
check (rawmemchr (one, 'c') == one+2, 1); /* Basic test. */
|
494 |
|
|
check (rawmemchr (one, 'd') == one+3, 2); /* End of string. */
|
495 |
|
|
check (rawmemchr (one, 'a') == one, 3); /* Beginning. */
|
496 |
|
|
check (rawmemchr (one, '\0') == one+4, 4); /* Finding NUL. */
|
497 |
|
|
(void) strcpy (one, "ababa");
|
498 |
|
|
check (rawmemchr (one, 'b') == one+1, 5); /* Finding first. */
|
499 |
|
|
(void) strcpy (one, "");
|
500 |
|
|
check (rawmemchr (one, '\0') == one, 6); /* NUL in empty string. */
|
501 |
|
|
{
|
502 |
|
|
char buf[4096];
|
503 |
|
|
int i;
|
504 |
|
|
char *p;
|
505 |
|
|
for (i=0; i < 0x100; i++)
|
506 |
|
|
{
|
507 |
|
|
p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
|
508 |
|
|
strcpy (p, "OK");
|
509 |
|
|
strcpy (p+3, "BAD/WRONG");
|
510 |
|
|
check (rawmemchr (p, 'R') == p+8, 6+i);
|
511 |
|
|
}
|
512 |
|
|
}
|
513 |
|
|
}
|
514 |
|
|
|
515 |
|
|
static void
|
516 |
|
|
test_index (void)
|
517 |
|
|
{
|
518 |
|
|
it = "index";
|
519 |
|
|
check (index ("abcd", 'z') == NULL, 1); /* Not found. */
|
520 |
|
|
(void) strcpy (one, "abcd");
|
521 |
|
|
check (index (one, 'c') == one+2, 2); /* Basic test. */
|
522 |
|
|
check (index (one, 'd') == one+3, 3); /* End of string. */
|
523 |
|
|
check (index (one, 'a') == one, 4); /* Beginning. */
|
524 |
|
|
check (index (one, '\0') == one+4, 5); /* Finding NUL. */
|
525 |
|
|
(void) strcpy (one, "ababa");
|
526 |
|
|
check (index (one, 'b') == one+1, 6); /* Finding first. */
|
527 |
|
|
(void) strcpy (one, "");
|
528 |
|
|
check (index (one, 'b') == NULL, 7); /* Empty string. */
|
529 |
|
|
check (index (one, '\0') == one, 8); /* NUL in empty string. */
|
530 |
|
|
}
|
531 |
|
|
|
532 |
|
|
static void
|
533 |
|
|
test_strrchr (void)
|
534 |
|
|
{
|
535 |
|
|
it = "strrchr";
|
536 |
|
|
check (strrchr ("abcd", 'z') == NULL, 1); /* Not found. */
|
537 |
|
|
(void) strcpy (one, "abcd");
|
538 |
|
|
check (strrchr (one, 'c') == one+2, 2); /* Basic test. */
|
539 |
|
|
check (strrchr (one, 'd') == one+3, 3); /* End of string. */
|
540 |
|
|
check (strrchr (one, 'a') == one, 4); /* Beginning. */
|
541 |
|
|
check (strrchr (one, '\0') == one+4, 5); /* Finding NUL. */
|
542 |
|
|
(void) strcpy (one, "ababa");
|
543 |
|
|
check (strrchr (one, 'b') == one+3, 6); /* Finding last. */
|
544 |
|
|
(void) strcpy (one, "");
|
545 |
|
|
check (strrchr (one, 'b') == NULL, 7); /* Empty string. */
|
546 |
|
|
check (strrchr (one, '\0') == one, 8); /* NUL in empty string. */
|
547 |
|
|
{
|
548 |
|
|
char buf[4096];
|
549 |
|
|
int i;
|
550 |
|
|
char *p;
|
551 |
|
|
for (i=0; i < 0x100; i++)
|
552 |
|
|
{
|
553 |
|
|
p = (char *) ((unsigned long int) (buf + 0xff) & ~0xff) + i;
|
554 |
|
|
strcpy (p, "OK");
|
555 |
|
|
strcpy (p+3, "BAD/WRONG");
|
556 |
|
|
check (strrchr (p, '/') == NULL, 9+i);
|
557 |
|
|
}
|
558 |
|
|
}
|
559 |
|
|
}
|
560 |
|
|
|
561 |
|
|
static void
|
562 |
|
|
test_memrchr (void)
|
563 |
|
|
{
|
564 |
|
|
size_t l;
|
565 |
|
|
it = "memrchr";
|
566 |
|
|
check (memrchr ("abcd", 'z', 5) == NULL, 1); /* Not found. */
|
567 |
|
|
(void) strcpy (one, "abcd");
|
568 |
|
|
l = strlen (one) + 1;
|
569 |
|
|
check (memrchr (one, 'c', l) == one+2, 2); /* Basic test. */
|
570 |
|
|
check (memrchr (one, 'd', l) == one+3, 3); /* End of string. */
|
571 |
|
|
check (memrchr (one, 'a', l) == one, 4); /* Beginning. */
|
572 |
|
|
check (memrchr (one, '\0', l) == one+4, 5); /* Finding NUL. */
|
573 |
|
|
(void) strcpy (one, "ababa");
|
574 |
|
|
l = strlen (one) + 1;
|
575 |
|
|
check (memrchr (one, 'b', l) == one+3, 6); /* Finding last. */
|
576 |
|
|
(void) strcpy (one, "");
|
577 |
|
|
l = strlen (one) + 1;
|
578 |
|
|
check (memrchr (one, 'b', l) == NULL, 7); /* Empty string. */
|
579 |
|
|
check (memrchr (one, '\0', l) == one, 8); /* NUL in empty string. */
|
580 |
|
|
|
581 |
|
|
/* now test all possible alignment and length combinations to catch
|
582 |
|
|
bugs due to unrolled loops (assuming unrolling is limited to no
|
583 |
|
|
more than 128 byte chunks: */
|
584 |
|
|
{
|
585 |
|
|
char buf[128 + sizeof(long)];
|
586 |
|
|
long align, len, i, pos;
|
587 |
|
|
|
588 |
|
|
for (align = 0; align < (long) sizeof(long); ++align) {
|
589 |
|
|
for (len = 0; len < (long) (sizeof(buf) - align); ++len) {
|
590 |
|
|
for (i = 0; i < len; ++i)
|
591 |
|
|
buf[align + i] = 'x'; /* don't depend on memset... */
|
592 |
|
|
|
593 |
|
|
for (pos = len - 1; pos >= 0; --pos) {
|
594 |
|
|
#if 0
|
595 |
|
|
printf("align %d, len %d, pos %d\n", align, len, pos);
|
596 |
|
|
#endif
|
597 |
|
|
check(memrchr(buf + align, 'x', len) == buf + align + pos, 9);
|
598 |
|
|
check(memrchr(buf + align + pos + 1, 'x', len - (pos + 1)) == NULL,
|
599 |
|
|
10);
|
600 |
|
|
buf[align + pos] = '-';
|
601 |
|
|
}
|
602 |
|
|
}
|
603 |
|
|
}
|
604 |
|
|
}
|
605 |
|
|
}
|
606 |
|
|
|
607 |
|
|
static void
|
608 |
|
|
test_rindex (void)
|
609 |
|
|
{
|
610 |
|
|
it = "rindex";
|
611 |
|
|
check (rindex ("abcd", 'z') == NULL, 1); /* Not found. */
|
612 |
|
|
(void) strcpy (one, "abcd");
|
613 |
|
|
check (rindex (one, 'c') == one+2, 2); /* Basic test. */
|
614 |
|
|
check (rindex (one, 'd') == one+3, 3); /* End of string. */
|
615 |
|
|
check (rindex (one, 'a') == one, 4); /* Beginning. */
|
616 |
|
|
check (rindex (one, '\0') == one+4, 5); /* Finding NUL. */
|
617 |
|
|
(void) strcpy (one, "ababa");
|
618 |
|
|
check (rindex (one, 'b') == one+3, 6); /* Finding last. */
|
619 |
|
|
(void) strcpy (one, "");
|
620 |
|
|
check (rindex (one, 'b') == NULL, 7); /* Empty string. */
|
621 |
|
|
check (rindex (one, '\0') == one, 8); /* NUL in empty string. */
|
622 |
|
|
}
|
623 |
|
|
|
624 |
|
|
static void
|
625 |
|
|
test_strpbrk (void)
|
626 |
|
|
{
|
627 |
|
|
it = "strpbrk";
|
628 |
|
|
check(strpbrk("abcd", "z") == NULL, 1); /* Not found. */
|
629 |
|
|
(void) strcpy(one, "abcd");
|
630 |
|
|
check(strpbrk(one, "c") == one+2, 2); /* Basic test. */
|
631 |
|
|
check(strpbrk(one, "d") == one+3, 3); /* End of string. */
|
632 |
|
|
check(strpbrk(one, "a") == one, 4); /* Beginning. */
|
633 |
|
|
check(strpbrk(one, "") == NULL, 5); /* Empty search list. */
|
634 |
|
|
check(strpbrk(one, "cb") == one+1, 6); /* Multiple search. */
|
635 |
|
|
(void) strcpy(one, "abcabdea");
|
636 |
|
|
check(strpbrk(one, "b") == one+1, 7); /* Finding first. */
|
637 |
|
|
check(strpbrk(one, "cb") == one+1, 8); /* With multiple search. */
|
638 |
|
|
check(strpbrk(one, "db") == one+1, 9); /* Another variant. */
|
639 |
|
|
(void) strcpy(one, "");
|
640 |
|
|
check(strpbrk(one, "bc") == NULL, 10); /* Empty string. */
|
641 |
|
|
(void) strcpy(one, "");
|
642 |
|
|
check(strpbrk(one, "bcd") == NULL, 11); /* Empty string. */
|
643 |
|
|
(void) strcpy(one, "");
|
644 |
|
|
check(strpbrk(one, "bcde") == NULL, 12); /* Empty string. */
|
645 |
|
|
check(strpbrk(one, "") == NULL, 13); /* Both strings empty. */
|
646 |
|
|
(void) strcpy(one, "abcabdea");
|
647 |
|
|
check(strpbrk(one, "befg") == one+1, 14); /* Finding first. */
|
648 |
|
|
check(strpbrk(one, "cbr") == one+1, 15); /* With multiple search. */
|
649 |
|
|
check(strpbrk(one, "db") == one+1, 16); /* Another variant. */
|
650 |
|
|
check(strpbrk(one, "efgh") == one+6, 17); /* And yet another. */
|
651 |
|
|
}
|
652 |
|
|
|
653 |
|
|
static void
|
654 |
|
|
test_strstr (void)
|
655 |
|
|
{
|
656 |
|
|
it = "strstr";
|
657 |
|
|
check(strstr("abcd", "z") == NULL, 1); /* Not found. */
|
658 |
|
|
check(strstr("abcd", "abx") == NULL, 2); /* Dead end. */
|
659 |
|
|
(void) strcpy(one, "abcd");
|
660 |
|
|
check(strstr(one, "c") == one+2, 3); /* Basic test. */
|
661 |
|
|
check(strstr(one, "bc") == one+1, 4); /* Multichar. */
|
662 |
|
|
check(strstr(one, "d") == one+3, 5); /* End of string. */
|
663 |
|
|
check(strstr(one, "cd") == one+2, 6); /* Tail of string. */
|
664 |
|
|
check(strstr(one, "abc") == one, 7); /* Beginning. */
|
665 |
|
|
check(strstr(one, "abcd") == one, 8); /* Exact match. */
|
666 |
|
|
check(strstr(one, "abcde") == NULL, 9); /* Too long. */
|
667 |
|
|
check(strstr(one, "de") == NULL, 10); /* Past end. */
|
668 |
|
|
check(strstr(one, "") == one, 11); /* Finding empty. */
|
669 |
|
|
(void) strcpy(one, "ababa");
|
670 |
|
|
check(strstr(one, "ba") == one+1, 12); /* Finding first. */
|
671 |
|
|
(void) strcpy(one, "");
|
672 |
|
|
check(strstr(one, "b") == NULL, 13); /* Empty string. */
|
673 |
|
|
check(strstr(one, "") == one, 14); /* Empty in empty string. */
|
674 |
|
|
(void) strcpy(one, "bcbca");
|
675 |
|
|
check(strstr(one, "bca") == one+2, 15); /* False start. */
|
676 |
|
|
(void) strcpy(one, "bbbcabbca");
|
677 |
|
|
check(strstr(one, "bbca") == one+1, 16); /* With overlap. */
|
678 |
|
|
}
|
679 |
|
|
|
680 |
|
|
static void
|
681 |
|
|
test_strspn (void)
|
682 |
|
|
{
|
683 |
|
|
it = "strspn";
|
684 |
|
|
check(strspn("abcba", "abc") == 5, 1); /* Whole string. */
|
685 |
|
|
check(strspn("abcba", "ab") == 2, 2); /* Partial. */
|
686 |
|
|
check(strspn("abc", "qx") == 0, 3); /* None. */
|
687 |
|
|
check(strspn("", "ab") == 0, 4); /* Null string. */
|
688 |
|
|
check(strspn("abc", "") == 0, 5); /* Null search list. */
|
689 |
|
|
}
|
690 |
|
|
|
691 |
|
|
static void
|
692 |
|
|
test_strcspn (void)
|
693 |
|
|
{
|
694 |
|
|
it = "strcspn";
|
695 |
|
|
check(strcspn("abcba", "qx") == 5, 1); /* Whole string. */
|
696 |
|
|
check(strcspn("abcba", "cx") == 2, 2); /* Partial. */
|
697 |
|
|
check(strcspn("abc", "abc") == 0, 3); /* None. */
|
698 |
|
|
check(strcspn("", "ab") == 0, 4); /* Null string. */
|
699 |
|
|
check(strcspn("abc", "") == 3, 5); /* Null search list. */
|
700 |
|
|
}
|
701 |
|
|
|
702 |
|
|
static void
|
703 |
|
|
test_strtok (void)
|
704 |
|
|
{
|
705 |
|
|
it = "strtok";
|
706 |
|
|
(void) strcpy(one, "first, second, third");
|
707 |
|
|
equal(strtok(one, ", "), "first", 1); /* Basic test. */
|
708 |
|
|
equal(one, "first", 2);
|
709 |
|
|
equal(strtok((char *)NULL, ", "), "second", 3);
|
710 |
|
|
equal(strtok((char *)NULL, ", "), "third", 4);
|
711 |
|
|
check(strtok((char *)NULL, ", ") == NULL, 5);
|
712 |
|
|
(void) strcpy(one, ", first, ");
|
713 |
|
|
equal(strtok(one, ", "), "first", 6); /* Extra delims, 1 tok. */
|
714 |
|
|
check(strtok((char *)NULL, ", ") == NULL, 7);
|
715 |
|
|
(void) strcpy(one, "1a, 1b; 2a, 2b");
|
716 |
|
|
equal(strtok(one, ", "), "1a", 8); /* Changing delim lists. */
|
717 |
|
|
equal(strtok((char *)NULL, "; "), "1b", 9);
|
718 |
|
|
equal(strtok((char *)NULL, ", "), "2a", 10);
|
719 |
|
|
(void) strcpy(two, "x-y");
|
720 |
|
|
equal(strtok(two, "-"), "x", 11); /* New string before done. */
|
721 |
|
|
equal(strtok((char *)NULL, "-"), "y", 12);
|
722 |
|
|
check(strtok((char *)NULL, "-") == NULL, 13);
|
723 |
|
|
(void) strcpy(one, "a,b, c,, ,d");
|
724 |
|
|
equal(strtok(one, ", "), "a", 14); /* Different separators. */
|
725 |
|
|
equal(strtok((char *)NULL, ", "), "b", 15);
|
726 |
|
|
equal(strtok((char *)NULL, " ,"), "c", 16); /* Permute list too. */
|
727 |
|
|
equal(strtok((char *)NULL, " ,"), "d", 17);
|
728 |
|
|
check(strtok((char *)NULL, ", ") == NULL, 18);
|
729 |
|
|
check(strtok((char *)NULL, ", ") == NULL, 19); /* Persistence. */
|
730 |
|
|
(void) strcpy(one, ", ");
|
731 |
|
|
check(strtok(one, ", ") == NULL, 20); /* No tokens. */
|
732 |
|
|
(void) strcpy(one, "");
|
733 |
|
|
check(strtok(one, ", ") == NULL, 21); /* Empty string. */
|
734 |
|
|
(void) strcpy(one, "abc");
|
735 |
|
|
equal(strtok(one, ", "), "abc", 22); /* No delimiters. */
|
736 |
|
|
check(strtok((char *)NULL, ", ") == NULL, 23);
|
737 |
|
|
(void) strcpy(one, "abc");
|
738 |
|
|
equal(strtok(one, ""), "abc", 24); /* Empty delimiter list. */
|
739 |
|
|
check(strtok((char *)NULL, "") == NULL, 25);
|
740 |
|
|
(void) strcpy(one, "abcdefgh");
|
741 |
|
|
(void) strcpy(one, "a,b,c");
|
742 |
|
|
equal(strtok(one, ","), "a", 26); /* Basics again... */
|
743 |
|
|
equal(strtok((char *)NULL, ","), "b", 27);
|
744 |
|
|
equal(strtok((char *)NULL, ","), "c", 28);
|
745 |
|
|
check(strtok((char *)NULL, ",") == NULL, 29);
|
746 |
|
|
equal(one+6, "gh", 30); /* Stomped past end? */
|
747 |
|
|
equal(one, "a", 31); /* Stomped old tokens? */
|
748 |
|
|
equal(one+2, "b", 32);
|
749 |
|
|
equal(one+4, "c", 33);
|
750 |
|
|
}
|
751 |
|
|
|
752 |
|
|
static void
|
753 |
|
|
test_strtok_r (void)
|
754 |
|
|
{
|
755 |
|
|
it = "strtok_r";
|
756 |
|
|
(void) strcpy(one, "first, second, third");
|
757 |
|
|
cp = NULL; /* Always initialize cp to make sure it doesn't point to some old data. */
|
758 |
|
|
equal(strtok_r(one, ", ", &cp), "first", 1); /* Basic test. */
|
759 |
|
|
equal(one, "first", 2);
|
760 |
|
|
equal(strtok_r((char *)NULL, ", ", &cp), "second", 3);
|
761 |
|
|
equal(strtok_r((char *)NULL, ", ", &cp), "third", 4);
|
762 |
|
|
check(strtok_r((char *)NULL, ", ", &cp) == NULL, 5);
|
763 |
|
|
(void) strcpy(one, ", first, ");
|
764 |
|
|
cp = NULL;
|
765 |
|
|
equal(strtok_r(one, ", ", &cp), "first", 6); /* Extra delims, 1 tok. */
|
766 |
|
|
check(strtok_r((char *)NULL, ", ", &cp) == NULL, 7);
|
767 |
|
|
(void) strcpy(one, "1a, 1b; 2a, 2b");
|
768 |
|
|
cp = NULL;
|
769 |
|
|
equal(strtok_r(one, ", ", &cp), "1a", 8); /* Changing delim lists. */
|
770 |
|
|
equal(strtok_r((char *)NULL, "; ", &cp), "1b", 9);
|
771 |
|
|
equal(strtok_r((char *)NULL, ", ", &cp), "2a", 10);
|
772 |
|
|
(void) strcpy(two, "x-y");
|
773 |
|
|
cp = NULL;
|
774 |
|
|
equal(strtok_r(two, "-", &cp), "x", 11); /* New string before done. */
|
775 |
|
|
equal(strtok_r((char *)NULL, "-", &cp), "y", 12);
|
776 |
|
|
check(strtok_r((char *)NULL, "-", &cp) == NULL, 13);
|
777 |
|
|
(void) strcpy(one, "a,b, c,, ,d");
|
778 |
|
|
cp = NULL;
|
779 |
|
|
equal(strtok_r(one, ", ", &cp), "a", 14); /* Different separators. */
|
780 |
|
|
equal(strtok_r((char *)NULL, ", ", &cp), "b", 15);
|
781 |
|
|
equal(strtok_r((char *)NULL, " ,", &cp), "c", 16); /* Permute list too. */
|
782 |
|
|
equal(strtok_r((char *)NULL, " ,", &cp), "d", 17);
|
783 |
|
|
check(strtok_r((char *)NULL, ", ", &cp) == NULL, 18);
|
784 |
|
|
check(strtok_r((char *)NULL, ", ", &cp) == NULL, 19); /* Persistence. */
|
785 |
|
|
(void) strcpy(one, ", ");
|
786 |
|
|
cp = NULL;
|
787 |
|
|
check(strtok_r(one, ", ", &cp) == NULL, 20); /* No tokens. */
|
788 |
|
|
(void) strcpy(one, "");
|
789 |
|
|
cp = NULL;
|
790 |
|
|
check(strtok_r(one, ", ", &cp) == NULL, 21); /* Empty string. */
|
791 |
|
|
check(strtok_r((char *)NULL, ", ", &cp) == NULL, 22); /* Persistence. */
|
792 |
|
|
(void) strcpy(one, "abc");
|
793 |
|
|
cp = NULL;
|
794 |
|
|
equal(strtok_r(one, ", ", &cp), "abc", 23); /* No delimiters. */
|
795 |
|
|
check(strtok_r((char *)NULL, ", ", &cp) == NULL, 24);
|
796 |
|
|
(void) strcpy(one, "abc");
|
797 |
|
|
cp = NULL;
|
798 |
|
|
equal(strtok_r(one, "", &cp), "abc", 25); /* Empty delimiter list. */
|
799 |
|
|
check(strtok_r((char *)NULL, "", &cp) == NULL, 26);
|
800 |
|
|
(void) strcpy(one, "abcdefgh");
|
801 |
|
|
(void) strcpy(one, "a,b,c");
|
802 |
|
|
cp = NULL;
|
803 |
|
|
equal(strtok_r(one, ",", &cp), "a", 27); /* Basics again... */
|
804 |
|
|
equal(strtok_r((char *)NULL, ",", &cp), "b", 28);
|
805 |
|
|
equal(strtok_r((char *)NULL, ",", &cp), "c", 29);
|
806 |
|
|
check(strtok_r((char *)NULL, ",", &cp) == NULL, 30);
|
807 |
|
|
equal(one+6, "gh", 31); /* Stomped past end? */
|
808 |
|
|
equal(one, "a", 32); /* Stomped old tokens? */
|
809 |
|
|
equal(one+2, "b", 33);
|
810 |
|
|
equal(one+4, "c", 34);
|
811 |
|
|
}
|
812 |
|
|
|
813 |
|
|
static void
|
814 |
|
|
test_strsep (void)
|
815 |
|
|
{
|
816 |
|
|
char *ptr;
|
817 |
|
|
it = "strsep";
|
818 |
|
|
cp = strcpy(one, "first, second, third");
|
819 |
|
|
equal(strsep(&cp, ", "), "first", 1); /* Basic test. */
|
820 |
|
|
equal(one, "first", 2);
|
821 |
|
|
equal(strsep(&cp, ", "), "", 3);
|
822 |
|
|
equal(strsep(&cp, ", "), "second", 4);
|
823 |
|
|
equal(strsep(&cp, ", "), "", 5);
|
824 |
|
|
equal(strsep(&cp, ", "), "third", 6);
|
825 |
|
|
check(strsep(&cp, ", ") == NULL, 7);
|
826 |
|
|
cp = strcpy(one, ", first, ");
|
827 |
|
|
equal(strsep(&cp, ", "), "", 8);
|
828 |
|
|
equal(strsep(&cp, ", "), "", 9);
|
829 |
|
|
equal(strsep(&cp, ", "), "first", 10); /* Extra delims, 1 tok. */
|
830 |
|
|
equal(strsep(&cp, ", "), "", 11);
|
831 |
|
|
equal(strsep(&cp, ", "), "", 12);
|
832 |
|
|
check(strsep(&cp, ", ") == NULL, 13);
|
833 |
|
|
cp = strcpy(one, "1a, 1b; 2a, 2b");
|
834 |
|
|
equal(strsep(&cp, ", "), "1a", 14); /* Changing delim lists. */
|
835 |
|
|
equal(strsep(&cp, ", "), "", 15);
|
836 |
|
|
equal(strsep(&cp, "; "), "1b", 16);
|
837 |
|
|
equal(strsep(&cp, ", "), "", 17);
|
838 |
|
|
equal(strsep(&cp, ", "), "2a", 18);
|
839 |
|
|
cp = strcpy(two, "x-y");
|
840 |
|
|
equal(strsep(&cp, "-"), "x", 19); /* New string before done. */
|
841 |
|
|
equal(strsep(&cp, "-"), "y", 20);
|
842 |
|
|
check(strsep(&cp, "-") == NULL, 21);
|
843 |
|
|
cp = strcpy(one, "a,b, c,, ,d ");
|
844 |
|
|
equal(strsep(&cp, ", "), "a", 22); /* Different separators. */
|
845 |
|
|
equal(strsep(&cp, ", "), "b", 23);
|
846 |
|
|
equal(strsep(&cp, " ,"), "", 24);
|
847 |
|
|
equal(strsep(&cp, " ,"), "c", 25); /* Permute list too. */
|
848 |
|
|
equal(strsep(&cp, " ,"), "", 26);
|
849 |
|
|
equal(strsep(&cp, " ,"), "", 27);
|
850 |
|
|
equal(strsep(&cp, " ,"), "", 28);
|
851 |
|
|
equal(strsep(&cp, " ,"), "d", 29);
|
852 |
|
|
equal(strsep(&cp, " ,"), "", 30);
|
853 |
|
|
check(strsep(&cp, ", ") == NULL, 31);
|
854 |
|
|
check(strsep(&cp, ", ") == NULL, 32); /* Persistence. */
|
855 |
|
|
cp = strcpy(one, ", ");
|
856 |
|
|
equal(strsep(&cp, ", "), "", 33);
|
857 |
|
|
equal(strsep(&cp, ", "), "", 34);
|
858 |
|
|
equal(strsep(&cp, ", "), "", 35);
|
859 |
|
|
check(strsep(&cp, ", ") == NULL, 36); /* No tokens. */
|
860 |
|
|
cp = strcpy(one, "");
|
861 |
|
|
equal(strsep(&cp, ", "), "", 37);
|
862 |
|
|
check(strsep(&cp, ", ") == NULL, 38); /* Empty string. */
|
863 |
|
|
cp = strcpy(one, "abc");
|
864 |
|
|
equal(strsep(&cp, ", "), "abc", 39); /* No delimiters. */
|
865 |
|
|
check(strsep(&cp, ", ") == NULL, 40);
|
866 |
|
|
cp = strcpy(one, "abc");
|
867 |
|
|
equal(strsep(&cp, ""), "abc", 41); /* Empty delimiter list. */
|
868 |
|
|
check(strsep(&cp, "") == NULL, 42);
|
869 |
|
|
(void) strcpy(one, "abcdefgh");
|
870 |
|
|
cp = strcpy(one, "a,b,c");
|
871 |
|
|
equal(strsep(&cp, ","), "a", 43); /* Basics again... */
|
872 |
|
|
equal(strsep(&cp, ","), "b", 44);
|
873 |
|
|
equal(strsep(&cp, ","), "c", 45);
|
874 |
|
|
check(strsep(&cp, ",") == NULL, 46);
|
875 |
|
|
equal(one+6, "gh", 47); /* Stomped past end? */
|
876 |
|
|
equal(one, "a", 48); /* Stomped old tokens? */
|
877 |
|
|
equal(one+2, "b", 49);
|
878 |
|
|
equal(one+4, "c", 50);
|
879 |
|
|
|
880 |
|
|
{
|
881 |
|
|
char text[] = "This,is,a,test";
|
882 |
|
|
char *list = strdupa (text);
|
883 |
|
|
equal (strsep (&list, ","), "This", 51);
|
884 |
|
|
equal (strsep (&list, ","), "is", 52);
|
885 |
|
|
equal (strsep (&list, ","), "a", 53);
|
886 |
|
|
equal (strsep (&list, ","), "test", 54);
|
887 |
|
|
check (strsep (&list, ",") == NULL, 55);
|
888 |
|
|
}
|
889 |
|
|
|
890 |
|
|
cp = strcpy(one, "a,b, c,, ,d,");
|
891 |
|
|
equal(strsep(&cp, ","), "a", 56); /* Different separators. */
|
892 |
|
|
equal(strsep(&cp, ","), "b", 57);
|
893 |
|
|
equal(strsep(&cp, ","), " c", 58); /* Permute list too. */
|
894 |
|
|
equal(strsep(&cp, ","), "", 59);
|
895 |
|
|
equal(strsep(&cp, ","), " ", 60);
|
896 |
|
|
equal(strsep(&cp, ","), "d", 61);
|
897 |
|
|
equal(strsep(&cp, ","), "", 62);
|
898 |
|
|
check(strsep(&cp, ",") == NULL, 63);
|
899 |
|
|
check(strsep(&cp, ",") == NULL, 64); /* Persistence. */
|
900 |
|
|
|
901 |
|
|
cp = strcpy(one, "a,b, c,, ,d,");
|
902 |
|
|
equal(strsep(&cp, "xy,"), "a", 65); /* Different separators. */
|
903 |
|
|
equal(strsep(&cp, "x,y"), "b", 66);
|
904 |
|
|
equal(strsep(&cp, ",xy"), " c", 67); /* Permute list too. */
|
905 |
|
|
equal(strsep(&cp, "xy,"), "", 68);
|
906 |
|
|
equal(strsep(&cp, "x,y"), " ", 69);
|
907 |
|
|
equal(strsep(&cp, ",xy"), "d", 70);
|
908 |
|
|
equal(strsep(&cp, "xy,"), "", 71);
|
909 |
|
|
check(strsep(&cp, "x,y") == NULL, 72);
|
910 |
|
|
check(strsep(&cp, ",xy") == NULL, 73); /* Persistence. */
|
911 |
|
|
|
912 |
|
|
cp = strcpy(one, "ABC");
|
913 |
|
|
one[4] = ':';
|
914 |
|
|
equal(strsep(&cp, "C"), "AB", 74); /* Access beyond NUL. */
|
915 |
|
|
ptr = strsep(&cp, ":");
|
916 |
|
|
equal(ptr, "", 75);
|
917 |
|
|
check(ptr == one + 3, 76);
|
918 |
|
|
check(cp == NULL, 77);
|
919 |
|
|
|
920 |
|
|
cp = strcpy(one, "ABC");
|
921 |
|
|
one[4] = ':';
|
922 |
|
|
equal(strsep(&cp, "CD"), "AB", 78); /* Access beyond NUL. */
|
923 |
|
|
ptr = strsep(&cp, ":.");
|
924 |
|
|
equal(ptr, "", 79);
|
925 |
|
|
check(ptr == one + 3, 80);
|
926 |
|
|
|
927 |
|
|
cp = strcpy(one, "ABC"); /* No token in string. */
|
928 |
|
|
equal(strsep(&cp, ","), "ABC", 81);
|
929 |
|
|
check(cp == NULL, 82);
|
930 |
|
|
|
931 |
|
|
*one = '\0'; /* Empty string. */
|
932 |
|
|
cp = one;
|
933 |
|
|
ptr = strsep(&cp, ",");
|
934 |
|
|
equal(ptr, "", 83);
|
935 |
|
|
check(ptr == one, 84);
|
936 |
|
|
check(cp == NULL, 85);
|
937 |
|
|
|
938 |
|
|
*one = '\0'; /* Empty string and no token. */
|
939 |
|
|
cp = one;
|
940 |
|
|
ptr = strsep(&cp, "");
|
941 |
|
|
equal(ptr, "", 86);
|
942 |
|
|
check(ptr == one , 87);
|
943 |
|
|
check(cp == NULL, 88);
|
944 |
|
|
}
|
945 |
|
|
|
946 |
|
|
static void
|
947 |
|
|
test_memcmp (void)
|
948 |
|
|
{
|
949 |
|
|
it = "memcmp";
|
950 |
|
|
check(memcmp("a", "a", 1) == 0, 1); /* Identity. */
|
951 |
|
|
check(memcmp("abc", "abc", 3) == 0, 2); /* Multicharacter. */
|
952 |
|
|
check(memcmp("abcd", "abce", 4) < 0, 3); /* Honestly unequal. */
|
953 |
|
|
check(memcmp("abce", "abcd", 4) > 0, 4);
|
954 |
|
|
check(memcmp("alph", "beta", 4) < 0, 5);
|
955 |
|
|
check(memcmp("a\203", "a\003", 2) > 0, 6);
|
956 |
|
|
check(memcmp("abce", "abcd", 3) == 0, 7); /* Count limited. */
|
957 |
|
|
check(memcmp("abc", "def", 0) == 0, 8); /* Zero count. */
|
958 |
|
|
}
|
959 |
|
|
|
960 |
|
|
static void
|
961 |
|
|
test_memchr (void)
|
962 |
|
|
{
|
963 |
|
|
it = "memchr";
|
964 |
|
|
check(memchr("abcd", 'z', 4) == NULL, 1); /* Not found. */
|
965 |
|
|
(void) strcpy(one, "abcd");
|
966 |
|
|
check(memchr(one, 'c', 4) == one+2, 2); /* Basic test. */
|
967 |
|
|
check(memchr(one, ~0xff|'c', 4) == one+2, 2); /* ignore highorder bits. */
|
968 |
|
|
check(memchr(one, 'd', 4) == one+3, 3); /* End of string. */
|
969 |
|
|
check(memchr(one, 'a', 4) == one, 4); /* Beginning. */
|
970 |
|
|
check(memchr(one, '\0', 5) == one+4, 5); /* Finding NUL. */
|
971 |
|
|
(void) strcpy(one, "ababa");
|
972 |
|
|
check(memchr(one, 'b', 5) == one+1, 6); /* Finding first. */
|
973 |
|
|
check(memchr(one, 'b', 0) == NULL, 7); /* Zero count. */
|
974 |
|
|
check(memchr(one, 'a', 1) == one, 8); /* Singleton case. */
|
975 |
|
|
(void) strcpy(one, "a\203b");
|
976 |
|
|
check(memchr(one, 0203, 3) == one+1, 9); /* Unsignedness. */
|
977 |
|
|
|
978 |
|
|
/* now test all possible alignment and length combinations to catch
|
979 |
|
|
bugs due to unrolled loops (assuming unrolling is limited to no
|
980 |
|
|
more than 128 byte chunks: */
|
981 |
|
|
{
|
982 |
|
|
char buf[128 + sizeof(long)];
|
983 |
|
|
long align, len, i, pos;
|
984 |
|
|
|
985 |
|
|
for (align = 0; align < (long) sizeof(long); ++align) {
|
986 |
|
|
for (len = 0; len < (long) (sizeof(buf) - align); ++len) {
|
987 |
|
|
for (i = 0; i < len; ++i) {
|
988 |
|
|
buf[align + i] = 'x'; /* don't depend on memset... */
|
989 |
|
|
}
|
990 |
|
|
for (pos = 0; pos < len; ++pos) {
|
991 |
|
|
#if 0
|
992 |
|
|
printf("align %d, len %d, pos %d\n", align, len, pos);
|
993 |
|
|
#endif
|
994 |
|
|
check(memchr(buf + align, 'x', len) == buf + align + pos, 10);
|
995 |
|
|
check(memchr(buf + align, 'x', pos) == NULL, 11);
|
996 |
|
|
buf[align + pos] = '-';
|
997 |
|
|
}
|
998 |
|
|
}
|
999 |
|
|
}
|
1000 |
|
|
}
|
1001 |
|
|
}
|
1002 |
|
|
|
1003 |
|
|
static void
|
1004 |
|
|
test_memcpy (void)
|
1005 |
|
|
{
|
1006 |
|
|
int i;
|
1007 |
|
|
it = "memcpy";
|
1008 |
|
|
check(memcpy(one, "abc", 4) == one, 1); /* Returned value. */
|
1009 |
|
|
equal(one, "abc", 2); /* Did the copy go right? */
|
1010 |
|
|
|
1011 |
|
|
(void) strcpy(one, "abcdefgh");
|
1012 |
|
|
(void) memcpy(one+1, "xyz", 2);
|
1013 |
|
|
equal(one, "axydefgh", 3); /* Basic test. */
|
1014 |
|
|
|
1015 |
|
|
(void) strcpy(one, "abc");
|
1016 |
|
|
(void) memcpy(one, "xyz", 0);
|
1017 |
|
|
equal(one, "abc", 4); /* Zero-length copy. */
|
1018 |
|
|
|
1019 |
|
|
(void) strcpy(one, "hi there");
|
1020 |
|
|
(void) strcpy(two, "foo");
|
1021 |
|
|
(void) memcpy(two, one, 9);
|
1022 |
|
|
equal(two, "hi there", 5); /* Just paranoia. */
|
1023 |
|
|
equal(one, "hi there", 6); /* Stomped on source? */
|
1024 |
|
|
|
1025 |
|
|
for (i = 0; i < 16; i++)
|
1026 |
|
|
{
|
1027 |
|
|
const char *x = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
|
1028 |
|
|
strcpy (one, x);
|
1029 |
|
|
check (memcpy (one + i, "hi there", 9) == one + i,
|
1030 |
|
|
7 + (i * 6)); /* Unaligned destination. */
|
1031 |
|
|
check (memcmp (one, x, i) == 0, 8 + (i * 6)); /* Wrote under? */
|
1032 |
|
|
equal (one + i, "hi there", 9 + (i * 6));
|
1033 |
|
|
check (one[i + 9] == 'x', 10 + (i * 6)); /* Wrote over? */
|
1034 |
|
|
check (memcpy (two, one + i, 9) == two,
|
1035 |
|
|
11 + (i * 6)); /* Unaligned source. */
|
1036 |
|
|
equal (two, "hi there", 12 + (i * 6));
|
1037 |
|
|
}
|
1038 |
|
|
}
|
1039 |
|
|
|
1040 |
|
|
static void
|
1041 |
|
|
test_mempcpy (void)
|
1042 |
|
|
{
|
1043 |
|
|
int i;
|
1044 |
|
|
it = "mempcpy";
|
1045 |
|
|
check(mempcpy(one, "abc", 4) == one + 4, 1); /* Returned value. */
|
1046 |
|
|
equal(one, "abc", 2); /* Did the copy go right? */
|
1047 |
|
|
|
1048 |
|
|
(void) strcpy(one, "abcdefgh");
|
1049 |
|
|
(void) mempcpy(one+1, "xyz", 2);
|
1050 |
|
|
equal(one, "axydefgh", 3); /* Basic test. */
|
1051 |
|
|
|
1052 |
|
|
(void) strcpy(one, "abc");
|
1053 |
|
|
(void) mempcpy(one, "xyz", 0);
|
1054 |
|
|
equal(one, "abc", 4); /* Zero-length copy. */
|
1055 |
|
|
|
1056 |
|
|
(void) strcpy(one, "hi there");
|
1057 |
|
|
(void) strcpy(two, "foo");
|
1058 |
|
|
(void) mempcpy(two, one, 9);
|
1059 |
|
|
equal(two, "hi there", 5); /* Just paranoia. */
|
1060 |
|
|
equal(one, "hi there", 6); /* Stomped on source? */
|
1061 |
|
|
|
1062 |
|
|
for (i = 0; i < 16; i++)
|
1063 |
|
|
{
|
1064 |
|
|
const char *x = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
|
1065 |
|
|
strcpy (one, x);
|
1066 |
|
|
check (mempcpy (one + i, "hi there", 9) == one + i + 9,
|
1067 |
|
|
7 + (i * 6)); /* Unaligned destination. */
|
1068 |
|
|
check (memcmp (one, x, i) == 0, 8 + (i * 6)); /* Wrote under? */
|
1069 |
|
|
equal (one + i, "hi there", 9 + (i * 6));
|
1070 |
|
|
check (one[i + 9] == 'x', 10 + (i * 6)); /* Wrote over? */
|
1071 |
|
|
check (mempcpy (two, one + i, 9) == two + 9,
|
1072 |
|
|
11 + (i * 6)); /* Unaligned source. */
|
1073 |
|
|
equal (two, "hi there", 12 + (i * 6));
|
1074 |
|
|
}
|
1075 |
|
|
}
|
1076 |
|
|
|
1077 |
|
|
static void
|
1078 |
|
|
test_memmove (void)
|
1079 |
|
|
{
|
1080 |
|
|
it = "memmove";
|
1081 |
|
|
check(memmove(one, "abc", 4) == one, 1); /* Returned value. */
|
1082 |
|
|
equal(one, "abc", 2); /* Did the copy go right? */
|
1083 |
|
|
|
1084 |
|
|
(void) strcpy(one, "abcdefgh");
|
1085 |
|
|
(void) memmove(one+1, "xyz", 2);
|
1086 |
|
|
equal(one, "axydefgh", 3); /* Basic test. */
|
1087 |
|
|
|
1088 |
|
|
(void) strcpy(one, "abc");
|
1089 |
|
|
(void) memmove(one, "xyz", 0);
|
1090 |
|
|
equal(one, "abc", 4); /* Zero-length copy. */
|
1091 |
|
|
|
1092 |
|
|
(void) strcpy(one, "hi there");
|
1093 |
|
|
(void) strcpy(two, "foo");
|
1094 |
|
|
(void) memmove(two, one, 9);
|
1095 |
|
|
equal(two, "hi there", 5); /* Just paranoia. */
|
1096 |
|
|
equal(one, "hi there", 6); /* Stomped on source? */
|
1097 |
|
|
|
1098 |
|
|
(void) strcpy(one, "abcdefgh");
|
1099 |
|
|
(void) memmove(one+1, one, 9);
|
1100 |
|
|
equal(one, "aabcdefgh", 7); /* Overlap, right-to-left. */
|
1101 |
|
|
|
1102 |
|
|
(void) strcpy(one, "abcdefgh");
|
1103 |
|
|
(void) memmove(one+1, one+2, 7);
|
1104 |
|
|
equal(one, "acdefgh", 8); /* Overlap, left-to-right. */
|
1105 |
|
|
|
1106 |
|
|
(void) strcpy(one, "abcdefgh");
|
1107 |
|
|
(void) memmove(one, one, 9);
|
1108 |
|
|
equal(one, "abcdefgh", 9); /* 100% overlap. */
|
1109 |
|
|
}
|
1110 |
|
|
|
1111 |
|
|
static void
|
1112 |
|
|
test_memccpy (void)
|
1113 |
|
|
{
|
1114 |
|
|
/* First test like memcpy, then the search part The SVID, the only
|
1115 |
|
|
place where memccpy is mentioned, says overlap might fail, so we
|
1116 |
|
|
don't try it. Besides, it's hard to see the rationale for a
|
1117 |
|
|
non-left-to-right memccpy. */
|
1118 |
|
|
it = "memccpy";
|
1119 |
|
|
check(memccpy(one, "abc", 'q', 4) == NULL, 1); /* Returned value. */
|
1120 |
|
|
equal(one, "abc", 2); /* Did the copy go right? */
|
1121 |
|
|
|
1122 |
|
|
(void) strcpy(one, "abcdefgh");
|
1123 |
|
|
(void) memccpy(one+1, "xyz", 'q', 2);
|
1124 |
|
|
equal(one, "axydefgh", 3); /* Basic test. */
|
1125 |
|
|
|
1126 |
|
|
(void) strcpy(one, "abc");
|
1127 |
|
|
(void) memccpy(one, "xyz", 'q', 0);
|
1128 |
|
|
equal(one, "abc", 4); /* Zero-length copy. */
|
1129 |
|
|
|
1130 |
|
|
(void) strcpy(one, "hi there");
|
1131 |
|
|
(void) strcpy(two, "foo");
|
1132 |
|
|
(void) memccpy(two, one, 'q', 9);
|
1133 |
|
|
equal(two, "hi there", 5); /* Just paranoia. */
|
1134 |
|
|
equal(one, "hi there", 6); /* Stomped on source? */
|
1135 |
|
|
|
1136 |
|
|
(void) strcpy(one, "abcdefgh");
|
1137 |
|
|
(void) strcpy(two, "horsefeathers");
|
1138 |
|
|
check(memccpy(two, one, 'f', 9) == two+6, 7); /* Returned value. */
|
1139 |
|
|
equal(one, "abcdefgh", 8); /* Source intact? */
|
1140 |
|
|
equal(two, "abcdefeathers", 9); /* Copy correct? */
|
1141 |
|
|
|
1142 |
|
|
(void) strcpy(one, "abcd");
|
1143 |
|
|
(void) strcpy(two, "bumblebee");
|
1144 |
|
|
check(memccpy(two, one, 'a', 4) == two+1, 10); /* First char. */
|
1145 |
|
|
equal(two, "aumblebee", 11);
|
1146 |
|
|
check(memccpy(two, one, 'd', 4) == two+4, 12); /* Last char. */
|
1147 |
|
|
equal(two, "abcdlebee", 13);
|
1148 |
|
|
(void) strcpy(one, "xyz");
|
1149 |
|
|
check(memccpy(two, one, 'x', 1) == two+1, 14); /* Singleton. */
|
1150 |
|
|
equal(two, "xbcdlebee", 15);
|
1151 |
|
|
}
|
1152 |
|
|
|
1153 |
|
|
static void
|
1154 |
|
|
test_memset (void)
|
1155 |
|
|
{
|
1156 |
|
|
int i;
|
1157 |
|
|
|
1158 |
|
|
it = "memset";
|
1159 |
|
|
(void) strcpy(one, "abcdefgh");
|
1160 |
|
|
check(memset(one+1, 'x', 3) == one+1, 1); /* Return value. */
|
1161 |
|
|
equal(one, "axxxefgh", 2); /* Basic test. */
|
1162 |
|
|
|
1163 |
|
|
(void) memset(one+2, 'y', 0);
|
1164 |
|
|
equal(one, "axxxefgh", 3); /* Zero-length set. */
|
1165 |
|
|
|
1166 |
|
|
(void) memset(one+5, 0, 1);
|
1167 |
|
|
equal(one, "axxxe", 4); /* Zero fill. */
|
1168 |
|
|
equal(one+6, "gh", 5); /* And the leftover. */
|
1169 |
|
|
|
1170 |
|
|
(void) memset(one+2, 010045, 1);
|
1171 |
|
|
equal(one, "ax\045xe", 6); /* Unsigned char convert. */
|
1172 |
|
|
|
1173 |
|
|
/* Non-8bit fill character. */
|
1174 |
|
|
memset (one, 0x101, sizeof (one));
|
1175 |
|
|
for (i = 0; i < (int) sizeof (one); ++i)
|
1176 |
|
|
check (one[i] == '\01', 7);
|
1177 |
|
|
|
1178 |
|
|
/* Test for more complex versions of memset, for all alignments and
|
1179 |
|
|
lengths up to 256. This test takes a little while, perhaps it should
|
1180 |
|
|
be made weaker? */
|
1181 |
|
|
{
|
1182 |
|
|
char data[512];
|
1183 |
|
|
int j;
|
1184 |
|
|
int k;
|
1185 |
|
|
int c;
|
1186 |
|
|
|
1187 |
|
|
for (i = 0; i < 512; i++)
|
1188 |
|
|
data[i] = 'x';
|
1189 |
|
|
for (c = 0; c <= 'y'; c += 'y') /* check for memset(,0,) and
|
1190 |
|
|
memset(,'y',) */
|
1191 |
|
|
for (j = 0; j < 256; j++)
|
1192 |
|
|
for (i = 0; i < 256; i++)
|
1193 |
|
|
{
|
1194 |
|
|
memset (data + i, c, j);
|
1195 |
|
|
for (k = 0; k < i; k++)
|
1196 |
|
|
if (data[k] != 'x')
|
1197 |
|
|
goto fail;
|
1198 |
|
|
for (k = i; k < i+j; k++)
|
1199 |
|
|
{
|
1200 |
|
|
if (data[k] != c)
|
1201 |
|
|
goto fail;
|
1202 |
|
|
data[k] = 'x';
|
1203 |
|
|
}
|
1204 |
|
|
for (k = i+j; k < 512; k++)
|
1205 |
|
|
if (data[k] != 'x')
|
1206 |
|
|
goto fail;
|
1207 |
|
|
continue;
|
1208 |
|
|
|
1209 |
|
|
fail:
|
1210 |
|
|
check (0, 8 + i + j * 256 + (c != 0) * 256 * 256);
|
1211 |
|
|
}
|
1212 |
|
|
}
|
1213 |
|
|
}
|
1214 |
|
|
|
1215 |
|
|
static void
|
1216 |
|
|
test_bcopy (void)
|
1217 |
|
|
{
|
1218 |
|
|
/* Much like memcpy. Berklix manual is silent about overlap, so
|
1219 |
|
|
don't test it. */
|
1220 |
|
|
it = "bcopy";
|
1221 |
|
|
(void) bcopy("abc", one, 4);
|
1222 |
|
|
equal(one, "abc", 1); /* Simple copy. */
|
1223 |
|
|
|
1224 |
|
|
(void) strcpy(one, "abcdefgh");
|
1225 |
|
|
(void) bcopy("xyz", one+1, 2);
|
1226 |
|
|
equal(one, "axydefgh", 2); /* Basic test. */
|
1227 |
|
|
|
1228 |
|
|
(void) strcpy(one, "abc");
|
1229 |
|
|
(void) bcopy("xyz", one, 0);
|
1230 |
|
|
equal(one, "abc", 3); /* Zero-length copy. */
|
1231 |
|
|
|
1232 |
|
|
(void) strcpy(one, "hi there");
|
1233 |
|
|
(void) strcpy(two, "foo");
|
1234 |
|
|
(void) bcopy(one, two, 9);
|
1235 |
|
|
equal(two, "hi there", 4); /* Just paranoia. */
|
1236 |
|
|
equal(one, "hi there", 5); /* Stomped on source? */
|
1237 |
|
|
}
|
1238 |
|
|
|
1239 |
|
|
static void
|
1240 |
|
|
test_bzero (void)
|
1241 |
|
|
{
|
1242 |
|
|
it = "bzero";
|
1243 |
|
|
(void) strcpy(one, "abcdef");
|
1244 |
|
|
bzero(one+2, 2);
|
1245 |
|
|
equal(one, "ab", 1); /* Basic test. */
|
1246 |
|
|
equal(one+3, "", 2);
|
1247 |
|
|
equal(one+4, "ef", 3);
|
1248 |
|
|
|
1249 |
|
|
(void) strcpy(one, "abcdef");
|
1250 |
|
|
bzero(one+2, 0);
|
1251 |
|
|
equal(one, "abcdef", 4); /* Zero-length copy. */
|
1252 |
|
|
}
|
1253 |
|
|
|
1254 |
|
|
static void
|
1255 |
|
|
test_strndup (void)
|
1256 |
|
|
{
|
1257 |
|
|
char *p, *q;
|
1258 |
|
|
it = "strndup";
|
1259 |
|
|
p = strndup("abcdef", 12);
|
1260 |
|
|
check(p != NULL, 1);
|
1261 |
|
|
if (p != NULL)
|
1262 |
|
|
{
|
1263 |
|
|
equal(p, "abcdef", 2);
|
1264 |
|
|
q = strndup(p + 1, 2);
|
1265 |
|
|
check(q != NULL, 3);
|
1266 |
|
|
if (q != NULL)
|
1267 |
|
|
equal(q, "bc", 4);
|
1268 |
|
|
free (q);
|
1269 |
|
|
}
|
1270 |
|
|
free (p);
|
1271 |
|
|
p = strndup("abc def", 3);
|
1272 |
|
|
check(p != NULL, 5);
|
1273 |
|
|
if (p != NULL)
|
1274 |
|
|
equal(p, "abc", 6);
|
1275 |
|
|
free (p);
|
1276 |
|
|
}
|
1277 |
|
|
|
1278 |
|
|
static void
|
1279 |
|
|
test_bcmp (void)
|
1280 |
|
|
{
|
1281 |
|
|
it = "bcmp";
|
1282 |
|
|
check(bcmp("a", "a", 1) == 0, 1); /* Identity. */
|
1283 |
|
|
check(bcmp("abc", "abc", 3) == 0, 2); /* Multicharacter. */
|
1284 |
|
|
check(bcmp("abcd", "abce", 4) != 0, 3); /* Honestly unequal. */
|
1285 |
|
|
check(bcmp("abce", "abcd", 4) != 0, 4);
|
1286 |
|
|
check(bcmp("alph", "beta", 4) != 0, 5);
|
1287 |
|
|
check(bcmp("abce", "abcd", 3) == 0, 6); /* Count limited. */
|
1288 |
|
|
check(bcmp("abc", "def", 0) == 0, 8); /* Zero count. */
|
1289 |
|
|
}
|
1290 |
|
|
|
1291 |
|
|
static void
|
1292 |
|
|
test_strerror (void)
|
1293 |
|
|
{
|
1294 |
|
|
it = "strerror";
|
1295 |
|
|
check(strerror(EDOM) != 0, 1);
|
1296 |
|
|
check(strerror(ERANGE) != 0, 2);
|
1297 |
|
|
check(strerror(ENOENT) != 0, 3);
|
1298 |
|
|
}
|
1299 |
|
|
|
1300 |
|
|
int
|
1301 |
|
|
main (void)
|
1302 |
|
|
{
|
1303 |
|
|
int status;
|
1304 |
|
|
|
1305 |
|
|
/* Test strcmp first because we use it to test other things. */
|
1306 |
|
|
test_strcmp ();
|
1307 |
|
|
|
1308 |
|
|
/* Test strcpy next because we need it to set up other tests. */
|
1309 |
|
|
test_strcpy ();
|
1310 |
|
|
|
1311 |
|
|
/* A closely related function is stpcpy. */
|
1312 |
|
|
test_stpcpy ();
|
1313 |
|
|
|
1314 |
|
|
/* stpncpy. */
|
1315 |
|
|
test_stpncpy ();
|
1316 |
|
|
|
1317 |
|
|
/* strcat. */
|
1318 |
|
|
test_strcat ();
|
1319 |
|
|
|
1320 |
|
|
/* strncat. */
|
1321 |
|
|
test_strncat ();
|
1322 |
|
|
|
1323 |
|
|
/* strncmp. */
|
1324 |
|
|
test_strncmp ();
|
1325 |
|
|
|
1326 |
|
|
/* strncpy. */
|
1327 |
|
|
test_strncpy ();
|
1328 |
|
|
|
1329 |
|
|
/* strlen. */
|
1330 |
|
|
test_strlen ();
|
1331 |
|
|
|
1332 |
|
|
/* strchr. */
|
1333 |
|
|
test_strchr ();
|
1334 |
|
|
|
1335 |
|
|
/* strchrnul. */
|
1336 |
|
|
test_strchrnul ();
|
1337 |
|
|
|
1338 |
|
|
/* rawmemchr. */
|
1339 |
|
|
test_rawmemchr ();
|
1340 |
|
|
|
1341 |
|
|
/* index - just like strchr. */
|
1342 |
|
|
test_index ();
|
1343 |
|
|
|
1344 |
|
|
/* strrchr. */
|
1345 |
|
|
test_strrchr ();
|
1346 |
|
|
|
1347 |
|
|
/* memrchr. */
|
1348 |
|
|
test_memrchr ();
|
1349 |
|
|
|
1350 |
|
|
/* rindex - just like strrchr. */
|
1351 |
|
|
test_rindex ();
|
1352 |
|
|
|
1353 |
|
|
/* strpbrk - somewhat like strchr. */
|
1354 |
|
|
test_strpbrk ();
|
1355 |
|
|
|
1356 |
|
|
/* strstr - somewhat like strchr. */
|
1357 |
|
|
test_strstr ();
|
1358 |
|
|
|
1359 |
|
|
/* strspn. */
|
1360 |
|
|
test_strspn ();
|
1361 |
|
|
|
1362 |
|
|
/* strcspn. */
|
1363 |
|
|
test_strcspn ();
|
1364 |
|
|
|
1365 |
|
|
/* strtok - the hard one. */
|
1366 |
|
|
test_strtok ();
|
1367 |
|
|
|
1368 |
|
|
/* strtok_r. */
|
1369 |
|
|
test_strtok_r ();
|
1370 |
|
|
|
1371 |
|
|
/* strsep. */
|
1372 |
|
|
test_strsep ();
|
1373 |
|
|
|
1374 |
|
|
/* memcmp. */
|
1375 |
|
|
test_memcmp ();
|
1376 |
|
|
|
1377 |
|
|
/* memchr. */
|
1378 |
|
|
test_memchr ();
|
1379 |
|
|
|
1380 |
|
|
/* memcpy - need not work for overlap. */
|
1381 |
|
|
test_memcpy ();
|
1382 |
|
|
|
1383 |
|
|
/* memmove - must work on overlap. */
|
1384 |
|
|
test_memmove ();
|
1385 |
|
|
|
1386 |
|
|
/* mempcpy */
|
1387 |
|
|
test_mempcpy ();
|
1388 |
|
|
|
1389 |
|
|
/* memccpy. */
|
1390 |
|
|
test_memccpy ();
|
1391 |
|
|
|
1392 |
|
|
/* memset. */
|
1393 |
|
|
test_memset ();
|
1394 |
|
|
|
1395 |
|
|
/* bcopy. */
|
1396 |
|
|
test_bcopy ();
|
1397 |
|
|
|
1398 |
|
|
/* bzero. */
|
1399 |
|
|
test_bzero ();
|
1400 |
|
|
|
1401 |
|
|
/* bcmp - somewhat like memcmp. */
|
1402 |
|
|
test_bcmp ();
|
1403 |
|
|
|
1404 |
|
|
/* strndup. */
|
1405 |
|
|
test_strndup ();
|
1406 |
|
|
|
1407 |
|
|
/* strerror - VERY system-dependent. */
|
1408 |
|
|
test_strerror ();
|
1409 |
|
|
|
1410 |
|
|
|
1411 |
|
|
if (errors == 0)
|
1412 |
|
|
{
|
1413 |
|
|
status = EXIT_SUCCESS;
|
1414 |
|
|
puts("No errors.");
|
1415 |
|
|
}
|
1416 |
|
|
else
|
1417 |
|
|
{
|
1418 |
|
|
status = EXIT_FAILURE;
|
1419 |
|
|
printf("%d errors.\n", errors);
|
1420 |
|
|
}
|
1421 |
|
|
|
1422 |
|
|
return status;
|
1423 |
|
|
}
|