Remove unused localedata/th_TH.in
[glibc.git] / string / test-memccpy.c
blob432ad8acdd069a3d233c2fb3e3eed7732af91667
1 /* Test and measure memccpy functions.
2 Copyright (C) 1999-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #define TEST_MAIN
20 #define TEST_NAME "memccpy"
21 #include "test-string.h"
23 IMPL (memccpy, 1)
25 /* Naive implementation to verify results. */
26 void *
27 simple_memccpy (void *dst, const void *src, int c, size_t n)
29 const char *s = src;
30 char *d = dst;
32 while (n-- > 0)
33 if ((*d++ = *s++) == (char) c)
34 return d;
36 return NULL;
39 typedef void *(*proto_t) (void *, const void *, int c, size_t);
41 static void
42 do_one_test (impl_t *impl, void *dst, const void *src, int c, size_t len,
43 size_t n)
45 void *expect = len > n ? NULL : (char *) dst + len;
46 if (CALL (impl, dst, src, c, n) != expect)
48 error (0, 0, "Wrong result in function %s %p %p", impl->name,
49 CALL (impl, dst, src, c, n), expect);
50 ret = 1;
51 return;
54 if (memcmp (dst, src, len > n ? n : len) != 0)
56 error (0, 0, "Wrong result in function %s", impl->name);
57 ret = 1;
58 return;
62 static void
63 do_test (size_t align1, size_t align2, int c, size_t len, size_t n,
64 int max_char)
66 size_t i;
67 char *s1, *s2;
69 align1 &= 7;
70 if (align1 + len >= page_size)
71 return;
73 align2 &= 7;
74 if (align2 + len >= page_size)
75 return;
77 s1 = (char *) (buf1 + align1);
78 s2 = (char *) (buf2 + align2);
80 for (i = 0; i < len - 1; ++i)
82 s1[i] = 32 + 23 * i % (max_char - 32);
83 if (s1[i] == (char) c)
84 --s1[i];
86 s1[len - 1] = c;
87 for (i = len; i + align1 < page_size && i < len + 64; ++i)
88 s1[i] = 32 + 32 * i % (max_char - 32);
90 FOR_EACH_IMPL (impl, 0)
91 do_one_test (impl, s2, s1, c, len, n);
94 static void
95 do_random_tests (void)
97 size_t i, j, n, align1, align2, len, size, mode;
98 unsigned char *p1 = buf1 + page_size - 512;
99 unsigned char *p2 = buf2 + page_size - 512;
100 unsigned char *res, c;
102 for (n = 0; n < ITERATIONS; n++)
104 mode = random ();
105 c = random ();
106 if (mode & 1)
108 size = random () & 255;
109 align1 = 512 - size - (random () & 15);
110 if (mode & 2)
111 align2 = align1 - (random () & 24);
112 else
113 align2 = align1 - (random () & 31);
114 if (mode & 4)
116 j = align1;
117 align1 = align2;
118 align2 = j;
120 if (mode & 8)
121 len = size - (random () & 31);
122 else
123 len = 512;
124 if (len >= 512)
125 len = random () & 511;
127 else
129 align1 = random () & 31;
130 if (mode & 2)
131 align2 = random () & 31;
132 else
133 align2 = align1 + (random () & 24);
134 len = random () & 511;
135 j = align1;
136 if (align2 > j)
137 j = align2;
138 if (mode & 4)
140 size = random () & 511;
141 if (size + j > 512)
142 size = 512 - j - (random() & 31);
144 else
145 size = 512 - j;
146 if ((mode & 8) && len + j >= 512)
147 len = 512 - j - (random () & 7);
149 j = len + align1 + 64;
150 if (j > 512)
151 j = 512;
152 for (i = 0; i < j; i++)
154 if (i == len + align1)
155 p1[i] = c;
156 else
158 p1[i] = random () & 255;
159 if (i >= align1 && i < len + align1 && p1[i] == c)
160 p1[i] = (random () & 127) + 3 + c;
164 FOR_EACH_IMPL (impl, 1)
166 unsigned char *expect;
167 memset (p2 - 64, '\1', 512 + 64);
168 res = CALL (impl, p2 + align2, p1 + align1, (char) c, size);
169 if (len >= size)
170 expect = NULL;
171 else
172 expect = p2 + align2 + len + 1;
174 if (res != expect)
176 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %d) %p != %p",
177 n, impl->name, align1, align2, len, size, c, res, expect);
178 ret = 1;
180 for (j = 0; j < align2 + 64; ++j)
182 if (p2[j - 64] != '\1')
184 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
185 n, impl->name, align1, align2, len);
186 ret = 1;
187 break;
190 j = align2 + len + 1;
191 if (size + align2 < j)
192 j = size + align2;
193 for (; j < 512; ++j)
195 if (p2[j] != '\1')
197 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
198 n, impl->name, align1, align2, len);
199 ret = 1;
200 break;
203 j = len + 1;
204 if (size < j)
205 j = size;
206 if (memcmp (p1 + align1, p2 + align2, j))
208 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
209 n, impl->name, align1, align2, len);
210 ret = 1;
217 test_main (void)
219 size_t i;
221 test_init ();
223 printf ("%28s", "");
224 FOR_EACH_IMPL (impl, 0)
225 printf ("\t%s", impl->name);
226 putchar ('\n');
228 for (i = 1; i < 8; ++i)
230 do_test (i, i, 12, 16, 16, 127);
231 do_test (i, i, 23, 16, 16, 255);
232 do_test (i, 2 * i, 28, 16, 16, 127);
233 do_test (2 * i, i, 31, 16, 16, 255);
234 do_test (8 - i, 2 * i, 1, 1 << i, 2 << i, 127);
235 do_test (2 * i, 8 - i, 17, 2 << i, 1 << i, 127);
236 do_test (8 - i, 2 * i, 0, 1 << i, 2 << i, 255);
237 do_test (2 * i, 8 - i, i, 2 << i, 1 << i, 255);
240 for (i = 1; i < 8; ++i)
242 do_test (0, 0, i, 4 << i, 8 << i, 127);
243 do_test (0, 0, i, 16 << i, 8 << i, 127);
244 do_test (8 - i, 2 * i, i, 4 << i, 8 << i, 127);
245 do_test (8 - i, 2 * i, i, 16 << i, 8 << i, 127);
248 do_random_tests ();
249 return ret;
252 #include <support/test-driver.c>