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.
20 #define MAX_2 (2 * MAX_1 + MAX_1 / 10)
22 void eprintf (int line
, char *result
, char *expected
, int size
)
25 printf ("Failure at line %d, result is <%.*s>, should be <%s> of size %d\n",
26 line
, size
, result
, expected
, size
);
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
)
36 for (i
= 0; i
< size
; ++i
)
38 target
[i
] = source
[i
];
42 void myset (char *target
, char ch
, int size
)
46 for (i
= 0; i
< size
; ++i
)
54 char target
[MAX_1
] = "A";
57 char array
[] = "abcdefghijklmnopqrstuvwxz";
58 char array2
[] = "0123456789!@#$%^&*(";
66 char *tmp1
, *tmp2
, *tmp3
, *tmp4
, *tmp5
, *tmp6
, *tmp7
;
67 int i
, j
, k
, x
, z
, align_test_iterations
;
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);
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);
102 if (strcpy (target
, "") != target
||
103 strncpy (tmp2
, "", 4) != tmp2
||
104 strcat (target
, "") != target
)
106 eprintf (__LINE__
, target
, "", 0);
110 if (target
[0] != '\0' || strncmp (target
, "", 1) ||
111 memcmp (tmp2
, "\0\0\0\0", 4))
113 eprintf (__LINE__
, target
, "", 0);
118 if (strncat (tmp2
, "1", 3) != tmp2
||
119 memcmp (tmp2
, "1\0A", 3))
121 eprintf (__LINE__
, tmp2
, "1\0A", 3);
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);
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);
148 if (strcpy (tmp3
, "Y") != tmp3
||
149 strcat (tmp3
, "Y") != tmp3
||
150 memset (target
, 'Y', 2) != target
)
152 eprintf (__LINE__
, target
, "Y", 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);
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);
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);
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);
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);
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);
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);
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)
242 z
= 2 * sizeof(long);
246 align_test_iterations
= 1;
249 for (x
= 0; x
< align_test_iterations
; ++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__
);
271 if (strcpy (expected
, tmp2
) != expected
)
273 printf ("error at line %d\n", __LINE__
);
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__
);
284 myset (tmp4
, first_char
, i
);
286 if (strncpy (tmp5
, tmp1
, i
+1) != tmp5
||
287 strcat (tmp5
, tmp1
) != tmp5
)
289 printf ("error at line %d\n", __LINE__
);
292 mycopy (tmp6
, tmp1
, i
);
293 mycopy (tmp6
+ i
, tmp1
, i
+ 1);
295 tmp7
[2*i
+z
] = second_char
;
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
);
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
);
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
);
342 tmp4
[i
-k
] = first_char
;
346 j
= ((2 * j
) >> 2) << 2;