1570af302Sopenharmony_ci#include <string.h>
2570af302Sopenharmony_ci#include <stdlib.h>
3570af302Sopenharmony_ci#include <stdint.h>
4570af302Sopenharmony_ci#include "test.h"
5570af302Sopenharmony_ci
6570af302Sopenharmony_ci#define N 500
7570af302Sopenharmony_cistatic char buf[N];
8570af302Sopenharmony_cistatic char buf2[N];
9570af302Sopenharmony_ci
10570af302Sopenharmony_cistatic void *(*volatile pmemset)(void *, int, size_t);
11570af302Sopenharmony_ci
12570af302Sopenharmony_cistatic char *aligned(void *p)
13570af302Sopenharmony_ci{
14570af302Sopenharmony_ci	return (char*)(((uintptr_t)p + 63) & -64);
15570af302Sopenharmony_ci}
16570af302Sopenharmony_ci
17570af302Sopenharmony_cistatic void test_align(int align, int len)
18570af302Sopenharmony_ci{
19570af302Sopenharmony_ci	char *s = aligned(buf+64) + align;
20570af302Sopenharmony_ci	char *want = aligned(buf2+64) + align;
21570af302Sopenharmony_ci	char *p;
22570af302Sopenharmony_ci	int i;
23570af302Sopenharmony_ci
24570af302Sopenharmony_ci	if (len + 64 > buf + N - s || len + 64 > buf2 + N - want)
25570af302Sopenharmony_ci		abort();
26570af302Sopenharmony_ci	for (i = 0; i < N; i++)
27570af302Sopenharmony_ci		buf[i] = buf2[i] = ' ';
28570af302Sopenharmony_ci	for (i = 0; i < len; i++)
29570af302Sopenharmony_ci		want[i] = '#';
30570af302Sopenharmony_ci	p = pmemset(s, '#', len);
31570af302Sopenharmony_ci	if (p != s)
32570af302Sopenharmony_ci		t_error("memset(%p,...) returned %p\n", s, p);
33570af302Sopenharmony_ci	for (i = -64; i < len+64; i++)
34570af302Sopenharmony_ci		if (s[i] != want[i]) {
35570af302Sopenharmony_ci			t_error("memset(align %d, '#', %d) failed at pos %d\n", align, len, i);
36570af302Sopenharmony_ci			t_printf("got : '%.*s'\n", len+128, s-64);
37570af302Sopenharmony_ci			t_printf("want: '%.*s'\n", len+128, want-64);
38570af302Sopenharmony_ci			break;
39570af302Sopenharmony_ci		}
40570af302Sopenharmony_ci}
41570af302Sopenharmony_ci
42570af302Sopenharmony_cistatic void test_value(int c)
43570af302Sopenharmony_ci{
44570af302Sopenharmony_ci	int i;
45570af302Sopenharmony_ci
46570af302Sopenharmony_ci	pmemset(buf, c, 10);
47570af302Sopenharmony_ci	for (i = 0; i < 10; i++)
48570af302Sopenharmony_ci		if ((unsigned char)buf[i] != (unsigned char)c) {
49570af302Sopenharmony_ci			t_error("memset(%d) failed: got %d\n", c, buf[i]);
50570af302Sopenharmony_ci			break;
51570af302Sopenharmony_ci		}
52570af302Sopenharmony_ci}
53570af302Sopenharmony_ci
54570af302Sopenharmony_ciint main(void)
55570af302Sopenharmony_ci{
56570af302Sopenharmony_ci	int i,j,k;
57570af302Sopenharmony_ci
58570af302Sopenharmony_ci	pmemset = memset;
59570af302Sopenharmony_ci
60570af302Sopenharmony_ci	for (i = 0; i < 64; i++)
61570af302Sopenharmony_ci		for (j = 0; j < N-256; j++)
62570af302Sopenharmony_ci			test_align(i,j);
63570af302Sopenharmony_ci
64570af302Sopenharmony_ci	test_value('c');
65570af302Sopenharmony_ci	test_value(0);
66570af302Sopenharmony_ci	test_value(-1);
67570af302Sopenharmony_ci	test_value(-5);
68570af302Sopenharmony_ci	test_value(0xab);
69570af302Sopenharmony_ci	return t_status;
70570af302Sopenharmony_ci}
71