add __NR_waitid defines for alpha and ia64
[glibc/history.git] / string / test-strncmp.c
blob431b64d6afb00cb0feb3aef96e1d00bfc6593947
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 (size_t align1, size_t align2, size_t len, size_t n, int max_char,
90 int exp_result)
92 size_t i;
93 char *s1, *s2;
95 if (n == 0)
96 return;
98 align1 &= 7;
99 if (align1 + n + 1 >= page_size)
100 return;
102 align2 &= 7;
103 if (align2 + n + 1 >= page_size)
104 return;
106 s1 = buf1 + align1;
107 s2 = buf2 + align2;
109 for (i = 0; i < n; i++)
110 s1[i] = s2[i] = 1 + 23 * i % max_char;
112 s1[n] = 24 + exp_result;
113 s2[n] = 23;
114 s1[len] = 0;
115 s2[len] = 0;
116 if (exp_result < 0)
117 s2[len] = 32;
118 else if (exp_result > 0)
119 s1[len] = 64;
120 if (len >= n)
121 s2[n - 1] -= exp_result;
123 if (HP_TIMING_AVAIL)
124 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len, n, align1, align2);
126 FOR_EACH_IMPL (impl, 0)
127 do_one_test (impl, s1, s2, n, exp_result);
129 if (HP_TIMING_AVAIL)
130 putchar ('\n');
133 static void
134 do_random_tests (void)
136 size_t i, j, n, align1, align2, pos, len1, len2, size;
137 int result;
138 long r;
139 unsigned char *p1 = buf1 + page_size - 512;
140 unsigned char *p2 = buf2 + page_size - 512;
142 for (n = 0; n < ITERATIONS; n++)
144 align1 = random () & 31;
145 if (random () & 1)
146 align2 = random () & 31;
147 else
148 align2 = align1 + (random () & 24);
149 pos = random () & 511;
150 size = random () & 511;
151 j = align1 > align2 ? align1 : align2;
152 if (pos + j >= 511)
153 pos = 510 - j - (random () & 7);
154 len1 = random () & 511;
155 if (pos >= len1 && (random () & 1))
156 len1 = pos + (random () & 7);
157 if (len1 + j >= 512)
158 len1 = 511 - j - (random () & 7);
159 if (pos >= len1)
160 len2 = len1;
161 else
162 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
163 j = (pos > len2 ? pos : len2) + align1 + 64;
164 if (j > 512)
165 j = 512;
166 for (i = 0; i < j; ++i)
168 p1[i] = random () & 255;
169 if (i < len1 + align1 && !p1[i])
171 p1[i] = random () & 255;
172 if (!p1[i])
173 p1[i] = 1 + (random () & 127);
176 for (i = 0; i < j; ++i)
178 p2[i] = random () & 255;
179 if (i < len2 + align2 && !p2[i])
181 p2[i] = random () & 255;
182 if (!p2[i])
183 p2[i] = 1 + (random () & 127);
187 result = 0;
188 memcpy (p2 + align2, p1 + align1, pos);
189 if (pos < len1)
191 if (p2[align2 + pos] == p1[align1 + pos])
193 p2[align2 + pos] = random () & 255;
194 if (p2[align2 + pos] == p1[align1 + pos])
195 p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
198 if (pos < size)
200 if (p1[align1 + pos] < p2[align2 + pos])
201 result = -1;
202 else
203 result = 1;
206 p1[len1 + align1] = 0;
207 p2[len2 + align2] = 0;
209 FOR_EACH_IMPL (impl, 1)
211 r = CALL (impl, p1 + align1, p2 + align2, size);
212 /* Test whether on 64-bit architectures where ABI requires
213 callee to promote has the promotion been done. */
214 asm ("" : "=g" (r) : "0" (r));
215 if ((r == 0 && result)
216 || (r < 0 && result >= 0)
217 || (r > 0 && result <= 0))
219 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
220 n, impl->name, align1, align2, len1, len2, pos, size, r, result, p1, p2);
221 ret = 1;
228 test_main (void)
230 size_t i;
232 test_init ();
234 printf ("%23s", "");
235 FOR_EACH_IMPL (impl, 0)
236 printf ("\t%s", impl->name);
237 putchar ('\n');
239 for (i =0; i < 16; ++i)
241 do_test (0, 0, 8, i, 127, 0);
242 do_test (0, 0, 8, i, 127, -1);
243 do_test (0, 0, 8, i, 127, 1);
244 do_test (i, i, 8, i, 127, 0);
245 do_test (i, i, 8, i, 127, 1);
246 do_test (i, i, 8, i, 127, -1);
247 do_test (i, 2 * i, 8, i, 127, 0);
248 do_test (2 * i, i, 8, i, 127, 1);
249 do_test (i, 3 * i, 8, i, 127, -1);
250 do_test (0, 0, 8, i, 255, 0);
251 do_test (0, 0, 8, i, 255, -1);
252 do_test (0, 0, 8, i, 255, 1);
253 do_test (i, i, 8, i, 255, 0);
254 do_test (i, i, 8, i, 255, 1);
255 do_test (i, i, 8, i, 255, -1);
256 do_test (i, 2 * i, 8, i, 255, 0);
257 do_test (2 * i, i, 8, i, 255, 1);
258 do_test (i, 3 * i, 8, i, 255, -1);
261 for (i = 1; i < 8; ++i)
263 do_test (0, 0, 8 << i, 16 << i, 127, 0);
264 do_test (0, 0, 8 << i, 16 << i, 127, 1);
265 do_test (0, 0, 8 << i, 16 << i, 127, -1);
266 do_test (0, 0, 8 << i, 16 << i, 255, 0);
267 do_test (0, 0, 8 << i, 16 << i, 255, 1);
268 do_test (0, 0, 8 << i, 16 << i, 255, -1);
269 do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 0);
270 do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 1);
271 do_test (2 * i, i, 8 << i, 16 << i, 255, 0);
272 do_test (2 * i, i, 8 << i, 16 << i, 255, 1);
275 do_random_tests ();
276 return ret;
279 #include "../test-skeleton.c"