Update IA64 libm-test-ulps
[glibc.git] / string / test-memcmp.c
blob15389305348017379b0c999528a996ce46d86457
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/>. */
21 #define TEST_MAIN
22 #ifdef WIDE
23 # define TEST_NAME "wmemcmp"
24 #else
25 # define TEST_NAME "memcmp"
26 #endif
27 #include "test-string.h"
28 #ifdef WIDE
29 # include <inttypes.h>
30 # include <wchar.h>
32 # define MEMCMP wmemcmp
33 # define MEMCPY wmemcpy
34 # define SIMPLE_MEMCMP simple_wmemcmp
35 # define CHAR wchar_t
36 # define UCHAR wchar_t
37 # define CHARBYTES 4
38 # define CHAR__MIN WCHAR_MIN
39 # define CHAR__MAX WCHAR_MAX
40 int
41 simple_wmemcmp (const wchar_t *s1, const wchar_t *s2, size_t n)
43 int ret = 0;
44 /* Warning!
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++;}
49 return ret;
51 #else
52 # include <limits.h>
54 # define MEMCMP memcmp
55 # define MEMCPY memcpy
56 # define SIMPLE_MEMCMP simple_memcmp
57 # define CHAR char
58 # define MAX_CHAR 255
59 # define UCHAR unsigned char
60 # define CHARBYTES 1
61 # define CHAR__MIN CHAR_MIN
62 # define CHAR__MAX CHAR_MAX
64 int
65 simple_memcmp (const char *s1, const char *s2, size_t n)
67 int ret = 0;
69 while (n-- && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
70 return ret;
72 #endif
74 typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
76 IMPL (SIMPLE_MEMCMP, 0)
77 IMPL (MEMCMP, 1)
79 static int
80 check_result (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t len,
81 int exp_result)
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,
89 result, exp_result);
90 ret = 1;
91 return -1;
94 return 0;
97 static void
98 do_one_test (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t len,
99 int exp_result)
101 if (check_result (impl, s1, s2, len, exp_result) < 0)
102 return;
105 static void
106 do_test (size_t align1, size_t align2, size_t len, int exp_result)
108 size_t i;
109 CHAR *s1, *s2;
111 if (len == 0)
112 return;
114 align1 &= 63;
115 if (align1 + (len + 1) * CHARBYTES >= page_size)
116 return;
118 align2 &= 63;
119 if (align2 + (len + 1) * CHARBYTES >= page_size)
120 return;
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;
128 s1[len] = align1;
129 s2[len] = align2;
130 s2[len - 1] -= exp_result;
132 FOR_EACH_IMPL (impl, 0)
133 do_one_test (impl, s1, s2, len, exp_result);
136 static void
137 do_random_tests (void)
139 size_t i, j, n, align1, align2, pos, len;
140 int result;
141 long r;
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;
148 if (random () & 1)
149 align2 = random () & 31;
150 else
151 align2 = align1 + (random () & 24);
152 pos = random () & 511;
153 j = align1;
154 if (align2 > j)
155 j = align2;
156 if (pos + j >= 512)
157 pos = 511 - j - (random () & 7);
158 len = random () & 511;
159 if (len + j >= 512)
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;
168 result = 0;
169 if (pos >= len)
170 MEMCPY ((CHAR *) p2 + align2, (const CHAR *) p1 + align1, len);
171 else
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])
182 result = -1;
183 else
184 result = 1;
187 FOR_EACH_IMPL (impl, 1)
189 r = CALL (impl, (CHAR *) p1 + align1, (const CHAR *) p2 + align2,
190 len);
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);
197 ret = 1;
203 static void
204 check1 (void)
206 CHAR s1[116], s2[116];
207 int n, exp_result;
209 s1[0] = -108;
210 s2[0] = -108;
211 s1[1] = 99;
212 s2[1] = 99;
213 s1[2] = -113;
214 s2[2] = -113;
215 s1[3] = 1;
216 s2[3] = 1;
217 s1[4] = 116;
218 s2[4] = 116;
219 s1[5] = 99;
220 s2[5] = 99;
221 s1[6] = -113;
222 s2[6] = -113;
223 s1[7] = 1;
224 s2[7] = 1;
225 s1[8] = 84;
226 s2[8] = 84;
227 s1[9] = 99;
228 s2[9] = 99;
229 s1[10] = -113;
230 s2[10] = -113;
231 s1[11] = 1;
232 s2[11] = 1;
233 s1[12] = 52;
234 s2[12] = 52;
235 s1[13] = 99;
236 s2[13] = 99;
237 s1[14] = -113;
238 s2[14] = -113;
239 s1[15] = 1;
240 s2[15] = 1;
241 s1[16] = -76;
242 s2[16] = -76;
243 s1[17] = -14;
244 s2[17] = -14;
245 s1[18] = -109;
246 s2[18] = -109;
247 s1[19] = 1;
248 s2[19] = 1;
249 s1[20] = -108;
250 s2[20] = -108;
251 s1[21] = -14;
252 s2[21] = -14;
253 s1[22] = -109;
254 s2[22] = -109;
255 s1[23] = 1;
256 s2[23] = 1;
257 s1[24] = 84;
258 s2[24] = 84;
259 s1[25] = -15;
260 s2[25] = -15;
261 s1[26] = -109;
262 s2[26] = -109;
263 s1[27] = 1;
264 s2[27] = 1;
265 s1[28] = 52;
266 s2[28] = 52;
267 s1[29] = -15;
268 s2[29] = -15;
269 s1[30] = -109;
270 s2[30] = -109;
271 s1[31] = 1;
272 s2[31] = 1;
273 s1[32] = 20;
274 s2[32] = 20;
275 s1[33] = -15;
276 s2[33] = -15;
277 s1[34] = -109;
278 s2[34] = -109;
279 s1[35] = 1;
280 s2[35] = 1;
281 s1[36] = 20;
282 s2[36] = 20;
283 s1[37] = -14;
284 s2[37] = -14;
285 s1[38] = -109;
286 s2[38] = -109;
287 s1[39] = 1;
288 s2[39] = 1;
289 s1[40] = 52;
290 s2[40] = 52;
291 s1[41] = -14;
292 s2[41] = -14;
293 s1[42] = -109;
294 s2[42] = -109;
295 s1[43] = 1;
296 s2[43] = 1;
297 s1[44] = 84;
298 s2[44] = 84;
299 s1[45] = -14;
300 s2[45] = -14;
301 s1[46] = -109;
302 s2[46] = -109;
303 s1[47] = 1;
304 s2[47] = 1;
305 s1[48] = 116;
306 s2[48] = 116;
307 s1[49] = -14;
308 s2[49] = -14;
309 s1[50] = -109;
310 s2[50] = -109;
311 s1[51] = 1;
312 s2[51] = 1;
313 s1[52] = 116;
314 s2[52] = 116;
315 s1[53] = -15;
316 s2[53] = -15;
317 s1[54] = -109;
318 s2[54] = -109;
319 s1[55] = 1;
320 s2[55] = 1;
321 s1[56] = -44;
322 s2[56] = -44;
323 s1[57] = -14;
324 s2[57] = -14;
325 s1[58] = -109;
326 s2[58] = -109;
327 s1[59] = 1;
328 s2[59] = 1;
329 s1[60] = -108;
330 s2[60] = -108;
331 s1[61] = -15;
332 s2[61] = -15;
333 s1[62] = -109;
334 s2[62] = -109;
335 s1[63] = 1;
336 s2[63] = 1;
337 s1[64] = -76;
338 s2[64] = -76;
339 s1[65] = -15;
340 s2[65] = -15;
341 s1[66] = -109;
342 s2[66] = -109;
343 s1[67] = 1;
344 s2[67] = 1;
345 s1[68] = -44;
346 s2[68] = -44;
347 s1[69] = -15;
348 s2[69] = -15;
349 s1[70] = -109;
350 s2[70] = -109;
351 s1[71] = 1;
352 s2[71] = 1;
353 s1[72] = -12;
354 s2[72] = -12;
355 s1[73] = -15;
356 s2[73] = -15;
357 s1[74] = -109;
358 s2[74] = -109;
359 s1[75] = 1;
360 s2[75] = 1;
361 s1[76] = -12;
362 s2[76] = -12;
363 s1[77] = -14;
364 s2[77] = -14;
365 s1[78] = -109;
366 s2[78] = -109;
367 s1[79] = 1;
368 s2[79] = 1;
369 s1[80] = 20;
370 s2[80] = -68;
371 s1[81] = -12;
372 s2[81] = 64;
373 s1[82] = -109;
374 s2[82] = -106;
375 s1[83] = 1;
376 s2[83] = 1;
377 s1[84] = -12;
378 s2[84] = -12;
379 s1[85] = -13;
380 s2[85] = -13;
381 s1[86] = -109;
382 s2[86] = -109;
383 s1[87] = 1;
384 s2[87] = 1;
385 s1[88] = -44;
386 s2[88] = -44;
387 s1[89] = -13;
388 s2[89] = -13;
389 s1[90] = -109;
390 s2[90] = -109;
391 s1[91] = 1;
392 s2[91] = 1;
393 s1[92] = -76;
394 s2[92] = -76;
395 s1[93] = -13;
396 s2[93] = -13;
397 s1[94] = -109;
398 s2[94] = -109;
399 s1[95] = 1;
400 s2[95] = 1;
401 s1[96] = -108;
402 s2[96] = -108;
403 s1[97] = -13;
404 s2[97] = -13;
405 s1[98] = -109;
406 s2[98] = -109;
407 s1[99] = 1;
408 s2[99] = 1;
409 s1[100] = 116;
410 s2[100] = 116;
411 s1[101] = CHAR__MIN;
412 s2[101] = CHAR__MAX;
413 s1[102] = -109;
414 s2[102] = -109;
415 s1[103] = 1;
416 s2[103] = 1;
417 s1[104] = 84;
418 s2[104] = 84;
419 s1[105] = -13;
420 s2[105] = -13;
421 s1[106] = -109;
422 s2[106] = -109;
423 s1[107] = 1;
424 s2[107] = 1;
425 s1[108] = 52;
426 s2[108] = 52;
427 s1[109] = -13;
428 s2[109] = -13;
429 s1[110] = -109;
430 s2[110] = -109;
431 s1[111] = 1;
432 s2[111] = 1;
433 s1[112] = CHAR__MAX;
434 s2[112] = CHAR__MIN;
435 s1[113] = -13;
436 s2[113] = -13;
437 s1[114] = -109;
438 s2[114] = -109;
439 s1[115] = 1;
440 s2[115] = 1;
442 n = 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. */
453 static void
454 check2 (void)
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);
476 test_main (void)
478 size_t i;
480 test_init ();
482 check1 ();
483 check2 ();
485 printf ("%23s", "");
486 FOR_EACH_IMPL (impl, 0)
487 printf ("\t%s", impl->name);
488 putchar ('\n');
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);
522 do_random_tests ();
523 return ret;
526 #include <support/test-driver.c>