1 /* Test and measure memcmp functions.
2 Copyright (C) 1999-2017 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 do_test (size_t align1
, size_t align2
, size_t len
, int exp_result
)
115 if (align1
+ (len
+ 1) * CHARBYTES
>= page_size
)
119 if (align2
+ (len
+ 1) * CHARBYTES
>= page_size
)
122 s1
= (CHAR
*) (buf1
+ align1
);
123 s2
= (CHAR
*) (buf2
+ align2
);
125 for (i
= 0; i
< len
; i
++)
126 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% CHAR__MAX
;
130 s2
[len
- 1] -= exp_result
;
132 FOR_EACH_IMPL (impl
, 0)
133 do_one_test (impl
, s1
, s2
, len
, exp_result
);
137 do_random_tests (void)
139 size_t i
, j
, n
, align1
, align2
, pos
, len
;
142 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
- 512 * CHARBYTES
);
143 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
- 512 * CHARBYTES
);
145 for (n
= 0; n
< ITERATIONS
; n
++)
147 align1
= random () & 31;
149 align2
= random () & 31;
151 align2
= align1
+ (random () & 24);
152 pos
= random () & 511;
157 pos
= 511 - j
- (random () & 7);
158 len
= random () & 511;
160 len
= 511 - j
- (random () & 7);
161 j
= len
+ align1
+ 64;
162 if (j
> 512) j
= 512;
163 for (i
= 0; i
< j
; ++i
)
164 p1
[i
] = random () & 255;
165 for (i
= 0; i
< j
; ++i
)
166 p2
[i
] = random () & 255;
170 MEMCPY ((CHAR
*) p2
+ align2
, (const CHAR
*) p1
+ align1
, len
);
173 MEMCPY ((CHAR
*) p2
+ align2
, (const CHAR
*) p1
+ align1
, pos
);
174 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
176 p2
[align2
+ pos
] = random () & 255;
177 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
178 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
181 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
187 FOR_EACH_IMPL (impl
, 1)
189 r
= CALL (impl
, (CHAR
*) p1
+ align1
, (const CHAR
*) p2
+ align2
,
191 if ((r
== 0 && result
)
192 || (r
< 0 && result
>= 0)
193 || (r
> 0 && result
<= 0))
195 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
196 n
, impl
->name
, align1
* CHARBYTES
& 63, align2
* CHARBYTES
& 63, len
, pos
, r
, result
, p1
, p2
);
206 CHAR s1
[116], s2
[116];
443 for (size_t i
= 0; i
< n
; i
++)
444 for (size_t len
= 0; len
<= n
- i
; ++len
)
446 exp_result
= SIMPLE_MEMCMP (s1
+ i
, s2
+ i
, len
);
447 FOR_EACH_IMPL (impl
, 0)
448 check_result (impl
, s1
+ i
, s2
+ i
, len
, exp_result
);
452 /* This test checks that memcmp doesn't overrun buffers. */
456 size_t max_length
= page_size
/ sizeof (CHAR
);
458 /* Initialize buf2 to the same values as buf1. The bug requires the
459 last compared byte to be different. */
460 memcpy (buf2
, buf1
, page_size
);
461 ((char *) buf2
)[page_size
- 1] ^= 0x11;
463 for (size_t length
= 1; length
< max_length
; length
++)
465 CHAR
*s1
= (CHAR
*) buf1
+ max_length
- length
;
466 CHAR
*s2
= (CHAR
*) buf2
+ max_length
- length
;
468 const int exp_result
= SIMPLE_MEMCMP (s1
, s2
, length
);
470 FOR_EACH_IMPL (impl
, 0)
471 check_result (impl
, s1
, s2
, length
, exp_result
);
486 FOR_EACH_IMPL (impl
, 0)
487 printf ("\t%s", impl
->name
);
490 for (i
= 1; i
< 16; ++i
)
492 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, 0);
493 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, 1);
494 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, -1);
497 for (i
= 0; i
< 16; ++i
)
499 do_test (0, 0, i
, 0);
500 do_test (0, 0, i
, 1);
501 do_test (0, 0, i
, -1);
504 for (i
= 1; i
< 10; ++i
)
506 do_test (0, 0, 2 << i
, 0);
507 do_test (0, 0, 2 << i
, 1);
508 do_test (0, 0, 2 << i
, -1);
509 do_test (0, 0, 16 << i
, 0);
510 do_test ((8 - i
) * CHARBYTES
, (2 * i
) * CHARBYTES
, 16 << i
, 0);
511 do_test (0, 0, 16 << i
, 1);
512 do_test (0, 0, 16 << i
, -1);
515 for (i
= 1; i
< 8; ++i
)
517 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, 0);
518 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, 1);
519 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, -1);
526 #include <support/test-driver.c>