1/*
2 * strchrnul 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 _GNU_SOURCE
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
18static const struct fun
19{
20 const char *name;
21 char *(*fun)(const char *s, int c);
22} funtab[] = {
23#define F(x) {#x, x},
24F(strchrnul)
25#if __aarch64__
26F(__strchrnul_aarch64)
27# if __ARM_FEATURE_SVE
28F(__strchrnul_aarch64_sve)
29# endif
30#endif
31#undef F
32 {0, 0}
33};
34
35static 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
41static char sbuf[LEN+2*A];
42
43static void *alignup(void *p)
44{
45 return (void*)(((uintptr_t)p + A-1) & -A);
46}
47
48static void test(const struct fun *fun, int align, int seekpos, int len)
49{
50 char *src = alignup(p: sbuf);
51 char *s = src + align;
52 char *f = seekpos != -1 ? s + seekpos : s + len - 1;
53 int seekchar = 0x1;
54 void *p;
55
56 if (len > LEN || seekpos >= len - 1 || align >= A)
57 abort();
58 if (seekchar >= 'a' && seekchar <= 'a' + 23)
59 abort();
60
61 for (int i = 0; i < len + A; i++)
62 src[i] = '?';
63 for (int i = 0; i < len - 2; i++)
64 s[i] = 'a' + i%23;
65 if (seekpos != -1)
66 s[seekpos] = seekchar;
67 s[len - 1] = '\0';
68
69 p = fun->fun(s, seekchar);
70
71 if (p != f) {
72 ERR("%s(%p,0x%02x,%d) returned %p\n", fun->name, s, seekchar, len, p);
73 ERR("expected: %p\n", f);
74 abort();
75 }
76}
77
78int main()
79{
80 int r = 0;
81 for (int i=0; funtab[i].name; i++) {
82 test_status = 0;
83 for (int a = 0; a < A; a++) {
84 int n;
85 for (n = 1; n < 100; n++) {
86 for (int sp = 0; sp < n - 1; sp++)
87 test(fun: funtab+i, align: a, seekpos: sp, len: n);
88 test(fun: funtab+i, align: a, seekpos: -1, len: n);
89 }
90 for (; n < LEN; n *= 2) {
91 test(fun: funtab+i, align: a, seekpos: -1, len: n);
92 test(fun: funtab+i, align: a, seekpos: n / 2, len: n);
93 }
94 }
95 printf(format: "%s %s\n", test_status ? "FAIL" : "PASS", funtab[i].name);
96 if (test_status)
97 r = -1;
98 }
99 return r;
100}
101

source code of libc/AOR_v20.02/string/test/strchrnul.c