]> git.ozlabs.org Git - ccan-lca-2011.git/blob - ccan/cdump/test/run-save-restore-test.c
cdump: first cut of translation of Tridge's genstruct junkcode.
[ccan-lca-2011.git] / ccan / cdump / test / run-save-restore-test.c
1 #include <ccan/cdump/cdump.h>
2 #include <ccan/cdump/cdump.c>
3 #include <ccan/cdump/cdump_internal.h>
4 #include <ccan/cdump/test/test_header.h>
5 #include <ccan/str/str.h>
6 #include <ccan/tap/tap.h>
7 #include <stddef.h>
8 #include <string.h>
9
10 /* This is as generated by cdump_parse: see run-parse.c */
11 #include <ccan/cdump/test/example_generated-decls.h>
12 #include <ccan/cdump/test/example_generated-defs.h>
13
14 static struct test2 *new_test2(const void *ctx, struct test2 *next)
15 {
16         struct test2 *t;
17
18         t = talloc(ctx, struct test2);
19         t->x1 = 1;
20         t->foo = talloc_strdup(t, "foo");
21         memset(t->fstring, 'a', 20);
22         t->dlen = 100;
23         t->dfoo = talloc_array(t, char, t->dlen);
24         memset(t->dfoo, 7, t->dlen);
25         t->fvalue = RASBERRY;
26         t->next = next;
27         return t;
28 }
29
30 static bool test2eq(const struct test2 *a, const struct test2 *b)
31 {
32         if (a->x1 != b->x1)
33                 return false;
34         if (!a->foo != !b->foo)
35                 return false;
36         if (a->foo && !streq(a->foo, b->foo))
37                 return false;
38         if (memcmp(a->fstring, b->fstring, 20) != 0)
39                 return false;
40         if (a->dlen != b->dlen)
41                 return false;
42         if (!a->dfoo != !b->dfoo)
43                 return false;
44         if (memcmp(a->dfoo, b->dfoo, a->dlen) != 0)
45                 return false;
46         if (a->fvalue != b->fvalue)
47                 return false;
48         if (!a->next != !b->next)
49                 return false;
50         if (a->next)
51                 return test2eq(a->next, b->next);
52         return true;
53 }
54
55 int main(void)
56 {
57         struct test2 *a, *b;
58         char *astr;
59         char *toplevel = talloc_strdup(NULL, "toplevel");
60
61         plan_tests(9);
62         a = new_test2(toplevel, NULL);
63         astr = cdump_bundle(toplevel, cdump_struct_test2, a);
64         ok1(astr);
65         ok1(talloc_find_parent_byname(astr, "toplevel") == toplevel);
66
67         b = talloc_zero(toplevel, struct test2);
68         ok1(cdump_unbundle(toplevel, cdump_struct_test2, b, astr));
69         ok1(test2eq(a, b));
70
71         /* Test chaining. */
72         b->x1++;
73         a->next = b;
74
75         astr = cdump_bundle(toplevel, cdump_struct_test2, a);
76         ok1(astr);
77         ok1(talloc_find_parent_byname(astr, "toplevel") == toplevel);
78
79         b = talloc_zero(toplevel, struct test2);
80         ok1(cdump_unbundle(toplevel, cdump_struct_test2, b, astr));
81         ok1(talloc_find_parent_byname(b, "toplevel") == toplevel);
82
83         ok1(test2eq(a, b));
84         talloc_free(toplevel);
85         
86         return exit_status();
87 }