1 /*
2  * strcmp 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 "stringlib.h"
14 
15 static const struct fun
16 {
17 	const char *name;
18 	int (*fun)(const char *s1, const char *s2);
19 } funtab[] = {
20 #define F(x) {#x, x},
21 F(strcmp)
22 #if __aarch64__
23 F(__strcmp_aarch64)
24 # if __ARM_FEATURE_SVE
25 F(__strcmp_aarch64_sve)
26 # endif
27 #elif __arm__
28 # if __ARM_ARCH >= 7 && __ARM_ARCH_ISA_ARM >= 1
29 F(__strcmp_arm)
30 # elif __ARM_ARCH == 6 && __ARM_ARCH_6M__ >= 1
31 F(__strcmp_armv6m)
32 # endif
33 #endif
34 #undef F
35 	{0, 0}
36 };
37 
38 static int test_status;
39 #define ERR(...) (test_status=1, printf(__VA_ARGS__))
40 
41 #define A 32
42 #define LEN 250000
43 static char s1buf[LEN+2*A];
44 static char s2buf[LEN+2*A];
45 
alignup(void * p)46 static void *alignup(void *p)
47 {
48 	return (void*)(((uintptr_t)p + A-1) & -A);
49 }
50 
test(const struct fun * fun,int s1align,int s2align,int len,int diffpos)51 static void test(const struct fun *fun, int s1align, int s2align, int len, int diffpos)
52 {
53 	char *src1 = alignup(s1buf);
54 	char *src2 = alignup(s2buf);
55 	char *s1 = src1 + s1align;
56 	char *s2 = src2 + s2align;
57 	int r;
58 
59 	if (len > LEN || s1align >= A || s2align >= A)
60 		abort();
61 	if (diffpos > 1 && diffpos >= len-1)
62 		abort();
63 
64 	for (int i = 0; i < len+A; i++)
65 		src1[i] = src2[i] = '?';
66 	for (int i = 0; i < len-1; i++)
67 		s1[i] = s2[i] = 'a' + i%23;
68 	if (diffpos > 1)
69 		s1[diffpos]++;
70 	s1[len] = s2[len] = '\0';
71 
72 	r = fun->fun(s1, s2);
73 
74 	if (((diffpos <= 1) && r != 0) || (diffpos > 1 && r == 0)) {
75 		ERR("%s(align %d, align %d, %d) failed, returned %d\n",
76 			fun->name, s1align, s2align, len, r);
77 		ERR("src1: %.*s\n", s1align+len+1, src1);
78 		ERR("src2: %.*s\n", s2align+len+1, src2);
79 	}
80 }
81 
main()82 int main()
83 {
84 	int r = 0;
85 	for (int i=0; funtab[i].name; i++) {
86 		test_status = 0;
87 		for (int d = 0; d < A; d++)
88 			for (int s = 0; s < A; s++) {
89 				int n;
90 				for (n = 0; n < 100; n++) {
91 					test(funtab+i, d, s, n, 0);
92 					test(funtab+i, d, s, n, n / 2);
93 				}
94 				for (; n < LEN; n *= 2) {
95 					test(funtab+i, d, s, n, 0);
96 					test(funtab+i, d, s, n, n / 2);
97 				}
98 			}
99 		printf("%s %s\n", test_status ? "FAIL" : "PASS", funtab[i].name);
100 		if (test_status)
101 			r = -1;
102 	}
103 	return r;
104 }
105