1 /*
2  * Copyright (C) 2002 by Red Hat, Incorporated. All rights reserved.
3  *
4  * Permission to use, copy, modify, and distribute this software
5  * is freely granted, provided that this notice is preserved.
6  */
7 
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 
12 #ifndef MAX_1
13 #if defined(__SPU__) || __SIZEOF_SIZE_T__ == 2
14 #define MAX_1 11000
15 #else
16 #define MAX_1 33000
17 #endif
18 #endif
19 
20 /* Ignore warnings about odd (but intended) use of functions */
21 #pragma GCC diagnostic ignored "-Wmemset-transposed-args"
22 #ifndef __clang__
23 #pragma GCC diagnostic ignored "-Wstringop-truncation"
24 #endif
25 #pragma GCC diagnostic ignored "-Wunused-value"
26 
27 #define MAX_2 (2 * MAX_1 + MAX_1 / 10)
28 
eprintf(int line,char * result,char * expected,int size)29 void eprintf (int line, char *result, char *expected, int size)
30 {
31   if (size != 0)
32     printf ("Failure at line %d, result is <%.*s>, should be <%s> of size %d\n",
33              line, size, result, expected, size);
34   else
35     printf ("Failure at line %d, result is <%s>, should be <%s>\n",
36              line, result, expected);
37 }
38 
mycopy(char * target,char * source,int size)39 void mycopy (char *target, char *source, int size)
40 {
41   int i;
42 
43   for (i = 0; i < size; ++i)
44     {
45       target[i] = source[i];
46     }
47 }
48 
myset(char * target,char ch,int size)49 void myset (char *target, char ch, int size)
50 {
51   int i;
52 
53   for (i = 0; i < size; ++i)
54     {
55       target[i] = ch;
56     }
57 }
58 
getbuf(size_t size,const char * template)59 char *getbuf(size_t size, const char *template)
60 {
61     char *buf = calloc(size, 1);
62     if (!buf) {
63         printf("not enough memory\n");
64         exit(77);
65     }
66     if (template)
67         strcpy(buf, template);
68     return buf;
69 }
70 
main(void)71 int main(void)
72 {
73   char first_char;
74   char second_char;
75   char array[] = "abcdefghijklmnopqrstuvwxz";
76   char array2[] = "0123456789!@#$%^&*(";
77   char *tmp1, *tmp2, *tmp3, *tmp4, *tmp5, *tmp6, *tmp7;
78   unsigned i, j, k, x, z = 0, align_test_iterations;
79 
80   int test_failed = 0;
81 
82   char *target = getbuf(MAX_1, "A");
83   char *buffer2 = getbuf(MAX_1, NULL);
84   char *buffer3 = getbuf(MAX_1, NULL);
85   char *buffer4 = getbuf(MAX_1, NULL);
86   char *buffer5 = getbuf(MAX_2, NULL);
87   char *buffer6 = getbuf(MAX_2, NULL);
88   char *buffer7 = getbuf(MAX_2, NULL);
89   char *expected = getbuf(MAX_1, NULL);
90 
91   tmp1 = target;
92   tmp2 = buffer2;
93   tmp3 = buffer3;
94   tmp4 = buffer4;
95   tmp5 = buffer5;
96   tmp7 = buffer7;
97 
98   tmp2[0] = 'Z';
99   tmp2[1] = '\0';
100 
101   if (memset (target, 'X', 0) != target ||
102       memcpy (target, "Y", 0) != target ||
103       memmove (target, "K", 0) != target ||
104       strncpy (tmp2, "4", 0) != tmp2 ||
105       strncat (tmp2, "123", 0) != tmp2 ||
106       strcat (target, "") != target)
107     {
108       eprintf (__LINE__, target, "A", 0);
109       test_failed = 1;
110     }
111 
112   if (strcmp (target, "A") || strlen(target) != 1 || memchr (target, 'A', 0) != NULL
113       || memcmp (target, "J", 0) || strncmp (target, "A", 1) || strncmp (target, "J", 0) ||
114       tmp2[0] != 'Z' || tmp2[1] != '\0')
115     {
116       eprintf (__LINE__, target, "A", 0);
117       test_failed = 1;
118     }
119 
120   tmp2[2] = 'A';
121   if (strcpy (target, "") != target ||
122       strncpy (tmp2, "", 4) != tmp2 ||
123       strcat (target, "") != target)
124     {
125       eprintf (__LINE__, target, "", 0);
126       test_failed = 1;
127     }
128 
129   if (target[0] != '\0' || strncmp (target, "", 1) ||
130       memcmp (tmp2, "\0\0\0\0", 4))
131     {
132       eprintf (__LINE__, target, "", 0);
133       test_failed = 1;
134     }
135 
136   tmp2[2] = 'A';
137   if (strncat (tmp2, "1", 3) != tmp2 ||
138       memcmp (tmp2, "1\0A", 3))
139     {
140       eprintf (__LINE__, tmp2, "1\0A", 3);
141       test_failed = 1;
142     }
143 
144   if (strcpy (tmp3, target) != tmp3 ||
145       strcat (tmp3, "X") != tmp3 ||
146       strncpy (tmp2, "X", 2) != tmp2 ||
147       memset (target, tmp2[0], 1) != target)
148     {
149       eprintf (__LINE__, target, "X", 0);
150       test_failed = 1;
151     }
152 
153   if (strcmp (target, "X") || strlen (target) != 1 ||
154       memchr (target, 'X', 2) != target ||
155       strchr (target, 'X') != target ||
156       memchr (target, 'Y', 2) != NULL ||
157       strchr (target, 'Y') != NULL ||
158       strcmp (tmp3, target) ||
159       strncmp (tmp3, target, 2) ||
160       memcmp (target, "K", 0) ||
161       strncmp (target, tmp3, 3))
162     {
163       eprintf (__LINE__, target, "X", 0);
164       test_failed = 1;
165     }
166 
167   if (strcpy (tmp3, "Y") != tmp3 ||
168       strcat (tmp3, "Y") != tmp3 ||
169       memset (target, 'Y', 2) != target)
170     {
171       eprintf (__LINE__, target, "Y", 0);
172       test_failed = 1;
173     }
174 
175   target[2] = '\0';
176   if (memcmp (target, "YY", 2) || strcmp (target, "YY") ||
177       strlen (target) != 2 || memchr (target, 'Y', 2) != target ||
178       strcmp (tmp3, target) ||
179       strncmp (target, tmp3, 3) ||
180       strncmp (target, tmp3, 4) ||
181       strncmp (target, tmp3, 2) ||
182       strchr (target, 'Y') != target)
183     {
184       eprintf (__LINE__, target, "YY", 2);
185       test_failed = 1;
186     }
187 
188   strcpy (target, "WW");
189   if (memcmp (target, "WW", 2) || strcmp (target, "WW") ||
190       strlen (target) != 2 || memchr (target, 'W', 2) != target ||
191       strchr (target, 'W') != target)
192     {
193       eprintf (__LINE__, target, "WW", 2);
194       test_failed = 1;
195     }
196 
197   if (strncpy (target, "XX", 16) != target ||
198       memcmp (target, "XX\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
199     {
200       eprintf (__LINE__, target, "XX\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16);
201       test_failed = 1;
202     }
203 
204   if (strcpy (tmp3, "ZZ") != tmp3 ||
205       strcat (tmp3, "Z") != tmp3 ||
206       memcpy (tmp4, "Z", 2) != tmp4 ||
207       strcat (tmp4, "ZZ") != tmp4 ||
208       memset (target, 'Z', 3) != target)
209     {
210       eprintf (__LINE__, target, "ZZZ", 3);
211       test_failed = 1;
212     }
213 
214   target[3] = '\0';
215   tmp5[0] = '\0';
216   strncat (tmp5, "123", 2);
217   if (memcmp (target, "ZZZ", 3) || strcmp (target, "ZZZ") ||
218       strcmp (tmp3, target) || strcmp (tmp4, target) ||
219       strncmp (target, "ZZZ", 4) || strncmp (target, "ZZY", 3) <= 0 ||
220       strncmp ("ZZY", target, 4) >= 0 ||
221       memcmp (tmp5, "12", 3) ||
222       strlen (target) != 3)
223     {
224       eprintf (__LINE__, target, "ZZZ", 3);
225       test_failed = 1;
226     }
227 
228   target[2] = 'K';
229   if (memcmp (target, "ZZZ", 2) || strcmp (target, "ZZZ") >= 0 ||
230       memcmp (target, "ZZZ", 3) >= 0 || strlen (target) != 3 ||
231       memchr (target, 'K', 3) != target + 2 ||
232       strncmp (target, "ZZZ", 2) || strncmp (target, "ZZZ", 4) >= 0 ||
233       strchr (target, 'K') != target + 2)
234     {
235       eprintf (__LINE__, target, "ZZK", 3);
236       test_failed = 1;
237     }
238 
239   strcpy (target, "AAA");
240   if (memcmp (target, "AAA", 3) || strcmp (target, "AAA") ||
241       strncmp (target, "AAA", 3) ||
242       strlen (target) != 3)
243     {
244       eprintf (__LINE__, target, "AAA", 3);
245       test_failed = 1;
246     }
247 
248   j = 5;
249   while (j < MAX_1)
250     {
251       for (i = j-1; i <= j+1; ++i)
252         {
253 	  /* don't bother checking unaligned data in the larger
254 	     sizes since it will waste time without performing additional testing */
255 	  if (i <= 16 * sizeof(long))
256 	    {
257 	      align_test_iterations = 2*sizeof(long);
258               if (i <= 2 * sizeof(long) + 1)
259                 z = 2;
260 	      else
261 	        z = 2 * sizeof(long);
262             }
263 	  else
264             {
265 	      align_test_iterations = 1;
266             }
267 
268 	  for (x = 0; x < align_test_iterations; ++x)
269 	    {
270 	      tmp1 = target + x;
271 	      tmp2 = buffer2 + x;
272 	      tmp3 = buffer3 + x;
273 	      tmp4 = buffer4 + x;
274 	      tmp5 = buffer5 + x;
275 	      tmp6 = buffer6 + x;
276 
277 	      first_char = array[i % (sizeof(array) - 1)];
278 	      second_char = array2[i % (sizeof(array2) - 1)];
279 	      memset (tmp1, first_char, i);
280 	      mycopy (tmp2, tmp1, i);
281 	      myset (tmp2 + z, second_char, i - z - 1);
282 	      if (memcpy (tmp1 + z, tmp2 + z, i - z - 1) != tmp1 + z)
283 		{
284 		  printf ("error at line %d\n", __LINE__);
285 		  test_failed = 1;
286 		}
287 
288 	      tmp1[i] = '\0';
289 	      tmp2[i] = '\0';
290 	      if (strcpy (expected, tmp2) != expected)
291 		{
292 		  printf ("error at line %d\n", __LINE__);
293 		  test_failed = 1;
294 		}
295 	      tmp2[i-z] = first_char + 1;
296 	      if (memmove (tmp2 + z + 1, tmp2 + z, i - z - 1) != tmp2 + z + 1 ||
297 		  memset (tmp3, first_char, i) != tmp3)
298 		{
299 		  printf ("error at line %d\n", __LINE__);
300 		  test_failed = 1;
301 		}
302 
303 	      myset (tmp4, first_char, i);
304 	      tmp5[0] = '\0';
305 	      if (strncpy (tmp5, tmp1, i+1) != tmp5 ||
306 		  strcat (tmp5, tmp1) != tmp5)
307 		{
308 		  printf ("error at line %d\n", __LINE__);
309 		  test_failed = 1;
310 		}
311 	      mycopy (tmp6, tmp1, i);
312 	      mycopy (tmp6 + i, tmp1, i + 1);
313 
314 	      tmp7[2*i+z] = second_char;
315               strcpy (tmp7, tmp1);
316 
317 	      strchr (tmp1, second_char);
318 
319 	      if (memcmp (tmp1, expected, i) || strcmp (tmp1, expected) ||
320 		  strncmp (tmp1, expected, i) ||
321                   strncmp (tmp1, expected, i+1) ||
322 		  strcmp (tmp1, tmp2) >= 0 || memcmp (tmp1, tmp2, i) >= 0 ||
323 		  strncmp (tmp1, tmp2, i+1) >= 0 ||
324 		  strlen (tmp1) != i || memchr (tmp1, first_char, i) != tmp1 ||
325 		  strchr (tmp1, first_char) != tmp1 ||
326 		  memchr (tmp1, second_char, i) != tmp1 + z ||
327 		  strchr (tmp1, second_char) != tmp1 + z ||
328 		  strcmp (tmp5, tmp6) ||
329 		  strncat (tmp7, tmp1, i+2) != tmp7 ||
330 		  strcmp (tmp7, tmp6) ||
331 		  tmp7[2*i+z] != second_char)
332 		{
333 		  eprintf (__LINE__, tmp1, expected, 0);
334 		  printf ("x is %d\n",x);
335 		  printf ("i is %d\n", i);
336 		  printf ("tmp1 is <%p>\n", tmp1);
337 		  printf ("tmp5 is <%p> <%s>\n", tmp5, tmp5);
338 		  printf ("tmp6 is <%p> <%s>\n", tmp6, tmp6);
339 		  test_failed = 1;
340 		}
341 
342 	      for (k = 1; k <= align_test_iterations && k <= i; ++k)
343 		{
344 		  if (memcmp (tmp3, tmp4, i - k + 1) != 0 ||
345 		      strncmp (tmp3, tmp4, i - k + 1) != 0)
346 		    {
347 		      printf ("Failure at line %d, comparing %.*s with %.*s\n",
348 			      __LINE__, i, tmp3, i, tmp4);
349 		      test_failed = 1;
350 		    }
351 		  tmp4[i-k] = first_char + 1;
352 		  if (memcmp (tmp3, tmp4, i) >= 0 ||
353 		      strncmp (tmp3, tmp4, i) >= 0 ||
354 		      memcmp (tmp4, tmp3, i) <= 0 ||
355 		      strncmp (tmp4, tmp3, i) <= 0)
356 		    {
357 		      printf ("Failure at line %d, comparing %.*s with %.*s\n",
358 			      __LINE__, i, tmp3, i, tmp4);
359 		      test_failed = 1;
360 		    }
361 		  tmp4[i-k] = first_char;
362 		}
363 	    }
364         }
365       j = ((2 * j) >> 2) << 2;
366     }
367 
368   if (test_failed)
369     abort();
370   else
371     exit(0);
372 }
373