1 #include <ccan/tal/tal.h>
2 #include <ccan/tal/tal.c>
3 #include <ccan/tap/tap.h>
5 static enum tal_notify_type expect;
6 static void *expect_info;
8 static unsigned int notified1, notified2, notified_null;
10 /* Make sure we always move on resize. */
11 static void *my_realloc(void *old, size_t size)
13 void *new = realloc(old, size);
15 void *p = malloc(size);
23 static void notify1(char *p, enum tal_notify_type notify, void *info)
26 ok1(notify == expect);
27 if (expect_info == &expect_info)
30 ok1(info == expect_info);
34 static void notify2(char *ctx UNNEEDED,
35 enum tal_notify_type notify UNNEEDED,
41 static void notify_null(void *p, enum tal_notify_type notify, void *info)
44 ok1(notify == expect);
45 if (expect_info == &expect_info)
48 ok1(info == expect_info);
52 static bool seen_move, seen_resize;
53 static void resize_notifier(char *p, enum tal_notify_type notify, void *info)
55 if (notify == TAL_NOTIFY_MOVE) {
62 } else if (notify == TAL_NOTIFY_RESIZE) {
66 ok1((size_t)info == 100);
69 fail("Unexpected notifier %i", notify);
74 char *child, *new_ctx;
78 ctx = tal(NULL, char);
79 ok1(tal_add_notifier(ctx, 511, notify1));
83 expect = TAL_NOTIFY_STEAL;
85 ok1(tal_steal(NULL, ctx) == ctx);
88 expect = TAL_NOTIFY_ADD_NOTIFIER;
89 expect_info = notify2;
90 ok1(tal_add_notifier(ctx, TAL_NOTIFY_RENAME|TAL_NOTIFY_ADD_NOTIFIER
91 |TAL_NOTIFY_DEL_NOTIFIER, notify2));
95 expect = TAL_NOTIFY_RENAME;
96 expect_info = (char *)"newname";
97 ok1(tal_set_name(ctx, (char *)expect_info));
101 expect = TAL_NOTIFY_DEL_NOTIFIER;
102 expect_info = notify2;
103 ok1(tal_del_notifier(ctx, notify2));
107 /* Failed delete should not call notifier! */
108 expect = TAL_NOTIFY_DEL_NOTIFIER;
109 expect_info = notify2;
110 ok1(!tal_del_notifier(ctx, notify2));
114 expect = TAL_NOTIFY_ADD_CHILD;
115 expect_info = &expect_info;
116 child = tal(ctx, char);
119 ok1(expect_info == child);
121 expect = TAL_NOTIFY_DEL_CHILD;
127 expect = TAL_NOTIFY_FREE;
133 /* Notifiers on NULL work, too. */
134 ok1(tal_add_notifier(NULL, TAL_NOTIFY_ADD_CHILD|TAL_NOTIFY_DEL_CHILD,
136 expect = TAL_NOTIFY_ADD_CHILD;
137 expect_info = &expect_info;
138 child = tal(NULL, char);
139 ok1(notified_null == 1);
141 expect = TAL_NOTIFY_DEL_CHILD;
144 ok1(notified_null == 2);
145 ok1(tal_del_notifier(NULL, notify_null));
147 tal_set_backend(NULL, my_realloc, NULL, NULL);
148 ctx = new_ctx = tal(NULL, char);
149 ok1(tal_add_notifier(new_ctx, 511, resize_notifier));
150 ok1(tal_resize(&new_ctx, 100));
153 tal_del_notifier(new_ctx, resize_notifier);
157 return exit_status();