fix void* pointer arithmetic in string_strchr.c
[libc-test.git] / src / functional / string_strchr.c
blob718649a3939590d376d8947fe4ef209cd3aa9aea
1 #include <string.h>
2 #include "test.h"
4 static char buf[512];
6 static void *aligned(void *p)
8 return (void*)(((uintptr_t)p + 63) & -64U);
11 static void *aligncpy(void *p, size_t len, size_t a)
13 return memcpy((char*)aligned(buf)+a, p, len);
16 #define N(s, c) { \
17 int align; \
18 for (align=0; align<8; align++) { \
19 char *p = aligncpy(s, sizeof s, align); \
20 char *q = strchr(p, c); \
21 if (q) \
22 t_error("strchr(%s,%s) with align=%d returned str+%d, wanted 0\n", #s, #c, align, q-p); \
23 } \
26 #define T(s, c, n) { \
27 int align; \
28 for (align=0; align<8; align++) { \
29 char *p = aligncpy(s, sizeof s, align); \
30 char *q = strchr(p, c); \
31 if (q == 0) \
32 t_error("strchr(%s,%s) with align=%d returned 0, wanted str+%d\n", #s, #c, align, n); \
33 else if (q - p != n) \
34 t_error("strchr(%s,%s) with align=%d returned str+%d, wanted str+%d\n", #s, #c, align, q-p, n); \
35 } \
38 int main(void)
40 int i;
41 char a[128];
42 char s[256];
44 for (i = 0; i < 128; i++)
45 a[i] = (i+1) & 127;
46 for (i = 0; i < 256; i++)
47 *((unsigned char*)s+i) = i+1;
49 N("\0aaa", 'a')
50 N("a\0bb", 'b')
51 N("ab\0c", 'c')
52 N("abc\0d", 'd')
53 N("abc abc\0x", 'x')
54 N(a, 128)
55 N(a, 255)
57 T("", 0, 0)
58 T("a", 'a', 0)
59 T("a", 'a'+256, 0)
60 T("a", 0, 1)
61 T("abb", 'b', 1)
62 T("aabb", 'b', 2)
63 T("aaabb", 'b', 3)
64 T("aaaabb", 'b', 4)
65 T("aaaaabb", 'b', 5)
66 T("aaaaaabb", 'b', 6)
67 T("abc abc", 'c', 2)
68 T(s, 1, 0)
69 T(s, 2, 1)
70 T(s, 10, 9)
71 T(s, 11, 10)
72 T(s, 127, 126)
73 T(s, 128, 127)
74 T(s, 255, 254)
75 T(s, 0, 255)
77 return t_status;