Fix strchr test
[platform/upstream/glibc.git] / string / test-memcmp.c
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.
6
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.
11
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.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with the GNU C Library; if not, write to the Free
19    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20    02111-1307 USA.  */
21
22 #define TEST_MAIN
23 #include "test-string.h"
24 #ifdef WIDE
25 # include <inttypes.h>
26 # include <wchar.h>
27
28 # define MEMCMP wmemcmp
29 # define MEMCPY wmemcpy
30 # define SIMPLE_MEMCMP simple_wmemcmp
31 # define CHAR wchar_t
32 # define UCHAR wchar_t
33 # define CHARBYTES 4
34 # define CHAR__MIN WCHAR_MIN
35 # define CHAR__MAX WCHAR_MAX
36 int
37 simple_wmemcmp (const wchar_t *s1, const wchar_t *s2, size_t n)
38 {
39   int ret = 0;
40   /* Warning!
41         wmemcmp has to use SIGNED comparison for elements.
42         memcmp has to use UNSIGNED comparison for elemnts.
43   */
44   while (n-- && (ret = *s1 < *s2 ? -1 : *s1 == *s2 ? 0 : 1) == 0) {s1++; s2++;}
45   return ret;
46 }
47 #else
48 # define MEMCMP memcmp
49 # define MEMCPY memcpy
50 # define SIMPLE_MEMCMP simple_memcmp
51 # define CHAR char
52 # define MAX_CHAR 255
53 # define UCHAR unsigned char
54 # define CHARBYTES 1
55 # define CHAR__MIN CHAR_MIN
56 # define CHAR__MAX CHAR_MAX
57
58 int
59 simple_memcmp (const char *s1, const char *s2, size_t n)
60 {
61   int ret = 0;
62
63   while (n-- && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
64   return ret;
65 }
66 #endif
67
68 typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
69
70 IMPL (SIMPLE_MEMCMP, 0)
71 IMPL (MEMCMP, 1)
72
73 static int
74 check_result (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t len,
75               int exp_result)
76 {
77   int result = CALL (impl, s1, s2, len);
78   if ((exp_result == 0 && result != 0)
79       || (exp_result < 0 && result >= 0)
80       || (exp_result > 0 && result <= 0))
81     {
82       error (0, 0, "Wrong result in function %s %d %d", impl->name,
83              result, exp_result);
84       ret = 1;
85       return -1;
86     }
87
88   return 0;
89 }
90
91 static void
92 do_one_test (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t len,
93              int exp_result)
94 {
95   if (check_result (impl, s1, s2, len, exp_result) < 0)
96     return;
97
98   if (HP_TIMING_AVAIL)
99     {
100       hp_timing_t start __attribute ((unused));
101       hp_timing_t stop __attribute ((unused));
102       hp_timing_t best_time = ~ (hp_timing_t) 0;
103       size_t i;
104
105       for (i = 0; i < 32; ++i)
106         {
107           HP_TIMING_NOW (start);
108           CALL (impl, s1, s2, len);
109           HP_TIMING_NOW (stop);
110           HP_TIMING_BEST (best_time, start, stop);
111         }
112
113       printf ("\t%zd", (size_t) best_time);
114     }
115 }
116
117 static void
118 do_test (size_t align1, size_t align2, size_t len, int exp_result)
119 {
120   size_t i;
121   CHAR *s1, *s2;
122
123   if (len == 0)
124     return;
125
126   align1 &= 63;
127   if (align1 + (len + 1) * CHARBYTES >= page_size)
128     return;
129
130   align2 &= 63;
131   if (align2 + (len + 1) * CHARBYTES >= page_size)
132     return;
133
134   s1 = (CHAR *) (buf1 + align1);
135   s2 = (CHAR *) (buf2 + align2);
136
137   for (i = 0; i < len; i++)
138     s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % CHAR__MAX;
139
140   s1[len] = align1;
141   s2[len] = align2;
142   s2[len - 1] -= exp_result;
143
144   if (HP_TIMING_AVAIL)
145     printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
146
147   FOR_EACH_IMPL (impl, 0)
148     do_one_test (impl, s1, s2, len, exp_result);
149
150   if (HP_TIMING_AVAIL)
151     putchar ('\n');
152 }
153
154 static void
155 do_random_tests (void)
156 {
157   size_t i, j, n, align1, align2, pos, len;
158   int result;
159   long r;
160   UCHAR *p1 =  (UCHAR *) (buf1 + page_size - 512 * CHARBYTES);
161   UCHAR *p2 =  (UCHAR *) (buf2 + page_size - 512 * CHARBYTES);
162
163   for (n = 0; n < ITERATIONS; n++)
164     {
165    align1 = random () & 31;
166       if (random () & 1)
167         align2 = random () & 31;
168       else
169         align2 = align1 + (random () & 24);
170       pos = random () & 511;
171       j = align1;
172       if (align2 > j)
173         j = align2;
174       if (pos + j >= 512)
175         pos = 511 - j - (random () & 7);
176       len = random () & 511;
177       if (len + j >= 512)
178         len = 511 - j - (random () & 7);
179       j = len + align1 + 64;
180       if (j > 512) j = 512;
181       for (i = 0; i < j; ++i)
182         p1[i] = random () & 255;
183       for (i = 0; i < j; ++i)
184         p2[i] = random () & 255;
185
186       result = 0;
187       if (pos >= len)
188         MEMCPY ((CHAR *) p2 + align2, (const CHAR *) p1 + align1, len);
189       else
190         {
191           MEMCPY ((CHAR *) p2 + align2, (const CHAR *) p1 + align1, pos);
192           if (p2[align2 + pos] == p1[align1 + pos])
193             {
194               p2[align2 + pos] = random () & 255;
195               if (p2[align2 + pos] == p1[align1 + pos])
196                 p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
197             }
198
199           if (p1[align1 + pos] < p2[align2 + pos])
200             result = -1;
201           else
202             result = 1;
203         }
204
205       FOR_EACH_IMPL (impl, 1)
206         {
207           r = CALL (impl, (CHAR *) p1 + align1, (const CHAR *) p2 + align2,
208                     len);
209           if ((r == 0 && result)
210               || (r < 0 && result >= 0)
211               || (r > 0 && result <= 0))
212             {
213               error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
214                      n, impl->name, align1 * CHARBYTES & 63,  align2 * CHARBYTES & 63, len, pos, r, result, p1, p2);
215               ret = 1;
216             }
217         }
218     }
219 }
220
221 static void
222 check1 (void)
223 {
224   CHAR s1[116], s2[116];
225   int n, exp_result;
226
227   s1[0] = -108;
228   s2[0] = -108;
229   s1[1] = 99;
230   s2[1] = 99;
231   s1[2] = -113;
232   s2[2] = -113;
233   s1[3] = 1;
234   s2[3] = 1;
235   s1[4] = 116;
236   s2[4] = 116;
237   s1[5] = 99;
238   s2[5] = 99;
239   s1[6] = -113;
240   s2[6] = -113;
241   s1[7] = 1;
242   s2[7] = 1;
243   s1[8] = 84;
244   s2[8] = 84;
245   s1[9] = 99;
246   s2[9] = 99;
247   s1[10] = -113;
248   s2[10] = -113;
249   s1[11] = 1;
250   s2[11] = 1;
251   s1[12] = 52;
252   s2[12] = 52;
253   s1[13] = 99;
254   s2[13] = 99;
255   s1[14] = -113;
256   s2[14] = -113;
257   s1[15] = 1;
258   s2[15] = 1;
259   s1[16] = -76;
260   s2[16] = -76;
261   s1[17] = -14;
262   s2[17] = -14;
263   s1[18] = -109;
264   s2[18] = -109;
265   s1[19] = 1;
266   s2[19] = 1;
267   s1[20] = -108;
268   s2[20] = -108;
269   s1[21] = -14;
270   s2[21] = -14;
271   s1[22] = -109;
272   s2[22] = -109;
273   s1[23] = 1;
274   s2[23] = 1;
275   s1[24] = 84;
276   s2[24] = 84;
277   s1[25] = -15;
278   s2[25] = -15;
279   s1[26] = -109;
280   s2[26] = -109;
281   s1[27] = 1;
282   s2[27] = 1;
283   s1[28] = 52;
284   s2[28] = 52;
285   s1[29] = -15;
286   s2[29] = -15;
287   s1[30] = -109;
288   s2[30] = -109;
289   s1[31] = 1;
290   s2[31] = 1;
291   s1[32] = 20;
292   s2[32] = 20;
293   s1[33] = -15;
294   s2[33] = -15;
295   s1[34] = -109;
296   s2[34] = -109;
297   s1[35] = 1;
298   s2[35] = 1;
299   s1[36] = 20;
300   s2[36] = 20;
301   s1[37] = -14;
302   s2[37] = -14;
303   s1[38] = -109;
304   s2[38] = -109;
305   s1[39] = 1;
306   s2[39] = 1;
307   s1[40] = 52;
308   s2[40] = 52;
309   s1[41] = -14;
310   s2[41] = -14;
311   s1[42] = -109;
312   s2[42] = -109;
313   s1[43] = 1;
314   s2[43] = 1;
315   s1[44] = 84;
316   s2[44] = 84;
317   s1[45] = -14;
318   s2[45] = -14;
319   s1[46] = -109;
320   s2[46] = -109;
321   s1[47] = 1;
322   s2[47] = 1;
323   s1[48] = 116;
324   s2[48] = 116;
325   s1[49] = -14;
326   s2[49] = -14;
327   s1[50] = -109;
328   s2[50] = -109;
329   s1[51] = 1;
330   s2[51] = 1;
331   s1[52] = 116;
332   s2[52] = 116;
333   s1[53] = -15;
334   s2[53] = -15;
335   s1[54] = -109;
336   s2[54] = -109;
337   s1[55] = 1;
338   s2[55] = 1;
339   s1[56] = -44;
340   s2[56] = -44;
341   s1[57] = -14;
342   s2[57] = -14;
343   s1[58] = -109;
344   s2[58] = -109;
345   s1[59] = 1;
346   s2[59] = 1;
347   s1[60] = -108;
348   s2[60] = -108;
349   s1[61] = -15;
350   s2[61] = -15;
351   s1[62] = -109;
352   s2[62] = -109;
353   s1[63] = 1;
354   s2[63] = 1;
355   s1[64] = -76;
356   s2[64] = -76;
357   s1[65] = -15;
358   s2[65] = -15;
359   s1[66] = -109;
360   s2[66] = -109;
361   s1[67] = 1;
362   s2[67] = 1;
363   s1[68] = -44;
364   s2[68] = -44;
365   s1[69] = -15;
366   s2[69] = -15;
367   s1[70] = -109;
368   s2[70] = -109;
369   s1[71] = 1;
370   s2[71] = 1;
371   s1[72] = -12;
372   s2[72] = -12;
373   s1[73] = -15;
374   s2[73] = -15;
375   s1[74] = -109;
376   s2[74] = -109;
377   s1[75] = 1;
378   s2[75] = 1;
379   s1[76] = -12;
380   s2[76] = -12;
381   s1[77] = -14;
382   s2[77] = -14;
383   s1[78] = -109;
384   s2[78] = -109;
385   s1[79] = 1;
386   s2[79] = 1;
387   s1[80] = 20;
388   s2[80] = -68;
389   s1[81] = -12;
390   s2[81] = 64;
391   s1[82] = -109;
392   s2[82] = -106;
393   s1[83] = 1;
394   s2[83] = 1;
395   s1[84] = -12;
396   s2[84] = -12;
397   s1[85] = -13;
398   s2[85] = -13;
399   s1[86] = -109;
400   s2[86] = -109;
401   s1[87] = 1;
402   s2[87] = 1;
403   s1[88] = -44;
404   s2[88] = -44;
405   s1[89] = -13;
406   s2[89] = -13;
407   s1[90] = -109;
408   s2[90] = -109;
409   s1[91] = 1;
410   s2[91] = 1;
411   s1[92] = -76;
412   s2[92] = -76;
413   s1[93] = -13;
414   s2[93] = -13;
415   s1[94] = -109;
416   s2[94] = -109;
417   s1[95] = 1;
418   s2[95] = 1;
419   s1[96] = -108;
420   s2[96] = -108;
421   s1[97] = -13;
422   s2[97] = -13;
423   s1[98] = -109;
424   s2[98] = -109;
425   s1[99] = 1;
426   s2[99] = 1;
427   s1[100] = 116;
428   s2[100] = 116;
429   s1[101] = CHAR__MIN;
430   s2[101] = CHAR__MAX;
431   s1[102] = -109;
432   s2[102] = -109;
433   s1[103] = 1;
434   s2[103] = 1;
435   s1[104] = 84;
436   s2[104] = 84;
437   s1[105] = -13;
438   s2[105] = -13;
439   s1[106] = -109;
440   s2[106] = -109;
441   s1[107] = 1;
442   s2[107] = 1;
443   s1[108] = 52;
444   s2[108] = 52;
445   s1[109] = -13;
446   s2[109] = -13;
447   s1[110] = -109;
448   s2[110] = -109;
449   s1[111] = 1;
450   s2[111] = 1;
451   s1[112] = CHAR__MAX;
452   s2[112] = CHAR__MIN;
453   s1[113] = -13;
454   s2[113] = -13;
455   s1[114] = -109;
456   s2[114] = -109;
457   s1[115] = 1;
458   s2[115] = 1;
459
460   n = 116;
461   for (size_t i = 0; i < n; i++)
462     {
463       exp_result = SIMPLE_MEMCMP (s1 + i, s2 + i, n - i);
464       FOR_EACH_IMPL (impl, 0)
465         check_result (impl, s1 + i, s2 + i, n - i, exp_result);
466     }
467 }
468
469 int
470 test_main (void)
471 {
472   size_t i;
473
474   test_init ();
475
476   check1 ();
477
478   printf ("%23s", "");
479   FOR_EACH_IMPL (impl, 0)
480     printf ("\t%s", impl->name);
481   putchar ('\n');
482
483   for (i = 1; i < 16; ++i)
484     {
485       do_test (i * CHARBYTES, i * CHARBYTES, i, 0);
486       do_test (i * CHARBYTES, i * CHARBYTES, i, 1);
487       do_test (i * CHARBYTES, i * CHARBYTES, i, -1);
488     }
489
490   for (i = 0; i < 16; ++i)
491     {
492       do_test (0, 0, i, 0);
493       do_test (0, 0, i, 1);
494       do_test (0, 0, i, -1);
495     }
496
497   for (i = 1; i < 10; ++i)
498     {
499       do_test (0, 0, 2 << i, 0);
500       do_test (0, 0, 2 << i, 1);
501       do_test (0, 0, 2 << i, -1);
502       do_test (0, 0, 16 << i, 0);
503       do_test ((8 - i) * CHARBYTES, (2 * i) * CHARBYTES, 16 << i, 0);
504       do_test (0, 0, 16 << i, 1);
505       do_test (0, 0, 16 << i, -1);
506     }
507
508   for (i = 1; i < 8; ++i)
509     {
510       do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 0);
511       do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 1);
512       do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, -1);
513     }
514
515   do_random_tests ();
516   return ret;
517 }
518 #include "../test-skeleton.c"