* tst-cancel4.c (do_test): Use %zd instead of %d when printing cnt.
[glibc/pb-stable.git] / string / test-strcpy.c
blobc198a9d59e2fd96291cee5c22db1ddbf40cefcf8
1 /* Test and measure strcpy 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
19 02111-1307 USA. */
21 #ifndef STRCPY_RESULT
22 # define STRCPY_RESULT(dst, len) dst
23 # define TEST_MAIN
24 # include "test-string.h"
26 char *simple_strcpy (char *, const char *);
28 IMPL (simple_strcpy, 0)
29 IMPL (strcpy, 1)
31 char *
32 simple_strcpy (char *dst, const char *src)
34 char *ret = dst;
35 while ((*dst++ = *src++) != '\0');
36 return ret;
38 #endif
40 typedef char *(*proto_t) (char *, const char *);
42 static void
43 do_one_test (impl_t *impl, char *dst, const char *src,
44 size_t len __attribute__((unused)))
46 if (CALL (impl, dst, src) != STRCPY_RESULT (dst, len))
48 error (0, 0, "Wrong result in function %s %p %p", impl->name,
49 CALL (impl, dst, src), STRCPY_RESULT (dst, len));
50 ret = 1;
51 return;
54 if (strcmp (dst, src) != 0)
56 error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
57 impl->name, dst, src);
58 ret = 1;
59 return;
62 if (HP_TIMING_AVAIL)
64 hp_timing_t start, stop, best_time = ~ (hp_timing_t) 0;
65 size_t i;
67 for (i = 0; i < 32; ++i)
69 HP_TIMING_NOW (start);
70 CALL (impl, dst, src);
71 HP_TIMING_NOW (stop);
72 HP_TIMING_BEST (best_time, start, stop);
75 printf ("\t%zd", (size_t) best_time);
79 static void
80 do_test (size_t align1, size_t align2, size_t len, int max_char)
82 size_t i;
83 char *s1, *s2;
85 align1 &= 7;
86 if (align1 + len >= page_size)
87 return;
89 align2 &= 7;
90 if (align2 + len >= page_size)
91 return;
93 s1 = buf1 + align1;
94 s2 = buf2 + align2;
96 for (i = 0; i < len; i++)
97 s1[i] = 32 + 23 * i % (max_char - 32);
98 s1[len] = 0;
100 if (HP_TIMING_AVAIL)
101 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
103 FOR_EACH_IMPL (impl, 0)
104 do_one_test (impl, s2, s1, len);
106 if (HP_TIMING_AVAIL)
107 putchar ('\n');
110 static void
111 do_random_tests (void)
113 size_t i, j, n, align1, align2, len;
114 unsigned char *p1 = buf1 + page_size - 512;
115 unsigned char *p2 = buf2 + page_size - 512;
116 unsigned char *res;
118 for (n = 0; n < ITERATIONS; n++)
120 align1 = random () & 31;
121 if (random () & 1)
122 align2 = random () & 31;
123 else
124 align2 = align1 + (random () & 24);
125 len = random () & 511;
126 j = align1;
127 if (align2 > j)
128 j = align2;
129 if (len + j >= 511)
130 len = 510 - j - (random () & 7);
131 j = len + align1 + 64;
132 if (j > 512)
133 j = 512;
134 for (i = 0; i < j; i++)
136 if (i == len + align1)
137 p1[i] = 0;
138 else
140 p1[i] = random () & 255;
141 if (i >= align1 && i < len + align1 && !p1[i])
142 p1[i] = (random () & 127) + 3;
146 FOR_EACH_IMPL (impl, 1)
148 memset (p2 - 64, '\1', 512 + 64);
149 res = CALL (impl, p2 + align2, p1 + align1);
150 if (res != STRCPY_RESULT (p2 + align2, len))
152 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
153 n, impl->name, align1, align2, len, res,
154 STRCPY_RESULT (p2 + align2, len));
155 ret = 1;
157 for (j = 0; j < align2 + 64; ++j)
159 if (p2[j - 64] != '\1')
161 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
162 n, impl->name, align1, align2, len);
163 ret = 1;
164 break;
167 for (j = align2 + len + 1; j < 512; ++j)
169 if (p2[j] != '\1')
171 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
172 n, impl->name, align1, align2, len);
173 ret = 1;
174 break;
177 if (memcmp (p1 + align1, p2 + align2, len + 1))
179 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
180 n, impl->name, align1, align2, len);
181 ret = 1;
188 test_main (void)
190 size_t i;
192 test_init ();
194 printf ("%23s", "");
195 FOR_EACH_IMPL (impl, 0)
196 printf ("\t%s", impl->name);
197 putchar ('\n');
199 for (i = 0; i < 16; ++i)
201 do_test (0, 0, i, 127);
202 do_test (0, 0, i, 255);
203 do_test (0, i, i, 127);
204 do_test (i, 0, i, 255);
207 for (i = 1; i < 8; ++i)
209 do_test (0, 0, 8 << i, 127);
210 do_test (8 - i, 2 * i, 8 << i, 127);
213 for (i = 1; i < 8; ++i)
215 do_test (i, 2 * i, 8 << i, 127);
216 do_test (2 * i, i, 8 << i, 255);
217 do_test (i, i, 8 << i, 127);
218 do_test (i, i, 8 << i, 255);
221 do_random_tests ();
222 return ret;
225 #include "../test-skeleton.c"