Last change caused infinite loops because of missing loop increment.
[glibc.git] / string / stratcliff.c
blob5165be2d131673d6760666b4400ed182f8053d4d
1 /* Test for string function add boundaries of usable memory.
2 Copyright (C) 1996,1997,1999-2003,2007,2009,2010
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #define _GNU_SOURCE 1
24 /* Make sure we don't test the optimized inline functions if we want to
25 test the real implementation. */
26 #undef __USE_STRING_INLINES
28 #include <errno.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/mman.h>
33 #include <sys/param.h>
35 #ifndef CHAR
36 # define L(c) c
37 # define CHAR char
38 # define MEMSET memset
39 # define STRLEN strlen
40 # define STRNLEN strnlen
41 # define STRCHR strchr
42 # define STRRCHR strrchr
43 # define STRCPY strcpy
44 # define STRNCPY strncpy
45 # define MEMCMP memcmp
46 # define STPCPY stpcpy
47 # define STPNCPY stpncpy
48 # define MEMCPY memcpy
49 # define MEMPCPY mempcpy
50 # define MEMCHR memchr
51 # define STRCMP strcmp
52 # define STRNCMP strncmp
53 #endif
56 #define STRINGIFY(s) STRINGIFY2 (s)
57 #define STRINGIFY2(s) #s
60 static int
61 do_test (void)
63 int size = sysconf (_SC_PAGESIZE);
64 int nchars = size / sizeof (CHAR);
65 CHAR *adr;
66 CHAR *dest;
67 int result = 0;
69 adr = (CHAR *) mmap (NULL, 3 * size, PROT_READ | PROT_WRITE,
70 MAP_PRIVATE | MAP_ANON, -1, 0);
71 dest = (CHAR *) mmap (NULL, 3 * size, PROT_READ | PROT_WRITE,
72 MAP_PRIVATE | MAP_ANON, -1, 0);
73 if (adr == MAP_FAILED || dest == MAP_FAILED)
75 if (errno == ENOSYS)
76 puts ("No test, mmap not available.");
77 else
79 printf ("mmap failed: %m");
80 result = 1;
83 else
85 int inner, middle, outer;
87 mprotect (adr, size, PROT_NONE);
88 mprotect (adr + 2 * nchars, size, PROT_NONE);
89 adr += nchars;
91 mprotect (dest, size, PROT_NONE);
92 mprotect (dest + 2 * nchars, size, PROT_NONE);
93 dest += nchars;
95 MEMSET (adr, L('T'), nchars);
97 /* strlen/wcslen test */
98 for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
100 for (inner = MAX (outer, nchars - 64); inner < nchars; ++inner)
102 adr[inner] = L('\0');
104 if (STRLEN (&adr[outer]) != (size_t) (inner - outer))
106 printf ("%s flunked for outer = %d, inner = %d\n",
107 STRINGIFY (STRLEN), outer, inner);
108 result = 1;
111 adr[inner] = L('T');
115 /* strnlen/wcsnlen test */
116 for (outer = nchars; outer >= MAX (0, nchars - 128); --outer)
118 for (inner = MAX (outer, nchars - 64); inner < nchars; ++inner)
120 adr[inner] = L('\0');
122 if (STRNLEN (&adr[outer], inner - outer + 1)
123 != (size_t) (inner - outer))
125 printf ("%s flunked for outer = %d, inner = %d\n",
126 STRINGIFY (STRNLEN), outer, inner);
127 result = 1;
130 adr[inner] = L('T');
133 for (outer = nchars; outer >= MAX (0, nchars - 128); --outer)
135 for (inner = MAX (outer, nchars - 64); inner <= nchars; ++inner)
137 if (STRNLEN (&adr[outer], inner - outer)
138 != (size_t) (inner - outer))
140 printf ("%s flunked bounded for outer = %d, inner = %d\n",
141 STRINGIFY (STRNLEN), outer, inner);
142 result = 1;
147 /* strchr/wcschr test */
148 for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
150 for (middle = MAX (outer, nchars - 64); middle < nchars; ++middle)
152 for (inner = middle; inner < nchars; ++inner)
154 adr[middle] = L('V');
155 adr[inner] = L('\0');
157 CHAR *cp = STRCHR (&adr[outer], L('V'));
159 if ((inner == middle && cp != NULL)
160 || (inner != middle
161 && (cp - &adr[outer]) != middle - outer))
163 printf ("%s flunked for outer = %d, middle = %d, "
164 "inner = %d\n",
165 STRINGIFY (STRCHR), outer, middle, inner);
166 result = 1;
169 adr[inner] = L('T');
170 adr[middle] = L('T');
175 /* Special test. */
176 adr[nchars - 1] = L('\0');
177 if (STRCHR (&adr[nchars - 1], L('\n')) != NULL)
179 printf ("%s flunked test of empty string at end of page\n",
180 STRINGIFY (STRCHR));
181 result = 1;
184 /* strrchr/wcsrchr test */
185 for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
187 for (middle = MAX (outer, nchars - 64); middle < nchars; ++middle)
189 for (inner = middle; inner < nchars; ++inner)
191 adr[middle] = L('V');
192 adr[inner] = L('\0');
194 CHAR *cp = STRRCHR (&adr[outer], L('V'));
196 if ((inner == middle && cp != NULL)
197 || (inner != middle
198 && (cp - &adr[outer]) != middle - outer))
200 printf ("%s flunked for outer = %d, middle = %d, "
201 "inner = %d\n",
202 STRINGIFY (STRRCHR), outer, middle, inner);
203 result = 1;
206 adr[inner] = L('T');
207 adr[middle] = L('T');
212 /* memchr test */
213 for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
215 for (middle = MAX (outer, nchars - 64); middle < nchars; ++middle)
217 adr[middle] = L('V');
219 CHAR *cp = MEMCHR (&adr[outer], L('V'), 3 * size);
221 if (cp - &adr[outer] != middle - outer)
223 printf ("%s flunked for outer = %d, middle = %d\n",
224 STRINGIFY (MEMCHR), outer, middle);
225 result = 1;
228 adr[middle] = L('T');
231 for (outer = nchars; outer >= MAX (0, nchars - 128); --outer)
233 CHAR *cp = MEMCHR (&adr[outer], L('V'), nchars - outer);
235 if (cp != NULL)
237 printf ("%s flunked for outer = %d\n",
238 STRINGIFY (MEMCHR), outer);
239 result = 1;
243 /* This function only exists for single-byte characters. */
244 #ifndef WCSTEST
245 /* rawmemchr test */
246 for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
248 for (middle = MAX (outer, nchars - 64); middle < nchars; ++middle)
250 adr[middle] = L('V');
252 CHAR *cp = rawmemchr (&adr[outer], L('V'));
254 if (cp - &adr[outer] != middle - outer)
256 printf ("%s flunked for outer = %d, middle = %d\n",
257 STRINGIFY (rawmemchr), outer, middle);
258 result = 1;
261 adr[middle] = L('T');
264 #endif
266 /* strcpy/wcscpy test */
267 for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
269 for (inner = MAX (outer, nchars - 64); inner < nchars; ++inner)
271 adr[inner] = L('\0');
273 if (STRCPY (dest, &adr[outer]) != dest
274 || STRLEN (dest) != (size_t) (inner - outer))
276 printf ("%s flunked for outer = %d, inner = %d\n",
277 STRINGIFY (STRCPY), outer, inner);
278 result = 1;
281 adr[inner] = L('T');
285 /* strcmp/wcscmp tests */
286 for (outer = 1; outer < 32; ++outer)
287 for (middle = 0; middle < 16; ++middle)
289 MEMSET (adr + middle, L('T'), 256);
290 adr[256] = L('\0');
291 MEMSET (dest + nchars - outer, L('T'), outer - 1);
292 dest[nchars - 1] = L('\0');
294 if (STRCMP (adr + middle, dest + nchars - outer) <= 0)
296 printf ("%s 1 flunked for outer = %d, middle = %d\n",
297 STRINGIFY (STRCMP), outer, middle);
298 result = 1;
301 if (STRCMP (dest + nchars - outer, adr + middle) >= 0)
303 printf ("%s 2 flunked for outer = %d, middle = %d\n",
304 STRINGIFY (STRCMP), outer, middle);
305 result = 1;
309 /* strncmp/wcsncmp tests */
310 for (outer = 1; outer < 32; ++outer)
311 for (middle = 0; middle < 16; ++middle)
313 MEMSET (adr + middle, L('T'), 256);
314 adr[256] = L('\0');
315 MEMSET (dest + nchars - outer, L('T'), outer - 1);
316 dest[nchars - 1] = L('U');
318 for (inner = 0; inner < outer; ++inner)
320 if (STRNCMP (adr + middle, dest + nchars - outer, inner) != 0)
322 printf ("%s 1 flunked for outer = %d, middle = %d, "
323 "inner = %d\n",
324 STRINGIFY (STRNCMP), outer, middle, inner);
325 result = 1;
328 if (STRNCMP (dest + nchars - outer, adr + middle, inner) != 0)
330 printf ("%s 2 flunked for outer = %d, middle = %d, "
331 "inner = %d\n",
332 STRINGIFY (STRNCMP), outer, middle, inner);
333 result = 1;
337 if (STRNCMP (adr + middle, dest + nchars - outer, outer) >= 0)
339 printf ("%s 1 flunked for outer = %d, middle = %d, full\n",
340 STRINGIFY (STRNCMP), outer, middle);
341 result = 1;
344 if (STRNCMP (dest + nchars - outer, adr + middle, outer) <= 0)
346 printf ("%s 2 flunked for outer = %d, middle = %d, full\n",
347 STRINGIFY (STRNCMP), outer, middle);
348 result = 1;
352 /* strncpy/wcsncpy tests */
353 adr[nchars - 1] = L('T');
354 for (outer = nchars; outer >= MAX (0, nchars - 128); --outer)
356 size_t len;
358 for (len = 0; len < nchars - outer; ++len)
360 if (STRNCPY (dest, &adr[outer], len) != dest
361 || MEMCMP (dest, &adr[outer], len) != 0)
363 printf ("outer %s flunked for outer = %d, len = %Zd\n",
364 STRINGIFY (STRNCPY), outer, len);
365 result = 1;
369 adr[nchars - 1] = L('\0');
371 for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
373 for (inner = MAX (outer, nchars - 64); inner < nchars; ++inner)
375 size_t len;
377 adr[inner] = L('\0');
379 for (len = 0; len < nchars - outer + 64; ++len)
381 if (STRNCPY (dest, &adr[outer], len) != dest
382 || MEMCMP (dest, &adr[outer],
383 MIN (inner - outer, len)) != 0
384 || (inner - outer < len
385 && STRLEN (dest) != (inner - outer)))
387 printf ("%s flunked for outer = %d, inner = %d, "
388 "len = %Zd\n",
389 STRINGIFY (STRNCPY), outer, inner, len);
390 result = 1;
392 if (STRNCPY (dest + 1, &adr[outer], len) != dest + 1
393 || MEMCMP (dest + 1, &adr[outer],
394 MIN (inner - outer, len)) != 0
395 || (inner - outer < len
396 && STRLEN (dest + 1) != (inner - outer)))
398 printf ("%s+1 flunked for outer = %d, inner = %d, "
399 "len = %Zd\n",
400 STRINGIFY (STRNCPY), outer, inner, len);
401 result = 1;
405 adr[inner] = L('T');
409 /* stpcpy/wcpcpy test */
410 for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
412 for (inner = MAX (outer, nchars - 64); inner < nchars; ++inner)
414 adr[inner] = L('\0');
416 if ((STPCPY (dest, &adr[outer]) - dest) != inner - outer)
418 printf ("%s flunked for outer = %d, inner = %d\n",
419 STRINGIFY (STPCPY), outer, inner);
420 result = 1;
423 adr[inner] = L('T');
427 /* stpncpy/wcpncpy test */
428 adr[nchars - 1] = L('T');
429 for (outer = nchars; outer >= MAX (0, nchars - 128); --outer)
431 size_t len;
433 for (len = 0; len < nchars - outer; ++len)
435 if (STPNCPY (dest, &adr[outer], len) != dest + len
436 || MEMCMP (dest, &adr[outer], len) != 0)
438 printf ("outer %s flunked for outer = %d, len = %Zd\n",
439 STRINGIFY (STPNCPY), outer, len);
440 result = 1;
444 adr[nchars - 1] = L('\0');
446 for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
448 for (middle = MAX (outer, nchars - 64); middle < nchars; ++middle)
450 adr[middle] = L('\0');
452 for (inner = 0; inner < nchars - outer; ++ inner)
454 if ((STPNCPY (dest, &adr[outer], inner) - dest)
455 != MIN (inner, middle - outer))
457 printf ("%s flunked for outer = %d, middle = %d, "
458 "inner = %d\n",
459 STRINGIFY (STPNCPY), outer, middle, inner);
460 result = 1;
464 adr[middle] = L('T');
468 /* memcpy/wmemcpy test */
469 for (outer = nchars; outer >= MAX (0, nchars - 128); --outer)
470 for (inner = 0; inner < nchars - outer; ++inner)
471 if (MEMCPY (dest, &adr[outer], inner) != dest)
473 printf ("%s flunked for outer = %d, inner = %d\n",
474 STRINGIFY (MEMCPY), outer, inner);
475 result = 1;
478 /* mempcpy/wmempcpy test */
479 for (outer = nchars; outer >= MAX (0, nchars - 128); --outer)
480 for (inner = 0; inner < nchars - outer; ++inner)
481 if (MEMPCPY (dest, &adr[outer], inner) != dest + inner)
483 printf ("%s flunked for outer = %d, inner = %d\n",
484 STRINGIFY (MEMPCPY), outer, inner);
485 result = 1;
488 /* This function only exists for single-byte characters. */
489 #ifndef WCSTEST
490 /* memccpy test */
491 memset (adr, '\0', nchars);
492 for (outer = nchars; outer >= MAX (0, nchars - 128); --outer)
493 for (inner = 0; inner < nchars - outer; ++inner)
494 if (memccpy (dest, &adr[outer], L('\1'), inner) != NULL)
496 printf ("memccpy flunked full copy for outer = %d, inner = %d\n",
497 outer, inner);
498 result = 1;
500 for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
501 for (middle = 0; middle < nchars - outer; ++middle)
503 memset (dest, L('\2'), middle + 1);
504 for (inner = 0; inner < middle; ++inner)
506 adr[outer + inner] = L('\1');
508 if (memccpy (dest, &adr[outer], '\1', middle + 128)
509 != dest + inner + 1)
511 printf ("\
512 memccpy flunked partial copy for outer = %d, middle = %d, inner = %d\n",
513 outer, middle, inner);
514 result = 1;
516 else if (dest[inner + 1] != L('\2'))
518 printf ("\
519 memccpy copied too much for outer = %d, middle = %d, inner = %d\n",
520 outer, middle, inner);
521 result = 1;
523 adr[outer + inner] = L('\0');
526 #endif
529 return result;
532 #define TEST_FUNCTION do_test ()
533 #include "../test-skeleton.c"