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/>. */
23
# define TEST_NAME "wmemcmp"
25
# define TEST_NAME "memcmp"
27
#include "test-string.h"
29
# include <inttypes.h>
32
# define MEMCMP wmemcmp
33
# define MEMCPY wmemcpy
34
# define SIMPLE_MEMCMP simple_wmemcmp
36
# define UCHAR wchar_t
38
# define CHAR__MIN WCHAR_MIN
39
# define CHAR__MAX WCHAR_MAX
41
simple_wmemcmp (const wchar_t *s1, const wchar_t *s2, size_t n)
45
wmemcmp has to use SIGNED comparison for elements.
46
memcmp has to use UNSIGNED comparison for elemnts.
48
while (n-- && (ret = *s1 < *s2 ? -1 : *s1 == *s2 ? 0 : 1) == 0) {s1++; s2++;}
54
# define MEMCMP memcmp
55
# define MEMCPY memcpy
56
# define SIMPLE_MEMCMP simple_memcmp
59
# define UCHAR unsigned char
61
# define CHAR__MIN CHAR_MIN
62
# define CHAR__MAX CHAR_MAX
65
simple_memcmp (const char *s1, const char *s2, size_t n)
69
while (n-- && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
74
typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
76
IMPL (SIMPLE_MEMCMP, 0)
80
check_result (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t len,
83
int result = CALL (impl, s1, s2, len);
84
if ((exp_result == 0 && result != 0)
85
|| (exp_result < 0 && result >= 0)
86
|| (exp_result > 0 && result <= 0))
88
error (0, 0, "Wrong result in function %s %d %d", impl->name,
98
do_one_test (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t len,
101
if (check_result (impl, s1, s2, len, exp_result) < 0)
106
hp_timing_t start __attribute ((unused));
107
hp_timing_t stop __attribute ((unused));
108
hp_timing_t best_time = ~ (hp_timing_t) 0;
111
for (i = 0; i < 32; ++i)
113
HP_TIMING_NOW (start);
114
CALL (impl, s1, s2, len);
115
HP_TIMING_NOW (stop);
116
HP_TIMING_BEST (best_time, start, stop);
119
printf ("\t%zd", (size_t) best_time);
124
do_test (size_t align1, size_t align2, size_t len, int exp_result)
133
if (align1 + (len + 1) * CHARBYTES >= page_size)
137
if (align2 + (len + 1) * CHARBYTES >= page_size)
140
s1 = (CHAR *) (buf1 + align1);
141
s2 = (CHAR *) (buf2 + align2);
143
for (i = 0; i < len; i++)
144
s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % CHAR__MAX;
148
s2[len - 1] -= exp_result;
151
printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
153
FOR_EACH_IMPL (impl, 0)
154
do_one_test (impl, s1, s2, len, exp_result);
161
do_random_tests (void)
163
size_t i, j, n, align1, align2, pos, len;
166
UCHAR *p1 = (UCHAR *) (buf1 + page_size - 512 * CHARBYTES);
167
UCHAR *p2 = (UCHAR *) (buf2 + page_size - 512 * CHARBYTES);
169
for (n = 0; n < ITERATIONS; n++)
171
align1 = random () & 31;
173
align2 = random () & 31;
175
align2 = align1 + (random () & 24);
176
pos = random () & 511;
181
pos = 511 - j - (random () & 7);
182
len = random () & 511;
184
len = 511 - j - (random () & 7);
185
j = len + align1 + 64;
186
if (j > 512) j = 512;
187
for (i = 0; i < j; ++i)
188
p1[i] = random () & 255;
189
for (i = 0; i < j; ++i)
190
p2[i] = random () & 255;
194
MEMCPY ((CHAR *) p2 + align2, (const CHAR *) p1 + align1, len);
197
MEMCPY ((CHAR *) p2 + align2, (const CHAR *) p1 + align1, pos);
198
if (p2[align2 + pos] == p1[align1 + pos])
200
p2[align2 + pos] = random () & 255;
201
if (p2[align2 + pos] == p1[align1 + pos])
202
p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
205
if (p1[align1 + pos] < p2[align2 + pos])
211
FOR_EACH_IMPL (impl, 1)
213
r = CALL (impl, (CHAR *) p1 + align1, (const CHAR *) p2 + align2,
215
if ((r == 0 && result)
216
|| (r < 0 && result >= 0)
217
|| (r > 0 && result <= 0))
219
error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
220
n, impl->name, align1 * CHARBYTES & 63, align2 * CHARBYTES & 63, len, pos, r, result, p1, p2);
230
CHAR s1[116], s2[116];
467
for (size_t i = 0; i < n; i++)
469
exp_result = SIMPLE_MEMCMP (s1 + i, s2 + i, n - i);
470
FOR_EACH_IMPL (impl, 0)
471
check_result (impl, s1 + i, s2 + i, n - i, exp_result);
485
FOR_EACH_IMPL (impl, 0)
486
printf ("\t%s", impl->name);
489
for (i = 1; i < 16; ++i)
491
do_test (i * CHARBYTES, i * CHARBYTES, i, 0);
492
do_test (i * CHARBYTES, i * CHARBYTES, i, 1);
493
do_test (i * CHARBYTES, i * CHARBYTES, i, -1);
496
for (i = 0; i < 16; ++i)
498
do_test (0, 0, i, 0);
499
do_test (0, 0, i, 1);
500
do_test (0, 0, i, -1);
503
for (i = 1; i < 10; ++i)
505
do_test (0, 0, 2 << i, 0);
506
do_test (0, 0, 2 << i, 1);
507
do_test (0, 0, 2 << i, -1);
508
do_test (0, 0, 16 << i, 0);
509
do_test ((8 - i) * CHARBYTES, (2 * i) * CHARBYTES, 16 << i, 0);
510
do_test (0, 0, 16 << i, 1);
511
do_test (0, 0, 16 << i, -1);
514
for (i = 1; i < 8; ++i)
516
do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 0);
517
do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 1);
518
do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, -1);
524
#include "test-skeleton.c"