brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · b977cee Raw
113 lines · c
1/*2 * memset test.3 *4 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5 * See https://llvm.org/LICENSE.txt for license information.6 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7 */8 9#include <stdint.h>10#include <stdio.h>11#include <stdlib.h>12#include <string.h>13#include "stringlib.h"14 15static const struct fun16{17	const char *name;18	void *(*fun)(void *s, int c, size_t n);19} funtab[] = {20#define F(x) {#x, x},21F(memset)22#if __aarch64__23F(__memset_aarch64)24#elif __arm__25F(__memset_arm)26#endif27#undef F28	{0, 0}29};30 31static int test_status;32#define ERR(...) (test_status=1, printf(__VA_ARGS__))33 34#define A 3235#define LEN 25000036static unsigned char sbuf[LEN+2*A];37 38static void *alignup(void *p)39{40	return (void*)(((uintptr_t)p + A-1) & -A);41}42 43static void err(const char *name, unsigned char *src, int salign, int c, int len)44{45	ERR("%s(align %d, %d, %d) failed\n", name, salign, c, len);46	ERR("got : %.*s\n", salign+len+1, src);47}48 49static void test(const struct fun *fun, int salign, int c, int len)50{51	unsigned char *src = alignup(sbuf);52	unsigned char *s = src + salign;53	void *p;54	int i;55 56	if (len > LEN || salign >= A)57		abort();58	for (i = 0; i < len+A; i++)59		src[i] = '?';60	for (i = 0; i < len; i++)61		s[i] = 'a' + i%23;62	for (; i<len%A; i++)63		s[i] = '*';64 65	p = fun->fun(s, c, len);66	if (p != s)67		ERR("%s(%p,..) returned %p\n", fun->name, s, p);68 69	for (i = 0; i < salign; i++) {70		if (src[i] != '?') {71			err(fun->name, src, salign, c, len);72			return;73		}74	}75	for (i = salign; i < len; i++) {76		if (src[i] != (unsigned char)c) {77			err(fun->name, src, salign, c, len);78			return;79		}80	}81	for (; i < len%A; i++) {82		if (src[i] != '*') {83			err(fun->name, src, salign, c, len);84			return;85		}86	}87}88 89int main()90{91	int r = 0;92	for (int i=0; funtab[i].name; i++) {93		test_status = 0;94		for (int s = 0; s < A; s++) {95			int n;96			for (n = 0; n < 100; n++) {97				test(funtab+i, s, 0, n);98				test(funtab+i, s, 0x25, n);99				test(funtab+i, s, 0xaa25, n);100			}101			for (; n < LEN; n *= 2) {102				test(funtab+i, s, 0, n);103				test(funtab+i, s, 0x25, n);104				test(funtab+i, s, 0xaa25, n);105			}106		}107		printf("%s %s\n", test_status ? "FAIL" : "PASS", funtab[i].name);108		if (test_status)109			r = -1;110	}111	return r;112}113