fix string_memset so buffers are aligned properly
[libc-test.git] / src / functional / string_memset.c
blobc85ade21c0ae065ef93760ce855772db94c519bf
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include "test.h"
6 #define N 400
7 static char buf[N];
8 static char buf2[N];
10 static void *(*volatile pmemset)(void *, int, size_t);
12 static char *aligned(void *p)
14 return (char*)(((uintptr_t)p + 63) & -64U);
17 static void test_align(int align, int len)
19 char *s = aligned(buf+64) + align;
20 char *want = aligned(buf2+64) + align;
21 char *p;
22 int i;
24 if (len + 64 > buf + N - s || len + 64 > buf2 + N - want)
25 abort();
26 for (i = 0; i < N; i++)
27 buf[i] = buf2[i] = ' ';
28 for (i = 0; i < len; i++)
29 want[i] = '#';
30 p = pmemset(s, '#', len);
31 if (p != s)
32 t_error("memset(%p,...) returned %p\n", s, p);
33 for (i = -64; i < len+64; i++)
34 if (s[i] != want[i]) {
35 t_error("memset(align %d, '#', %d) failed at pos %d\n", align, len, i);
36 t_printf("got : '%.*s'\n", len+128, s-64);
37 t_printf("want: '%.*s'\n", len+128, want-64);
38 break;
42 static void test_value(int c)
44 int i;
46 pmemset(buf, c, 10);
47 for (i = 0; i < 10; i++)
48 if ((unsigned char)buf[i] != (unsigned char)c) {
49 t_error("memset(%d) failed: got %d\n", c, buf[i]);
50 break;
54 int main(void)
56 int i,j,k;
58 pmemset = memset;
60 for (i = 0; i < 16; i++)
61 for (j = 0; j < 200; j++)
62 test_align(i,j);
64 test_value('c');
65 test_value(0);
66 test_value(-1);
67 test_value(-5);
68 test_value(0xab);
69 return t_status;