summaryrefslogtreecommitdiff
path: root/ccan/tal/test/run-notifier.c
blob: b57c902e80ba9c3b7ec6a9ea285b4c0be0b40419 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <ccan/tal/tal.h>
#include <ccan/tal/tal.c>
#include <ccan/tap/tap.h>

static enum tal_notify_type expect;
static void *expect_info;
static char *ctx;
static unsigned int notified1, notified2;

/* Make sure we always move on resize. */
static void *my_realloc(void *old, size_t size)
{
	void *new = realloc(old, size);
	if (new == old) {
		void *p = malloc(size);
		memcpy(p, old, size);
		free(old);
		new = p;
	}
	return new;
}

static void notify1(char *p, enum tal_notify_type notify, void *info)
{
	ok1(ctx == ctx);
	ok1(notify == expect);
	if (expect_info == &expect_info)
		expect_info = info;
	else
		ok1(info == expect_info);
	notified1++;
}

static void notify2(char *ctx, enum tal_notify_type notify, void *info)
{
	notified2++;
}

static bool seen_move, seen_resize;
static void resize_notifier(char *p, enum tal_notify_type notify, void *info)
{
	if (notify == TAL_NOTIFY_MOVE) {
		ok1(!seen_move);
		ok1(!seen_resize);
		ok1(info == ctx);
		ok1(p != ctx);
		ctx = p;
		seen_move = true;
	} else if (notify == TAL_NOTIFY_RESIZE) {
		ok1(!seen_resize);
		ok1(seen_move);
		ok1(p == ctx);
		ok1((size_t)info == 100);
		seen_resize = true;
	} else
		fail("Unexpected notifier %i", notify);
}

int main(void)
{
	char *child, *new_ctx;

	plan_tests(56);

	ctx = tal(NULL, char);
	ok1(tal_add_notifier(ctx, 511, notify1));
	ok1(notified1 == 0);
	ok1(notified2 == 0);

	expect = TAL_NOTIFY_STEAL;
	expect_info = NULL;
	ok1(tal_steal(NULL, ctx) == ctx);
	ok1(notified1 == 1);

	expect = TAL_NOTIFY_ADD_NOTIFIER;
	expect_info = notify2;
	ok1(tal_add_notifier(ctx, TAL_NOTIFY_RENAME|TAL_NOTIFY_ADD_NOTIFIER
			     |TAL_NOTIFY_DEL_NOTIFIER, notify2));
	ok1(notified1 == 2);
	ok1(notified2 == 0);

	expect = TAL_NOTIFY_RENAME;
	expect_info = (char *)"newname";
	ok1(tal_set_name(ctx, (char *)expect_info));
	ok1(notified1 == 3);
	ok1(notified2 == 1);

	expect = TAL_NOTIFY_DEL_NOTIFIER;
	expect_info = notify2;
	ok1(tal_del_notifier(ctx, notify2));
	ok1(notified1 == 4);
	ok1(notified2 == 1);

	/* Failed delete should not call notifier! */
	expect = TAL_NOTIFY_DEL_NOTIFIER;
	expect_info = notify2;
	ok1(!tal_del_notifier(ctx, notify2));
	ok1(notified1 == 4);
	ok1(notified2 == 1);

	expect = TAL_NOTIFY_ADD_CHILD;
	expect_info = &expect_info;
	child = tal(ctx, char);
	ok1(notified1 == 5);
	ok1(notified2 == 1);
	ok1(expect_info == child);

	expect = TAL_NOTIFY_DEL_CHILD;
	expect_info = child;
	tal_free(child);
	ok1(notified1 == 6);
	ok1(notified2 == 1);

	expect = TAL_NOTIFY_FREE;
	expect_info = ctx;
	tal_free(ctx);
	ok1(notified1 == 7);
	ok1(notified2 == 1);

	tal_set_backend(NULL, my_realloc, NULL, NULL);
	ctx = new_ctx = tal(NULL, char);
	ok1(tal_add_notifier(new_ctx, 511, resize_notifier));
	ok1(tal_resize(&new_ctx, 100));
	ok1(seen_move);
	ok1(seen_resize);
	tal_del_notifier(new_ctx, resize_notifier);
	tal_free(new_ctx);

	tal_cleanup();
	return exit_status();
}