summaryrefslogtreecommitdiff
path: root/ccan/tal/test/run-destructor.c
blob: 873548889e91472013c3901b92bae8f01ae4e25c (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
#include <ccan/tal/tal.h>
#include <ccan/tal/tal.c>
#include <ccan/tap/tap.h>

static char *parent, *child;
static int destroy_count;

/* Parent gets destroyed first. */
static void destroy_parent(char *p)
{
	ok1(p == parent);
	ok1(destroy_count == 0);
	/* Can still access child. */
	*child = '1';
	destroy_count++;
}

static void destroy_child(char *p)
{
	ok1(p == child);
	ok1(destroy_count == 1);
	/* Can still access parent (though destructor has been called). */
	*parent = '1';
	destroy_count++;
}

static void destroy_inc(char *p)
{
	destroy_count++;
}

int main(void)
{
	char *child2;

	plan_tests(18);

	destroy_count = 0;
	parent = tal(NULL, char);
	child = tal(parent, char);
	ok1(tal_add_destructor(parent, destroy_parent));
	ok1(tal_add_destructor(child, destroy_child));
	tal_free(parent);
	ok1(destroy_count == 2);

	destroy_count = 0;
	parent = tal(NULL, char);
	child = tal(parent, char);
	ok1(tal_add_destructor(parent, destroy_parent));
	ok1(tal_add_destructor(child, destroy_child));
	ok1(tal_del_destructor(child, destroy_child));
	tal_free(parent);
	ok1(destroy_count == 1);

	destroy_count = 0;
	parent = tal(NULL, char);
	child = tal(parent, char);
	child2 = tal(parent, char);
	ok1(tal_add_destructor(parent, destroy_inc));
	ok1(tal_add_destructor(parent, destroy_inc));
	ok1(tal_add_destructor(child, destroy_inc));
	ok1(tal_add_destructor(child2, destroy_inc));
	tal_free(parent);
	ok1(destroy_count == 4);

	tal_cleanup();
	return exit_status();
}