Fix typo.
[glibc.git] / string / test-strncmp.c
blob5adf0eb311e4151ff0afac7a4232116aa0f603c6
1 /* Test and measure strncmp functions.
2 Copyright (C) 1999, 2002, 2003 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 int (*proto_t) (const char *, const char *, size_t);
25 int simple_strncmp (const char *, const char *, size_t);
26 int stupid_strncmp (const char *, const char *, size_t);
28 IMPL (stupid_strncmp, 0)
29 IMPL (simple_strncmp, 0)
30 IMPL (strncmp, 1)
32 int
33 simple_strncmp (const char *s1, const char *s2, size_t n)
35 int ret = 0;
37 while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0
38 && *s1++);
39 return ret;
42 int
43 stupid_strncmp (const char *s1, const char *s2, size_t n)
45 size_t ns1 = strnlen (s1, n) + 1, ns2 = strnlen (s2, n) + 1;
46 int ret = 0;
48 n = ns1 < n ? ns1 : n;
49 n = ns2 < n ? ns2 : n;
50 while (n-- && (ret = *(unsigned char *) s1++ - * (unsigned char *) s2++) == 0);
51 return ret;
54 static void
55 do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n,
56 int exp_result)
58 int result = CALL (impl, s1, s2, n);
59 if ((exp_result == 0 && result != 0)
60 || (exp_result < 0 && result >= 0)
61 || (exp_result > 0 && result <= 0))
63 error (0, 0, "Wrong result in function %s %d %d", impl->name,
64 result, exp_result);
65 ret = 1;
66 return;
69 if (HP_TIMING_AVAIL)
71 hp_timing_t start __attribute ((unused));
72 hp_timing_t stop __attribute ((unused));
73 hp_timing_t best_time = ~ (hp_timing_t) 0;
74 size_t i;
76 for (i = 0; i < 32; ++i)
78 HP_TIMING_NOW (start);
79 CALL (impl, s1, s2, n);
80 HP_TIMING_NOW (stop);
81 HP_TIMING_BEST (best_time, start, stop);
84 printf ("\t%zd", (size_t) best_time);
88 static void
89 do_test_limit (size_t align1, size_t align2, size_t len, size_t n, int max_char,
90 int exp_result)
92 size_t i, align_n;
93 char *s1, *s2;
95 if (n == 0)
97 s1 = (char*)(buf1 + page_size);
98 s2 = (char*)(buf2 + page_size);
99 if (HP_TIMING_AVAIL)
100 printf ("Length %4zd/%4zd:", len, n);
102 FOR_EACH_IMPL (impl, 0)
103 do_one_test (impl, s1, s2, n, 0);
105 if (HP_TIMING_AVAIL)
106 putchar ('\n');
108 return;
111 align1 &= 15;
112 align2 &= 15;
113 align_n = (page_size - n) & 15;
115 s1 = (char*)(buf1 + page_size - n);
116 s2 = (char*)(buf2 + page_size - n);
118 if (align1 < align_n)
119 s1 -= (align_n - align1);
121 if (align2 < align_n)
122 s2 -= (align_n - align2);
124 for (i = 0; i < n; i++)
125 s1[i] = s2[i] = 1 + 23 * i % max_char;
127 if (len < n)
129 s1[len] = 0;
130 s2[len] = 0;
131 if (exp_result < 0)
132 s2[len] = 32;
133 else if (exp_result > 0)
134 s1[len] = 64;
137 if (HP_TIMING_AVAIL)
138 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len, n, align1, align2);
140 FOR_EACH_IMPL (impl, 0)
141 do_one_test (impl, s1, s2, n, exp_result);
143 if (HP_TIMING_AVAIL)
144 putchar ('\n');
147 static void
148 do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char,
149 int exp_result)
151 size_t i;
152 char *s1, *s2;
154 if (n == 0)
155 return;
157 align1 &= 7;
158 if (align1 + n + 1 >= page_size)
159 return;
161 align2 &= 7;
162 if (align2 + n + 1 >= page_size)
163 return;
165 s1 = (char*)(buf1 + align1);
166 s2 = (char*)(buf2 + align2);
168 for (i = 0; i < n; i++)
169 s1[i] = s2[i] = 1 + 23 * i % max_char;
171 s1[n] = 24 + exp_result;
172 s2[n] = 23;
173 s1[len] = 0;
174 s2[len] = 0;
175 if (exp_result < 0)
176 s2[len] = 32;
177 else if (exp_result > 0)
178 s1[len] = 64;
179 if (len >= n)
180 s2[n - 1] -= exp_result;
182 if (HP_TIMING_AVAIL)
183 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len, n, align1, align2);
185 FOR_EACH_IMPL (impl, 0)
186 do_one_test (impl, (char*)s1, (char*)s2, n, exp_result);
188 if (HP_TIMING_AVAIL)
189 putchar ('\n');
192 static void
193 do_random_tests (void)
195 size_t i, j, n, align1, align2, pos, len1, len2, size;
196 int result;
197 long r;
198 unsigned char *p1 = buf1 + page_size - 512;
199 unsigned char *p2 = buf2 + page_size - 512;
201 for (n = 0; n < ITERATIONS; n++)
203 align1 = random () & 31;
204 if (random () & 1)
205 align2 = random () & 31;
206 else
207 align2 = align1 + (random () & 24);
208 pos = random () & 511;
209 size = random () & 511;
210 j = align1 > align2 ? align1 : align2;
211 if (pos + j >= 511)
212 pos = 510 - j - (random () & 7);
213 len1 = random () & 511;
214 if (pos >= len1 && (random () & 1))
215 len1 = pos + (random () & 7);
216 if (len1 + j >= 512)
217 len1 = 511 - j - (random () & 7);
218 if (pos >= len1)
219 len2 = len1;
220 else
221 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
222 j = (pos > len2 ? pos : len2) + align1 + 64;
223 if (j > 512)
224 j = 512;
225 for (i = 0; i < j; ++i)
227 p1[i] = random () & 255;
228 if (i < len1 + align1 && !p1[i])
230 p1[i] = random () & 255;
231 if (!p1[i])
232 p1[i] = 1 + (random () & 127);
235 for (i = 0; i < j; ++i)
237 p2[i] = random () & 255;
238 if (i < len2 + align2 && !p2[i])
240 p2[i] = random () & 255;
241 if (!p2[i])
242 p2[i] = 1 + (random () & 127);
246 result = 0;
247 memcpy (p2 + align2, p1 + align1, pos);
248 if (pos < len1)
250 if (p2[align2 + pos] == p1[align1 + pos])
252 p2[align2 + pos] = random () & 255;
253 if (p2[align2 + pos] == p1[align1 + pos])
254 p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
257 if (pos < size)
259 if (p1[align1 + pos] < p2[align2 + pos])
260 result = -1;
261 else
262 result = 1;
265 p1[len1 + align1] = 0;
266 p2[len2 + align2] = 0;
268 FOR_EACH_IMPL (impl, 1)
270 r = CALL (impl, (char*)(p1 + align1), (char*)(p2 + align2), size);
271 /* Test whether on 64-bit architectures where ABI requires
272 callee to promote has the promotion been done. */
273 asm ("" : "=g" (r) : "0" (r));
274 if ((r == 0 && result)
275 || (r < 0 && result >= 0)
276 || (r > 0 && result <= 0))
278 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
279 n, impl->name, align1, align2, len1, len2, pos, size, r, result, p1, p2);
280 ret = 1;
287 test_main (void)
289 size_t i;
291 test_init ();
293 printf ("%23s", "");
294 FOR_EACH_IMPL (impl, 0)
295 printf ("\t%s", impl->name);
296 putchar ('\n');
298 for (i =0; i < 16; ++i)
300 do_test (0, 0, 8, i, 127, 0);
301 do_test (0, 0, 8, i, 127, -1);
302 do_test (0, 0, 8, i, 127, 1);
303 do_test (i, i, 8, i, 127, 0);
304 do_test (i, i, 8, i, 127, 1);
305 do_test (i, i, 8, i, 127, -1);
306 do_test (i, 2 * i, 8, i, 127, 0);
307 do_test (2 * i, i, 8, i, 127, 1);
308 do_test (i, 3 * i, 8, i, 127, -1);
309 do_test (0, 0, 8, i, 255, 0);
310 do_test (0, 0, 8, i, 255, -1);
311 do_test (0, 0, 8, i, 255, 1);
312 do_test (i, i, 8, i, 255, 0);
313 do_test (i, i, 8, i, 255, 1);
314 do_test (i, i, 8, i, 255, -1);
315 do_test (i, 2 * i, 8, i, 255, 0);
316 do_test (2 * i, i, 8, i, 255, 1);
317 do_test (i, 3 * i, 8, i, 255, -1);
320 for (i = 1; i < 8; ++i)
322 do_test (0, 0, 8 << i, 16 << i, 127, 0);
323 do_test (0, 0, 8 << i, 16 << i, 127, 1);
324 do_test (0, 0, 8 << i, 16 << i, 127, -1);
325 do_test (0, 0, 8 << i, 16 << i, 255, 0);
326 do_test (0, 0, 8 << i, 16 << i, 255, 1);
327 do_test (0, 0, 8 << i, 16 << i, 255, -1);
328 do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 0);
329 do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 1);
330 do_test (2 * i, i, 8 << i, 16 << i, 255, 0);
331 do_test (2 * i, i, 8 << i, 16 << i, 255, 1);
334 do_test_limit (0, 0, 0, 0, 127, 0);
335 do_test_limit (4, 0, 21, 20, 127, 0);
336 do_test_limit (0, 4, 21, 20, 127, 0);
337 do_test_limit (8, 0, 25, 24, 127, 0);
338 do_test_limit (0, 8, 25, 24, 127, 0);
340 for (i = 0; i < 8; ++i)
342 do_test_limit (0, 0, 17 - i, 16 - i, 127, 0);
343 do_test_limit (0, 0, 17 - i, 16 - i, 255, 0);
344 do_test_limit (0, 0, 15 - i, 16 - i, 127, 0);
345 do_test_limit (0, 0, 15 - i, 16 - i, 127, 1);
346 do_test_limit (0, 0, 15 - i, 16 - i, 127, -1);
347 do_test_limit (0, 0, 15 - i, 16 - i, 255, 0);
348 do_test_limit (0, 0, 15 - i, 16 - i, 255, 1);
349 do_test_limit (0, 0, 15 - i, 16 - i, 255, -1);
352 do_random_tests ();
353 return ret;
356 #include "../test-skeleton.c"