Update.
[glibc.git] / string / stratcliff.c
blobae1b7ae0ca886ebe7f8675b29eb3888891694930
1 /* Test for string function add boundaries of usable memory.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #define _GNU_SOURCE 1
23 /* Make sure we don't test the optimized inline functions if we want to
24 test the real implementation. */
25 #undef __USE_STRING_INLINES
27 #include <errno.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/mman.h>
32 #include <sys/param.h>
34 #ifndef MAX
35 #define MAX(a, b) ((a) > (b) ? (a) : (b))
36 #endif
38 int
39 main (int argc, char *argv[])
41 int size = sysconf (_SC_PAGESIZE);
42 char *adr, *dest;
43 int result = 0;
45 adr = (char *) mmap (NULL, 3 * size, PROT_READ|PROT_WRITE,
46 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
47 dest = (char *) mmap (NULL, 3*size, PROT_READ|PROT_WRITE,
48 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
49 if (adr == MAP_FAILED || dest == MAP_FAILED)
51 if (errno == ENOSYS)
52 puts ("No test, mmap not available.");
53 else
55 printf ("mmap failed: %m");
56 result = 1;
59 else
61 int inner, middle, outer;
63 mprotect(adr, size, PROT_NONE);
64 mprotect(adr+2*size, size, PROT_NONE);
65 adr += size;
67 mprotect(dest, size, PROT_NONE);
68 mprotect(dest+2*size, size, PROT_NONE);
69 dest += size;
71 memset (adr, 'T', size);
73 /* strlen test */
74 for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
76 for (inner = MAX (outer, size - 64); inner < size; ++inner)
78 adr[inner] = '\0';
80 if (strlen (&adr[outer]) != (size_t) (inner - outer))
82 printf ("strlen flunked for outer = %d, inner = %d\n",
83 outer, inner);
84 result = 1;
87 adr[inner] = 'T';
91 /* strchr test */
92 for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
94 for (middle = MAX (outer, size - 64); middle < size; ++middle)
96 for (inner = middle; inner < size; ++inner)
98 char *cp;
99 adr[middle] = 'V';
100 adr[inner] = '\0';
102 cp = strchr (&adr[outer], 'V');
104 if ((inner == middle && cp != NULL)
105 || (inner != middle
106 && (cp - &adr[outer]) != middle - outer))
108 printf ("strchr flunked for outer = %d, middle = %d, "
109 "inner = %d\n", outer, middle, inner);
110 result = 1;
113 adr[inner] = 'T';
114 adr[middle] = 'T';
119 /* strrchr test */
120 for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
122 for (middle = MAX (outer, size - 64); middle < size; ++middle)
124 for (inner = middle; inner < size; ++inner)
126 char *cp;
127 adr[middle] = 'V';
128 adr[inner] = '\0';
130 cp = strrchr (&adr[outer], 'V');
132 if ((inner == middle && cp != NULL)
133 || (inner != middle
134 && (cp - &adr[outer]) != middle - outer))
136 printf ("strrchr flunked for outer = %d, middle = %d, "
137 "inner = %d\n", outer, middle, inner);
138 result = 1;
141 adr[inner] = 'T';
142 adr[middle] = 'T';
147 /* strcpy test */
148 for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
150 for (inner = MAX (outer, size - 64); inner < size; ++inner)
152 adr[inner] = '\0';
154 if (strcpy (dest, &adr[outer]) != dest
155 || strlen (dest) != (size_t) (inner - outer))
157 printf ("strcpy flunked for outer = %d, inner = %d\n",
158 outer, inner);
159 result = 1;
162 adr[inner] = 'T';
166 /* stpcpy test */
167 for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
169 for (inner = MAX (outer, size - 64); inner < size; ++inner)
171 adr[inner] = '\0';
173 if ((stpcpy (dest, &adr[outer]) - dest) != inner - outer)
175 printf ("stpcpy flunked for outer = %d, inner = %d\n",
176 outer, inner);
177 result = 1;
180 adr[inner] = 'T';
185 return result;