Path: blob/main/contrib/arm-optimized-routines/string/test/strnlen.c
39534 views
/*1* strnlen test.2*3* Copyright (c) 2019-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;25size_t (*fun) (const char *s, size_t m);26int test_mte;27} funtab[] = {28// clang-format off29F(strnlen, 0)30#if __aarch64__31F(__strnlen_aarch64, 1)32# if __ARM_FEATURE_SVE33F(__strnlen_aarch64_sve, 1)34# endif35#endif36{0, 0, 0}37// clang-format on38};39#undef F4041#define ALIGN 3242#define LEN 51243static char *sbuf;4445static void *46alignup (void *p)47{48return (void *) (((uintptr_t) p + ALIGN - 1) & -ALIGN);49}5051static void52test (const struct fun *fun, int align, size_t maxlen, size_t len)53{54char *src = alignup (sbuf);55char *s = src + align;56size_t r;57size_t e = maxlen < len ? maxlen : len;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;71if ((len + align) & 1)72s[e + 1] = 0;7374size_t mte_len = maxlen < len + 1 ? maxlen : len + 1;75s = tag_buffer (s, mte_len, fun->test_mte);76r = fun->fun (s, maxlen);77untag_buffer (s, mte_len, fun->test_mte);7879if (r != e)80{81ERR ("%s (%p, %zu) len %zu returned %zu, expected %zu\n",82fun->name, s, maxlen, len, r, e);83quote ("input", s, len);84}85}8687int88main (void)89{90sbuf = mte_mmap (LEN + 3 * ALIGN);91int r = 0;92for (int i = 0; funtab[i].name; i++)93{94err_count = 0;95for (int a = 0; a < ALIGN; a++)96for (int n = 0; n < LEN; n++)97{98for (int maxlen = 0; maxlen < LEN; maxlen++)99test (funtab + i, a, maxlen, n);100test (funtab + i, a, SIZE_MAX - a, n);101}102char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";103printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);104if (err_count)105r = -1;106}107return r;108}109110111