fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / newlib / testsuite / newlib.string / tstring.c
blobe4339232b607809fbbc5ca9cb0d2da786cee45f7
1 /*
2 * Copyright (C) 2002 by Red Hat, Incorporated. All rights reserved.
4 * Permission to use, copy, modify, and distribute this software
5 * is freely granted, provided that this notice is preserved.
6 */
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
12 #ifndef MAX_1
13 #define MAX_1 33000
14 #endif
16 #define MAX_2 (2 * MAX_1 + MAX_1 / 10)
18 void eprintf (int line, char *result, char *expected, int size)
20 if (size != 0)
21 printf ("Failure at line %d, result is <%.*s>, should be <%s> of size %d\n",
22 line, size, result, expected, size);
23 else
24 printf ("Failure at line %d, result is <%s>, should be <%s>\n",
25 line, result, expected);
28 void mycopy (char *target, char *source, int size)
30 int i;
32 for (i = 0; i < size; ++i)
34 target[i] = source[i];
38 void myset (char *target, char ch, int size)
40 int i;
42 for (i = 0; i < size; ++i)
44 target[i] = ch;
48 int main()
50 char target[MAX_1] = "A";
51 char first_char;
52 char second_char;
53 char array[] = "abcdefghijklmnopqrstuvwxz";
54 char array2[] = "0123456789!@#$%^&*(";
55 char buffer2[MAX_1];
56 char buffer3[MAX_1];
57 char buffer4[MAX_1];
58 char buffer5[MAX_2];
59 char buffer6[MAX_2];
60 char buffer7[MAX_2];
61 char expected[MAX_1];
62 char *tmp1, *tmp2, *tmp3, *tmp4, *tmp5, *tmp6, *tmp7;
63 int i, j, k, x, z, align_test_iterations;
65 int test_failed = 0;
67 tmp1 = target;
68 tmp2 = buffer2;
69 tmp3 = buffer3;
70 tmp4 = buffer4;
71 tmp5 = buffer5;
72 tmp6 = buffer6;
73 tmp7 = buffer7;
75 tmp2[0] = 'Z';
76 tmp2[1] = '\0';
78 if (memset (target, 'X', 0) != target ||
79 memcpy (target, "Y", 0) != target ||
80 memmove (target, "K", 0) != target ||
81 strncpy (tmp2, "4", 0) != tmp2 ||
82 strncat (tmp2, "123", 0) != tmp2 ||
83 strcat (target, "") != target)
85 eprintf (__LINE__, target, "A", 0);
86 test_failed = 1;
89 if (strcmp (target, "A") || strlen(target) != 1 || memchr (target, 'A', 0) != NULL
90 || memcmp (target, "J", 0) || strncmp (target, "A", 1) || strncmp (target, "J", 0) ||
91 tmp2[0] != 'Z' || tmp2[1] != '\0')
93 eprintf (__LINE__, target, "A", 0);
94 test_failed = 1;
97 tmp2[2] = 'A';
98 if (strcpy (target, "") != target ||
99 strncpy (tmp2, "", 4) != tmp2 ||
100 strcat (target, "") != target)
102 eprintf (__LINE__, target, "", 0);
103 test_failed = 1;
106 if (target[0] != '\0' || strncmp (target, "", 1) ||
107 memcmp (tmp2, "\0\0\0\0", 4))
109 eprintf (__LINE__, target, "", 0);
110 test_failed = 1;
113 tmp2[2] = 'A';
114 if (strncat (tmp2, "1", 3) != tmp2 ||
115 memcmp (tmp2, "1\0A", 3))
117 eprintf (__LINE__, tmp2, "1\0A", 3);
118 test_failed = 1;
121 if (strcpy (tmp3, target) != tmp3 ||
122 strcat (tmp3, "X") != tmp3 ||
123 strncpy (tmp2, "X", 2) != tmp2 ||
124 memset (target, tmp2[0], 1) != target)
126 eprintf (__LINE__, target, "X", 0);
127 test_failed = 1;
130 if (strcmp (target, "X") || strlen (target) != 1 ||
131 memchr (target, 'X', 2) != target ||
132 strchr (target, 'X') != target ||
133 memchr (target, 'Y', 2) != NULL ||
134 strchr (target, 'Y') != NULL ||
135 strcmp (tmp3, target) ||
136 strncmp (tmp3, target, 2) ||
137 memcmp (target, "K", 0) ||
138 strncmp (target, tmp3, 3))
140 eprintf (__LINE__, target, "X", 0);
141 test_failed = 1;
144 if (strcpy (tmp3, "Y") != tmp3 ||
145 strcat (tmp3, "Y") != tmp3 ||
146 memset (target, 'Y', 2) != target)
148 eprintf (__LINE__, target, "Y", 0);
149 test_failed = 1;
152 target[2] = '\0';
153 if (memcmp (target, "YY", 2) || strcmp (target, "YY") ||
154 strlen (target) != 2 || memchr (target, 'Y', 2) != target ||
155 strcmp (tmp3, target) ||
156 strncmp (target, tmp3, 3) ||
157 strncmp (target, tmp3, 4) ||
158 strncmp (target, tmp3, 2) ||
159 strchr (target, 'Y') != target)
161 eprintf (__LINE__, target, "YY", 2);
162 test_failed = 1;
165 strcpy (target, "WW");
166 if (memcmp (target, "WW", 2) || strcmp (target, "WW") ||
167 strlen (target) != 2 || memchr (target, 'W', 2) != target ||
168 strchr (target, 'W') != target)
170 eprintf (__LINE__, target, "WW", 2);
171 test_failed = 1;
174 if (strncpy (target, "XX", 16) != target ||
175 memcmp (target, "XX\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
177 eprintf (__LINE__, target, "XX\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16);
178 test_failed = 1;
181 if (strcpy (tmp3, "ZZ") != tmp3 ||
182 strcat (tmp3, "Z") != tmp3 ||
183 memcpy (tmp4, "Z", 2) != tmp4 ||
184 strcat (tmp4, "ZZ") != tmp4 ||
185 memset (target, 'Z', 3) != target)
187 eprintf (__LINE__, target, "ZZZ", 3);
188 test_failed = 1;
191 target[3] = '\0';
192 tmp5[0] = '\0';
193 strncat (tmp5, "123", 2);
194 if (memcmp (target, "ZZZ", 3) || strcmp (target, "ZZZ") ||
195 strcmp (tmp3, target) || strcmp (tmp4, target) ||
196 strncmp (target, "ZZZ", 4) || strncmp (target, "ZZY", 3) <= 0 ||
197 strncmp ("ZZY", target, 4) >= 0 ||
198 memcmp (tmp5, "12", 3) ||
199 strlen (target) != 3)
201 eprintf (__LINE__, target, "ZZZ", 3);
202 test_failed = 1;
205 target[2] = 'K';
206 if (memcmp (target, "ZZZ", 2) || strcmp (target, "ZZZ") >= 0 ||
207 memcmp (target, "ZZZ", 3) >= 0 || strlen (target) != 3 ||
208 memchr (target, 'K', 3) != target + 2 ||
209 strncmp (target, "ZZZ", 2) || strncmp (target, "ZZZ", 4) >= 0 ||
210 strchr (target, 'K') != target + 2)
212 eprintf (__LINE__, target, "ZZK", 3);
213 test_failed = 1;
216 strcpy (target, "AAA");
217 if (memcmp (target, "AAA", 3) || strcmp (target, "AAA") ||
218 strncmp (target, "AAA", 3) ||
219 strlen (target) != 3)
221 eprintf (__LINE__, target, "AAA", 3);
222 test_failed = 1;
225 j = 5;
226 while (j < MAX_1)
228 for (i = j-1; i <= j+1; ++i)
230 /* don't bother checking unaligned data in the larger
231 sizes since it will waste time without performing additional testing */
232 if (i <= 16 * sizeof(long))
234 align_test_iterations = 2*sizeof(long);
235 if (i <= 2 * sizeof(long) + 1)
236 z = 2;
237 else
238 z = 2 * sizeof(long);
240 else
242 align_test_iterations = 1;
245 for (x = 0; x < align_test_iterations; ++x)
247 tmp1 = target + x;
248 tmp2 = buffer2 + x;
249 tmp3 = buffer3 + x;
250 tmp4 = buffer4 + x;
251 tmp5 = buffer5 + x;
252 tmp6 = buffer6 + x;
254 first_char = array[i % (sizeof(array) - 1)];
255 second_char = array2[i % (sizeof(array2) - 1)];
256 memset (tmp1, first_char, i);
257 mycopy (tmp2, tmp1, i);
258 myset (tmp2 + z, second_char, i - z - 1);
259 if (memcpy (tmp1 + z, tmp2 + z, i - z - 1) != tmp1 + z)
261 printf ("error at line %d\n", __LINE__);
262 test_failed = 1;
265 tmp1[i] = '\0';
266 tmp2[i] = '\0';
267 if (strcpy (expected, tmp2) != expected)
269 printf ("error at line %d\n", __LINE__);
270 test_failed = 1;
272 tmp2[i-z] = first_char + 1;
273 if (memmove (tmp2 + z + 1, tmp2 + z, i - z - 1) != tmp2 + z + 1 ||
274 memset (tmp3, first_char, i) != tmp3)
276 printf ("error at line %d\n", __LINE__);
277 test_failed = 1;
280 myset (tmp4, first_char, i);
281 tmp5[0] = '\0';
282 if (strncpy (tmp5, tmp1, i+1) != tmp5 ||
283 strcat (tmp5, tmp1) != tmp5)
285 printf ("error at line %d\n", __LINE__);
286 test_failed = 1;
288 mycopy (tmp6, tmp1, i);
289 mycopy (tmp6 + i, tmp1, i + 1);
291 tmp7[2*i+z] = second_char;
292 strcpy (tmp7, tmp1);
294 strchr (tmp1, second_char);
296 if (memcmp (tmp1, expected, i) || strcmp (tmp1, expected) ||
297 strncmp (tmp1, expected, i) ||
298 strncmp (tmp1, expected, i+1) ||
299 strcmp (tmp1, tmp2) >= 0 || memcmp (tmp1, tmp2, i) >= 0 ||
300 strncmp (tmp1, tmp2, i+1) >= 0 ||
301 strlen (tmp1) != i || memchr (tmp1, first_char, i) != tmp1 ||
302 strchr (tmp1, first_char) != tmp1 ||
303 memchr (tmp1, second_char, i) != tmp1 + z ||
304 strchr (tmp1, second_char) != tmp1 + z ||
305 strcmp (tmp5, tmp6) ||
306 strncat (tmp7, tmp1, i+2) != tmp7 ||
307 strcmp (tmp7, tmp6) ||
308 tmp7[2*i+z] != second_char)
310 eprintf (__LINE__, tmp1, expected, 0);
311 printf ("x is %d\n",x);
312 printf ("i is %d\n", i);
313 printf ("tmp1 is <%p>\n", tmp1);
314 printf ("tmp5 is <%p> <%s>\n", tmp5, tmp5);
315 printf ("tmp6 is <%p> <%s>\n", tmp6, tmp6);
316 test_failed = 1;
319 for (k = 1; k <= align_test_iterations && k <= i; ++k)
321 if (memcmp (tmp3, tmp4, i - k + 1) != 0 ||
322 strncmp (tmp3, tmp4, i - k + 1) != 0)
324 printf ("Failure at line %d, comparing %.*s with %.*s\n",
325 __LINE__, i, tmp3, i, tmp4);
326 test_failed = 1;
328 tmp4[i-k] = first_char + 1;
329 if (memcmp (tmp3, tmp4, i) >= 0 ||
330 strncmp (tmp3, tmp4, i) >= 0 ||
331 memcmp (tmp4, tmp3, i) <= 0 ||
332 strncmp (tmp4, tmp3, i) <= 0)
334 printf ("Failure at line %d, comparing %.*s with %.*s\n",
335 __LINE__, i, tmp3, i, tmp4);
336 test_failed = 1;
338 tmp4[i-k] = first_char;
342 j = ((2 * j) >> 2) << 2;
345 if (test_failed)
346 abort();
347 else
348 exit(0);