1 | /* |
2 | * strlen test. |
3 | * |
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 |
7 | */ |
8 | |
9 | #include <stdint.h> |
10 | #include <stdio.h> |
11 | #include <stdlib.h> |
12 | #include <string.h> |
13 | #include <limits.h> |
14 | #include "stringlib.h" |
15 | |
16 | static const struct fun |
17 | { |
18 | const char *name; |
19 | size_t (*fun)(const char *s); |
20 | } funtab[] = { |
21 | #define F(x) {#x, x}, |
22 | F(strlen) |
23 | #if __aarch64__ |
24 | F(__strlen_aarch64) |
25 | F(__strlen_aarch64_mte) |
26 | # if __ARM_FEATURE_SVE |
27 | F(__strlen_aarch64_sve) |
28 | # endif |
29 | #elif __arm__ |
30 | # if __ARM_ARCH >= 6 && __ARM_ARCH_ISA_THUMB == 2 |
31 | F(__strlen_armv6t2) |
32 | # endif |
33 | #endif |
34 | #undef F |
35 | {0, 0} |
36 | }; |
37 | |
38 | static int test_status; |
39 | #define ERR(...) (test_status=1, printf(__VA_ARGS__)) |
40 | |
41 | #define A 32 |
42 | #define SP 512 |
43 | #define LEN 250000 |
44 | static char sbuf[LEN+2*A]; |
45 | |
46 | static void *alignup(void *p) |
47 | { |
48 | return (void*)(((uintptr_t)p + A-1) & -A); |
49 | } |
50 | |
51 | static void test(const struct fun *fun, int align, int len) |
52 | { |
53 | char *src = alignup(p: sbuf); |
54 | char *s = src + align; |
55 | size_t r; |
56 | |
57 | if (len > LEN || align >= A) |
58 | abort(); |
59 | |
60 | for (int i = 0; i < len + A; i++) |
61 | src[i] = '?'; |
62 | for (int i = 0; i < len - 2; i++) |
63 | s[i] = 'a' + i%23; |
64 | s[len - 1] = '\0'; |
65 | |
66 | r = fun->fun(s); |
67 | if (r != len-1) { |
68 | ERR("%s(%p) returned %zu\n" , fun->name, s, r); |
69 | ERR("input: %.*s\n" , align+len+1, src); |
70 | ERR("expected: %d\n" , len); |
71 | abort(); |
72 | } |
73 | } |
74 | |
75 | int main() |
76 | { |
77 | int r = 0; |
78 | for (int i=0; funtab[i].name; i++) { |
79 | test_status = 0; |
80 | for (int a = 0; a < A; a++) { |
81 | int n; |
82 | for (n = 1; n < 100; n++) |
83 | test(fun: funtab+i, align: a, len: n); |
84 | for (; n < LEN; n *= 2) |
85 | test(fun: funtab+i, align: a, len: n); |
86 | } |
87 | printf(format: "%s %s\n" , test_status ? "FAIL" : "PASS" , funtab[i].name); |
88 | if (test_status) |
89 | r = -1; |
90 | } |
91 | return r; |
92 | } |
93 | |