4 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 * See https://llvm.org/LICENSE.txt for license information.
6 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
13 #include "stringlib.h"
15 static const struct fun
18 void *(*fun
)(void *s
, int c
, size_t n
);
31 static int test_status
;
32 #define ERR(...) (test_status=1, printf(__VA_ARGS__))
36 static unsigned char sbuf
[LEN
+2*A
];
38 static void *alignup(void *p
)
40 return (void*)(((uintptr_t)p
+ A
-1) & -A
);
43 static void err(const char *name
, unsigned char *src
, int salign
, int c
, int len
)
45 ERR("%s(align %d, %d, %d) failed\n", name
, salign
, c
, len
);
46 ERR("got : %.*s\n", salign
+len
+1, src
);
49 static void test(const struct fun
*fun
, int salign
, int c
, int len
)
51 unsigned char *src
= alignup(sbuf
);
52 unsigned char *s
= src
+ salign
;
56 if (len
> LEN
|| salign
>= A
)
58 for (i
= 0; i
< len
+A
; i
++)
60 for (i
= 0; i
< len
; i
++)
65 p
= fun
->fun(s
, c
, len
);
67 ERR("%s(%p,..) returned %p\n", fun
->name
, s
, p
);
69 for (i
= 0; i
< salign
; i
++) {
71 err(fun
->name
, src
, salign
, c
, len
);
75 for (i
= salign
; i
< len
; i
++) {
76 if (src
[i
] != (unsigned char)c
) {
77 err(fun
->name
, src
, salign
, c
, len
);
81 for (; i
< len
%A
; i
++) {
83 err(fun
->name
, src
, salign
, c
, len
);
92 for (int i
=0; funtab
[i
].name
; i
++) {
94 for (int s
= 0; s
< A
; s
++) {
96 for (n
= 0; n
< 100; n
++) {
97 test(funtab
+i
, s
, 0, n
);
98 test(funtab
+i
, s
, 0x25, n
);
99 test(funtab
+i
, s
, 0xaa25, n
);
101 for (; n
< LEN
; n
*= 2) {
102 test(funtab
+i
, s
, 0, n
);
103 test(funtab
+i
, s
, 0x25, n
);
104 test(funtab
+i
, s
, 0xaa25, n
);
107 printf("%s %s\n", test_status
? "FAIL" : "PASS", funtab
[i
].name
);