1/*
2 * memchr 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 void *(*fun)(const void *, int c, size_t n);
20} funtab[] = {
21#define F(x) {#x, x},
22F(memchr)
23#if __aarch64__
24F(__memchr_aarch64)
25# if __ARM_FEATURE_SVE
26F(__memchr_aarch64_sve)
27# endif
28#elif __arm__
29F(__memchr_arm)
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 unsigned 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 unsigned char *src = alignup(p: sbuf);
51 unsigned char *s = src + align;
52 unsigned char *f = len ? s + seekpos : 0;
53 int seekchar = 0x1;
54 int i;
55 void *p;
56
57 if (len > LEN || seekpos >= len || align >= A)
58 abort();
59
60 for (i = 0; i < seekpos; i++)
61 s[i] = 'a' + i%23;
62 s[i++] = seekchar;
63 for (; i < len; i++)
64 s[i] = 'a' + i%23;
65
66 p = fun->fun(s, seekchar, len);
67
68 if (p != f) {
69 ERR("%s(%p,0x%02x,%d) returned %p\n", fun->name, s, seekchar, len, p);
70 ERR("expected: %p\n", f);
71 abort();
72 }
73}
74
75int 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 for (int n = 0; n < 100; n++)
82 for (int sp = 0; sp < n-1; sp++)
83 test(fun: funtab+i, align: a, seekpos: sp, len: n);
84 for (int n = 100; n < LEN; n *= 2) {
85 test(fun: funtab+i, align: a, seekpos: n-1, len: n);
86 test(fun: funtab+i, align: a, seekpos: n/2, len: n);
87 }
88 }
89 printf(format: "%s %s\n", test_status ? "FAIL" : "PASS", funtab[i].name);
90 if (test_status)
91 r = -1;
92 }
93 return r;
94}
95

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