Path: blob/main/contrib/arm-optimized-routines/string/test/memrchr.c
39534 views
/*1* memchr test.2*3* Copyright (c) 2020, 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 <limits.h>16#include "mte.h"17#include "stringlib.h"18#include "stringtest.h"1920#define F(x, mte) {#x, x, mte},2122static const struct fun23{24const char *name;25void *(*fun) (const void *s, int c, size_t n);26int test_mte;27} funtab[] = {28// clang-format off29F(memrchr, 0)30#if __aarch64__31F(__memrchr_aarch64, 1)32#endif33{0, 0, 0}34// clang-format on35};36#undef F3738#define ALIGN 3239#define LEN 51240static char *sbuf;4142static void *43alignup (void *p)44{45return (void *) (((uintptr_t) p + ALIGN) & -ALIGN);46}4748static void49test (const struct fun *fun, int align, size_t seekpos, size_t len,50size_t maxlen)51{52char *src = alignup (sbuf);53char *s = src + align;54char *f = seekpos < maxlen ? s + seekpos : NULL;55int seekchar = 1;56void *p;5758if (err_count >= ERR_LIMIT)59return;60if (len > LEN || seekpos > LEN || align > ALIGN)61abort ();6263for (int i = 0; src + i < s; i++)64src[i] = seekchar;65for (int i = 0; i <= ALIGN; i++)66s[len + i] = seekchar;67for (int i = 0; i < len; i++)68s[i] = 'a' + (i & 31);69s[seekpos] = seekchar;70s[((len ^ align) & 1) && seekpos < maxlen ? seekpos - 1 : len] = seekchar;7172s = tag_buffer (s, maxlen, fun->test_mte);73p = fun->fun (s, seekchar, maxlen);74untag_buffer (s, maxlen, fun->test_mte);75p = untag_pointer (p);7677if (p != f)78{79ERR ("%s (%p, 0x%02x, %zu) returned %p, expected %p\n", fun->name, s,80seekchar, maxlen, p, f);81quote ("input", s, len);82}83}8485int86main (void)87{88sbuf = mte_mmap (LEN + 3 * ALIGN);89int r = 0;90for (int i = 0; funtab[i].name; i++)91{92err_count = 0;93for (int a = 0; a < ALIGN; a++)94for (int n = 0; n < LEN; n++)95{96for (int sp = 0; sp < LEN; sp++)97test (funtab + i, a, sp, n, n);98}99char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";100printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);101if (err_count)102r = -1;103}104return r;105}106107108