1 /* Test and measure memchr functions.
2    Copyright (C) 1999-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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.
14 
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/>.  */
18 
19 #include <assert.h>
20 
21 #define TEST_MAIN
22 #define TEST_NAME "rawmemchr"
23 #include "test-string.h"
24 
25 typedef char *(*proto_t) (const char *, int);
26 char *simple_rawmemchr (const char *, int);
27 
28 IMPL (simple_rawmemchr, 0)
29 IMPL (rawmemchr, 1)
30 
31 char *
simple_rawmemchr(const char * s,int c)32 simple_rawmemchr (const char *s, int c)
33 {
34   while (1)
35     if (*s++ == (char) c)
36       return (char *) s - 1;
37   return NULL;
38 }
39 
40 static void
do_one_test(impl_t * impl,const char * s,int c,char * exp_res)41 do_one_test (impl_t *impl, const char *s, int c, char *exp_res)
42 {
43   char *res = CALL (impl, s, c);
44   if (res != exp_res)
45     {
46       error (0, 0, "Wrong result in function %s %p %p", impl->name,
47 	     res, exp_res);
48       ret = 1;
49       return;
50     }
51 }
52 
53 static void
do_test(size_t align,size_t pos,size_t len,int seek_char)54 do_test (size_t align, size_t pos, size_t len, int seek_char)
55 {
56   size_t i;
57   char *result;
58 
59   align &= 7;
60   if (align + len >= page_size)
61     return;
62 
63   for (i = 0; i < len; ++i)
64     {
65       buf1[align + i] = 1 + 23 * i % 127;
66       if (buf1[align + i] == seek_char)
67 	buf1[align + i] = seek_char + 1;
68     }
69   buf1[align + len] = 0;
70 
71   assert (pos < len);
72 
73   buf1[align + pos] = seek_char;
74   buf1[align + len] = -seek_char;
75   result = (char *) (buf1 + align + pos);
76 
77   FOR_EACH_IMPL (impl, 0)
78     do_one_test (impl, (char *) (buf1 + align), seek_char, result);
79 }
80 
81 static void
do_random_tests(void)82 do_random_tests (void)
83 {
84   size_t i, j, n, align, pos, len;
85   int seek_char;
86   char *result;
87   unsigned char *p = buf1 + page_size - 512;
88 
89   for (n = 0; n < ITERATIONS; n++)
90     {
91       align = random () & 15;
92       pos = random () & 511;
93       if (pos + align >= 512)
94 	pos = 511 - align - (random () & 7);
95       len = random () & 511;
96       if (len + align >= 512)
97 	len = 512 - align - (random () & 7);
98       if (pos >= len)
99 	continue;
100       seek_char = random () & 255;
101       j = len + align + 64;
102       if (j > 512)
103 	j = 512;
104 
105       for (i = 0; i < j; i++)
106 	{
107 	  if (i == pos + align)
108 	    p[i] = seek_char;
109 	  else
110 	    {
111 	      p[i] = random () & 255;
112 	      if (i < pos + align && p[i] == seek_char)
113 		p[i] = seek_char + 13;
114 	    }
115 	}
116 
117       assert (pos < len);
118       size_t r = random ();
119       if ((r & 31) == 0)
120 	len = ~(uintptr_t) (p + align) - ((r >> 5) & 31);
121       result = (char *) (p + pos + align);
122 
123       FOR_EACH_IMPL (impl, 1)
124 	if (CALL (impl, (char *) (p + align), seek_char) != result)
125 	  {
126 	    error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
127 		   n, impl->name, align, seek_char, len, pos,
128 		   CALL (impl, (char *) (p + align), seek_char),
129 		   result, p);
130 	    ret = 1;
131 	  }
132     }
133 }
134 
135 int
test_main(void)136 test_main (void)
137 {
138   size_t i;
139 
140   test_init ();
141 
142   printf ("%20s", "");
143   FOR_EACH_IMPL (impl, 0)
144     printf ("\t%s", impl->name);
145   putchar ('\n');
146 
147   for (i = 1; i < 7; ++i)
148     {
149       do_test (0, 16 << i, 2048, 23);
150       do_test (i, 64, 256, 23);
151       do_test (0, 16 << i, 2048, 0);
152       do_test (i, 64, 256, 0);
153     }
154   for (i = 1; i < 32; ++i)
155     {
156       do_test (0, i, i + 1, 23);
157       do_test (0, i, i + 1, 0);
158     }
159 
160   do_random_tests ();
161   return ret;
162 }
163 
164 #include <support/test-driver.c>
165