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/>. */
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
++;}
49 # define MEMCMP memcmp
50 # define MEMCPY memcpy
51 # define SIMPLE_MEMCMP simple_memcmp
54 # define UCHAR unsigned char
56 # define CHAR__MIN CHAR_MIN
57 # define CHAR__MAX CHAR_MAX
60 simple_memcmp (const char *s1
, const char *s2
, size_t n
)
64 while (n
-- && (ret
= *(unsigned char *) s1
++ - *(unsigned char *) s2
++) == 0);
69 typedef int (*proto_t
) (const CHAR
*, const CHAR
*, size_t);
71 IMPL (SIMPLE_MEMCMP
, 0)
75 check_result (impl_t
*impl
, const CHAR
*s1
, const CHAR
*s2
, size_t len
,
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
,
93 do_one_test (impl_t
*impl
, const CHAR
*s1
, const CHAR
*s2
, size_t len
,
96 if (check_result (impl
, s1
, s2
, len
, exp_result
) < 0)
101 hp_timing_t start
__attribute ((unused
));
102 hp_timing_t stop
__attribute ((unused
));
103 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
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
);
119 do_test (size_t align1
, size_t align2
, size_t len
, int exp_result
)
128 if (align1
+ (len
+ 1) * CHARBYTES
>= page_size
)
132 if (align2
+ (len
+ 1) * CHARBYTES
>= page_size
)
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
;
143 s2
[len
- 1] -= exp_result
;
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
);
156 do_random_tests (void)
158 size_t i
, j
, n
, align1
, align2
, pos
, len
;
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;
168 align2
= random () & 31;
170 align2
= align1
+ (random () & 24);
171 pos
= random () & 511;
176 pos
= 511 - j
- (random () & 7);
177 len
= random () & 511;
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;
189 MEMCPY ((CHAR
*) p2
+ align2
, (const CHAR
*) p1
+ align1
, len
);
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
])
206 FOR_EACH_IMPL (impl
, 1)
208 r
= CALL (impl
, (CHAR
*) p1
+ align1
, (const CHAR
*) p2
+ align2
,
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
);
225 CHAR s1
[116], s2
[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
);
480 FOR_EACH_IMPL (impl
, 0)
481 printf ("\t%s", impl
->name
);
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);
519 #include "../test-skeleton.c"