101 lines · c
1/*2 * strcpy 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-exception7 */8 9#include <stdint.h>10#include <stdio.h>11#include <stdlib.h>12#include <string.h>13#include "stringlib.h"14 15static const struct fun16{17 const char *name;18 char *(*fun)(char *dest, const char *src);19} funtab[] = {20#define F(x) {#x, x},21F(strcpy)22#if __aarch64__23F(__strcpy_aarch64)24# if __ARM_FEATURE_SVE25F(__strcpy_aarch64_sve)26# endif27#elif __arm__ && defined (__thumb2__) && !defined (__thumb__)28F(__strcpy_arm)29#endif30#undef F31 {0, 0}32};33 34static int test_status;35#define ERR(...) (test_status=1, printf(__VA_ARGS__))36 37#define A 3238#define LEN 25000039static char dbuf[LEN+2*A];40static char sbuf[LEN+2*A];41static char wbuf[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 dalign, int salign, int len)49{50 char *src = alignup(sbuf);51 char *dst = alignup(dbuf);52 char *want = wbuf;53 char *s = src + salign;54 char *d = dst + dalign;55 char *w = want + dalign;56 void *p;57 int i;58 59 if (len > LEN || dalign >= A || salign >= A)60 abort();61 for (i = 0; i < len+A; i++) {62 src[i] = '?';63 want[i] = dst[i] = '*';64 }65 for (i = 0; i < len; i++)66 s[i] = w[i] = 'a' + i%23;67 s[i] = w[i] = '\0';68 69 p = fun->fun(d, s);70 if (p != d)71 ERR("%s(%p,..) returned %p\n", fun->name, d, p);72 for (i = 0; i < len+A; i++) {73 if (dst[i] != want[i]) {74 ERR("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign, len);75 ERR("got : %.*s\n", dalign+len+1, dst);76 ERR("want: %.*s\n", dalign+len+1, want);77 break;78 }79 }80}81 82int 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);92 for (; n < LEN; n *= 2)93 test(funtab+i, d, s, n);94 }95 printf("%s %s\n", test_status ? "FAIL" : "PASS", funtab[i].name);96 if (test_status)97 r = -1;98 }99 return r;100}101