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.
16 #define MAX_2 (2 * MAX_1 + MAX_1 / 10)
18 void eprintf (int line
, char *result
, char *expected
, int size
)
21 printf ("Failure at line %d, result is <%.*s>, should be <%s> of size %d\n",
22 line
, size
, result
, expected
, size
);
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
)
32 for (i
= 0; i
< size
; ++i
)
34 target
[i
] = source
[i
];
38 void myset (char *target
, char ch
, int size
)
42 for (i
= 0; i
< size
; ++i
)
50 char target
[MAX_1
] = "A";
53 char array
[] = "abcdefghijklmnopqrstuvwxz";
54 char array2
[] = "0123456789!@#$%^&*(";
62 char *tmp1
, *tmp2
, *tmp3
, *tmp4
, *tmp5
, *tmp6
, *tmp7
;
63 int i
, j
, k
, x
, z
, align_test_iterations
;
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);
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);
98 if (strcpy (target
, "") != target
||
99 strncpy (tmp2
, "", 4) != tmp2
||
100 strcat (target
, "") != target
)
102 eprintf (__LINE__
, target
, "", 0);
106 if (target
[0] != '\0' || strncmp (target
, "", 1) ||
107 memcmp (tmp2
, "\0\0\0\0", 4))
109 eprintf (__LINE__
, target
, "", 0);
114 if (strncat (tmp2
, "1", 3) != tmp2
||
115 memcmp (tmp2
, "1\0A", 3))
117 eprintf (__LINE__
, tmp2
, "1\0A", 3);
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);
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);
144 if (strcpy (tmp3
, "Y") != tmp3
||
145 strcat (tmp3
, "Y") != tmp3
||
146 memset (target
, 'Y', 2) != target
)
148 eprintf (__LINE__
, target
, "Y", 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);
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);
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);
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);
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);
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);
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);
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)
238 z
= 2 * sizeof(long);
242 align_test_iterations
= 1;
245 for (x
= 0; x
< align_test_iterations
; ++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__
);
267 if (strcpy (expected
, tmp2
) != expected
)
269 printf ("error at line %d\n", __LINE__
);
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__
);
280 myset (tmp4
, first_char
, i
);
282 if (strncpy (tmp5
, tmp1
, i
+1) != tmp5
||
283 strcat (tmp5
, tmp1
) != tmp5
)
285 printf ("error at line %d\n", __LINE__
);
288 mycopy (tmp6
, tmp1
, i
);
289 mycopy (tmp6
+ i
, tmp1
, i
+ 1);
291 tmp7
[2*i
+z
] = second_char
;
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
);
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
);
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
);
338 tmp4
[i
-k
] = first_char
;
342 j
= ((2 * j
) >> 2) << 2;