Path: blob/main/contrib/arm-optimized-routines/string/test/stpcpy.c
39491 views
/*1* stpcpy test.2*3* Copyright (c) 2019-2022, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#ifndef _GNU_SOURCE8#define _GNU_SOURCE9#endif1011#include <stdint.h>12#include <stdio.h>13#include <stdlib.h>14#include <string.h>15#include "mte.h"16#include "stringlib.h"17#include "stringtest.h"1819#define F(x, mte) {#x, x, mte},2021static const struct fun22{23const char *name;24char *(*fun) (char *dest, const char *src);25int test_mte;26} funtab[] = {27// clang-format off28F(stpcpy, 0)29#if __aarch64__30F(__stpcpy_aarch64, 1)31# if __ARM_FEATURE_SVE32F(__stpcpy_aarch64_sve, 1)33# endif34#endif35{0, 0, 0}36// clang-format on37};38#undef F3940#define ALIGN 3241#define LEN 51242static char *dbuf;43static char *sbuf;44static char wbuf[LEN + 3 * ALIGN];4546static void *47alignup (void *p)48{49return (void *) (((uintptr_t) p + ALIGN - 1) & -ALIGN);50}5152static void53test (const struct fun *fun, int dalign, int salign, int len)54{55char *src = alignup (sbuf);56char *dst = alignup (dbuf);57char *want = wbuf;58char *s = src + salign;59char *d = dst + dalign;60char *w = want + dalign;61void *p;62int i;6364if (err_count >= ERR_LIMIT)65return;66if (len > LEN || dalign >= ALIGN || salign >= ALIGN)67abort ();68for (i = 0; i < len + ALIGN; i++)69{70src[i] = '?';71want[i] = dst[i] = '*';72}73for (int i = 0; src + i < s; i++)74src[i] = 0;75for (int i = 1; i <= ALIGN; i++)76s[len + i] = (len + salign) & 1 ? 1 : 0;77for (i = 0; i < len; i++)78s[i] = w[i] = 'a' + (i & 31);79s[len] = w[len] = '\0';8081s = tag_buffer (s, len + 1, fun->test_mte);82d = tag_buffer (d, len + 1, fun->test_mte);83p = fun->fun (d, s);84untag_buffer (s, len + 1, fun->test_mte);85untag_buffer (d, len + 1, fun->test_mte);8687if (p != d + len)88ERR ("%s (%p,..) returned %p expected %p\n", fun->name, d, p, d + len);8990for (i = 0; i < len + ALIGN; i++)91{92if (dst[i] != want[i])93{94ERR ("%s (align %d, align %d, %d) failed\n",95fun->name, dalign, salign, len);96quoteat ("got", dst, len + ALIGN, i);97quoteat ("want", want, len + ALIGN, i);98break;99}100}101}102103int104main (void)105{106sbuf = mte_mmap (LEN + 3 * ALIGN);107dbuf = mte_mmap (LEN + 3 * ALIGN);108int r = 0;109for (int i = 0; funtab[i].name; i++)110{111err_count = 0;112for (int d = 0; d < ALIGN; d++)113for (int s = 0; s < ALIGN; s++)114for (int n = 0; n < LEN; n++)115test (funtab + i, d, s, n);116117char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";118printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);119if (err_count)120r = -1;121}122return r;123}124125126