1 /* Test and measure memcmp functions.
2 Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 #include "test-string.h"
24 typedef int (*proto_t
) (const char *, const char *, size_t);
25 int simple_memcmp (const char *, const char *, size_t);
27 IMPL (simple_memcmp
, 0)
31 simple_memcmp (const char *s1
, const char *s2
, size_t n
)
36 && (ret
= *(unsigned char *) s1
++ - *(unsigned char *) s2
++) == 0);
41 do_one_test (impl_t
*impl
, const char *s1
, const char *s2
, size_t len
,
44 int result
= CALL (impl
, s1
, s2
, len
);
45 if ((exp_result
== 0 && result
!= 0)
46 || (exp_result
< 0 && result
>= 0)
47 || (exp_result
> 0 && result
<= 0))
49 error (0, 0, "Wrong result in function %s %d %d", impl
->name
,
57 hp_timing_t start
, stop
, best_time
= ~ (hp_timing_t
) 0;
60 for (i
= 0; i
< 32; ++i
)
62 HP_TIMING_NOW (start
);
63 CALL (impl
, s1
, s2
, len
);
65 HP_TIMING_BEST (best_time
, start
, stop
);
68 printf ("\t%zd", (size_t) best_time
);
73 do_test (size_t align1
, size_t align2
, size_t len
, int exp_result
)
82 if (align1
+ len
>= page_size
)
86 if (align2
+ len
>= page_size
)
92 for (i
= 0; i
< len
; i
++)
93 s1
[i
] = s2
[i
] = 1 + 23 * i
% 255;
97 s2
[len
- 1] -= exp_result
;
100 printf ("Length %4zd, alignment %2zd/%2zd:", len
, align1
, align2
);
102 FOR_EACH_IMPL (impl
, 0)
103 do_one_test (impl
, s1
, s2
, len
, exp_result
);
110 do_random_tests (void)
112 size_t i
, j
, n
, align1
, align2
, pos
, len
;
114 unsigned char *p1
= buf1
+ page_size
- 512;
115 unsigned char *p2
= buf2
+ page_size
- 512;
117 for (n
= 0; n
< ITERATIONS
; n
++)
119 align1
= random () & 31;
121 align2
= random () & 31;
123 align2
= align1
+ (random () & 24);
124 pos
= random () & 511;
129 pos
= 511 - j
- (random () & 7);
130 len
= random () & 511;
132 len
= 511 - j
- (random () & 7);
133 j
= len
+ align1
+ 64;
134 if (j
> 512) j
= 512;
135 for (i
= 0; i
< j
; ++i
)
136 p1
[i
] = random () & 255;
137 for (i
= 0; i
< j
; ++i
)
138 p2
[i
] = random () & 255;
142 memcpy (p2
+ align2
, p1
+ align1
, len
);
145 memcpy (p2
+ align2
, p1
+ align1
, pos
);
146 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
148 p2
[align2
+ pos
] = random () & 255;
149 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
150 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
153 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
159 FOR_EACH_IMPL (impl
, 1)
161 r
= CALL (impl
, p1
+ align1
, p2
+ align2
, len
);
162 if ((r
== 0 && result
)
163 || (r
< 0 && result
>= 0)
164 || (r
> 0 && result
<= 0))
166 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %d != %d, p1 %p p2 %p",
167 n
, impl
->name
, align1
, align2
, len
, pos
, r
, result
, p1
, p2
);
182 FOR_EACH_IMPL (impl
, 0)
183 printf ("\t%s", impl
->name
);
186 for (i
= 1; i
< 16; ++i
)
188 do_test (i
, i
, i
, 0);
189 do_test (i
, i
, i
, 1);
190 do_test (i
, i
, i
, -1);
193 for (i
= 0; i
< 16; ++i
)
195 do_test (0, 0, i
, 0);
196 do_test (0, 0, i
, 1);
197 do_test (0, 0, i
, -1);
200 for (i
= 1; i
< 10; ++i
)
202 do_test (0, 0, 2 << i
, 0);
203 do_test (0, 0, 2 << i
, 1);
204 do_test (0, 0, 2 << i
, -1);
205 do_test (0, 0, 16 << i
, 0);
206 do_test (8 - i
, 2 * i
, 16 << i
, 0);
207 do_test (0, 0, 16 << i
, 1);
208 do_test (0, 0, 16 << i
, -1);
211 for (i
= 1; i
< 8; ++i
)
213 do_test (i
, 2 * i
, 8 << i
, 0);
214 do_test (i
, 2 * i
, 8 << i
, 1);
215 do_test (i
, 2 * i
, 8 << i
, -1);
222 #include "../test-skeleton.c"