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