2.9
[glibc/nacl-glibc.git] / string / test-strcmp.c
blob769e9828fddb8b019ec4f6c3d80cb37e77a0f490
1 /* Test and measure strcmp 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, 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 *);
25 int simple_strcmp (const char *, const char *);
26 int stupid_strcmp (const char *, const char *);
28 IMPL (stupid_strcmp, 0)
29 IMPL (simple_strcmp, 0)
30 IMPL (strcmp, 1)
32 int
33 simple_strcmp (const char *s1, const char *s2)
35 int ret;
37 while ((ret = *(unsigned char *) s1 - *(unsigned char *) s2++) == 0
38 && *s1++);
39 return ret;
42 int
43 stupid_strcmp (const char *s1, const char *s2)
45 size_t ns1 = strlen (s1) + 1, ns2 = strlen (s2) + 1;
46 size_t n = ns1 < ns2 ? ns1 : ns2;
47 int ret = 0;
49 while (n--)
50 if ((ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) != 0)
51 break;
52 return ret;
55 static void
56 do_one_test (impl_t *impl, const char *s1, const char *s2, int exp_result)
58 int result = CALL (impl, s1, s2);
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);
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, int max_char,
90 int exp_result)
92 size_t i;
93 char *s1, *s2;
95 if (len == 0)
96 return;
98 align1 &= 7;
99 if (align1 + len + 1 >= page_size)
100 return;
102 align2 &= 7;
103 if (align2 + len + 1 >= page_size)
104 return;
106 s1 = (char *) (buf1 + align1);
107 s2 = (char *) (buf2 + align2);
109 for (i = 0; i < len; i++)
110 s1[i] = s2[i] = 1 + 23 * i % max_char;
112 s1[len] = s2[len] = 0;
113 s1[len + 1] = 23;
114 s2[len + 1] = 24 + exp_result;
115 s2[len - 1] -= exp_result;
117 if (HP_TIMING_AVAIL)
118 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
120 FOR_EACH_IMPL (impl, 0)
121 do_one_test (impl, s1, s2, exp_result);
123 if (HP_TIMING_AVAIL)
124 putchar ('\n');
127 static void
128 do_random_tests (void)
130 size_t i, j, n, align1, align2, pos, len1, len2;
131 int result;
132 long r;
133 unsigned char *p1 = buf1 + page_size - 512;
134 unsigned char *p2 = buf2 + page_size - 512;
136 for (n = 0; n < ITERATIONS; n++)
138 align1 = random () & 31;
139 if (random () & 1)
140 align2 = random () & 31;
141 else
142 align2 = align1 + (random () & 24);
143 pos = random () & 511;
144 j = align1 > align2 ? align1 : align2;
145 if (pos + j >= 511)
146 pos = 510 - j - (random () & 7);
147 len1 = random () & 511;
148 if (pos >= len1 && (random () & 1))
149 len1 = pos + (random () & 7);
150 if (len1 + j >= 512)
151 len1 = 511 - j - (random () & 7);
152 if (pos >= len1)
153 len2 = len1;
154 else
155 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
156 j = (pos > len2 ? pos : len2) + align1 + 64;
157 if (j > 512)
158 j = 512;
159 for (i = 0; i < j; ++i)
161 p1[i] = random () & 255;
162 if (i < len1 + align1 && !p1[i])
164 p1[i] = random () & 255;
165 if (!p1[i])
166 p1[i] = 1 + (random () & 127);
169 for (i = 0; i < j; ++i)
171 p2[i] = random () & 255;
172 if (i < len2 + align2 && !p2[i])
174 p2[i] = random () & 255;
175 if (!p2[i])
176 p2[i] = 1 + (random () & 127);
180 result = 0;
181 memcpy (p2 + align2, p1 + align1, pos);
182 if (pos < len1)
184 if (p2[align2 + pos] == p1[align1 + pos])
186 p2[align2 + pos] = random () & 255;
187 if (p2[align2 + pos] == p1[align1 + pos])
188 p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
191 if (p1[align1 + pos] < p2[align2 + pos])
192 result = -1;
193 else
194 result = 1;
196 p1[len1 + align1] = 0;
197 p2[len2 + align2] = 0;
199 FOR_EACH_IMPL (impl, 1)
201 r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2));
202 /* Test whether on 64-bit architectures where ABI requires
203 callee to promote has the promotion been done. */
204 asm ("" : "=g" (r) : "0" (r));
205 if ((r == 0 && result)
206 || (r < 0 && result >= 0)
207 || (r > 0 && result <= 0))
209 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
210 n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2);
211 ret = 1;
218 test_main (void)
220 size_t i;
222 test_init ();
224 printf ("%23s", "");
225 FOR_EACH_IMPL (impl, 0)
226 printf ("\t%s", impl->name);
227 putchar ('\n');
229 for (i = 1; i < 16; ++i)
231 do_test (i, i, i, 127, 0);
232 do_test (i, i, i, 127, 1);
233 do_test (i, i, i, 127, -1);
236 for (i = 1; i < 10; ++i)
238 do_test (0, 0, 2 << i, 127, 0);
239 do_test (0, 0, 2 << i, 254, 0);
240 do_test (0, 0, 2 << i, 127, 1);
241 do_test (0, 0, 2 << i, 254, 1);
242 do_test (0, 0, 2 << i, 127, -1);
243 do_test (0, 0, 2 << i, 254, -1);
246 for (i = 1; i < 8; ++i)
248 do_test (i, 2 * i, 8 << i, 127, 0);
249 do_test (2 * i, i, 8 << i, 254, 0);
250 do_test (i, 2 * i, 8 << i, 127, 1);
251 do_test (2 * i, i, 8 << i, 254, 1);
252 do_test (i, 2 * i, 8 << i, 127, -1);
253 do_test (2 * i, i, 8 << i, 254, -1);
256 do_random_tests ();
257 return ret;
260 #include "../test-skeleton.c"