1 /* Test and measure memcmp functions.
2 Copyright (C) 1999, 2002, 2003, 2005, 2011 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, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include "test-string.h"
25 # include <inttypes.h>
28 # define MEMCMP wmemcmp
29 # define MEMCPY wmemcpy
30 # define SIMPLE_MEMCMP simple_wmemcmp
32 # define UCHAR wchar_t
34 # define CHAR__MIN WCHAR_MIN
35 # define CHAR__MAX WCHAR_MAX
37 simple_wmemcmp (const wchar_t *s1, const wchar_t *s2, size_t n)
41 wmemcmp has to use SIGNED comparison for elements.
42 memcmp has to use UNSIGNED comparison for elemnts.
44 while (n-- && (ret = *s1 < *s2 ? -1 : *s1 == *s2 ? 0 : 1) == 0) {s1++; s2++;}
48 # define MEMCMP memcmp
49 # define MEMCPY memcpy
50 # define SIMPLE_MEMCMP simple_memcmp
53 # define UCHAR unsigned char
55 # define CHAR__MIN CHAR_MIN
56 # define CHAR__MAX CHAR_MAX
59 simple_memcmp (const char *s1, const char *s2, size_t n)
63 while (n-- && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
68 typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
70 IMPL (SIMPLE_MEMCMP, 0)
74 check_result (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t len,
77 int result = CALL (impl, s1, s2, len);
78 if ((exp_result == 0 && result != 0)
79 || (exp_result < 0 && result >= 0)
80 || (exp_result > 0 && result <= 0))
82 error (0, 0, "Wrong result in function %s %d %d", impl->name,
92 do_one_test (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t len,
95 if (check_result (impl, s1, s2, len, exp_result) < 0)
100 hp_timing_t start __attribute ((unused));
101 hp_timing_t stop __attribute ((unused));
102 hp_timing_t best_time = ~ (hp_timing_t) 0;
105 for (i = 0; i < 32; ++i)
107 HP_TIMING_NOW (start);
108 CALL (impl, s1, s2, len);
109 HP_TIMING_NOW (stop);
110 HP_TIMING_BEST (best_time, start, stop);
113 printf ("\t%zd", (size_t) best_time);
118 do_test (size_t align1, size_t align2, size_t len, int exp_result)
127 if (align1 + (len + 1) * CHARBYTES >= page_size)
131 if (align2 + (len + 1) * CHARBYTES >= page_size)
134 s1 = (CHAR *) (buf1 + align1);
135 s2 = (CHAR *) (buf2 + align2);
137 for (i = 0; i < len; i++)
138 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % CHAR__MAX;
142 s2[len - 1] -= exp_result;
145 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
147 FOR_EACH_IMPL (impl, 0)
148 do_one_test (impl, s1, s2, len, exp_result);
155 do_random_tests (void)
157 size_t i, j, n, align1, align2, pos, len;
160 UCHAR *p1 = (UCHAR *) (buf1 + page_size - 512 * CHARBYTES);
161 UCHAR *p2 = (UCHAR *) (buf2 + page_size - 512 * CHARBYTES);
163 for (n = 0; n < ITERATIONS; n++)
165 align1 = random () & 31;
167 align2 = random () & 31;
169 align2 = align1 + (random () & 24);
170 pos = random () & 511;
175 pos = 511 - j - (random () & 7);
176 len = random () & 511;
178 len = 511 - j - (random () & 7);
179 j = len + align1 + 64;
180 if (j > 512) j = 512;
181 for (i = 0; i < j; ++i)
182 p1[i] = random () & 255;
183 for (i = 0; i < j; ++i)
184 p2[i] = random () & 255;
188 MEMCPY ((CHAR *) p2 + align2, (const CHAR *) p1 + align1, len);
191 MEMCPY ((CHAR *) p2 + align2, (const CHAR *) p1 + align1, pos);
192 if (p2[align2 + pos] == p1[align1 + pos])
194 p2[align2 + pos] = random () & 255;
195 if (p2[align2 + pos] == p1[align1 + pos])
196 p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
199 if (p1[align1 + pos] < p2[align2 + pos])
205 FOR_EACH_IMPL (impl, 1)
207 r = CALL (impl, (CHAR *) p1 + align1, (const CHAR *) p2 + align2,
209 if ((r == 0 && result)
210 || (r < 0 && result >= 0)
211 || (r > 0 && result <= 0))
213 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
214 n, impl->name, align1 * CHARBYTES & 63, align2 * CHARBYTES & 63, len, pos, r, result, p1, p2);
224 CHAR s1[116], s2[116];
461 for (size_t i = 0; i < n; i++)
463 exp_result = SIMPLE_MEMCMP (s1 + i, s2 + i, n - i);
464 FOR_EACH_IMPL (impl, 0)
465 check_result (impl, s1 + i, s2 + i, n - i, exp_result);
479 FOR_EACH_IMPL (impl, 0)
480 printf ("\t%s", impl->name);
483 for (i = 1; i < 16; ++i)
485 do_test (i * CHARBYTES, i * CHARBYTES, i, 0);
486 do_test (i * CHARBYTES, i * CHARBYTES, i, 1);
487 do_test (i * CHARBYTES, i * CHARBYTES, i, -1);
490 for (i = 0; i < 16; ++i)
492 do_test (0, 0, i, 0);
493 do_test (0, 0, i, 1);
494 do_test (0, 0, i, -1);
497 for (i = 1; i < 10; ++i)
499 do_test (0, 0, 2 << i, 0);
500 do_test (0, 0, 2 << i, 1);
501 do_test (0, 0, 2 << i, -1);
502 do_test (0, 0, 16 << i, 0);
503 do_test ((8 - i) * CHARBYTES, (2 * i) * CHARBYTES, 16 << i, 0);
504 do_test (0, 0, 16 << i, 1);
505 do_test (0, 0, 16 << i, -1);
508 for (i = 1; i < 8; ++i)
510 do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 0);
511 do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 1);
512 do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, -1);
518 #include "../test-skeleton.c"