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
59 static char target[MAX_1] = "A";
60 static char buffer2[MAX_1];
61 static char buffer3[MAX_1];
62 static char buffer4[MAX_1];
63 static char buffer5[MAX_2];
64 static char buffer6[MAX_2];
65 static char buffer7[MAX_2];
66 static char expected[MAX_1];
67
main(void)68 int main(void)
69 {
70 char first_char;
71 char second_char;
72 char array[] = "abcdefghijklmnopqrstuvwxz";
73 char array2[] = "0123456789!@#$%^&*(";
74 char *tmp1, *tmp2, *tmp3, *tmp4, *tmp5, *tmp6, *tmp7;
75 unsigned i, j, k, x, z = 0, align_test_iterations;
76
77 int test_failed = 0;
78
79 tmp1 = target;
80 tmp2 = buffer2;
81 tmp3 = buffer3;
82 tmp4 = buffer4;
83 tmp5 = buffer5;
84 tmp6 = buffer6;
85 tmp7 = buffer7;
86
87 tmp2[0] = 'Z';
88 tmp2[1] = '\0';
89
90 if (memset (target, 'X', 0) != target ||
91 memcpy (target, "Y", 0) != target ||
92 memmove (target, "K", 0) != target ||
93 strncpy (tmp2, "4", 0) != tmp2 ||
94 strncat (tmp2, "123", 0) != tmp2 ||
95 strcat (target, "") != target)
96 {
97 eprintf (__LINE__, target, "A", 0);
98 test_failed = 1;
99 }
100
101 if (strcmp (target, "A") || strlen(target) != 1 || memchr (target, 'A', 0) != NULL
102 || memcmp (target, "J", 0) || strncmp (target, "A", 1) || strncmp (target, "J", 0) ||
103 tmp2[0] != 'Z' || tmp2[1] != '\0')
104 {
105 eprintf (__LINE__, target, "A", 0);
106 test_failed = 1;
107 }
108
109 tmp2[2] = 'A';
110 if (strcpy (target, "") != target ||
111 strncpy (tmp2, "", 4) != tmp2 ||
112 strcat (target, "") != target)
113 {
114 eprintf (__LINE__, target, "", 0);
115 test_failed = 1;
116 }
117
118 if (target[0] != '\0' || strncmp (target, "", 1) ||
119 memcmp (tmp2, "\0\0\0\0", 4))
120 {
121 eprintf (__LINE__, target, "", 0);
122 test_failed = 1;
123 }
124
125 tmp2[2] = 'A';
126 if (strncat (tmp2, "1", 3) != tmp2 ||
127 memcmp (tmp2, "1\0A", 3))
128 {
129 eprintf (__LINE__, tmp2, "1\0A", 3);
130 test_failed = 1;
131 }
132
133 if (strcpy (tmp3, target) != tmp3 ||
134 strcat (tmp3, "X") != tmp3 ||
135 strncpy (tmp2, "X", 2) != tmp2 ||
136 memset (target, tmp2[0], 1) != target)
137 {
138 eprintf (__LINE__, target, "X", 0);
139 test_failed = 1;
140 }
141
142 if (strcmp (target, "X") || strlen (target) != 1 ||
143 memchr (target, 'X', 2) != target ||
144 strchr (target, 'X') != target ||
145 memchr (target, 'Y', 2) != NULL ||
146 strchr (target, 'Y') != NULL ||
147 strcmp (tmp3, target) ||
148 strncmp (tmp3, target, 2) ||
149 memcmp (target, "K", 0) ||
150 strncmp (target, tmp3, 3))
151 {
152 eprintf (__LINE__, target, "X", 0);
153 test_failed = 1;
154 }
155
156 if (strcpy (tmp3, "Y") != tmp3 ||
157 strcat (tmp3, "Y") != tmp3 ||
158 memset (target, 'Y', 2) != target)
159 {
160 eprintf (__LINE__, target, "Y", 0);
161 test_failed = 1;
162 }
163
164 target[2] = '\0';
165 if (memcmp (target, "YY", 2) || strcmp (target, "YY") ||
166 strlen (target) != 2 || memchr (target, 'Y', 2) != target ||
167 strcmp (tmp3, target) ||
168 strncmp (target, tmp3, 3) ||
169 strncmp (target, tmp3, 4) ||
170 strncmp (target, tmp3, 2) ||
171 strchr (target, 'Y') != target)
172 {
173 eprintf (__LINE__, target, "YY", 2);
174 test_failed = 1;
175 }
176
177 strcpy (target, "WW");
178 if (memcmp (target, "WW", 2) || strcmp (target, "WW") ||
179 strlen (target) != 2 || memchr (target, 'W', 2) != target ||
180 strchr (target, 'W') != target)
181 {
182 eprintf (__LINE__, target, "WW", 2);
183 test_failed = 1;
184 }
185
186 if (strncpy (target, "XX", 16) != target ||
187 memcmp (target, "XX\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
188 {
189 eprintf (__LINE__, target, "XX\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16);
190 test_failed = 1;
191 }
192
193 if (strcpy (tmp3, "ZZ") != tmp3 ||
194 strcat (tmp3, "Z") != tmp3 ||
195 memcpy (tmp4, "Z", 2) != tmp4 ||
196 strcat (tmp4, "ZZ") != tmp4 ||
197 memset (target, 'Z', 3) != target)
198 {
199 eprintf (__LINE__, target, "ZZZ", 3);
200 test_failed = 1;
201 }
202
203 target[3] = '\0';
204 tmp5[0] = '\0';
205 strncat (tmp5, "123", 2);
206 if (memcmp (target, "ZZZ", 3) || strcmp (target, "ZZZ") ||
207 strcmp (tmp3, target) || strcmp (tmp4, target) ||
208 strncmp (target, "ZZZ", 4) || strncmp (target, "ZZY", 3) <= 0 ||
209 strncmp ("ZZY", target, 4) >= 0 ||
210 memcmp (tmp5, "12", 3) ||
211 strlen (target) != 3)
212 {
213 eprintf (__LINE__, target, "ZZZ", 3);
214 test_failed = 1;
215 }
216
217 target[2] = 'K';
218 if (memcmp (target, "ZZZ", 2) || strcmp (target, "ZZZ") >= 0 ||
219 memcmp (target, "ZZZ", 3) >= 0 || strlen (target) != 3 ||
220 memchr (target, 'K', 3) != target + 2 ||
221 strncmp (target, "ZZZ", 2) || strncmp (target, "ZZZ", 4) >= 0 ||
222 strchr (target, 'K') != target + 2)
223 {
224 eprintf (__LINE__, target, "ZZK", 3);
225 test_failed = 1;
226 }
227
228 strcpy (target, "AAA");
229 if (memcmp (target, "AAA", 3) || strcmp (target, "AAA") ||
230 strncmp (target, "AAA", 3) ||
231 strlen (target) != 3)
232 {
233 eprintf (__LINE__, target, "AAA", 3);
234 test_failed = 1;
235 }
236
237 j = 5;
238 while (j < MAX_1)
239 {
240 for (i = j-1; i <= j+1; ++i)
241 {
242 /* don't bother checking unaligned data in the larger
243 sizes since it will waste time without performing additional testing */
244 if (i <= 16 * sizeof(long))
245 {
246 align_test_iterations = 2*sizeof(long);
247 if (i <= 2 * sizeof(long) + 1)
248 z = 2;
249 else
250 z = 2 * sizeof(long);
251 }
252 else
253 {
254 align_test_iterations = 1;
255 }
256
257 for (x = 0; x < align_test_iterations; ++x)
258 {
259 tmp1 = target + x;
260 tmp2 = buffer2 + x;
261 tmp3 = buffer3 + x;
262 tmp4 = buffer4 + x;
263 tmp5 = buffer5 + x;
264 tmp6 = buffer6 + x;
265
266 first_char = array[i % (sizeof(array) - 1)];
267 second_char = array2[i % (sizeof(array2) - 1)];
268 memset (tmp1, first_char, i);
269 mycopy (tmp2, tmp1, i);
270 myset (tmp2 + z, second_char, i - z - 1);
271 if (memcpy (tmp1 + z, tmp2 + z, i - z - 1) != tmp1 + z)
272 {
273 printf ("error at line %d\n", __LINE__);
274 test_failed = 1;
275 }
276
277 tmp1[i] = '\0';
278 tmp2[i] = '\0';
279 if (strcpy (expected, tmp2) != expected)
280 {
281 printf ("error at line %d\n", __LINE__);
282 test_failed = 1;
283 }
284 tmp2[i-z] = first_char + 1;
285 if (memmove (tmp2 + z + 1, tmp2 + z, i - z - 1) != tmp2 + z + 1 ||
286 memset (tmp3, first_char, i) != tmp3)
287 {
288 printf ("error at line %d\n", __LINE__);
289 test_failed = 1;
290 }
291
292 myset (tmp4, first_char, i);
293 tmp5[0] = '\0';
294 if (strncpy (tmp5, tmp1, i+1) != tmp5 ||
295 strcat (tmp5, tmp1) != tmp5)
296 {
297 printf ("error at line %d\n", __LINE__);
298 test_failed = 1;
299 }
300 mycopy (tmp6, tmp1, i);
301 mycopy (tmp6 + i, tmp1, i + 1);
302
303 tmp7[2*i+z] = second_char;
304 strcpy (tmp7, tmp1);
305
306 strchr (tmp1, second_char);
307
308 if (memcmp (tmp1, expected, i) || strcmp (tmp1, expected) ||
309 strncmp (tmp1, expected, i) ||
310 strncmp (tmp1, expected, i+1) ||
311 strcmp (tmp1, tmp2) >= 0 || memcmp (tmp1, tmp2, i) >= 0 ||
312 strncmp (tmp1, tmp2, i+1) >= 0 ||
313 strlen (tmp1) != i || memchr (tmp1, first_char, i) != tmp1 ||
314 strchr (tmp1, first_char) != tmp1 ||
315 memchr (tmp1, second_char, i) != tmp1 + z ||
316 strchr (tmp1, second_char) != tmp1 + z ||
317 strcmp (tmp5, tmp6) ||
318 strncat (tmp7, tmp1, i+2) != tmp7 ||
319 strcmp (tmp7, tmp6) ||
320 tmp7[2*i+z] != second_char)
321 {
322 eprintf (__LINE__, tmp1, expected, 0);
323 printf ("x is %d\n",x);
324 printf ("i is %d\n", i);
325 printf ("tmp1 is <%p>\n", tmp1);
326 printf ("tmp5 is <%p> <%s>\n", tmp5, tmp5);
327 printf ("tmp6 is <%p> <%s>\n", tmp6, tmp6);
328 test_failed = 1;
329 }
330
331 for (k = 1; k <= align_test_iterations && k <= i; ++k)
332 {
333 if (memcmp (tmp3, tmp4, i - k + 1) != 0 ||
334 strncmp (tmp3, tmp4, i - k + 1) != 0)
335 {
336 printf ("Failure at line %d, comparing %.*s with %.*s\n",
337 __LINE__, i, tmp3, i, tmp4);
338 test_failed = 1;
339 }
340 tmp4[i-k] = first_char + 1;
341 if (memcmp (tmp3, tmp4, i) >= 0 ||
342 strncmp (tmp3, tmp4, i) >= 0 ||
343 memcmp (tmp4, tmp3, i) <= 0 ||
344 strncmp (tmp4, tmp3, i) <= 0)
345 {
346 printf ("Failure at line %d, comparing %.*s with %.*s\n",
347 __LINE__, i, tmp3, i, tmp4);
348 test_failed = 1;
349 }
350 tmp4[i-k] = first_char;
351 }
352 }
353 }
354 j = ((2 * j) >> 2) << 2;
355 }
356
357 if (test_failed)
358 abort();
359 else
360 exit(0);
361 }
362