]> git.ozlabs.org Git - ccan/blob - ccan/tal/test/run-test-backend.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / tal / test / run-test-backend.c
1 #include <stdlib.h>
2 #include <stdbool.h>
3
4 /* Make sure it always uses our allocation/resize/free fns! */
5 static bool my_alloc_called;
6
7 static void *my_alloc(size_t len)
8 {
9         my_alloc_called = true;
10         return (char *)malloc(len + 16) + 16;
11 }
12
13 static void my_free(void *p)
14 {
15         if (p)
16                 free((char *)p - 16);
17 }
18
19 static void *my_realloc(void *old, size_t new_size)
20 {
21         return (char *)realloc((char *)old - 16, new_size + 16) + 16;
22 }
23
24 #define free ((void (*)(void *))abort)
25 #define malloc ((void *(*)(size_t))abort)
26 #define realloc ((void *(*)(void *, size_t))abort)
27
28 #include <ccan/tal/tal.h>
29 #include <ccan/tal/tal.c>
30 #include <ccan/tap/tap.h>
31
32 #define NUM_ALLOCS 1000
33
34 static void destroy_p(void *p UNNEEDED)
35 {
36 }
37
38 int main(void)
39 {
40         void *p, *c[NUM_ALLOCS];
41         int i;
42         char *name;
43
44         /* Mostly we rely on the allocator (or valgrind) crashing. */
45         plan_tests(1);
46
47         tal_set_backend(my_alloc, my_realloc, my_free, NULL);
48
49         p = tal(NULL, char);
50         ok1(my_alloc_called);
51
52         /* Adding properties makes us allocated. */
53         tal_add_destructor(p, destroy_p);
54
55         tal_set_name(p, "test");
56         name = tal_arr(NULL, char, 6);
57         strcpy(name, "test2");
58         tal_set_name(p, name);
59         /* makes us free old name */
60         tal_set_name(p, name);
61         tal_free(name);
62
63         /* Add lots of children. */
64         for (i = 0; i < NUM_ALLOCS; i++)
65                 c[i] = tal(p, char);
66
67         /* Now steal a few. */
68         for (i = 1; i < NUM_ALLOCS / 2; i++)
69                 tal_steal(c[0], c[i]);
70
71         /* Now free individual ones.. */
72         for (i = NUM_ALLOCS / 2; i < NUM_ALLOCS; i++)
73                 tal_free(c[i]);
74
75         /* Finally, free the parent. */
76         tal_free(p);
77
78         tal_cleanup();
79         return exit_status();
80 }