Fix jn precision
[glibc.git] / string / test-strcmp.c
blobb8d85dca79cad04d2f3f8c7f2745e5a03696f52c
1 /* Test and measure STRCMP functions.
2 Copyright (C) 1999, 2002, 2003, 2005, 2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
5 Added wcscmp support by Liubov Dmitrieva <liubov.dmitrieva@gmail.com>, 2011.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #define TEST_MAIN
23 #include "test-string.h"
25 #ifdef WIDE
26 # include <inttypes.h>
27 # include <wchar.h>
29 # define L(str) L##str
30 # define STRCMP wcscmp
31 # define STRCPY wcscpy
32 # define STRLEN wcslen
33 # define MEMCPY wmemcpy
34 # define SIMPLE_STRCMP simple_wcscmp
35 # define STUPID_STRCMP stupid_wcscmp
36 # define CHAR wchar_t
37 # define UCHAR uint32_t
38 # define CHARBYTES 4
39 # define CHARBYTESLOG 2
40 # define CHARALIGN __alignof__ (CHAR)
41 # define MIDCHAR 0x7fffffff
42 # define LARGECHAR 0xfffffffe
43 #else
44 # define L(str) str
45 # define STRCMP strcmp
46 # define STRCPY strcpy
47 # define STRLEN strlen
48 # define MEMCPY memcpy
49 # define SIMPLE_STRCMP simple_strcmp
50 # define STUPID_STRCMP stupid_strcmp
51 # define CHAR char
52 # define UCHAR unsigned char
53 # define CHARBYTES 1
54 # define CHARBYTESLOG 0
55 # define CHARALIGN 1
56 # define MIDCHAR 0x7f
57 # define LARGECHAR 0xfe
58 #endif
59 typedef int (*proto_t) (const CHAR *, const CHAR *);
61 int
62 SIMPLE_STRCMP (const CHAR *s1, const CHAR *s2)
64 int ret;
66 while ((ret = *(UCHAR *) s1 - *(UCHAR *) s2++) == 0 && *s1++);
67 return ret;
70 int
71 STUPID_STRCMP (const CHAR *s1, const CHAR *s2)
73 size_t ns1 = STRLEN (s1) + 1;
74 size_t ns2 = STRLEN (s2) + 1;
75 size_t n = ns1 < ns2 ? ns1 : ns2;
76 int ret = 0;
78 while (n--)
79 if ((ret = *(UCHAR *) s1++ - *(UCHAR *) s2++) != 0)
80 break;
81 return ret;
84 IMPL (STUPID_STRCMP, 1)
85 IMPL (SIMPLE_STRCMP, 1)
86 IMPL (STRCMP, 1)
88 static int
89 check_result (impl_t *impl,
90 const CHAR *s1, const CHAR *s2,
91 int exp_result)
93 int result = CALL (impl, s1, s2);
94 if ((exp_result == 0 && result != 0)
95 || (exp_result < 0 && result >= 0)
96 || (exp_result > 0 && result <= 0))
98 error (0, 0, "Wrong result in function %s %d %d", impl->name,
99 result, exp_result);
100 ret = 1;
101 return -1;
104 return 0;
107 static void
108 do_one_test (impl_t *impl,
109 const CHAR *s1, const CHAR *s2,
110 int exp_result)
112 if (check_result (impl, s1, s2, exp_result) < 0)
113 return;
115 if (HP_TIMING_AVAIL)
117 hp_timing_t start __attribute ((unused));
118 hp_timing_t stop __attribute ((unused));
119 hp_timing_t best_time = ~ (hp_timing_t) 0;
120 size_t i;
122 for (i = 0; i < 32; ++i)
124 HP_TIMING_NOW (start);
125 CALL (impl, s1, s2);
126 HP_TIMING_NOW (stop);
127 HP_TIMING_BEST (best_time, start, stop);
130 printf ("\t%zd", (size_t) best_time);
134 static void
135 do_test (size_t align1, size_t align2, size_t len, int max_char,
136 int exp_result)
138 size_t i;
140 CHAR *s1, *s2;
142 if (len == 0)
143 return;
145 align1 &= 63;
146 if (align1 + (len + 1) * CHARBYTES >= page_size)
147 return;
149 align2 &= 63;
150 if (align2 + (len + 1) * CHARBYTES >= page_size)
151 return;
153 /* Put them close to the end of page. */
154 i = align1 + CHARBYTES * (len + 2);
155 s1 = (CHAR *) (buf1 + ((page_size - i) / 16 * 16) + align1);
156 i = align2 + CHARBYTES * (len + 2);
157 s2 = (CHAR *) (buf2 + ((page_size - i) / 16 * 16) + align2);
159 for (i = 0; i < len; i++)
160 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
162 s1[len] = s2[len] = 0;
163 s1[len + 1] = 23;
164 s2[len + 1] = 24 + exp_result;
165 s2[len - 1] -= exp_result;
167 if (HP_TIMING_AVAIL)
168 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
170 FOR_EACH_IMPL (impl, 0)
171 do_one_test (impl, s1, s2, exp_result);
173 if (HP_TIMING_AVAIL)
174 putchar ('\n');
177 static void
178 do_random_tests (void)
180 for (size_t a = 0; a < CHARBYTES; a += CHARALIGN)
181 for (size_t b = 0; b < CHARBYTES; b += CHARALIGN)
183 UCHAR *p1 = (UCHAR *) (buf1 + page_size - 512 * CHARBYTES - a);
184 UCHAR *p2 = (UCHAR *) (buf2 + page_size - 512 * CHARBYTES - b);
186 for (size_t n = 0; n < ITERATIONS; n++)
188 size_t align1 = random () & 31;
189 size_t align2;
190 if (random () & 1)
191 align2 = random () & 31;
192 else
193 align2 = align1 + (random () & 24);
194 size_t pos = random () & 511;
195 size_t j = align1 > align2 ? align1 : align2;
196 if (pos + j >= 511)
197 pos = 510 - j - (random () & 7);
198 size_t len1 = random () & 511;
199 if (pos >= len1 && (random () & 1))
200 len1 = pos + (random () & 7);
201 if (len1 + j >= 512)
202 len1 = 511 - j - (random () & 7);
203 size_t len2;
204 if (pos >= len1)
205 len2 = len1;
206 else
207 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
208 j = (pos > len2 ? pos : len2) + align1 + 64;
209 if (j > 512)
210 j = 512;
211 for (size_t i = 0; i < j; ++i)
213 p1[i] = random () & 255;
214 if (i < len1 + align1 && !p1[i])
216 p1[i] = random () & 255;
217 if (!p1[i])
218 p1[i] = 1 + (random () & 127);
221 for (size_t i = 0; i < j; ++i)
223 p2[i] = random () & 255;
224 if (i < len2 + align2 && !p2[i])
226 p2[i] = random () & 255;
227 if (!p2[i])
228 p2[i] = 1 + (random () & 127);
232 int result = 0;
233 MEMCPY ((CHAR *) (p2 + align2), (CHAR *) (p1 + align1), pos);
234 if (pos < len1)
236 if (p2[align2 + pos] == p1[align1 + pos])
238 p2[align2 + pos] = random () & 255;
239 if (p2[align2 + pos] == p1[align1 + pos])
240 p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
243 if (p1[align1 + pos] < p2[align2 + pos])
244 result = -1;
245 else
246 result = 1;
248 p1[len1 + align1] = 0;
249 p2[len2 + align2] = 0;
251 FOR_EACH_IMPL (impl, 1)
253 int r = CALL (impl, (CHAR *) (p1 + align1), (CHAR *) (p2 + align2));
254 /* Test whether on 64-bit architectures where ABI requires
255 callee to promote has the promotion been done. */
256 asm ("" : "=g" (r) : "0" (r));
257 if ((r == 0 && result)
258 || (r < 0 && result >= 0)
259 || (r > 0 && result <= 0))
261 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %d != %d, p1 %p p2 %p",
262 n, impl->name, (size_t) (p1 + align1) & 63, (size_t) (p1 + align2) & 63, len1, len2, pos, r, result, p1, p2);
263 ret = 1;
270 static void
271 check (void)
273 CHAR *s1 = (CHAR *) (buf1 + 0xb2c);
274 CHAR *s2 = (CHAR *) (buf1 + 0xfd8);
276 STRCPY(s1, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs"));
277 STRCPY(s2, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV"));
279 size_t l1 = STRLEN (s1);
280 size_t l2 = STRLEN (s2);
281 for (size_t i1 = 0; i1 < l1; i1++)
282 for (size_t i2 = 0; i2 < l2; i2++)
284 int exp_result = SIMPLE_STRCMP (s1 + i1, s2 + i2);
285 FOR_EACH_IMPL (impl, 0)
286 check_result (impl, s1 + i1, s2 + i2, exp_result);
292 test_main (void)
294 size_t i;
296 test_init ();
297 check();
299 printf ("%23s", "");
300 FOR_EACH_IMPL (impl, 0)
301 printf ("\t%s", impl->name);
302 putchar ('\n');
304 for (i = 1; i < 32; ++i)
306 do_test (CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 0);
307 do_test (CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 1);
308 do_test (CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, -1);
311 for (i = 1; i < 10 + CHARBYTESLOG; ++i)
313 do_test (0, 0, 2 << i, MIDCHAR, 0);
314 do_test (0, 0, 2 << i, LARGECHAR, 0);
315 do_test (0, 0, 2 << i, MIDCHAR, 1);
316 do_test (0, 0, 2 << i, LARGECHAR, 1);
317 do_test (0, 0, 2 << i, MIDCHAR, -1);
318 do_test (0, 0, 2 << i, LARGECHAR, -1);
319 do_test (0, CHARBYTES * i, 2 << i, MIDCHAR, 1);
320 do_test (CHARBYTES * i, CHARBYTES * (i + 1), 2 << i, LARGECHAR, 1);
323 for (i = 1; i < 8; ++i)
325 do_test (CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 0);
326 do_test (2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 0);
327 do_test (CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 1);
328 do_test (2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 1);
329 do_test (CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, -1);
330 do_test (2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, -1);
333 do_random_tests ();
334 return ret;
337 #include "../test-skeleton.c"