Blame src/functional/string_memset.c

Szabolcs Nagy 27d6fe
#include <string.h>
Szabolcs Nagy 27d6fe
#include <stdlib.h>
Szabolcs Nagy 27d6fe
#include <stdint.h>
Szabolcs Nagy 27d6fe
#include "test.h"
Szabolcs Nagy 27d6fe
Szabolcs Nagy 7f085c
#define N 500
Szabolcs Nagy 0b6bb4
static char buf[N];
Szabolcs Nagy 0b6bb4
static char buf2[N];
Szabolcs Nagy 27d6fe
Szabolcs Nagy 27d6fe
static void *(*volatile pmemset)(void *, int, size_t);
Szabolcs Nagy 27d6fe
Szabolcs Nagy 8dc17f
static char *aligned(void *p)
Szabolcs Nagy 27d6fe
{
Szabolcs Nagy c36565
	return (char*)(((uintptr_t)p + 63) & -64);
Szabolcs Nagy 27d6fe
}
Szabolcs Nagy 27d6fe
Szabolcs Nagy 27d6fe
static void test_align(int align, int len)
Szabolcs Nagy 27d6fe
{
Szabolcs Nagy 8dc17f
	char *s = aligned(buf+64) + align;
Szabolcs Nagy 8dc17f
	char *want = aligned(buf2+64) + align;
Szabolcs Nagy 27d6fe
	char *p;
Szabolcs Nagy 27d6fe
	int i;
Szabolcs Nagy 27d6fe
Szabolcs Nagy b511d2
	if (len + 64 > buf + N - s || len + 64 > buf2 + N - want)
Szabolcs Nagy 27d6fe
		abort();
Szabolcs Nagy 27d6fe
	for (i = 0; i < N; i++)
Szabolcs Nagy 0b6bb4
		buf[i] = buf2[i] = ' ';
Szabolcs Nagy 27d6fe
	for (i = 0; i < len; i++)
Szabolcs Nagy 8dc17f
		want[i] = '#';
Szabolcs Nagy 8dc17f
	p = pmemset(s, '#', len);
Szabolcs Nagy 8dc17f
	if (p != s)
Szabolcs Nagy 8dc17f
		t_error("memset(%p,...) returned %p\n", s, p);
Szabolcs Nagy b511d2
	for (i = -64; i < len+64; i++)
Szabolcs Nagy b511d2
		if (s[i] != want[i]) {
Szabolcs Nagy 8dc17f
			t_error("memset(align %d, '#', %d) failed at pos %d\n", align, len, i);
Szabolcs Nagy b511d2
			t_printf("got : '%.*s'\n", len+128, s-64);
Szabolcs Nagy b511d2
			t_printf("want: '%.*s'\n", len+128, want-64);
Szabolcs Nagy 27d6fe
			break;
Szabolcs Nagy 27d6fe
		}
Szabolcs Nagy 27d6fe
}
Szabolcs Nagy 27d6fe
Szabolcs Nagy 27d6fe
static void test_value(int c)
Szabolcs Nagy 27d6fe
{
Szabolcs Nagy 27d6fe
	int i;
Szabolcs Nagy 27d6fe
Szabolcs Nagy 27d6fe
	pmemset(buf, c, 10);
Szabolcs Nagy 27d6fe
	for (i = 0; i < 10; i++)
Szabolcs Nagy 27d6fe
		if ((unsigned char)buf[i] != (unsigned char)c) {
Szabolcs Nagy 27d6fe
			t_error("memset(%d) failed: got %d\n", c, buf[i]);
Szabolcs Nagy 27d6fe
			break;
Szabolcs Nagy 27d6fe
		}
Szabolcs Nagy 27d6fe
}
Szabolcs Nagy 27d6fe
Szabolcs Nagy 27d6fe
int main(void)
Szabolcs Nagy 27d6fe
{
Szabolcs Nagy 27d6fe
	int i,j,k;
Szabolcs Nagy 27d6fe
Szabolcs Nagy 27d6fe
	pmemset = memset;
Szabolcs Nagy 27d6fe
Szabolcs Nagy 7f085c
	for (i = 0; i < 64; i++)
Szabolcs Nagy 7f085c
		for (j = 0; j < N-200; j++)
Szabolcs Nagy 27d6fe
			test_align(i,j);
Szabolcs Nagy 27d6fe
Szabolcs Nagy 27d6fe
	test_value('c');
Szabolcs Nagy 27d6fe
	test_value(0);
Szabolcs Nagy 27d6fe
	test_value(-1);
Szabolcs Nagy 52786d
	test_value(-5);
Szabolcs Nagy 27d6fe
	test_value(0xab);
Szabolcs Nagy 27d6fe
	return t_status;
Szabolcs Nagy 27d6fe
}