ldconfig: trace origin paths with -v
[glibc.git] / string / test-strcmp.c
blob8d4784de802615edc459562dfe22d2f4291562cb
1 /* Test and measure strcmp and wcscmp functions.
2 Copyright (C) 1999-2020 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, see
19 <https://www.gnu.org/licenses/>. */
21 #define TEST_MAIN
22 #ifdef WIDE
23 # define TEST_NAME "wcscmp"
24 #else
25 # define TEST_NAME "strcmp"
26 #endif
27 #include "test-string.h"
29 #ifdef WIDE
30 # include <wchar.h>
32 # define L(str) L##str
33 # define STRCMP wcscmp
34 # define STRCPY wcscpy
35 # define STRLEN wcslen
36 # define MEMCPY wmemcpy
37 # define SIMPLE_STRCMP simple_wcscmp
38 # define STUPID_STRCMP stupid_wcscmp
39 # define CHAR wchar_t
40 # define UCHAR wchar_t
41 # define CHARBYTES 4
42 # define CHARBYTESLOG 2
43 # define CHARALIGN __alignof__ (CHAR)
44 # define MIDCHAR 0x7fffffff
45 # define LARGECHAR 0xfffffffe
46 # define CHAR__MAX WCHAR_MAX
47 # define CHAR__MIN WCHAR_MIN
49 /* Wcscmp uses signed semantics for comparison, not unsigned */
50 /* Avoid using substraction since possible overflow */
52 int
53 simple_wcscmp (const wchar_t *s1, const wchar_t *s2)
55 wchar_t c1, c2;
58 c1 = *s1++;
59 c2 = *s2++;
60 if (c2 == L'\0')
61 return c1 - c2;
63 while (c1 == c2);
65 return c1 < c2 ? -1 : 1;
68 int
69 stupid_wcscmp (const wchar_t *s1, const wchar_t *s2)
71 size_t ns1 = wcslen (s1) + 1;
72 size_t ns2 = wcslen (s2) + 1;
73 size_t n = ns1 < ns2 ? ns1 : ns2;
74 int ret = 0;
76 wchar_t c1, c2;
78 while (n--) {
79 c1 = *s1++;
80 c2 = *s2++;
81 if ((ret = c1 < c2 ? -1 : c1 == c2 ? 0 : 1) != 0)
82 break;
84 return ret;
87 #else
88 # include <limits.h>
90 # define L(str) str
91 # define STRCMP strcmp
92 # define STRCPY strcpy
93 # define STRLEN strlen
94 # define MEMCPY memcpy
95 # define SIMPLE_STRCMP simple_strcmp
96 # define STUPID_STRCMP stupid_strcmp
97 # define CHAR char
98 # define UCHAR unsigned char
99 # define CHARBYTES 1
100 # define CHARBYTESLOG 0
101 # define CHARALIGN 1
102 # define MIDCHAR 0x7f
103 # define LARGECHAR 0xfe
104 # define CHAR__MAX CHAR_MAX
105 # define CHAR__MIN CHAR_MIN
107 /* Strcmp uses unsigned semantics for comparison. */
109 simple_strcmp (const char *s1, const char *s2)
111 int ret;
113 while ((ret = *(unsigned char *) s1 - *(unsigned char*) s2++) == 0 && *s1++);
114 return ret;
118 stupid_strcmp (const char *s1, const char *s2)
120 size_t ns1 = strlen (s1) + 1;
121 size_t ns2 = strlen (s2) + 1;
122 size_t n = ns1 < ns2 ? ns1 : ns2;
123 int ret = 0;
125 while (n--)
126 if ((ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) != 0)
127 break;
128 return ret;
130 #endif
132 typedef int (*proto_t) (const CHAR *, const CHAR *);
134 IMPL (STUPID_STRCMP, 1)
135 IMPL (SIMPLE_STRCMP, 1)
136 IMPL (STRCMP, 1)
138 static int
139 check_result (impl_t *impl,
140 const CHAR *s1, const CHAR *s2,
141 int exp_result)
143 int result = CALL (impl, s1, s2);
144 if ((exp_result == 0 && result != 0)
145 || (exp_result < 0 && result >= 0)
146 || (exp_result > 0 && result <= 0))
148 error (0, 0, "Wrong result in function %s %d %d", impl->name,
149 result, exp_result);
150 ret = 1;
151 return -1;
154 return 0;
157 static void
158 do_one_test (impl_t *impl,
159 const CHAR *s1, const CHAR *s2,
160 int exp_result)
162 if (check_result (impl, s1, s2, exp_result) < 0)
163 return;
166 static void
167 do_test (size_t align1, size_t align2, size_t len, int max_char,
168 int exp_result)
170 size_t i;
172 CHAR *s1, *s2;
174 if (len == 0)
175 return;
177 align1 &= 63;
178 if (align1 + (len + 1) * CHARBYTES >= page_size)
179 return;
181 align2 &= 63;
182 if (align2 + (len + 1) * CHARBYTES >= page_size)
183 return;
185 /* Put them close to the end of page. */
186 i = align1 + CHARBYTES * (len + 2);
187 s1 = (CHAR *) (buf1 + ((page_size - i) / 16 * 16) + align1);
188 i = align2 + CHARBYTES * (len + 2);
189 s2 = (CHAR *) (buf2 + ((page_size - i) / 16 * 16) + align2);
191 for (i = 0; i < len; i++)
192 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
194 s1[len] = s2[len] = 0;
195 s1[len + 1] = 23;
196 s2[len + 1] = 24 + exp_result;
197 s2[len - 1] -= exp_result;
199 FOR_EACH_IMPL (impl, 0)
200 do_one_test (impl, s1, s2, exp_result);
203 static void
204 do_random_tests (void)
206 UCHAR *p1 = (UCHAR *) (buf1 + page_size - 512 * CHARBYTES);
207 UCHAR *p2 = (UCHAR *) (buf2 + page_size - 512 * CHARBYTES);
209 for (size_t n = 0; n < ITERATIONS; n++)
211 /* for wcscmp case align1 and align2 mean here alignment
212 in wchar_t symbols, it equal 4*k alignment in bytes, we
213 don't check other alignments like for example
214 p1 = (wchar_t *)(buf1 + 1)
215 because it's wrong using of wchar_t type. */
216 size_t align1 = random () & 31;
217 size_t align2;
218 if (random () & 1)
219 align2 = random () & 31;
220 else
221 align2 = align1 + (random () & 24);
222 size_t pos = random () & 511;
223 size_t j = align1 > align2 ? align1 : align2;
224 if (pos + j >= 511)
225 pos = 510 - j - (random () & 7);
226 size_t len1 = random () & 511;
227 if (pos >= len1 && (random () & 1))
228 len1 = pos + (random () & 7);
229 if (len1 + j >= 512)
230 len1 = 511 - j - (random () & 7);
231 size_t len2;
232 if (pos >= len1)
233 len2 = len1;
234 else
235 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
236 j = (pos > len2 ? pos : len2) + align1 + 64;
237 if (j > 512)
238 j = 512;
239 for (size_t i = 0; i < j; ++i)
241 p1[i] = random () & 255;
242 if (i < len1 + align1 && !p1[i])
244 p1[i] = random () & 255;
245 if (!p1[i])
246 p1[i] = 1 + (random () & 127);
249 for (size_t i = 0; i < j; ++i)
251 p2[i] = random () & 255;
252 if (i < len2 + align2 && !p2[i])
254 p2[i] = random () & 255;
255 if (!p2[i])
256 p2[i] = 1 + (random () & 127);
260 int result = 0;
261 MEMCPY (p2 + align2, p1 + align1, pos);
262 if (pos < len1)
264 if (p2[align2 + pos] == p1[align1 + pos])
266 p2[align2 + pos] = random () & 255;
267 if (p2[align2 + pos] == p1[align1 + pos])
268 p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
271 if (p1[align1 + pos] < p2[align2 + pos])
272 result = -1;
273 else
274 result = 1;
276 p1[len1 + align1] = 0;
277 p2[len2 + align2] = 0;
279 FOR_EACH_IMPL (impl, 1)
281 int r = CALL (impl, (CHAR *) (p1 + align1), (CHAR *) (p2 + align2));
282 /* Test whether on 64-bit architectures where ABI requires
283 callee to promote has the promotion been done. */
284 asm ("" : "=g" (r) : "0" (r));
285 if ((r == 0 && result)
286 || (r < 0 && result >= 0)
287 || (r > 0 && result <= 0))
289 error (0, 0, "Iteration %zd - wrong result in function %s (align in bytes: %zd, align in bytes: %zd, len1: %zd, len2: %zd, pos: %zd) %d != %d, p1 %p p2 %p",
290 n, impl->name, (size_t) (p1 + align1) & 63, (size_t) (p1 + align2) & 63, len1, len2, pos, r, result, p1, p2);
291 ret = 1;
297 static void
298 check (void)
300 CHAR *s1 = (CHAR *) (buf1 + 0xb2c);
301 CHAR *s2 = (CHAR *) (buf1 + 0xfd8);
303 STRCPY(s1, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs"));
304 STRCPY(s2, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV"));
306 /* Check correct working for negatives values */
308 s1[0] = 1;
309 s2[0] = 1;
310 s1[1] = 1;
311 s2[1] = 1;
312 s1[2] = -1;
313 s2[2] = 3;
314 s1[3] = 0;
315 s2[3] = -1;
317 /* Check possible overflow bug, actual more for wcscmp */
319 s1[7] = CHAR__MIN;
320 s2[7] = CHAR__MAX;
322 size_t l1 = STRLEN (s1);
323 size_t l2 = STRLEN (s2);
325 for (size_t i1 = 0; i1 < l1; i1++)
326 for (size_t i2 = 0; i2 < l2; i2++)
328 int exp_result = SIMPLE_STRCMP (s1 + i1, s2 + i2);
329 FOR_EACH_IMPL (impl, 0)
330 check_result (impl, s1 + i1, s2 + i2, exp_result);
333 /* Test cases where there are multiple zero bytes after the first. */
335 for (size_t i = 0; i < 16 + 1; i++)
337 s1[i] = 0x00;
338 s2[i] = 0x00;
341 for (size_t i = 0; i < 16; i++)
343 int exp_result;
345 for (int val = 0x01; val < 0x100; val++)
347 for (size_t j = 0; j < i; j++)
349 s1[j] = val;
350 s2[j] = val;
353 s2[i] = val;
355 exp_result = SIMPLE_STRCMP (s1, s2);
356 FOR_EACH_IMPL (impl, 0)
357 check_result (impl, s1, s2, exp_result);
364 test_main (void)
366 size_t i;
368 test_init ();
369 check();
371 printf ("%23s", "");
372 FOR_EACH_IMPL (impl, 0)
373 printf ("\t%s", impl->name);
374 putchar ('\n');
376 for (i = 1; i < 32; ++i)
378 do_test (CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 0);
379 do_test (CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 1);
380 do_test (CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, -1);
383 for (i = 1; i < 10 + CHARBYTESLOG; ++i)
385 do_test (0, 0, 2 << i, MIDCHAR, 0);
386 do_test (0, 0, 2 << i, LARGECHAR, 0);
387 do_test (0, 0, 2 << i, MIDCHAR, 1);
388 do_test (0, 0, 2 << i, LARGECHAR, 1);
389 do_test (0, 0, 2 << i, MIDCHAR, -1);
390 do_test (0, 0, 2 << i, LARGECHAR, -1);
391 do_test (0, CHARBYTES * i, 2 << i, MIDCHAR, 1);
392 do_test (CHARBYTES * i, CHARBYTES * (i + 1), 2 << i, LARGECHAR, 1);
395 for (i = 1; i < 8; ++i)
397 do_test (CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 0);
398 do_test (2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 0);
399 do_test (CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 1);
400 do_test (2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 1);
401 do_test (CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, -1);
402 do_test (2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, -1);
405 do_random_tests ();
406 return ret;
409 #include <support/test-driver.c>