]> git.ozlabs.org Git - ccan/blob - ccan/tcon/test/compile_ok-value.c
tcon: Encode integer values into "type" canaries
[ccan] / ccan / tcon / test / compile_ok-value.c
1 #include <ccan/tcon/tcon.h>
2 #include <ccan/build_assert/build_assert.h>
3 #include <stdlib.h>
4 #include <stddef.h>
5
6 struct container {
7         void *p;
8 };
9
10 struct val_container {
11         struct container raw;
12         TCON(TCON_VALUE(fixed_val, 17));
13 };
14
15 struct other_struct {
16         char junk1;
17         int x1;
18         long junk2;
19         char *x2;
20         short junk3;
21 };
22
23 struct offs_container {
24         struct container raw;
25         TCON(TCON_VALUE(off1, offsetof(struct other_struct, x1));
26              TCON_VALUE(off2, offsetof(struct other_struct, x2)));
27 };
28
29 int main(int argc, char *argv[])
30 {
31         struct val_container valcon;
32         struct offs_container offscon;
33         TCON_WRAP(struct container, TCON_VALUE(fixed_val, 17)) valconw;
34         TCON_WRAP(struct container,
35                   TCON_VALUE(off1, offsetof(struct other_struct, x1));
36                   TCON_VALUE(off2, offsetof(struct other_struct, x2))) offsconw;
37
38         BUILD_ASSERT(tcon_value(&valcon, fixed_val) == 17);
39         BUILD_ASSERT(tcon_value(&valconw, fixed_val) == 17);
40
41         BUILD_ASSERT(tcon_value(&offscon, off1)
42                      == offsetof(struct other_struct, x1));
43         BUILD_ASSERT(tcon_value(&offscon, off2)
44                      == offsetof(struct other_struct, x2));
45         BUILD_ASSERT(tcon_value(&offsconw, off1)
46                      == offsetof(struct other_struct, x1));
47         BUILD_ASSERT(tcon_value(&offsconw, off2)
48                      == offsetof(struct other_struct, x2));
49
50         return 0;
51 }