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

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