1 /*
2 * stpcpy 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 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include "stringlib.h"
15
16 static const struct fun
17 {
18 const char *name;
19 char *(*fun)(char *dest, const char *src);
20 } funtab[] = {
21 #define F(x) {#x, x},
22 F(stpcpy)
23 #if __aarch64__
24 F(__stpcpy_aarch64)
25 # if __ARM_FEATURE_SVE
26 F(__stpcpy_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 LEN 250000
38 static char dbuf[LEN+2*A];
39 static char sbuf[LEN+2*A];
40 static char wbuf[LEN+2*A];
41
alignup(void * p)42 static void *alignup(void *p)
43 {
44 return (void*)(((uintptr_t)p + A-1) & -A);
45 }
46
test(const struct fun * fun,int dalign,int salign,int len)47 static void test(const struct fun *fun, int dalign, int salign, int len)
48 {
49 char *src = alignup(sbuf);
50 char *dst = alignup(dbuf);
51 char *want = wbuf;
52 char *s = src + salign;
53 char *d = dst + dalign;
54 char *w = want + dalign;
55 void *p;
56 int i;
57
58 if (len > LEN || dalign >= A || salign >= A)
59 abort();
60 for (i = 0; i < len+A; i++) {
61 src[i] = '?';
62 want[i] = dst[i] = '*';
63 }
64 for (i = 0; i < len; i++)
65 s[i] = w[i] = 'a' + i%23;
66 s[i] = w[i] = '\0';
67
68 p = fun->fun(d, s);
69 if (p != d + len)
70 ERR("%s(%p,..) returned %p\n", fun->name, d, p);
71 for (i = 0; i < len+A; i++) {
72 if (dst[i] != want[i]) {
73 ERR("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign, len);
74 ERR("got : %.*s\n", dalign+len+1, dst);
75 ERR("want: %.*s\n", dalign+len+1, want);
76 break;
77 }
78 }
79 }
80
main()81 int main()
82 {
83 int r = 0;
84 for (int i=0; funtab[i].name; i++) {
85 test_status = 0;
86 for (int d = 0; d < A; d++)
87 for (int s = 0; s < A; s++) {
88 int n;
89 for (n = 0; n < 100; n++)
90 test(funtab+i, d, s, n);
91 for (; n < LEN; n *= 2)
92 test(funtab+i, d, s, n);
93 }
94 printf("%s %s\n", test_status ? "FAIL" : "PASS", funtab[i].name);
95 if (test_status)
96 r = -1;
97 }
98 return r;
99 }
100