Hurd: readlinkat
[glibc.git] / string / test-memcmp.c
blob69076d00beb8849b8685c6a064b313059c3fc98a
1 /* Test and measure memcmp functions.
2 Copyright (C) 1999-2012 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 wmemcmp 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 <http://www.gnu.org/licenses/>. */
21 #define TEST_MAIN
22 #include "test-string.h"
23 #ifdef WIDE
24 # include <inttypes.h>
25 # include <wchar.h>
27 # define MEMCMP wmemcmp
28 # define MEMCPY wmemcpy
29 # define SIMPLE_MEMCMP simple_wmemcmp
30 # define CHAR wchar_t
31 # define UCHAR wchar_t
32 # define CHARBYTES 4
33 # define CHAR__MIN WCHAR_MIN
34 # define CHAR__MAX WCHAR_MAX
35 int
36 simple_wmemcmp (const wchar_t *s1, const wchar_t *s2, size_t n)
38 int ret = 0;
39 /* Warning!
40 wmemcmp has to use SIGNED comparison for elements.
41 memcmp has to use UNSIGNED comparison for elemnts.
43 while (n-- && (ret = *s1 < *s2 ? -1 : *s1 == *s2 ? 0 : 1) == 0) {s1++; s2++;}
44 return ret;
46 #else
47 # include <limits.h>
49 # define MEMCMP memcmp
50 # define MEMCPY memcpy
51 # define SIMPLE_MEMCMP simple_memcmp
52 # define CHAR char
53 # define MAX_CHAR 255
54 # define UCHAR unsigned char
55 # define CHARBYTES 1
56 # define CHAR__MIN CHAR_MIN
57 # define CHAR__MAX CHAR_MAX
59 int
60 simple_memcmp (const char *s1, const char *s2, size_t n)
62 int ret = 0;
64 while (n-- && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
65 return ret;
67 #endif
69 typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
71 IMPL (SIMPLE_MEMCMP, 0)
72 IMPL (MEMCMP, 1)
74 static int
75 check_result (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t len,
76 int exp_result)
78 int result = CALL (impl, s1, s2, len);
79 if ((exp_result == 0 && result != 0)
80 || (exp_result < 0 && result >= 0)
81 || (exp_result > 0 && result <= 0))
83 error (0, 0, "Wrong result in function %s %d %d", impl->name,
84 result, exp_result);
85 ret = 1;
86 return -1;
89 return 0;
92 static void
93 do_one_test (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t len,
94 int exp_result)
96 if (check_result (impl, s1, s2, len, exp_result) < 0)
97 return;
99 if (HP_TIMING_AVAIL)
101 hp_timing_t start __attribute ((unused));
102 hp_timing_t stop __attribute ((unused));
103 hp_timing_t best_time = ~ (hp_timing_t) 0;
104 size_t i;
106 for (i = 0; i < 32; ++i)
108 HP_TIMING_NOW (start);
109 CALL (impl, s1, s2, len);
110 HP_TIMING_NOW (stop);
111 HP_TIMING_BEST (best_time, start, stop);
114 printf ("\t%zd", (size_t) best_time);
118 static void
119 do_test (size_t align1, size_t align2, size_t len, int exp_result)
121 size_t i;
122 CHAR *s1, *s2;
124 if (len == 0)
125 return;
127 align1 &= 63;
128 if (align1 + (len + 1) * CHARBYTES >= page_size)
129 return;
131 align2 &= 63;
132 if (align2 + (len + 1) * CHARBYTES >= page_size)
133 return;
135 s1 = (CHAR *) (buf1 + align1);
136 s2 = (CHAR *) (buf2 + align2);
138 for (i = 0; i < len; i++)
139 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % CHAR__MAX;
141 s1[len] = align1;
142 s2[len] = align2;
143 s2[len - 1] -= exp_result;
145 if (HP_TIMING_AVAIL)
146 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
148 FOR_EACH_IMPL (impl, 0)
149 do_one_test (impl, s1, s2, len, exp_result);
151 if (HP_TIMING_AVAIL)
152 putchar ('\n');
155 static void
156 do_random_tests (void)
158 size_t i, j, n, align1, align2, pos, len;
159 int result;
160 long r;
161 UCHAR *p1 = (UCHAR *) (buf1 + page_size - 512 * CHARBYTES);
162 UCHAR *p2 = (UCHAR *) (buf2 + page_size - 512 * CHARBYTES);
164 for (n = 0; n < ITERATIONS; n++)
166 align1 = random () & 31;
167 if (random () & 1)
168 align2 = random () & 31;
169 else
170 align2 = align1 + (random () & 24);
171 pos = random () & 511;
172 j = align1;
173 if (align2 > j)
174 j = align2;
175 if (pos + j >= 512)
176 pos = 511 - j - (random () & 7);
177 len = random () & 511;
178 if (len + j >= 512)
179 len = 511 - j - (random () & 7);
180 j = len + align1 + 64;
181 if (j > 512) j = 512;
182 for (i = 0; i < j; ++i)
183 p1[i] = random () & 255;
184 for (i = 0; i < j; ++i)
185 p2[i] = random () & 255;
187 result = 0;
188 if (pos >= len)
189 MEMCPY ((CHAR *) p2 + align2, (const CHAR *) p1 + align1, len);
190 else
192 MEMCPY ((CHAR *) p2 + align2, (const CHAR *) p1 + align1, pos);
193 if (p2[align2 + pos] == p1[align1 + pos])
195 p2[align2 + pos] = random () & 255;
196 if (p2[align2 + pos] == p1[align1 + pos])
197 p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
200 if (p1[align1 + pos] < p2[align2 + pos])
201 result = -1;
202 else
203 result = 1;
206 FOR_EACH_IMPL (impl, 1)
208 r = CALL (impl, (CHAR *) p1 + align1, (const CHAR *) p2 + align2,
209 len);
210 if ((r == 0 && result)
211 || (r < 0 && result >= 0)
212 || (r > 0 && result <= 0))
214 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
215 n, impl->name, align1 * CHARBYTES & 63, align2 * CHARBYTES & 63, len, pos, r, result, p1, p2);
216 ret = 1;
222 static void
223 check1 (void)
225 CHAR s1[116], s2[116];
226 int n, exp_result;
228 s1[0] = -108;
229 s2[0] = -108;
230 s1[1] = 99;
231 s2[1] = 99;
232 s1[2] = -113;
233 s2[2] = -113;
234 s1[3] = 1;
235 s2[3] = 1;
236 s1[4] = 116;
237 s2[4] = 116;
238 s1[5] = 99;
239 s2[5] = 99;
240 s1[6] = -113;
241 s2[6] = -113;
242 s1[7] = 1;
243 s2[7] = 1;
244 s1[8] = 84;
245 s2[8] = 84;
246 s1[9] = 99;
247 s2[9] = 99;
248 s1[10] = -113;
249 s2[10] = -113;
250 s1[11] = 1;
251 s2[11] = 1;
252 s1[12] = 52;
253 s2[12] = 52;
254 s1[13] = 99;
255 s2[13] = 99;
256 s1[14] = -113;
257 s2[14] = -113;
258 s1[15] = 1;
259 s2[15] = 1;
260 s1[16] = -76;
261 s2[16] = -76;
262 s1[17] = -14;
263 s2[17] = -14;
264 s1[18] = -109;
265 s2[18] = -109;
266 s1[19] = 1;
267 s2[19] = 1;
268 s1[20] = -108;
269 s2[20] = -108;
270 s1[21] = -14;
271 s2[21] = -14;
272 s1[22] = -109;
273 s2[22] = -109;
274 s1[23] = 1;
275 s2[23] = 1;
276 s1[24] = 84;
277 s2[24] = 84;
278 s1[25] = -15;
279 s2[25] = -15;
280 s1[26] = -109;
281 s2[26] = -109;
282 s1[27] = 1;
283 s2[27] = 1;
284 s1[28] = 52;
285 s2[28] = 52;
286 s1[29] = -15;
287 s2[29] = -15;
288 s1[30] = -109;
289 s2[30] = -109;
290 s1[31] = 1;
291 s2[31] = 1;
292 s1[32] = 20;
293 s2[32] = 20;
294 s1[33] = -15;
295 s2[33] = -15;
296 s1[34] = -109;
297 s2[34] = -109;
298 s1[35] = 1;
299 s2[35] = 1;
300 s1[36] = 20;
301 s2[36] = 20;
302 s1[37] = -14;
303 s2[37] = -14;
304 s1[38] = -109;
305 s2[38] = -109;
306 s1[39] = 1;
307 s2[39] = 1;
308 s1[40] = 52;
309 s2[40] = 52;
310 s1[41] = -14;
311 s2[41] = -14;
312 s1[42] = -109;
313 s2[42] = -109;
314 s1[43] = 1;
315 s2[43] = 1;
316 s1[44] = 84;
317 s2[44] = 84;
318 s1[45] = -14;
319 s2[45] = -14;
320 s1[46] = -109;
321 s2[46] = -109;
322 s1[47] = 1;
323 s2[47] = 1;
324 s1[48] = 116;
325 s2[48] = 116;
326 s1[49] = -14;
327 s2[49] = -14;
328 s1[50] = -109;
329 s2[50] = -109;
330 s1[51] = 1;
331 s2[51] = 1;
332 s1[52] = 116;
333 s2[52] = 116;
334 s1[53] = -15;
335 s2[53] = -15;
336 s1[54] = -109;
337 s2[54] = -109;
338 s1[55] = 1;
339 s2[55] = 1;
340 s1[56] = -44;
341 s2[56] = -44;
342 s1[57] = -14;
343 s2[57] = -14;
344 s1[58] = -109;
345 s2[58] = -109;
346 s1[59] = 1;
347 s2[59] = 1;
348 s1[60] = -108;
349 s2[60] = -108;
350 s1[61] = -15;
351 s2[61] = -15;
352 s1[62] = -109;
353 s2[62] = -109;
354 s1[63] = 1;
355 s2[63] = 1;
356 s1[64] = -76;
357 s2[64] = -76;
358 s1[65] = -15;
359 s2[65] = -15;
360 s1[66] = -109;
361 s2[66] = -109;
362 s1[67] = 1;
363 s2[67] = 1;
364 s1[68] = -44;
365 s2[68] = -44;
366 s1[69] = -15;
367 s2[69] = -15;
368 s1[70] = -109;
369 s2[70] = -109;
370 s1[71] = 1;
371 s2[71] = 1;
372 s1[72] = -12;
373 s2[72] = -12;
374 s1[73] = -15;
375 s2[73] = -15;
376 s1[74] = -109;
377 s2[74] = -109;
378 s1[75] = 1;
379 s2[75] = 1;
380 s1[76] = -12;
381 s2[76] = -12;
382 s1[77] = -14;
383 s2[77] = -14;
384 s1[78] = -109;
385 s2[78] = -109;
386 s1[79] = 1;
387 s2[79] = 1;
388 s1[80] = 20;
389 s2[80] = -68;
390 s1[81] = -12;
391 s2[81] = 64;
392 s1[82] = -109;
393 s2[82] = -106;
394 s1[83] = 1;
395 s2[83] = 1;
396 s1[84] = -12;
397 s2[84] = -12;
398 s1[85] = -13;
399 s2[85] = -13;
400 s1[86] = -109;
401 s2[86] = -109;
402 s1[87] = 1;
403 s2[87] = 1;
404 s1[88] = -44;
405 s2[88] = -44;
406 s1[89] = -13;
407 s2[89] = -13;
408 s1[90] = -109;
409 s2[90] = -109;
410 s1[91] = 1;
411 s2[91] = 1;
412 s1[92] = -76;
413 s2[92] = -76;
414 s1[93] = -13;
415 s2[93] = -13;
416 s1[94] = -109;
417 s2[94] = -109;
418 s1[95] = 1;
419 s2[95] = 1;
420 s1[96] = -108;
421 s2[96] = -108;
422 s1[97] = -13;
423 s2[97] = -13;
424 s1[98] = -109;
425 s2[98] = -109;
426 s1[99] = 1;
427 s2[99] = 1;
428 s1[100] = 116;
429 s2[100] = 116;
430 s1[101] = CHAR__MIN;
431 s2[101] = CHAR__MAX;
432 s1[102] = -109;
433 s2[102] = -109;
434 s1[103] = 1;
435 s2[103] = 1;
436 s1[104] = 84;
437 s2[104] = 84;
438 s1[105] = -13;
439 s2[105] = -13;
440 s1[106] = -109;
441 s2[106] = -109;
442 s1[107] = 1;
443 s2[107] = 1;
444 s1[108] = 52;
445 s2[108] = 52;
446 s1[109] = -13;
447 s2[109] = -13;
448 s1[110] = -109;
449 s2[110] = -109;
450 s1[111] = 1;
451 s2[111] = 1;
452 s1[112] = CHAR__MAX;
453 s2[112] = CHAR__MIN;
454 s1[113] = -13;
455 s2[113] = -13;
456 s1[114] = -109;
457 s2[114] = -109;
458 s1[115] = 1;
459 s2[115] = 1;
461 n = 116;
462 for (size_t i = 0; i < n; i++)
464 exp_result = SIMPLE_MEMCMP (s1 + i, s2 + i, n - i);
465 FOR_EACH_IMPL (impl, 0)
466 check_result (impl, s1 + i, s2 + i, n - i, exp_result);
471 test_main (void)
473 size_t i;
475 test_init ();
477 check1 ();
479 printf ("%23s", "");
480 FOR_EACH_IMPL (impl, 0)
481 printf ("\t%s", impl->name);
482 putchar ('\n');
484 for (i = 1; i < 16; ++i)
486 do_test (i * CHARBYTES, i * CHARBYTES, i, 0);
487 do_test (i * CHARBYTES, i * CHARBYTES, i, 1);
488 do_test (i * CHARBYTES, i * CHARBYTES, i, -1);
491 for (i = 0; i < 16; ++i)
493 do_test (0, 0, i, 0);
494 do_test (0, 0, i, 1);
495 do_test (0, 0, i, -1);
498 for (i = 1; i < 10; ++i)
500 do_test (0, 0, 2 << i, 0);
501 do_test (0, 0, 2 << i, 1);
502 do_test (0, 0, 2 << i, -1);
503 do_test (0, 0, 16 << i, 0);
504 do_test ((8 - i) * CHARBYTES, (2 * i) * CHARBYTES, 16 << i, 0);
505 do_test (0, 0, 16 << i, 1);
506 do_test (0, 0, 16 << i, -1);
509 for (i = 1; i < 8; ++i)
511 do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 0);
512 do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 1);
513 do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, -1);
516 do_random_tests ();
517 return ret;
519 #include "../test-skeleton.c"