Path: blob/main/contrib/arm-optimized-routines/string/test/strlen.c
39481 views
/*1* strlen test.2*3* Copyright (c) 2019-2022, 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;21size_t (*fun) (const char *s);22int test_mte;23} funtab[] = {24// clang-format off25F(strlen, 0)26#if __aarch64__27F(__strlen_aarch64, 0)28F(__strlen_aarch64_mte, 1)29# if __ARM_FEATURE_SVE30F(__strlen_aarch64_sve, 1)31# endif32#elif __arm__33# if __ARM_ARCH >= 6 && __ARM_ARCH_ISA_THUMB == 234F(__strlen_armv6t2, 0)35# endif36#endif37{0, 0, 0}38// clang-format on39};40#undef F4142#define ALIGN 3243#define LEN 51244static char *sbuf;4546static void *47alignup (void *p)48{49return (void *) (((uintptr_t) p + ALIGN - 1) & -ALIGN);50}5152static void53test (const struct fun *fun, int align, int len)54{55char *src = alignup (sbuf);56char *s = src + align;57size_t r;5859if (err_count >= ERR_LIMIT)60return;61if (len > LEN || align >= ALIGN)62abort ();6364for (int i = 0; src + i < s; i++)65src[i] = 0;66for (int i = 1; i <= ALIGN; i++)67s[len + i] = (len + align) & 1 ? 1 : 0;68for (int i = 0; i < len; i++)69s[i] = 'a' + (i & 31);70s[len] = '\0';7172s = tag_buffer (s, len + 1, fun->test_mte);73r = fun->fun (s);74untag_buffer (s, len + 1, fun->test_mte);7576if (r != len)77{78ERR ("%s (%p) returned %zu expected %d\n", fun->name, s, r, len);79quote ("input", src, len);80}81}8283int84main (void)85{86sbuf = mte_mmap (LEN + 3 * ALIGN);87int r = 0;88for (int i = 0; funtab[i].name; i++)89{90err_count = 0;91for (int a = 0; a < ALIGN; a++)92for (int n = 0; n < LEN; n++)93test (funtab + i, a, n);9495char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";96printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);97if (err_count)98r = -1;99}100return r;101}102103104