linux: fix ntp_gettime abi break (BZ# 30156)
[glibc.git] / string / test-rawmemchr.c
blobdfeaf1b2ee309204c70269bbcb521f459f8a2c70
1 /* Test and measure memchr 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 #include <assert.h>
20 #include <support/xunistd.h>
22 #define TEST_MAIN
23 #define TEST_NAME "rawmemchr"
24 #include "test-string.h"
26 typedef char *(*proto_t) (const char *, int);
27 char *simple_rawmemchr (const char *, int);
29 IMPL (simple_rawmemchr, 0)
30 IMPL (rawmemchr, 1)
32 char *
33 simple_rawmemchr (const char *s, int c)
35 while (1)
36 if (*s++ == (char) c)
37 return (char *) s - 1;
38 return NULL;
41 static void
42 do_one_test (impl_t *impl, const char *s, int c, char *exp_res)
44 char *res = CALL (impl, s, c);
45 if (res != exp_res)
47 error (0, 0, "Wrong result in function %s %p %p", impl->name,
48 res, exp_res);
49 ret = 1;
50 return;
54 static void
55 do_test_bz29234 (void)
57 size_t i, j;
58 char *ptr_start;
59 char *buf = xmmap (0, 8192, PROT_READ | PROT_WRITE,
60 MAP_PRIVATE | MAP_ANONYMOUS, -1);
62 memset (buf, -1, 8192);
64 ptr_start = buf + 4096 - 8;
66 /* Out of range matches before the start of a page. */
67 memset (ptr_start - 8, 0x1, 8);
69 for (j = 0; j < 8; ++j)
71 for (i = 0; i < 128; ++i)
73 ptr_start[i + j] = 0x1;
75 FOR_EACH_IMPL (impl, 0)
76 do_one_test (impl, (char *) (ptr_start + j), 0x1,
77 ptr_start + i + j);
79 ptr_start[i + j] = 0xff;
83 xmunmap (buf, 8192);
86 static void
87 do_test (size_t align, size_t pos, size_t len, int seek_char)
89 size_t i;
90 char *result;
92 align &= getpagesize () - 1;
93 if (align + len >= page_size)
94 return;
96 for (i = 0; i < len; ++i)
98 buf1[align + i] = 1 + 23 * i % 127;
99 if (buf1[align + i] == seek_char)
100 buf1[align + i] = seek_char + 1;
102 buf1[align + len] = 0;
104 assert (pos < len);
106 buf1[align + pos] = seek_char;
107 buf1[align + len] = -seek_char;
108 result = (char *) (buf1 + align + pos);
110 FOR_EACH_IMPL (impl, 0)
111 do_one_test (impl, (char *) (buf1 + align), seek_char, result);
114 static void
115 do_random_tests (void)
117 size_t i, j, n, align, pos, len;
118 int seek_char;
119 char *result;
120 unsigned char *p = buf1 + page_size - 512;
122 for (n = 0; n < ITERATIONS; n++)
124 align = random () & 15;
125 pos = random () & 511;
126 if (pos + align >= 512)
127 pos = 511 - align - (random () & 7);
128 len = random () & 511;
129 if (len + align >= 512)
130 len = 512 - align - (random () & 7);
131 if (pos >= len)
132 continue;
133 seek_char = random () & 255;
134 j = len + align + 64;
135 if (j > 512)
136 j = 512;
138 for (i = 0; i < j; i++)
140 if (i == pos + align)
141 p[i] = seek_char;
142 else
144 p[i] = random () & 255;
145 if (i < pos + align && p[i] == seek_char)
146 p[i] = seek_char + 13;
150 if (align)
152 p[align - 1] = seek_char;
153 if (align > 4)
154 p[align - 4] = seek_char;
157 assert (pos < len);
158 size_t r = random ();
159 if ((r & 31) == 0)
160 len = ~(uintptr_t) (p + align) - ((r >> 5) & 31);
161 result = (char *) (p + pos + align);
163 FOR_EACH_IMPL (impl, 1)
164 if (CALL (impl, (char *) (p + align), seek_char) != result)
166 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
167 n, impl->name, align, seek_char, len, pos,
168 CALL (impl, (char *) (p + align), seek_char),
169 result, p);
170 ret = 1;
173 if (align)
175 p[align - 1] = seek_char;
176 if (align > 4)
177 p[align - 4] = seek_char;
183 test_main (void)
185 size_t i;
187 test_init ();
189 printf ("%20s", "");
190 FOR_EACH_IMPL (impl, 0)
191 printf ("\t%s", impl->name);
192 putchar ('\n');
194 for (i = 1; i < 7; ++i)
196 do_test (0, 16 << i, 2048, 23);
197 do_test (i, 64, 256, 23);
198 do_test (0, 16 << i, 2048, 0);
199 do_test (i, 64, 256, 0);
201 do_test (getpagesize () - i, 64, 256, 23);
202 do_test (getpagesize () - i, 64, 256, 0);
204 for (i = 1; i < 32; ++i)
206 do_test (0, i, i + 1, 23);
207 do_test (0, i, i + 1, 0);
209 do_test (getpagesize () - 7, i, i + 1, 23);
210 do_test (getpagesize () - i / 2, i, i + 1, 23);
211 do_test (getpagesize () - i, i, i + 1, 23);
214 do_random_tests ();
215 do_test_bz29234 ();
216 return ret;
219 #include <support/test-driver.c>