]>
Commit | Line | Data |
---|---|---|
58ef9ef7 | 1 | /* Test and measure strcmp functions. |
9fb0cae8 | 2 | Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc. |
58ef9ef7 RM |
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 | |
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. */ | |
20 | ||
21 | #define TEST_MAIN | |
22 | #include "test-string.h" | |
23 | ||
24 | typedef int (*proto_t) (const char *, const char *); | |
25 | int simple_strcmp (const char *, const char *); | |
e8c1660f | 26 | int stupid_strcmp (const char *, const char *); |
58ef9ef7 | 27 | |
e8c1660f | 28 | IMPL (stupid_strcmp, 0) |
58ef9ef7 RM |
29 | IMPL (simple_strcmp, 0) |
30 | IMPL (strcmp, 1) | |
31 | ||
32 | int | |
33 | simple_strcmp (const char *s1, const char *s2) | |
34 | { | |
35 | int ret; | |
36 | ||
37 | while ((ret = *(unsigned char *) s1 - *(unsigned char *) s2++) == 0 | |
38 | && *s1++); | |
39 | return ret; | |
40 | } | |
41 | ||
e8c1660f RM |
42 | int |
43 | stupid_strcmp (const char *s1, const char *s2) | |
44 | { | |
45 | size_t ns1 = strlen (s1) + 1, ns2 = strlen (s2) + 1; | |
46 | size_t n = ns1 < ns2 ? ns1 : ns2; | |
47 | int ret = 0; | |
48 | ||
49 | while (n--) | |
50 | if ((ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) != 0) | |
51 | break; | |
52 | return ret; | |
53 | } | |
54 | ||
58ef9ef7 RM |
55 | static void |
56 | do_one_test (impl_t *impl, const char *s1, const char *s2, int exp_result) | |
57 | { | |
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)) | |
62 | { | |
63 | error (0, 0, "Wrong result in function %s %d %d", impl->name, | |
64 | result, exp_result); | |
65 | ret = 1; | |
66 | return; | |
67 | } | |
68 | ||
69 | if (HP_TIMING_AVAIL) | |
70 | { | |
71 | hp_timing_t start, stop, best_time = ~ (hp_timing_t) 0; | |
72 | size_t i; | |
73 | ||
74 | for (i = 0; i < 32; ++i) | |
75 | { | |
76 | HP_TIMING_NOW (start); | |
77 | CALL (impl, s1, s2); | |
78 | HP_TIMING_NOW (stop); | |
79 | HP_TIMING_BEST (best_time, start, stop); | |
80 | } | |
81 | ||
82 | printf ("\t%zd", (size_t) best_time); | |
83 | } | |
84 | } | |
85 | ||
86 | static void | |
87 | do_test (size_t align1, size_t align2, size_t len, int max_char, | |
88 | int exp_result) | |
89 | { | |
90 | size_t i; | |
91 | char *s1, *s2; | |
92 | ||
93 | if (len == 0) | |
94 | return; | |
95 | ||
96 | align1 &= 7; | |
97 | if (align1 + len + 1 >= page_size) | |
98 | return; | |
99 | ||
100 | align2 &= 7; | |
101 | if (align2 + len + 1 >= page_size) | |
102 | return; | |
103 | ||
104 | s1 = buf1 + align1; | |
105 | s2 = buf2 + align2; | |
106 | ||
107 | for (i = 0; i < len; i++) | |
108 | s1[i] = s2[i] = 1 + 23 * i % max_char; | |
109 | ||
110 | s1[len] = s2[len] = 0; | |
111 | s1[len + 1] = 23; | |
112 | s2[len + 1] = 24 + exp_result; | |
113 | s2[len - 1] -= exp_result; | |
114 | ||
115 | if (HP_TIMING_AVAIL) | |
116 | printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2); | |
117 | ||
118 | FOR_EACH_IMPL (impl, 0) | |
119 | do_one_test (impl, s1, s2, exp_result); | |
120 | ||
121 | if (HP_TIMING_AVAIL) | |
122 | putchar ('\n'); | |
123 | } | |
124 | ||
125 | static void | |
126 | do_random_tests (void) | |
127 | { | |
e8c1660f | 128 | size_t i, j, n, align1, align2, pos, len1, len2; |
9fb0cae8 RM |
129 | int result; |
130 | long r; | |
58ef9ef7 RM |
131 | unsigned char *p1 = buf1 + page_size - 512; |
132 | unsigned char *p2 = buf2 + page_size - 512; | |
133 | ||
134 | for (n = 0; n < ITERATIONS; n++) | |
135 | { | |
136 | align1 = random () & 31; | |
137 | if (random () & 1) | |
138 | align2 = random () & 31; | |
139 | else | |
140 | align2 = align1 + (random () & 24); | |
141 | pos = random () & 511; | |
e8c1660f | 142 | j = align1 > align2 ? align1 : align2; |
58ef9ef7 RM |
143 | if (pos + j >= 511) |
144 | pos = 510 - j - (random () & 7); | |
e8c1660f RM |
145 | len1 = random () & 511; |
146 | if (pos >= len1 && (random () & 1)) | |
147 | len1 = pos + (random () & 7); | |
148 | if (len1 + j >= 512) | |
149 | len1 = 511 - j - (random () & 7); | |
150 | if (pos >= len1) | |
151 | len2 = len1; | |
152 | else | |
153 | len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0); | |
154 | j = (pos > len2 ? pos : len2) + align1 + 64; | |
155 | if (j > 512) | |
156 | j = 512; | |
58ef9ef7 RM |
157 | for (i = 0; i < j; ++i) |
158 | { | |
159 | p1[i] = random () & 255; | |
e8c1660f | 160 | if (i < len1 + align1 && !p1[i]) |
58ef9ef7 RM |
161 | { |
162 | p1[i] = random () & 255; | |
163 | if (!p1[i]) | |
164 | p1[i] = 1 + (random () & 127); | |
165 | } | |
166 | } | |
167 | for (i = 0; i < j; ++i) | |
168 | { | |
169 | p2[i] = random () & 255; | |
e8c1660f | 170 | if (i < len2 + align2 && !p2[i]) |
58ef9ef7 RM |
171 | { |
172 | p2[i] = random () & 255; | |
173 | if (!p2[i]) | |
174 | p2[i] = 1 + (random () & 127); | |
175 | } | |
176 | } | |
177 | ||
178 | result = 0; | |
179 | memcpy (p2 + align2, p1 + align1, pos); | |
e8c1660f | 180 | if (pos < len1) |
58ef9ef7 RM |
181 | { |
182 | if (p2[align2 + pos] == p1[align1 + pos]) | |
183 | { | |
184 | p2[align2 + pos] = random () & 255; | |
185 | if (p2[align2 + pos] == p1[align1 + pos]) | |
186 | p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127); | |
187 | } | |
188 | ||
189 | if (p1[align1 + pos] < p2[align2 + pos]) | |
190 | result = -1; | |
191 | else | |
192 | result = 1; | |
193 | } | |
e8c1660f RM |
194 | p1[len1 + align1] = 0; |
195 | p2[len2 + align2] = 0; | |
58ef9ef7 RM |
196 | |
197 | FOR_EACH_IMPL (impl, 1) | |
198 | { | |
199 | r = CALL (impl, p1 + align1, p2 + align2); | |
9fb0cae8 RM |
200 | /* Test whether on 64-bit architectures where ABI requires |
201 | callee to promote has the promotion been done. */ | |
202 | asm ("" : "=g" (r) : "0" (r)); | |
58ef9ef7 RM |
203 | if ((r == 0 && result) |
204 | || (r < 0 && result >= 0) | |
205 | || (r > 0 && result <= 0)) | |
206 | { | |
9fb0cae8 | 207 | error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p", |
e8c1660f | 208 | n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2); |
58ef9ef7 RM |
209 | ret = 1; |
210 | } | |
211 | } | |
212 | } | |
213 | } | |
214 | ||
215 | int | |
216 | test_main (void) | |
217 | { | |
218 | size_t i; | |
219 | ||
220 | test_init (); | |
221 | ||
222 | printf ("%23s", ""); | |
223 | FOR_EACH_IMPL (impl, 0) | |
224 | printf ("\t%s", impl->name); | |
225 | putchar ('\n'); | |
226 | ||
227 | for (i = 1; i < 16; ++i) | |
228 | { | |
229 | do_test (i, i, i, 127, 0); | |
230 | do_test (i, i, i, 127, 1); | |
231 | do_test (i, i, i, 127, -1); | |
232 | } | |
233 | ||
234 | for (i = 1; i < 10; ++i) | |
235 | { | |
236 | do_test (0, 0, 2 << i, 127, 0); | |
237 | do_test (0, 0, 2 << i, 254, 0); | |
238 | do_test (0, 0, 2 << i, 127, 1); | |
239 | do_test (0, 0, 2 << i, 254, 1); | |
240 | do_test (0, 0, 2 << i, 127, -1); | |
241 | do_test (0, 0, 2 << i, 254, -1); | |
242 | } | |
243 | ||
244 | for (i = 1; i < 8; ++i) | |
245 | { | |
246 | do_test (i, 2 * i, 8 << i, 127, 0); | |
247 | do_test (2 * i, i, 8 << i, 254, 0); | |
248 | do_test (i, 2 * i, 8 << i, 127, 1); | |
249 | do_test (2 * i, i, 8 << i, 254, 1); | |
250 | do_test (i, 2 * i, 8 << i, 127, -1); | |
251 | do_test (2 * i, i, 8 << i, 254, -1); | |
252 | } | |
253 | ||
254 | do_random_tests (); | |
255 | return ret; | |
256 | } | |
257 | ||
258 | #include "../test-skeleton.c" |