Optimize for kernels which are known to have the vfork syscall.
[glibc/pb-stable.git] / string / test-strcat.c
blobd241741c5e70339813820bf85f4bd373ddbf8dc6
1 /* Test and measure strcat functions.
2 Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #define TEST_MAIN
22 #include "test-string.h"
24 typedef char *(*proto_t) (char *, const char *);
25 char *simple_strcat (char *, const char *);
27 IMPL (simple_strcat, 0)
28 IMPL (strcat, 1)
30 char *
31 simple_strcat (char *dst, const char *src)
33 char *ret = dst;
34 while (*dst++ != '\0');
35 --dst;
36 while ((*dst++ = *src++) != '\0');
37 return ret;
40 static void
41 do_one_test (impl_t *impl, char *dst, const char *src)
43 size_t k = strlen (dst);
44 if (CALL (impl, dst, src) != dst)
46 error (0, 0, "Wrong result in function %s %p %p", impl->name,
47 CALL (impl, dst, src), dst);
48 ret = 1;
49 return;
52 if (strcmp (dst + k, src) != 0)
54 error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
55 impl->name, dst, src);
56 ret = 1;
57 return;
60 if (HP_TIMING_AVAIL)
62 hp_timing_t start, stop, best_time = ~ (hp_timing_t) 0;
63 size_t i;
65 for (i = 0; i < 32; ++i)
67 dst[k] = '\0';
68 HP_TIMING_NOW (start);
69 CALL (impl, dst, src);
70 HP_TIMING_NOW (stop);
71 HP_TIMING_BEST (best_time, start, stop);
74 printf ("\t%zd", (size_t) best_time);
78 static void
79 do_test (size_t align1, size_t align2, size_t len1, size_t len2, int max_char)
81 size_t i;
82 char *s1, *s2;
84 align1 &= 7;
85 if (align1 + len1 >= page_size)
86 return;
88 align2 &= 7;
89 if (align2 + len1 + len2 >= page_size)
90 return;
92 s1 = buf1 + align1;
93 s2 = buf2 + align2;
95 for (i = 0; i < len1; ++i)
96 s1[i] = 32 + 23 * i % (max_char - 32);
97 s1[len1] = '\0';
99 for (i = 0; i < len2; i++)
100 s2[i] = 32 + 23 * i % (max_char - 32);
102 if (HP_TIMING_AVAIL)
103 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len1, len2, align1, align2);
105 FOR_EACH_IMPL (impl, 0)
107 s2[len2] = '\0';
108 do_one_test (impl, s2, s1);
111 if (HP_TIMING_AVAIL)
112 putchar ('\n');
115 static void
116 do_random_tests (void)
118 size_t i, j, n, align1, align2, len1, len2;
119 unsigned char *p1 = buf1 + page_size - 512;
120 unsigned char *p2 = buf2 + page_size - 512;
121 unsigned char *res;
123 for (n = 0; n < ITERATIONS; n++)
125 align1 = random () & 31;
126 if (random () & 1)
127 align2 = random () & 31;
128 else
129 align2 = align1 + (random () & 24);
130 len1 = random () & 511;
131 if (len1 + align2 > 512)
132 len2 = random () & 7;
133 else
134 len2 = (512 - len1 - align2) * (random () & (1024 * 1024 - 1))
135 / (1024 * 1024);
136 j = align1;
137 if (align2 + len2 > j)
138 j = align2 + len2;
139 if (len1 + j >= 511)
140 len1 = 510 - j - (random () & 7);
141 if (len1 >= 512)
142 len1 = 0;
143 if (align1 + len1 < 512 - 8)
145 j = 510 - align1 - len1 - (random () & 31);
146 if (j > 0 && j < 512)
147 align1 += j;
149 j = len1 + align1 + 64;
150 if (j > 512)
151 j = 512;
152 for (i = 0; i < j; i++)
154 if (i == len1 + align1)
155 p1[i] = 0;
156 else
158 p1[i] = random () & 255;
159 if (i >= align1 && i < len1 + align1 && !p1[i])
160 p1[i] = (random () & 127) + 3;
163 for (i = 0; i < len2; i++)
165 buf1[i] = random () & 255;
166 if (!buf1[i])
167 buf1[i] = (random () & 127) + 3;
169 buf1[len2] = 0;
171 FOR_EACH_IMPL (impl, 1)
173 memset (p2 - 64, '\1', align2 + 64);
174 memset (p2 + align2 + len2 + 1, '\1', 512 - align2 - len2 - 1);
175 memcpy (p2 + align2, buf1, len2 + 1);
176 res = CALL (impl, p2 + align2, p1 + align1);
177 if (res != p2 + align2)
179 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd %zd) %p != %p",
180 n, impl->name, align1, align2, len1, len2, res,
181 p2 + align2);
182 ret = 1;
184 for (j = 0; j < align2 + 64; ++j)
186 if (p2[j - 64] != '\1')
188 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd, %zd)",
189 n, impl->name, align1, align2, len1, len2);
190 ret = 1;
191 break;
194 if (memcmp (p2 + align2, buf1, len2))
196 error (0, 0, "Iteration %zd - garbage in string before, %s (%zd, %zd, %zd, %zd)",
197 n, impl->name, align1, align2, len1, len2);
198 ret = 1;
200 for (j = align2 + len1 + len2 + 1; j < 512; ++j)
202 if (p2[j] != '\1')
204 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd, %zd)",
205 n, impl->name, align1, align2, len1, len2);
206 ret = 1;
207 break;
210 if (memcmp (p1 + align1, p2 + align2 + len2, len1 + 1))
212 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd, %zd)",
213 n, impl->name, align1, align2, len1, len2);
214 ret = 1;
221 test_main (void)
223 size_t i;
225 test_init ();
227 printf ("%28s", "");
228 FOR_EACH_IMPL (impl, 0)
229 printf ("\t%s", impl->name);
230 putchar ('\n');
232 for (i = 0; i < 16; ++i)
234 do_test (0, 0, i, i, 127);
235 do_test (0, 0, i, i, 255);
236 do_test (0, i, i, i, 127);
237 do_test (i, 0, i, i, 255);
240 for (i = 1; i < 8; ++i)
242 do_test (0, 0, 8 << i, 8 << i, 127);
243 do_test (8 - i, 2 * i, 8 << i, 8 << i, 127);
244 do_test (0, 0, 8 << i, 2 << i, 127);
245 do_test (8 - i, 2 * i, 8 << i, 2 << i, 127);
248 for (i = 1; i < 8; ++i)
250 do_test (i, 2 * i, 8 << i, 1, 127);
251 do_test (2 * i, i, 8 << i, 1, 255);
252 do_test (i, i, 8 << i, 10, 127);
253 do_test (i, i, 8 << i, 10, 255);
256 do_random_tests ();
257 return ret;
260 #include "../test-skeleton.c"