]> sourceware.org Git - glibc.git/blame - string/test-strcasecmp.c
Hurd: Support --prefix=/usr special-casing for all GNU systems.
[glibc.git] / string / test-strcasecmp.c
CommitLineData
42e08a54
UD
1/* Test and measure strcasecmp functions.
2 Copyright (C) 1999, 2002, 2003, 2005, 2010 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
5
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.
10
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.
15
16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
42e08a54
UD
19
20#include <ctype.h>
21#define TEST_MAIN
22#include "test-string.h"
23
24typedef int (*proto_t) (const char *, const char *);
25static int simple_strcasecmp (const char *, const char *);
26static int stupid_strcasecmp (const char *, const char *);
27
28IMPL (stupid_strcasecmp, 0)
29IMPL (simple_strcasecmp, 0)
30IMPL (strcasecmp, 1)
31
32static int
33simple_strcasecmp (const char *s1, const char *s2)
34{
35 int ret;
36
37 while ((ret = ((unsigned char) tolower (*s1)
38 - (unsigned char) tolower (*s2))) == 0
39 && *s1++)
40 ++s2;
41 return ret;
42}
43
44static int
45stupid_strcasecmp (const char *s1, const char *s2)
46{
47 size_t ns1 = strlen (s1) + 1, ns2 = strlen (s2) + 1;
48 size_t n = ns1 < ns2 ? ns1 : ns2;
49 int ret = 0;
50
51 while (n--)
52 {
53 if ((ret = ((unsigned char) tolower (*s1)
54 - (unsigned char) tolower (*s2))) != 0)
55 break;
56 ++s1;
57 ++s2;
58 }
59 return ret;
60}
61
62static void
63do_one_test (impl_t *impl, const char *s1, const char *s2, int exp_result)
64{
65 int result = CALL (impl, s1, s2);
66 if ((exp_result == 0 && result != 0)
67 || (exp_result < 0 && result >= 0)
68 || (exp_result > 0 && result <= 0))
69 {
70 error (0, 0, "Wrong result in function %s %d %d", impl->name,
71 result, exp_result);
72 ret = 1;
73 return;
74 }
75
76 if (HP_TIMING_AVAIL)
77 {
78 hp_timing_t start __attribute ((unused));
79 hp_timing_t stop __attribute ((unused));
80 hp_timing_t best_time = ~ (hp_timing_t) 0;
81 size_t i;
82
83 for (i = 0; i < 32; ++i)
84 {
85 HP_TIMING_NOW (start);
86 CALL (impl, s1, s2);
87 HP_TIMING_NOW (stop);
88 HP_TIMING_BEST (best_time, start, stop);
89 }
90
91 printf ("\t%zd", (size_t) best_time);
92 }
93}
94
95static void
96do_test (size_t align1, size_t align2, size_t len, int max_char,
97 int exp_result)
98{
99 size_t i;
100 char *s1, *s2;
101
102 if (len == 0)
103 return;
104
105 align1 &= 7;
106 if (align1 + len + 1 >= page_size)
107 return;
108
109 align2 &= 7;
110 if (align2 + len + 1 >= page_size)
111 return;
112
113 s1 = (char *) (buf1 + align1);
114 s2 = (char *) (buf2 + align2);
115
116 for (i = 0; i < len; i++)
117 {
118 s1[i] = toupper (1 + 23 * i % max_char);
119 s2[i] = tolower (s1[i]);
120 }
121
122 s1[len] = s2[len] = 0;
123 s1[len + 1] = 23;
124 s2[len + 1] = 24 + exp_result;
125 if ((s2[len - 1] == 'z' && exp_result == -1)
126 || (s2[len - 1] == 'a' && exp_result == 1))
127 s1[len - 1] += exp_result;
128 else
129 s2[len - 1] -= exp_result;
130
131 if (HP_TIMING_AVAIL)
132 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
133
134 FOR_EACH_IMPL (impl, 0)
135 do_one_test (impl, s1, s2, exp_result);
136
137 if (HP_TIMING_AVAIL)
138 putchar ('\n');
139}
140
141static void
142do_random_tests (void)
143{
144 size_t i, j, n, align1, align2, pos, len1, len2;
145 int result;
146 long r;
147 unsigned char *p1 = buf1 + page_size - 512;
148 unsigned char *p2 = buf2 + page_size - 512;
149
150 for (n = 0; n < ITERATIONS; n++)
151 {
152 align1 = random () & 31;
153 if (random () & 1)
154 align2 = random () & 31;
155 else
156 align2 = align1 + (random () & 24);
157 pos = random () & 511;
158 j = align1 > align2 ? align1 : align2;
159 if (pos + j >= 511)
160 pos = 510 - j - (random () & 7);
161 len1 = random () & 511;
162 if (pos >= len1 && (random () & 1))
163 len1 = pos + (random () & 7);
164 if (len1 + j >= 512)
165 len1 = 511 - j - (random () & 7);
166 if (pos >= len1)
167 len2 = len1;
168 else
169 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
170 j = (pos > len2 ? pos : len2) + align1 + 64;
171 if (j > 512)
172 j = 512;
173 for (i = 0; i < j; ++i)
174 {
175 p1[i] = tolower (random () & 255);
176 if (i < len1 + align1 && !p1[i])
177 {
178 p1[i] = tolower (random () & 255);
179 if (!p1[i])
180 p1[i] = tolower (1 + (random () & 127));
181 }
182 }
183 for (i = 0; i < j; ++i)
184 {
185 p2[i] = toupper (random () & 255);
186 if (i < len2 + align2 && !p2[i])
187 {
188 p2[i] = toupper (random () & 255);
189 if (!p2[i])
190 toupper (p2[i] = 1 + (random () & 127));
191 }
192 }
193
194 result = 0;
195 memcpy (p2 + align2, p1 + align1, pos);
196 if (pos < len1)
197 {
198 if (tolower (p2[align2 + pos]) == p1[align1 + pos])
199 {
200 p2[align2 + pos] = toupper (random () & 255);
201 if (tolower (p2[align2 + pos]) == p1[align1 + pos])
202 p2[align2 + pos] = toupper (p1[align1 + pos]
203 + 3 + (random () & 127));
204 }
205
206 if (p1[align1 + pos] < tolower (p2[align2 + pos]))
207 result = -1;
208 else
209 result = 1;
210 }
211 p1[len1 + align1] = 0;
212 p2[len2 + align2] = 0;
213
214 FOR_EACH_IMPL (impl, 1)
215 {
216 r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2));
217 /* Test whether on 64-bit architectures where ABI requires
218 callee to promote has the promotion been done. */
219 asm ("" : "=g" (r) : "0" (r));
220 if ((r == 0 && result)
221 || (r < 0 && result >= 0)
222 || (r > 0 && result <= 0))
223 {
224 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
225 n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2);
226 ret = 1;
227 }
228 }
229 }
230}
231
232int
233test_main (void)
234{
235 size_t i;
236
237 test_init ();
238
239 printf ("%23s", "");
240 FOR_EACH_IMPL (impl, 0)
241 printf ("\t%s", impl->name);
242 putchar ('\n');
243
244 for (i = 1; i < 16; ++i)
245 {
246 do_test (i, i, i, 127, 0);
247 do_test (i, i, i, 127, 1);
248 do_test (i, i, i, 127, -1);
249 }
250
251 for (i = 1; i < 10; ++i)
252 {
253 do_test (0, 0, 2 << i, 127, 0);
254 do_test (0, 0, 2 << i, 254, 0);
255 do_test (0, 0, 2 << i, 127, 1);
256 do_test (0, 0, 2 << i, 254, 1);
257 do_test (0, 0, 2 << i, 127, -1);
258 do_test (0, 0, 2 << i, 254, -1);
259 }
260
261 for (i = 1; i < 8; ++i)
262 {
263 do_test (i, 2 * i, 8 << i, 127, 0);
264 do_test (2 * i, i, 8 << i, 254, 0);
265 do_test (i, 2 * i, 8 << i, 127, 1);
266 do_test (2 * i, i, 8 << i, 254, 1);
267 do_test (i, 2 * i, 8 << i, 127, -1);
268 do_test (2 * i, i, 8 << i, 254, -1);
269 }
270
271 do_random_tests ();
272 return ret;
273}
274
275#include "../test-skeleton.c"
This page took 0.08683 seconds and 5 git commands to generate.