.
[glibc/history.git] / string / test-memcmp.c
blobaf07a5e59935dc042a24955b86ec78343b4c9b79
1 /* Test and measure memcmp functions.
2 Copyright (C) 1999, 2002, 2003, 2005 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
19 02111-1307 USA. */
21 #define TEST_MAIN
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)
28 IMPL (memcmp, 1)
30 int
31 simple_memcmp (const char *s1, const char *s2, size_t n)
33 int ret = 0;
35 while (n--
36 && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
37 return ret;
40 static void
41 do_one_test (impl_t *impl, const char *s1, const char *s2, size_t len,
42 int exp_result)
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,
50 result, exp_result);
51 ret = 1;
52 return;
55 if (HP_TIMING_AVAIL)
57 hp_timing_t start __attribute ((unused));
58 hp_timing_t stop __attribute ((unused));
59 hp_timing_t best_time = ~ (hp_timing_t) 0;
60 size_t i;
62 for (i = 0; i < 32; ++i)
64 HP_TIMING_NOW (start);
65 CALL (impl, s1, s2, len);
66 HP_TIMING_NOW (stop);
67 HP_TIMING_BEST (best_time, start, stop);
70 printf ("\t%zd", (size_t) best_time);
74 static void
75 do_test (size_t align1, size_t align2, size_t len, int exp_result)
77 size_t i;
78 char *s1, *s2;
80 if (len == 0)
81 return;
83 align1 &= 7;
84 if (align1 + len >= page_size)
85 return;
87 align2 &= 7;
88 if (align2 + len >= page_size)
89 return;
91 s1 = (char *) (buf1 + align1);
92 s2 = (char *) (buf2 + align2);
94 for (i = 0; i < len; i++)
95 s1[i] = s2[i] = 1 + 23 * i % 255;
97 s1[len] = align1;
98 s2[len] = align2;
99 s2[len - 1] -= exp_result;
101 if (HP_TIMING_AVAIL)
102 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
104 FOR_EACH_IMPL (impl, 0)
105 do_one_test (impl, s1, s2, len, exp_result);
107 if (HP_TIMING_AVAIL)
108 putchar ('\n');
111 static void
112 do_random_tests (void)
114 size_t i, j, n, align1, align2, pos, len;
115 int result;
116 long r;
117 unsigned char *p1 = buf1 + page_size - 512;
118 unsigned char *p2 = buf2 + page_size - 512;
120 for (n = 0; n < ITERATIONS; n++)
122 align1 = random () & 31;
123 if (random () & 1)
124 align2 = random () & 31;
125 else
126 align2 = align1 + (random () & 24);
127 pos = random () & 511;
128 j = align1;
129 if (align2 > j)
130 j = align2;
131 if (pos + j >= 512)
132 pos = 511 - j - (random () & 7);
133 len = random () & 511;
134 if (len + j >= 512)
135 len = 511 - j - (random () & 7);
136 j = len + align1 + 64;
137 if (j > 512) j = 512;
138 for (i = 0; i < j; ++i)
139 p1[i] = random () & 255;
140 for (i = 0; i < j; ++i)
141 p2[i] = random () & 255;
143 result = 0;
144 if (pos >= len)
145 memcpy (p2 + align2, p1 + align1, len);
146 else
148 memcpy (p2 + align2, p1 + align1, pos);
149 if (p2[align2 + pos] == p1[align1 + pos])
151 p2[align2 + pos] = random () & 255;
152 if (p2[align2 + pos] == p1[align1 + pos])
153 p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
156 if (p1[align1 + pos] < p2[align2 + pos])
157 result = -1;
158 else
159 result = 1;
162 FOR_EACH_IMPL (impl, 1)
164 r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2), len);
165 /* Test whether on 64-bit architectures where ABI requires
166 callee to promote has the promotion been done. */
167 asm ("" : "=g" (r) : "0" (r));
168 if ((r == 0 && result)
169 || (r < 0 && result >= 0)
170 || (r > 0 && result <= 0))
172 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
173 n, impl->name, align1, align2, len, pos, r, result, p1, p2);
174 ret = 1;
181 test_main (void)
183 size_t i;
185 test_init ();
187 printf ("%23s", "");
188 FOR_EACH_IMPL (impl, 0)
189 printf ("\t%s", impl->name);
190 putchar ('\n');
192 for (i = 1; i < 16; ++i)
194 do_test (i, i, i, 0);
195 do_test (i, i, i, 1);
196 do_test (i, i, i, -1);
199 for (i = 0; i < 16; ++i)
201 do_test (0, 0, i, 0);
202 do_test (0, 0, i, 1);
203 do_test (0, 0, i, -1);
206 for (i = 1; i < 10; ++i)
208 do_test (0, 0, 2 << i, 0);
209 do_test (0, 0, 2 << i, 1);
210 do_test (0, 0, 2 << i, -1);
211 do_test (0, 0, 16 << i, 0);
212 do_test (8 - i, 2 * i, 16 << i, 0);
213 do_test (0, 0, 16 << i, 1);
214 do_test (0, 0, 16 << i, -1);
217 for (i = 1; i < 8; ++i)
219 do_test (i, 2 * i, 8 << i, 0);
220 do_test (i, 2 * i, 8 << i, 1);
221 do_test (i, 2 * i, 8 << i, -1);
224 do_random_tests ();
225 return ret;
228 #include "../test-skeleton.c"