5c0dd0206c68afaf99dc4827f7872447306f593c
[libc-test] / src / functional / string_memset.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include "test.h"
5
6 #define N 400
7 static char buf[N];
8 static char buf2[N];
9
10 static void *(*volatile pmemset)(void *, int, size_t);
11
12 static char *aligned(void *p)
13 {
14         return (char*)(((uintptr_t)p + 63) & -64U);
15 }
16
17 static void test_align(int align, int len)
18 {
19         char *s = aligned(buf+64) + align;
20         char *want = aligned(buf2+64) + align;
21         char *p;
22         int i;
23
24         if (s - buf + len >= N)
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 = 0; i < N; i++)
34                 if (buf[i] != buf2[i]) {
35                         t_error("memset(align %d, '#', %d) failed at pos %d\n", align, len, i);
36                         t_printf("got : '%.*s'\n", N, s);
37                         t_printf("want: '%.*s'\n", N, want);
38                         break;
39                 }
40 }
41
42 static void test_value(int c)
43 {
44         int i;
45
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;
51                 }
52 }
53
54 int main(void)
55 {
56         int i,j,k;
57
58         pmemset = memset;
59
60         for (i = 0; i < 16; i++)
61                 for (j = 0; j < 200; j++)
62                         test_align(i,j);
63
64         test_value('c');
65         test_value(0);
66         test_value(-1);
67         test_value(-5);
68         test_value(0xab);
69         return t_status;
70 }