cdump: new module.
[ccan] / ccan / cdump / test / run-qualifiers.c
1 #include <ccan/cdump/cdump.h>
2 /* Include the C files directly. */
3 #include <ccan/cdump/cdump.c>
4 #include <ccan/tap/tap.h>
5
6 int main(void)
7 {
8         struct cdump_definitions *defs;
9         const struct cdump_type *t, *p;
10         char *ctx = tal(NULL, char), *problems;
11
12         /* This is how many tests you plan to run */
13         plan_tests(63);
14
15         defs = cdump_extract(ctx,
16                              "struct foo {\n"
17                              "  long l;\n"
18                              "  long int li;\n"
19                              "  unsigned long *ulp;\n"
20                              "  unsigned long int *ulip;\n"
21                              "};", &problems);
22         ok1(defs);
23         ok1(tal_parent(defs) == ctx);
24         ok1(!problems);
25
26         ok1(strmap_empty(&defs->enums));
27         ok1(strmap_empty(&defs->unions));
28         t = strmap_get(&defs->structs, "foo");
29         ok1(t);
30         ok1(t->kind == CDUMP_STRUCT);
31         ok1(streq(t->name, "foo"));
32         ok1(tal_count(t->u.members) == 4);
33
34         ok1(streq(t->u.members[0].name, "l"));
35         p = t->u.members[0].type;
36         ok1(p->kind == CDUMP_UNKNOWN);
37         ok1(streq(p->name, "long"));
38
39         ok1(streq(t->u.members[1].name, "li"));
40         p = t->u.members[1].type;
41         ok1(p->kind == CDUMP_UNKNOWN);
42         ok1(streq(p->name, "long int"));
43
44         ok1(streq(t->u.members[2].name, "ulp"));
45         p = t->u.members[2].type;
46         ok1(p->kind == CDUMP_POINTER);
47         p = p->u.ptr;
48         ok1(p->kind == CDUMP_UNKNOWN);
49         ok1(streq(p->name, "unsigned long"));
50
51         ok1(streq(t->u.members[3].name, "ulip"));
52         p = t->u.members[3].type;
53         ok1(p->kind == CDUMP_POINTER);
54         p = p->u.ptr;
55         ok1(p->kind == CDUMP_UNKNOWN);
56         ok1(streq(p->name, "unsigned long int"));
57
58         defs = cdump_extract(ctx,
59                              "struct foo {\n"
60                              "  volatile long vl;\n"
61                              "  const long cl;\n"
62                              "  volatile const long long int *vclli;\n"
63                              "};", &problems);
64         ok1(defs);
65         ok1(tal_parent(defs) == ctx);
66         ok1(!problems);
67
68         ok1(strmap_empty(&defs->enums));
69         ok1(strmap_empty(&defs->unions));
70         t = strmap_get(&defs->structs, "foo");
71         ok1(t);
72         ok1(t->kind == CDUMP_STRUCT);
73         ok1(streq(t->name, "foo"));
74         ok1(tal_count(t->u.members) == 3);
75
76         ok1(streq(t->u.members[0].name, "vl"));
77         ok1(streq(t->u.members[0].qualifiers, "volatile"));
78         p = t->u.members[0].type;
79         ok1(p->kind == CDUMP_UNKNOWN);
80         ok1(streq(p->name, "long"));
81
82         ok1(streq(t->u.members[1].name, "cl"));
83         ok1(streq(t->u.members[1].qualifiers, "const"));
84         p = t->u.members[1].type;
85         ok1(p->kind == CDUMP_UNKNOWN);
86         ok1(streq(p->name, "long"));
87
88         ok1(streq(t->u.members[2].name, "vclli"));
89         ok1(streq(t->u.members[2].qualifiers, "volatile const"));
90         p = t->u.members[2].type;
91         ok1(p->kind == CDUMP_POINTER);
92         p = p->u.ptr;
93         ok1(p->kind == CDUMP_UNKNOWN);
94         ok1(streq(p->name, "long long int"));
95
96         defs = cdump_extract(ctx,
97                              "struct foo {\n"
98                              "  volatile struct bar *a, b;\n"
99                              "};", &problems);
100         ok1(defs);
101         ok1(tal_parent(defs) == ctx);
102         ok1(!problems);
103
104         ok1(strmap_empty(&defs->enums));
105         ok1(strmap_empty(&defs->unions));
106         t = strmap_get(&defs->structs, "foo");
107         ok1(t);
108         ok1(t->kind == CDUMP_STRUCT);
109         ok1(streq(t->name, "foo"));
110         ok1(tal_count(t->u.members) == 2);
111
112         ok1(streq(t->u.members[0].name, "a"));
113         ok1(streq(t->u.members[0].qualifiers, "volatile"));
114         p = t->u.members[0].type;
115         ok1(p->kind == CDUMP_POINTER);
116         p = p->u.ptr;
117         ok1(p->kind == CDUMP_STRUCT);
118         ok1(streq(p->name, "bar"));
119
120         ok1(streq(t->u.members[1].name, "b"));
121         ok1(streq(t->u.members[1].qualifiers, "volatile"));
122         p = t->u.members[1].type;
123         ok1(p->kind == CDUMP_STRUCT);
124         ok1(streq(p->name, "bar"));
125
126         tal_free(ctx);
127
128         /* This exits depending on whether all tests passed */
129         return exit_status();
130 }