1 /* Test and measure memccpy functions.
2    Copyright (C) 1999-2022 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 #define TEST_MAIN
20 #define TEST_NAME "memccpy"
21 #include "test-string.h"
22 
23 IMPL (memccpy, 1)
24 
25 /* Naive implementation to verify results.  */
26 void *
simple_memccpy(void * dst,const void * src,int c,size_t n)27 simple_memccpy (void *dst, const void *src, int c, size_t n)
28 {
29   const char *s = src;
30   char *d = dst;
31 
32   while (n-- > 0)
33     if ((*d++ = *s++) == (char) c)
34       return d;
35 
36   return NULL;
37 }
38 
39 typedef void *(*proto_t) (void *, const void *, int c, size_t);
40 
41 static void
do_one_test(impl_t * impl,void * dst,const void * src,int c,size_t len,size_t n)42 do_one_test (impl_t *impl, void *dst, const void *src, int c, size_t len,
43 	     size_t n)
44 {
45   void *expect = len > n ? NULL : (char *) dst + len;
46   if (CALL (impl, dst, src, c, n) != expect)
47     {
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;
52     }
53 
54   if (memcmp (dst, src, len > n ? n : len) != 0)
55     {
56       error (0, 0, "Wrong result in function %s", impl->name);
57       ret = 1;
58       return;
59     }
60 }
61 
62 static void
do_test(size_t align1,size_t align2,int c,size_t len,size_t n,int max_char)63 do_test (size_t align1, size_t align2, int c, size_t len, size_t n,
64 	 int max_char)
65 {
66   size_t i;
67   char *s1, *s2;
68 
69   align1 &= 7;
70   if (align1 + len >= page_size)
71     return;
72 
73   align2 &= 7;
74   if (align2 + len >= page_size)
75     return;
76 
77   s1 = (char *) (buf1 + align1);
78   s2 = (char *) (buf2 + align2);
79 
80   for (i = 0; i < len - 1; ++i)
81     {
82       s1[i] = 32 + 23 * i % (max_char - 32);
83       if (s1[i] == (char) c)
84 	--s1[i];
85     }
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);
89 
90   FOR_EACH_IMPL (impl, 0)
91     do_one_test (impl, s2, s1, c, len, n);
92 }
93 
94 static void
do_random_tests(void)95 do_random_tests (void)
96 {
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;
101 
102   for (n = 0; n < ITERATIONS; n++)
103     {
104       mode = random ();
105       c = random ();
106       if (mode & 1)
107 	{
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)
115 	    {
116 	      j = align1;
117 	      align1 = align2;
118 	      align2 = j;
119 	    }
120 	  if (mode & 8)
121 	    len = size - (random () & 31);
122 	  else
123 	    len = 512;
124 	  if (len >= 512)
125 	    len = random () & 511;
126 	}
127       else
128 	{
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)
139 	    {
140 	      size = random () & 511;
141 	      if (size + j > 512)
142 		size = 512 - j - (random() & 31);
143 	    }
144 	  else
145 	    size = 512 - j;
146 	  if ((mode & 8) && len + j >= 512)
147 	    len = 512 - j - (random () & 7);
148 	}
149       j = len + align1 + 64;
150       if (j > 512)
151 	j = 512;
152       for (i = 0; i < j; i++)
153 	{
154 	  if (i == len + align1)
155 	    p1[i] = c;
156 	  else
157 	    {
158 	      p1[i] = random () & 255;
159 	      if (i >= align1 && i < len + align1 && p1[i] == c)
160 		p1[i] = (random () & 127) + 3 + c;
161 	    }
162 	}
163 
164       FOR_EACH_IMPL (impl, 1)
165 	{
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;
173 
174 	  if (res != expect)
175 	    {
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;
179 	    }
180 	  for (j = 0; j < align2 + 64; ++j)
181 	    {
182 	      if (p2[j - 64] != '\1')
183 		{
184 		  error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
185 			 n, impl->name, align1, align2, len);
186 		  ret = 1;
187 		  break;
188 		}
189 	    }
190 	  j = align2 + len + 1;
191 	  if (size + align2 < j)
192 	    j = size + align2;
193 	  for (; j < 512; ++j)
194 	    {
195 	      if (p2[j] != '\1')
196 		{
197 		  error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
198 			 n, impl->name, align1, align2, len);
199 		  ret = 1;
200 		  break;
201 		}
202 	    }
203 	  j = len + 1;
204 	  if (size < j)
205 	    j = size;
206 	  if (memcmp (p1 + align1, p2 + align2, j))
207 	    {
208 	      error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
209 		     n, impl->name, align1, align2, len);
210 	      ret = 1;
211 	    }
212 	}
213     }
214 }
215 
216 int
test_main(void)217 test_main (void)
218 {
219   size_t i;
220 
221   test_init ();
222 
223   printf ("%28s", "");
224   FOR_EACH_IMPL (impl, 0)
225     printf ("\t%s", impl->name);
226   putchar ('\n');
227 
228   for (i = 1; i < 8; ++i)
229     {
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);
238     }
239 
240   for (i = 1; i < 8; ++i)
241     {
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);
246     }
247 
248   do_random_tests ();
249   return ret;
250 }
251 
252 #include <support/test-driver.c>
253