Fix typos detected by github.com/client9/misspell
[ccan] / ccan / tal / test / run-notifier.c
1 #include <ccan/tal/tal.h>
2 #include <ccan/tal/tal.c>
3 #include <ccan/tap/tap.h>
4
5 static enum tal_notify_type expect;
6 static void *expect_info;
7 static char *ctx;
8 static unsigned int notified1, notified2;
9
10 /* Make sure we always move on resize. */
11 static void *my_realloc(void *old, size_t size)
12 {
13         void *new = realloc(old, size);
14         if (new == old) {
15                 void *p = malloc(size);
16                 memcpy(p, old, size);
17                 free(old);
18                 new = p;
19         }
20         return new;
21 }
22
23 static void notify1(char *p UNNEEDED, enum tal_notify_type notify, void *info)
24 {
25         ok1(ctx == ctx);
26         ok1(notify == expect);
27         if (expect_info == &expect_info)
28                 expect_info = info;
29         else
30                 ok1(info == expect_info);
31         notified1++;
32 }
33
34 static void notify2(char *ctx UNNEEDED,
35                     enum tal_notify_type notify UNNEEDED,
36                     void *info UNNEEDED)
37 {
38         notified2++;
39 }
40
41 static bool seen_move, seen_resize;
42 static void resize_notifier(char *p, enum tal_notify_type notify, void *info)
43 {
44         if (notify == TAL_NOTIFY_MOVE) {
45                 ok1(!seen_move);
46                 ok1(!seen_resize);
47                 ok1(info == ctx);
48                 ok1(p != ctx);
49                 ctx = p;
50                 seen_move = true;
51         } else if (notify == TAL_NOTIFY_RESIZE) {
52                 ok1(!seen_resize);
53                 ok1(seen_move);
54                 ok1(p == ctx);
55                 ok1((size_t)info == 100);
56                 seen_resize = true;
57         } else
58                 fail("Unexpected notifier %i", notify);
59 }
60
61 int main(void)
62 {
63         char *child, *new_ctx;
64
65         plan_tests(56);
66
67         ctx = tal(NULL, char);
68         ok1(tal_add_notifier(ctx, 511, notify1));
69         ok1(notified1 == 0);
70         ok1(notified2 == 0);
71
72         expect = TAL_NOTIFY_STEAL;
73         expect_info = NULL;
74         ok1(tal_steal(NULL, ctx) == ctx);
75         ok1(notified1 == 1);
76
77         expect = TAL_NOTIFY_ADD_NOTIFIER;
78         expect_info = notify2;
79         ok1(tal_add_notifier(ctx, TAL_NOTIFY_RENAME|TAL_NOTIFY_ADD_NOTIFIER
80                              |TAL_NOTIFY_DEL_NOTIFIER, notify2));
81         ok1(notified1 == 2);
82         ok1(notified2 == 0);
83
84         expect = TAL_NOTIFY_RENAME;
85         expect_info = (char *)"newname";
86         ok1(tal_set_name(ctx, (char *)expect_info));
87         ok1(notified1 == 3);
88         ok1(notified2 == 1);
89
90         expect = TAL_NOTIFY_DEL_NOTIFIER;
91         expect_info = notify2;
92         ok1(tal_del_notifier(ctx, notify2));
93         ok1(notified1 == 4);
94         ok1(notified2 == 1);
95
96         /* Failed delete should not call notifier! */
97         expect = TAL_NOTIFY_DEL_NOTIFIER;
98         expect_info = notify2;
99         ok1(!tal_del_notifier(ctx, notify2));
100         ok1(notified1 == 4);
101         ok1(notified2 == 1);
102
103         expect = TAL_NOTIFY_ADD_CHILD;
104         expect_info = &expect_info;
105         child = tal(ctx, char);
106         ok1(notified1 == 5);
107         ok1(notified2 == 1);
108         ok1(expect_info == child);
109
110         expect = TAL_NOTIFY_DEL_CHILD;
111         expect_info = child;
112         tal_free(child);
113         ok1(notified1 == 6);
114         ok1(notified2 == 1);
115
116         expect = TAL_NOTIFY_FREE;
117         expect_info = ctx;
118         tal_free(ctx);
119         ok1(notified1 == 7);
120         ok1(notified2 == 1);
121
122         tal_set_backend(NULL, my_realloc, NULL, NULL);
123         ctx = new_ctx = tal(NULL, char);
124         ok1(tal_add_notifier(new_ctx, 511, resize_notifier));
125         ok1(tal_resize(&new_ctx, 100));
126         ok1(seen_move);
127         ok1(seen_resize);
128         tal_del_notifier(new_ctx, resize_notifier);
129         tal_free(new_ctx);
130
131         tal_cleanup();
132         return exit_status();
133 }