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, see
19 <http://www.gnu.org/licenses/>. */
22 #include "test-string.h"
24 # include <inttypes.h>
27 # define MEMCMP wmemcmp
28 # define MEMCPY wmemcpy
29 # define SIMPLE_MEMCMP simple_wmemcmp
31 # define UCHAR wchar_t
33 # define CHAR__MIN WCHAR_MIN
34 # define CHAR__MAX WCHAR_MAX
36 simple_wmemcmp (const wchar_t *s1
, const wchar_t *s2
, size_t n
)
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
++;}
47 # define MEMCMP memcmp
48 # define MEMCPY memcpy
49 # define SIMPLE_MEMCMP simple_memcmp
52 # define UCHAR unsigned char
54 # define CHAR__MIN CHAR_MIN
55 # define CHAR__MAX CHAR_MAX
58 simple_memcmp (const char *s1
, const char *s2
, size_t n
)
62 while (n
-- && (ret
= *(unsigned char *) s1
++ - *(unsigned char *) s2
++) == 0);
67 typedef int (*proto_t
) (const CHAR
*, const CHAR
*, size_t);
69 IMPL (SIMPLE_MEMCMP
, 0)
73 check_result (impl_t
*impl
, const CHAR
*s1
, const CHAR
*s2
, size_t len
,
76 int result
= CALL (impl
, s1
, s2
, len
);
77 if ((exp_result
== 0 && result
!= 0)
78 || (exp_result
< 0 && result
>= 0)
79 || (exp_result
> 0 && result
<= 0))
81 error (0, 0, "Wrong result in function %s %d %d", impl
->name
,
91 do_one_test (impl_t
*impl
, const CHAR
*s1
, const CHAR
*s2
, size_t len
,
94 if (check_result (impl
, s1
, s2
, len
, exp_result
) < 0)
99 hp_timing_t start
__attribute ((unused
));
100 hp_timing_t stop
__attribute ((unused
));
101 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
104 for (i
= 0; i
< 32; ++i
)
106 HP_TIMING_NOW (start
);
107 CALL (impl
, s1
, s2
, len
);
108 HP_TIMING_NOW (stop
);
109 HP_TIMING_BEST (best_time
, start
, stop
);
112 printf ("\t%zd", (size_t) best_time
);
117 do_test (size_t align1
, size_t align2
, size_t len
, int exp_result
)
126 if (align1
+ (len
+ 1) * CHARBYTES
>= page_size
)
130 if (align2
+ (len
+ 1) * CHARBYTES
>= page_size
)
133 s1
= (CHAR
*) (buf1
+ align1
);
134 s2
= (CHAR
*) (buf2
+ align2
);
136 for (i
= 0; i
< len
; i
++)
137 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% CHAR__MAX
;
141 s2
[len
- 1] -= exp_result
;
144 printf ("Length %4zd, alignment %2zd/%2zd:", len
, align1
, align2
);
146 FOR_EACH_IMPL (impl
, 0)
147 do_one_test (impl
, s1
, s2
, len
, exp_result
);
154 do_random_tests (void)
156 size_t i
, j
, n
, align1
, align2
, pos
, len
;
159 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
- 512 * CHARBYTES
);
160 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
- 512 * CHARBYTES
);
162 for (n
= 0; n
< ITERATIONS
; n
++)
164 align1
= random () & 31;
166 align2
= random () & 31;
168 align2
= align1
+ (random () & 24);
169 pos
= random () & 511;
174 pos
= 511 - j
- (random () & 7);
175 len
= random () & 511;
177 len
= 511 - j
- (random () & 7);
178 j
= len
+ align1
+ 64;
179 if (j
> 512) j
= 512;
180 for (i
= 0; i
< j
; ++i
)
181 p1
[i
] = random () & 255;
182 for (i
= 0; i
< j
; ++i
)
183 p2
[i
] = random () & 255;
187 MEMCPY ((CHAR
*) p2
+ align2
, (const CHAR
*) p1
+ align1
, len
);
190 MEMCPY ((CHAR
*) p2
+ align2
, (const CHAR
*) p1
+ align1
, pos
);
191 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
193 p2
[align2
+ pos
] = random () & 255;
194 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
195 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
198 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
204 FOR_EACH_IMPL (impl
, 1)
206 r
= CALL (impl
, (CHAR
*) p1
+ align1
, (const CHAR
*) p2
+ align2
,
208 if ((r
== 0 && result
)
209 || (r
< 0 && result
>= 0)
210 || (r
> 0 && result
<= 0))
212 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
213 n
, impl
->name
, align1
* CHARBYTES
& 63, align2
* CHARBYTES
& 63, len
, pos
, r
, result
, p1
, p2
);
223 CHAR s1
[116], s2
[116];
460 for (size_t i
= 0; i
< n
; i
++)
462 exp_result
= SIMPLE_MEMCMP (s1
+ i
, s2
+ i
, n
- i
);
463 FOR_EACH_IMPL (impl
, 0)
464 check_result (impl
, s1
+ i
, s2
+ i
, n
- i
, exp_result
);
478 FOR_EACH_IMPL (impl
, 0)
479 printf ("\t%s", impl
->name
);
482 for (i
= 1; i
< 16; ++i
)
484 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, 0);
485 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, 1);
486 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, -1);
489 for (i
= 0; i
< 16; ++i
)
491 do_test (0, 0, i
, 0);
492 do_test (0, 0, i
, 1);
493 do_test (0, 0, i
, -1);
496 for (i
= 1; i
< 10; ++i
)
498 do_test (0, 0, 2 << i
, 0);
499 do_test (0, 0, 2 << i
, 1);
500 do_test (0, 0, 2 << i
, -1);
501 do_test (0, 0, 16 << i
, 0);
502 do_test ((8 - i
) * CHARBYTES
, (2 * i
) * CHARBYTES
, 16 << i
, 0);
503 do_test (0, 0, 16 << i
, 1);
504 do_test (0, 0, 16 << i
, -1);
507 for (i
= 1; i
< 8; ++i
)
509 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, 0);
510 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, 1);
511 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, -1);
517 #include "../test-skeleton.c"