Path: blob/main/contrib/arm-optimized-routines/string/test/strchr.c
39481 views
/*1* strchr test.2*3* Copyright (c) 2019-2020, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include <stdint.h>8#include <stdio.h>9#include <stdlib.h>10#include <string.h>11#include <limits.h>12#include "mte.h"13#include "stringlib.h"14#include "stringtest.h"1516#define F(x, mte) {#x, x, mte},1718static const struct fun19{20const char *name;21char *(*fun) (const char *s, int c);22int test_mte;23} funtab[] = {24// clang-format off25F(strchr, 0)26#if __aarch64__27F(__strchr_aarch64, 0)28F(__strchr_aarch64_mte, 1)29# if __ARM_FEATURE_SVE30F(__strchr_aarch64_sve, 1)31# endif32#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 - 1) & -ALIGN);46}4748static void49test (const struct fun *fun, int align, int seekpos, int len)50{51char *src = alignup (sbuf);52char *s = src + align;53char *f = seekpos != -1 ? s + seekpos : 0;54int seekchar = 0x1;55void *p;5657if (err_count >= ERR_LIMIT)58return;59if (len > LEN || seekpos >= len || align >= ALIGN)60abort ();6162for (int i = 0; src + i < s; i++)63src[i] = (i + len) & 1 ? seekchar : 0;64for (int i = 1; i <= ALIGN; i++)65s[len + i] = (i + len) & 1 ? seekchar : 0;66for (int i = 0; i < len; i++)67s[i] = 'a' + (i & 31);68if (seekpos != -1)69s[seekpos] = seekchar;70if (seekpos != -1 && (len + align) & 1)71s[seekpos + 1] = seekchar;72s[len] = '\0';7374s = tag_buffer (s, len + 1, fun->test_mte);75p = fun->fun (s, seekchar);76untag_buffer (s, len + 1, fun->test_mte);77p = untag_pointer (p);7879if (p != f)80{81ERR ("%s (%p, 0x%02x) len %d returned %p, expected %p pos %d\n",82fun->name, s, seekchar, len, p, f, seekpos);83quote ("input", s, len);84}8586s = tag_buffer (s, len + 1, fun->test_mte);87p = fun->fun (s, 0);88untag_buffer (s, len + 1, fun->test_mte);8990if (p != s + len)91{92ERR ("%s (%p, 0x%02x) len %d returned %p, expected %p pos %d\n",93fun->name, s, 0, len, p, f, len);94quote ("input", s, len);95}96}9798int99main (void)100{101sbuf = mte_mmap (LEN + 3 * ALIGN);102int r = 0;103for (int i = 0; funtab[i].name; i++)104{105err_count = 0;106for (int a = 0; a < ALIGN; a++)107for (int n = 0; n < LEN; n++)108{109for (int sp = 0; sp < n; sp++)110test (funtab + i, a, sp, n);111test (funtab + i, a, -1, n);112}113114char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";115printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);116if (err_count)117r = -1;118}119return r;120}121122123