Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* Test and measure memccpy functions.
2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
5
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.
10
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.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#define TEST_MAIN
21#define TEST_NAME "memccpy"
22#include "test-string.h"
23
24void *simple_memccpy (void *, const void *, int, size_t);
25void *stupid_memccpy (void *, const void *, int, size_t);
26
27IMPL (stupid_memccpy, 0)
28IMPL (simple_memccpy, 0)
29IMPL (memccpy, 1)
30
31void *
32simple_memccpy (void *dst, const void *src, int c, size_t n)
33{
34 const char *s = src;
35 char *d = dst;
36
37 while (n-- > 0)
38 if ((*d++ = *s++) == (char) c)
39 return d;
40
41 return NULL;
42}
43
44void *
45stupid_memccpy (void *dst, const void *src, int c, size_t n)
46{
47 void *p = memchr (src, c, n);
48
49 if (p != NULL)
50 return mempcpy (dst, src, p - src + 1);
51
52 memcpy (dst, src, n);
53 return NULL;
54}
55
56typedef void *(*proto_t) (void *, const void *, int c, size_t);
57
58static void
59do_one_test (impl_t *impl, void *dst, const void *src, int c, size_t len,
60 size_t n)
61{
62 void *expect = len > n ? NULL : (char *) dst + len;
63 if (CALL (impl, dst, src, c, n) != expect)
64 {
65 error (0, 0, "Wrong result in function %s %p %p", impl->name,
66 CALL (impl, dst, src, c, n), expect);
67 ret = 1;
68 return;
69 }
70
71 if (memcmp (dst, src, len > n ? n : len) != 0)
72 {
73 error (0, 0, "Wrong result in function %s", impl->name);
74 ret = 1;
75 return;
76 }
77}
78
79static void
80do_test (size_t align1, size_t align2, int c, size_t len, size_t n,
81 int max_char)
82{
83 size_t i;
84 char *s1, *s2;
85
86 align1 &= 7;
87 if (align1 + len >= page_size)
88 return;
89
90 align2 &= 7;
91 if (align2 + len >= page_size)
92 return;
93
94 s1 = (char *) (buf1 + align1);
95 s2 = (char *) (buf2 + align2);
96
97 for (i = 0; i < len - 1; ++i)
98 {
99 s1[i] = 32 + 23 * i % (max_char - 32);
100 if (s1[i] == (char) c)
101 --s1[i];
102 }
103 s1[len - 1] = c;
104 for (i = len; i + align1 < page_size && i < len + 64; ++i)
105 s1[i] = 32 + 32 * i % (max_char - 32);
106
107 FOR_EACH_IMPL (impl, 0)
108 do_one_test (impl, s2, s1, c, len, n);
109}
110
111static void
112do_random_tests (void)
113{
114 size_t i, j, n, align1, align2, len, size, mode;
115 unsigned char *p1 = buf1 + page_size - 512;
116 unsigned char *p2 = buf2 + page_size - 512;
117 unsigned char *res, c;
118
119 for (n = 0; n < ITERATIONS; n++)
120 {
121 mode = random ();
122 c = random ();
123 if (mode & 1)
124 {
125 size = random () & 255;
126 align1 = 512 - size - (random () & 15);
127 if (mode & 2)
128 align2 = align1 - (random () & 24);
129 else
130 align2 = align1 - (random () & 31);
131 if (mode & 4)
132 {
133 j = align1;
134 align1 = align2;
135 align2 = j;
136 }
137 if (mode & 8)
138 len = size - (random () & 31);
139 else
140 len = 512;
141 if (len >= 512)
142 len = random () & 511;
143 }
144 else
145 {
146 align1 = random () & 31;
147 if (mode & 2)
148 align2 = random () & 31;
149 else
150 align2 = align1 + (random () & 24);
151 len = random () & 511;
152 j = align1;
153 if (align2 > j)
154 j = align2;
155 if (mode & 4)
156 {
157 size = random () & 511;
158 if (size + j > 512)
159 size = 512 - j - (random() & 31);
160 }
161 else
162 size = 512 - j;
163 if ((mode & 8) && len + j >= 512)
164 len = 512 - j - (random () & 7);
165 }
166 j = len + align1 + 64;
167 if (j > 512)
168 j = 512;
169 for (i = 0; i < j; i++)
170 {
171 if (i == len + align1)
172 p1[i] = c;
173 else
174 {
175 p1[i] = random () & 255;
176 if (i >= align1 && i < len + align1 && p1[i] == c)
177 p1[i] = (random () & 127) + 3 + c;
178 }
179 }
180
181 FOR_EACH_IMPL (impl, 1)
182 {
183 unsigned char *expect;
184 memset (p2 - 64, '\1', 512 + 64);
185 res = CALL (impl, p2 + align2, p1 + align1, (char) c, size);
186 if (len >= size)
187 expect = NULL;
188 else
189 expect = p2 + align2 + len + 1;
190
191 if (res != expect)
192 {
193 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %d) %p != %p",
194 n, impl->name, align1, align2, len, size, c, res, expect);
195 ret = 1;
196 }
197 for (j = 0; j < align2 + 64; ++j)
198 {
199 if (p2[j - 64] != '\1')
200 {
201 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
202 n, impl->name, align1, align2, len);
203 ret = 1;
204 break;
205 }
206 }
207 j = align2 + len + 1;
208 if (size + align2 < j)
209 j = size + align2;
210 for (; j < 512; ++j)
211 {
212 if (p2[j] != '\1')
213 {
214 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
215 n, impl->name, align1, align2, len);
216 ret = 1;
217 break;
218 }
219 }
220 j = len + 1;
221 if (size < j)
222 j = size;
223 if (memcmp (p1 + align1, p2 + align2, j))
224 {
225 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
226 n, impl->name, align1, align2, len);
227 ret = 1;
228 }
229 }
230 }
231}
232
233int
234test_main (void)
235{
236 size_t i;
237
238 test_init ();
239
240 printf ("%28s", "");
241 FOR_EACH_IMPL (impl, 0)
242 printf ("\t%s", impl->name);
243 putchar ('\n');
244
245 for (i = 1; i < 8; ++i)
246 {
247 do_test (i, i, 12, 16, 16, 127);
248 do_test (i, i, 23, 16, 16, 255);
249 do_test (i, 2 * i, 28, 16, 16, 127);
250 do_test (2 * i, i, 31, 16, 16, 255);
251 do_test (8 - i, 2 * i, 1, 1 << i, 2 << i, 127);
252 do_test (2 * i, 8 - i, 17, 2 << i, 1 << i, 127);
253 do_test (8 - i, 2 * i, 0, 1 << i, 2 << i, 255);
254 do_test (2 * i, 8 - i, i, 2 << i, 1 << i, 255);
255 }
256
257 for (i = 1; i < 8; ++i)
258 {
259 do_test (0, 0, i, 4 << i, 8 << i, 127);
260 do_test (0, 0, i, 16 << i, 8 << i, 127);
261 do_test (8 - i, 2 * i, i, 4 << i, 8 << i, 127);
262 do_test (8 - i, 2 * i, i, 16 << i, 8 << i, 127);
263 }
264
265 do_random_tests ();
266 return ret;
267}
268
269#include <support/test-driver.c>
270

Warning: That file was not part of the compilation database. It may have many parsing errors.