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
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
33 #include <sys/param.h>
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
56 #define STRINGIFY(s) STRINGIFY2 (s)
57 #define STRINGIFY2(s) #s
63 int size
= sysconf (_SC_PAGESIZE
);
64 int nchars
= size
/ sizeof (CHAR
);
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
)
76 puts ("No test, mmap not available.");
79 printf ("mmap failed: %m");
85 int inner
, middle
, outer
;
87 mprotect (adr
, size
, PROT_NONE
);
88 mprotect (adr
+ 2 * nchars
, size
, PROT_NONE
);
91 mprotect (dest
, size
, PROT_NONE
);
92 mprotect (dest
+ 2 * nchars
, size
, PROT_NONE
);
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
);
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
);
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
);
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
)
161 && (cp
- &adr
[outer
]) != middle
- outer
))
163 printf ("%s flunked for outer = %d, middle = %d, "
165 STRINGIFY (STRCHR
), outer
, middle
, inner
);
170 adr
[middle
] = L('T');
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",
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
)
198 && (cp
- &adr
[outer
]) != middle
- outer
))
200 printf ("%s flunked for outer = %d, middle = %d, "
202 STRINGIFY (STRRCHR
), outer
, middle
, inner
);
207 adr
[middle
] = L('T');
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
);
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
);
237 printf ("%s flunked for outer = %d\n",
238 STRINGIFY (MEMCHR
), outer
);
243 /* This function only exists for single-byte characters. */
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
);
261 adr
[middle
] = L('T');
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
);
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);
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
);
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
);
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);
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, "
324 STRINGIFY (STRNCMP
), outer
, middle
, inner
);
328 if (STRNCMP (dest
+ nchars
- outer
, adr
+ middle
, inner
) != 0)
330 printf ("%s 2 flunked for outer = %d, middle = %d, "
332 STRINGIFY (STRNCMP
), outer
, middle
, inner
);
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
);
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
);
352 /* strncpy/wcsncpy tests */
353 adr
[nchars
- 1] = L('T');
354 for (outer
= nchars
; outer
>= MAX (0, nchars
- 128); --outer
)
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
);
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
)
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, "
389 STRINGIFY (STRNCPY
), outer
, inner
, len
);
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, "
400 STRINGIFY (STRNCPY
), outer
, inner
, len
);
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
);
427 /* stpncpy/wcpncpy test */
428 adr
[nchars
- 1] = L('T');
429 for (outer
= nchars
; outer
>= MAX (0, nchars
- 128); --outer
)
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
);
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, "
459 STRINGIFY (STPNCPY
), outer
, middle
, inner
);
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
);
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
);
488 /* This function only exists for single-byte characters. */
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",
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)
512 memccpy flunked partial copy for outer = %d, middle = %d, inner = %d\n",
513 outer
, middle
, inner
);
516 else if (dest
[inner
+ 1] != L('\2'))
519 memccpy copied too much for outer = %d, middle = %d, inner = %d\n",
520 outer
, middle
, inner
);
523 adr
[outer
+ inner
] = L('\0');
532 #define TEST_FUNCTION do_test ()
533 #include "../test-skeleton.c"