98 lines · c
1/*2 * memcmp 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 int (*fun)(const void *s1, const void *s2, size_t n);19} funtab[] = {20#define F(x) {#x, x},21F(memcmp)22#if __aarch64__23F(__memcmp_aarch64)24# if __ARM_FEATURE_SVE25F(__memcmp_aarch64_sve)26# endif27#endif28#undef F29 {0, 0}30};31 32static int test_status;33#define ERR(...) (test_status=1, printf(__VA_ARGS__))34 35#define A 3236#define LEN 25000037static unsigned char s1buf[LEN+2*A];38static unsigned char s2buf[LEN+2*A];39 40static void *alignup(void *p)41{42 return (void*)(((uintptr_t)p + A-1) & -A);43}44 45static void test(const struct fun *fun, int s1align, int s2align, int len, int diffpos)46{47 unsigned char *src1 = alignup(s1buf);48 unsigned char *src2 = alignup(s2buf);49 unsigned char *s1 = src1 + s1align;50 unsigned char *s2 = src2 + s2align;51 int r;52 53 if (len > LEN || s1align >= A || s2align >= A)54 abort();55 if (diffpos && diffpos >= len)56 abort();57 58 for (int i = 0; i < len+A; i++)59 src1[i] = src2[i] = '?';60 for (int i = 0; i < len; i++)61 s1[i] = s2[i] = 'a' + i%23;62 if (diffpos)63 s1[diffpos]++;64 65 r = fun->fun(s1, s2, len);66 67 if ((!diffpos && r != 0) || (diffpos && r == 0)) {68 ERR("%s(align %d, align %d, %d) failed, returned %d\n",69 fun->name, s1align, s2align, len, r);70 ERR("src1: %.*s\n", s1align+len+1, src1);71 ERR("src2: %.*s\n", s2align+len+1, src2);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 d = 0; d < A; d++)81 for (int s = 0; s < A; s++) {82 int n;83 for (n = 0; n < 100; n++) {84 test(funtab+i, d, s, n, 0);85 test(funtab+i, d, s, n, n / 2);86 }87 for (; n < LEN; n *= 2) {88 test(funtab+i, d, s, n, 0);89 test(funtab+i, d, s, n, n / 2);90 }91 }92 printf("%s %s\n", test_status ? "FAIL" : "PASS", funtab[i].name);93 if (test_status)94 r = -1;95 }96 return r;97}98