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