Cygwin: //: fetch only one item per loop
[newlib-cygwin.git] / newlib / testsuite / newlib.string / tstring.c
blob9c5acb6d68733da2a2fd51c672a9b0e5cb2910da
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 #ifdef __SPU__
14 #define MAX_1 11000
15 #else
16 #define MAX_1 33000
17 #endif
18 #endif
20 #define MAX_2 (2 * MAX_1 + MAX_1 / 10)
22 void eprintf (int line, char *result, char *expected, int size)
24 if (size != 0)
25 printf ("Failure at line %d, result is <%.*s>, should be <%s> of size %d\n",
26 line, size, result, expected, size);
27 else
28 printf ("Failure at line %d, result is <%s>, should be <%s>\n",
29 line, result, expected);
32 void mycopy (char *target, char *source, int size)
34 int i;
36 for (i = 0; i < size; ++i)
38 target[i] = source[i];
42 void myset (char *target, char ch, int size)
44 int i;
46 for (i = 0; i < size; ++i)
48 target[i] = ch;
52 int main()
54 char target[MAX_1] = "A";
55 char first_char;
56 char second_char;
57 char array[] = "abcdefghijklmnopqrstuvwxz";
58 char array2[] = "0123456789!@#$%^&*(";
59 char buffer2[MAX_1];
60 char buffer3[MAX_1];
61 char buffer4[MAX_1];
62 char buffer5[MAX_2];
63 char buffer6[MAX_2];
64 char buffer7[MAX_2];
65 char expected[MAX_1];
66 char *tmp1, *tmp2, *tmp3, *tmp4, *tmp5, *tmp6, *tmp7;
67 int i, j, k, x, z, align_test_iterations;
69 int test_failed = 0;
71 tmp1 = target;
72 tmp2 = buffer2;
73 tmp3 = buffer3;
74 tmp4 = buffer4;
75 tmp5 = buffer5;
76 tmp6 = buffer6;
77 tmp7 = buffer7;
79 tmp2[0] = 'Z';
80 tmp2[1] = '\0';
82 if (memset (target, 'X', 0) != target ||
83 memcpy (target, "Y", 0) != target ||
84 memmove (target, "K", 0) != target ||
85 strncpy (tmp2, "4", 0) != tmp2 ||
86 strncat (tmp2, "123", 0) != tmp2 ||
87 strcat (target, "") != target)
89 eprintf (__LINE__, target, "A", 0);
90 test_failed = 1;
93 if (strcmp (target, "A") || strlen(target) != 1 || memchr (target, 'A', 0) != NULL
94 || memcmp (target, "J", 0) || strncmp (target, "A", 1) || strncmp (target, "J", 0) ||
95 tmp2[0] != 'Z' || tmp2[1] != '\0')
97 eprintf (__LINE__, target, "A", 0);
98 test_failed = 1;
101 tmp2[2] = 'A';
102 if (strcpy (target, "") != target ||
103 strncpy (tmp2, "", 4) != tmp2 ||
104 strcat (target, "") != target)
106 eprintf (__LINE__, target, "", 0);
107 test_failed = 1;
110 if (target[0] != '\0' || strncmp (target, "", 1) ||
111 memcmp (tmp2, "\0\0\0\0", 4))
113 eprintf (__LINE__, target, "", 0);
114 test_failed = 1;
117 tmp2[2] = 'A';
118 if (strncat (tmp2, "1", 3) != tmp2 ||
119 memcmp (tmp2, "1\0A", 3))
121 eprintf (__LINE__, tmp2, "1\0A", 3);
122 test_failed = 1;
125 if (strcpy (tmp3, target) != tmp3 ||
126 strcat (tmp3, "X") != tmp3 ||
127 strncpy (tmp2, "X", 2) != tmp2 ||
128 memset (target, tmp2[0], 1) != target)
130 eprintf (__LINE__, target, "X", 0);
131 test_failed = 1;
134 if (strcmp (target, "X") || strlen (target) != 1 ||
135 memchr (target, 'X', 2) != target ||
136 strchr (target, 'X') != target ||
137 memchr (target, 'Y', 2) != NULL ||
138 strchr (target, 'Y') != NULL ||
139 strcmp (tmp3, target) ||
140 strncmp (tmp3, target, 2) ||
141 memcmp (target, "K", 0) ||
142 strncmp (target, tmp3, 3))
144 eprintf (__LINE__, target, "X", 0);
145 test_failed = 1;
148 if (strcpy (tmp3, "Y") != tmp3 ||
149 strcat (tmp3, "Y") != tmp3 ||
150 memset (target, 'Y', 2) != target)
152 eprintf (__LINE__, target, "Y", 0);
153 test_failed = 1;
156 target[2] = '\0';
157 if (memcmp (target, "YY", 2) || strcmp (target, "YY") ||
158 strlen (target) != 2 || memchr (target, 'Y', 2) != target ||
159 strcmp (tmp3, target) ||
160 strncmp (target, tmp3, 3) ||
161 strncmp (target, tmp3, 4) ||
162 strncmp (target, tmp3, 2) ||
163 strchr (target, 'Y') != target)
165 eprintf (__LINE__, target, "YY", 2);
166 test_failed = 1;
169 strcpy (target, "WW");
170 if (memcmp (target, "WW", 2) || strcmp (target, "WW") ||
171 strlen (target) != 2 || memchr (target, 'W', 2) != target ||
172 strchr (target, 'W') != target)
174 eprintf (__LINE__, target, "WW", 2);
175 test_failed = 1;
178 if (strncpy (target, "XX", 16) != target ||
179 memcmp (target, "XX\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
181 eprintf (__LINE__, target, "XX\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16);
182 test_failed = 1;
185 if (strcpy (tmp3, "ZZ") != tmp3 ||
186 strcat (tmp3, "Z") != tmp3 ||
187 memcpy (tmp4, "Z", 2) != tmp4 ||
188 strcat (tmp4, "ZZ") != tmp4 ||
189 memset (target, 'Z', 3) != target)
191 eprintf (__LINE__, target, "ZZZ", 3);
192 test_failed = 1;
195 target[3] = '\0';
196 tmp5[0] = '\0';
197 strncat (tmp5, "123", 2);
198 if (memcmp (target, "ZZZ", 3) || strcmp (target, "ZZZ") ||
199 strcmp (tmp3, target) || strcmp (tmp4, target) ||
200 strncmp (target, "ZZZ", 4) || strncmp (target, "ZZY", 3) <= 0 ||
201 strncmp ("ZZY", target, 4) >= 0 ||
202 memcmp (tmp5, "12", 3) ||
203 strlen (target) != 3)
205 eprintf (__LINE__, target, "ZZZ", 3);
206 test_failed = 1;
209 target[2] = 'K';
210 if (memcmp (target, "ZZZ", 2) || strcmp (target, "ZZZ") >= 0 ||
211 memcmp (target, "ZZZ", 3) >= 0 || strlen (target) != 3 ||
212 memchr (target, 'K', 3) != target + 2 ||
213 strncmp (target, "ZZZ", 2) || strncmp (target, "ZZZ", 4) >= 0 ||
214 strchr (target, 'K') != target + 2)
216 eprintf (__LINE__, target, "ZZK", 3);
217 test_failed = 1;
220 strcpy (target, "AAA");
221 if (memcmp (target, "AAA", 3) || strcmp (target, "AAA") ||
222 strncmp (target, "AAA", 3) ||
223 strlen (target) != 3)
225 eprintf (__LINE__, target, "AAA", 3);
226 test_failed = 1;
229 j = 5;
230 while (j < MAX_1)
232 for (i = j-1; i <= j+1; ++i)
234 /* don't bother checking unaligned data in the larger
235 sizes since it will waste time without performing additional testing */
236 if (i <= 16 * sizeof(long))
238 align_test_iterations = 2*sizeof(long);
239 if (i <= 2 * sizeof(long) + 1)
240 z = 2;
241 else
242 z = 2 * sizeof(long);
244 else
246 align_test_iterations = 1;
249 for (x = 0; x < align_test_iterations; ++x)
251 tmp1 = target + x;
252 tmp2 = buffer2 + x;
253 tmp3 = buffer3 + x;
254 tmp4 = buffer4 + x;
255 tmp5 = buffer5 + x;
256 tmp6 = buffer6 + x;
258 first_char = array[i % (sizeof(array) - 1)];
259 second_char = array2[i % (sizeof(array2) - 1)];
260 memset (tmp1, first_char, i);
261 mycopy (tmp2, tmp1, i);
262 myset (tmp2 + z, second_char, i - z - 1);
263 if (memcpy (tmp1 + z, tmp2 + z, i - z - 1) != tmp1 + z)
265 printf ("error at line %d\n", __LINE__);
266 test_failed = 1;
269 tmp1[i] = '\0';
270 tmp2[i] = '\0';
271 if (strcpy (expected, tmp2) != expected)
273 printf ("error at line %d\n", __LINE__);
274 test_failed = 1;
276 tmp2[i-z] = first_char + 1;
277 if (memmove (tmp2 + z + 1, tmp2 + z, i - z - 1) != tmp2 + z + 1 ||
278 memset (tmp3, first_char, i) != tmp3)
280 printf ("error at line %d\n", __LINE__);
281 test_failed = 1;
284 myset (tmp4, first_char, i);
285 tmp5[0] = '\0';
286 if (strncpy (tmp5, tmp1, i+1) != tmp5 ||
287 strcat (tmp5, tmp1) != tmp5)
289 printf ("error at line %d\n", __LINE__);
290 test_failed = 1;
292 mycopy (tmp6, tmp1, i);
293 mycopy (tmp6 + i, tmp1, i + 1);
295 tmp7[2*i+z] = second_char;
296 strcpy (tmp7, tmp1);
298 strchr (tmp1, second_char);
300 if (memcmp (tmp1, expected, i) || strcmp (tmp1, expected) ||
301 strncmp (tmp1, expected, i) ||
302 strncmp (tmp1, expected, i+1) ||
303 strcmp (tmp1, tmp2) >= 0 || memcmp (tmp1, tmp2, i) >= 0 ||
304 strncmp (tmp1, tmp2, i+1) >= 0 ||
305 strlen (tmp1) != i || memchr (tmp1, first_char, i) != tmp1 ||
306 strchr (tmp1, first_char) != tmp1 ||
307 memchr (tmp1, second_char, i) != tmp1 + z ||
308 strchr (tmp1, second_char) != tmp1 + z ||
309 strcmp (tmp5, tmp6) ||
310 strncat (tmp7, tmp1, i+2) != tmp7 ||
311 strcmp (tmp7, tmp6) ||
312 tmp7[2*i+z] != second_char)
314 eprintf (__LINE__, tmp1, expected, 0);
315 printf ("x is %d\n",x);
316 printf ("i is %d\n", i);
317 printf ("tmp1 is <%p>\n", tmp1);
318 printf ("tmp5 is <%p> <%s>\n", tmp5, tmp5);
319 printf ("tmp6 is <%p> <%s>\n", tmp6, tmp6);
320 test_failed = 1;
323 for (k = 1; k <= align_test_iterations && k <= i; ++k)
325 if (memcmp (tmp3, tmp4, i - k + 1) != 0 ||
326 strncmp (tmp3, tmp4, i - k + 1) != 0)
328 printf ("Failure at line %d, comparing %.*s with %.*s\n",
329 __LINE__, i, tmp3, i, tmp4);
330 test_failed = 1;
332 tmp4[i-k] = first_char + 1;
333 if (memcmp (tmp3, tmp4, i) >= 0 ||
334 strncmp (tmp3, tmp4, i) >= 0 ||
335 memcmp (tmp4, tmp3, i) <= 0 ||
336 strncmp (tmp4, tmp3, i) <= 0)
338 printf ("Failure at line %d, comparing %.*s with %.*s\n",
339 __LINE__, i, tmp3, i, tmp4);
340 test_failed = 1;
342 tmp4[i-k] = first_char;
346 j = ((2 * j) >> 2) << 2;
349 if (test_failed)
350 abort();
351 else
352 exit(0);