]> git.ozlabs.org Git - ccan/blob - ccan/typesafe_cb/test/run.c
ef10d44b8ee6e21b796ccdd09aee3b0ec4774c73
[ccan] / ccan / typesafe_cb / test / run.c
1 #include <ccan/typesafe_cb/typesafe_cb.h>
2 #include <string.h>
3 #include <ccan/tap/tap.h>
4
5 static char dummy = 0;
6
7 /* The example usage. */
8 static void _set_some_value(void *val)
9 {
10         ok1(val == &dummy);
11 }
12
13 #define set_some_value(expr)                                            \
14         _set_some_value(cast_if_type(void *, (expr), (expr), unsigned long))
15
16 static void _callback_onearg(void (*fn)(void *arg), void *arg)
17 {
18         fn(arg);
19 }
20
21 static void _callback_preargs(void (*fn)(int a, int b, void *arg), void *arg)
22 {
23         fn(1, 2, arg);
24 }
25
26 static void _callback_postargs(void (*fn)(void *arg, int a, int b), void *arg)
27 {
28         fn(arg, 1, 2);
29 }
30
31 #define callback_onearg(cb, arg)                                        \
32         _callback_onearg(typesafe_cb(void, (cb), (arg)), (arg))
33
34 #define callback_preargs(cb, arg)                                       \
35         _callback_preargs(typesafe_cb_preargs(void, (cb), (arg), int, int), (arg))
36
37 #define callback_postargs(cb, arg)                                      \
38         _callback_postargs(typesafe_cb_postargs(void, (cb), (arg), int, int), (arg))
39
40 static void my_callback_onearg(char *p)
41 {
42         ok1(strcmp(p, "hello world") == 0);
43 }
44
45 static void my_callback_onearg_const(const char *p)
46 {
47         ok1(strcmp(p, "hello world") == 0);
48 }
49
50 static void my_callback_onearg_volatile(volatile char *p)
51 {
52         ok1(strcmp((char *)p, "hello world") == 0);
53 }
54
55 static void my_callback_preargs(int a, int b, char *p)
56 {
57         ok1(a == 1);
58         ok1(b == 2);
59         ok1(strcmp(p, "hello world") == 0);
60 }
61
62 #if 0 /* FIXME */
63 static void my_callback_preargs_const(int a, int b, const char *p)
64 {
65         ok1(a == 1);
66         ok1(b == 2);
67         ok1(strcmp(p, "hello world") == 0);
68 }
69
70 static void my_callback_preargs_volatile(int a, int b, volatile char *p)
71 {
72         ok1(a == 1);
73         ok1(b == 2);
74         ok1(strcmp((char *)p, "hello world") == 0);
75 }
76 #endif
77
78 static void my_callback_postargs(char *p, int a, int b)
79 {
80         ok1(a == 1);
81         ok1(b == 2);
82         ok1(strcmp(p, "hello world") == 0);
83 }
84
85 #if 0 /* FIXME */
86 static void my_callback_postargs_const(const char *p, int a, int b)
87 {
88         ok1(a == 1);
89         ok1(b == 2);
90         ok1(strcmp(p, "hello world") == 0);
91 }
92
93 static void my_callback_postargs_volatile(volatile char *p, int a, int b)
94 {
95         ok1(a == 1);
96         ok1(b == 2);
97         ok1(strcmp((char *)p, "hello world") == 0);
98 }
99 #endif
100
101 /* This is simply a compile test; we promised cast_if_type can be in a
102  * static initializer. */
103 struct callback_onearg
104 {
105         void (*fn)(void *arg);
106         void *arg;
107 };
108
109 struct callback_onearg cb_onearg
110 = { typesafe_cb(void, my_callback_onearg, "hello world"), "hello world" };
111
112 struct callback_preargs
113 {
114         void (*fn)(int a, int b, void *arg);
115         void *arg;
116 };
117
118 struct callback_preargs cb_preargs
119 = { typesafe_cb_preargs(void, my_callback_preargs, "hi", int, int), "hi" };
120
121 struct callback_postargs
122 {
123         void (*fn)(void *arg, int a, int b);
124         void *arg;
125 };
126
127 struct callback_postargs cb_postargs
128 = { typesafe_cb_postargs(void, my_callback_postargs, "hi", int, int), "hi" };
129
130 int main(int argc, char *argv[])
131 {
132         void *p = &dummy;
133         unsigned long l = (unsigned long)p;
134
135         plan_tests(2 + 3 + 3 + 3);
136         set_some_value(p);
137         set_some_value(l);
138
139         callback_onearg(my_callback_onearg, "hello world");
140         callback_onearg(my_callback_onearg_const, "hello world");
141         callback_onearg(my_callback_onearg_volatile, "hello world");
142
143         callback_preargs(my_callback_preargs, "hello world");
144 #if 0 /* FIXME */
145         callback_preargs(my_callback_preargs_const, "hello world");
146         callback_preargs(my_callback_preargs_volatile, "hello world");
147 #endif
148
149         callback_postargs(my_callback_postargs, "hello world");
150 #if 0 /* FIXME */
151         callback_postargs(my_callback_postargs_const, "hello world");
152         callback_postargs(my_callback_postargs_volatile, "hello world");
153 #endif
154
155         return exit_status();
156 }