Set GL(dl_nns) to 1 for vDSO in libc.a
[glibc.git] / string / test-strcat.c
blob7dcd7bc5b97f3ddb35cd3a28f2d2efae3854cb40
1 /* Test and measure strcat functions.
2 Copyright (C) 1999, 2002, 2003, 2005 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, see
18 <http://www.gnu.org/licenses/>. */
20 #define TEST_MAIN
21 #include "test-string.h"
23 typedef char *(*proto_t) (char *, const char *);
24 char *simple_strcat (char *, const char *);
26 IMPL (simple_strcat, 0)
27 IMPL (strcat, 1)
29 char *
30 simple_strcat (char *dst, const char *src)
32 char *ret = dst;
33 while (*dst++ != '\0');
34 --dst;
35 while ((*dst++ = *src++) != '\0');
36 return ret;
39 static void
40 do_one_test (impl_t *impl, char *dst, const char *src)
42 size_t k = strlen (dst);
43 if (CALL (impl, dst, src) != dst)
45 error (0, 0, "Wrong result in function %s %p %p", impl->name,
46 CALL (impl, dst, src), dst);
47 ret = 1;
48 return;
51 if (strcmp (dst + k, src) != 0)
53 error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
54 impl->name, dst, src);
55 ret = 1;
56 return;
59 if (HP_TIMING_AVAIL)
61 hp_timing_t start __attribute ((unused));
62 hp_timing_t stop __attribute ((unused));
63 hp_timing_t best_time = ~ (hp_timing_t) 0;
64 size_t i;
66 for (i = 0; i < 32; ++i)
68 dst[k] = '\0';
69 HP_TIMING_NOW (start);
70 CALL (impl, dst, src);
71 HP_TIMING_NOW (stop);
72 HP_TIMING_BEST (best_time, start, stop);
75 printf ("\t%zd", (size_t) best_time);
79 static void
80 do_test (size_t align1, size_t align2, size_t len1, size_t len2, int max_char)
82 size_t i;
83 char *s1, *s2;
85 align1 &= 7;
86 if (align1 + len1 >= page_size)
87 return;
89 align2 &= 7;
90 if (align2 + len1 + len2 >= page_size)
91 return;
93 s1 = (char *) (buf1 + align1);
94 s2 = (char *) (buf2 + align2);
96 for (i = 0; i < len1; ++i)
97 s1[i] = 32 + 23 * i % (max_char - 32);
98 s1[len1] = '\0';
100 for (i = 0; i < len2; i++)
101 s2[i] = 32 + 23 * i % (max_char - 32);
103 if (HP_TIMING_AVAIL)
104 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len1, len2, align1, align2);
106 FOR_EACH_IMPL (impl, 0)
108 s2[len2] = '\0';
109 do_one_test (impl, s2, s1);
112 if (HP_TIMING_AVAIL)
113 putchar ('\n');
116 static void
117 do_random_tests (void)
119 size_t i, j, n, align1, align2, len1, len2;
120 unsigned char *p1 = buf1 + page_size - 512;
121 unsigned char *p2 = buf2 + page_size - 512;
122 unsigned char *res;
124 for (n = 0; n < ITERATIONS; n++)
126 align1 = random () & 31;
127 if (random () & 1)
128 align2 = random () & 31;
129 else
130 align2 = align1 + (random () & 24);
131 len1 = random () & 511;
132 if (len1 + align2 > 512)
133 len2 = random () & 7;
134 else
135 len2 = (512 - len1 - align2) * (random () & (1024 * 1024 - 1))
136 / (1024 * 1024);
137 j = align1;
138 if (align2 + len2 > j)
139 j = align2 + len2;
140 if (len1 + j >= 511)
141 len1 = 510 - j - (random () & 7);
142 if (len1 >= 512)
143 len1 = 0;
144 if (align1 + len1 < 512 - 8)
146 j = 510 - align1 - len1 - (random () & 31);
147 if (j > 0 && j < 512)
148 align1 += j;
150 j = len1 + align1 + 64;
151 if (j > 512)
152 j = 512;
153 for (i = 0; i < j; i++)
155 if (i == len1 + align1)
156 p1[i] = 0;
157 else
159 p1[i] = random () & 255;
160 if (i >= align1 && i < len1 + align1 && !p1[i])
161 p1[i] = (random () & 127) + 3;
164 for (i = 0; i < len2; i++)
166 buf1[i] = random () & 255;
167 if (!buf1[i])
168 buf1[i] = (random () & 127) + 3;
170 buf1[len2] = 0;
172 FOR_EACH_IMPL (impl, 1)
174 memset (p2 - 64, '\1', align2 + 64);
175 memset (p2 + align2 + len2 + 1, '\1', 512 - align2 - len2 - 1);
176 memcpy (p2 + align2, buf1, len2 + 1);
177 res = (unsigned char *) CALL (impl, (char *) (p2 + align2),
178 (char *) (p1 + align1));
179 if (res != p2 + align2)
181 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd %zd) %p != %p",
182 n, impl->name, align1, align2, len1, len2, res,
183 p2 + align2);
184 ret = 1;
186 for (j = 0; j < align2 + 64; ++j)
188 if (p2[j - 64] != '\1')
190 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd, %zd)",
191 n, impl->name, align1, align2, len1, len2);
192 ret = 1;
193 break;
196 if (memcmp (p2 + align2, buf1, len2))
198 error (0, 0, "Iteration %zd - garbage in string before, %s (%zd, %zd, %zd, %zd)",
199 n, impl->name, align1, align2, len1, len2);
200 ret = 1;
202 for (j = align2 + len1 + len2 + 1; j < 512; ++j)
204 if (p2[j] != '\1')
206 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd, %zd)",
207 n, impl->name, align1, align2, len1, len2);
208 ret = 1;
209 break;
212 if (memcmp (p1 + align1, p2 + align2 + len2, len1 + 1))
214 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd, %zd)",
215 n, impl->name, align1, align2, len1, len2);
216 ret = 1;
223 test_main (void)
225 size_t i;
227 test_init ();
229 printf ("%28s", "");
230 FOR_EACH_IMPL (impl, 0)
231 printf ("\t%s", impl->name);
232 putchar ('\n');
234 for (i = 0; i < 16; ++i)
236 do_test (0, 0, i, i, 127);
237 do_test (0, 0, i, i, 255);
238 do_test (0, i, i, i, 127);
239 do_test (i, 0, i, i, 255);
242 for (i = 1; i < 8; ++i)
244 do_test (0, 0, 8 << i, 8 << i, 127);
245 do_test (8 - i, 2 * i, 8 << i, 8 << i, 127);
246 do_test (0, 0, 8 << i, 2 << i, 127);
247 do_test (8 - i, 2 * i, 8 << i, 2 << i, 127);
250 for (i = 1; i < 8; ++i)
252 do_test (i, 2 * i, 8 << i, 1, 127);
253 do_test (2 * i, i, 8 << i, 1, 255);
254 do_test (i, i, 8 << i, 10, 127);
255 do_test (i, i, 8 << i, 10, 255);
258 do_random_tests ();
259 return ret;
262 #include "../test-skeleton.c"