~linaro-toolchain-dev/cortex-strings/trunk

« back to all changes in this revision

Viewing changes to tests/test-memcmp.c

  • Committer: Matthew Gretton-Dann
  • Author(s): Marcus Shawcroft
  • Date: 2013-01-16 20:55:59 UTC
  • mto: This revision was merged to the branch mainline in revision 99.
  • Revision ID: matthew.gretton-dann@linaro.org-20130116205559-7nye0l7d8fvzdye3
This patch fixes an issue in the AArch64 strnlen implementation which
occurs if  ULONG_MAX-15 <= n <= ULONG_MAX.

Show diffs side-by-side

added added

removed removed

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