summaryrefslogtreecommitdiff
path: root/ccan/lstack/test/run.c
blob: d67ba3224e47a10f19d5c0e2d673268bf17ab402 (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
#include "config.h"

#include <ccan/lstack/lstack.h>
#include <ccan/tap/tap.h>

struct stacker {
	const char *name;
	struct lstack_link sl;
};

int main(void)
{
	LSTACK(s);
	struct stacker a = { "Alice" };
	struct stacker b = { "Bob" };
	struct stacker c = { "Carol" };
	struct stacker *stacker;

	/* This is how many tests you plan to run */
	plan_tests(18);

	ok1(lstack_empty(&s));
	ok1(lstack_top(&s, struct stacker, sl) == NULL);

	lstack_push(&s, &a, sl);

	ok1(!lstack_empty(&s));
	ok1(lstack_top(&s, struct stacker, sl) == &a);

	lstack_push(&s, &b, sl);

	ok1(!lstack_empty(&s));
	ok1(lstack_top(&s, struct stacker, sl) == &b);

	lstack_push(&s, &c, sl);

	ok1(!lstack_empty(&s));
	ok1(lstack_top(&s, struct stacker, sl) == &c);

	stacker = lstack_pop(&s, struct stacker, sl);
	ok1(stacker == &c);

	ok1(!lstack_empty(&s));
	ok1(lstack_top(&s, struct stacker, sl) == &b);

	stacker = lstack_pop(&s, struct stacker, sl);
	ok1(stacker == &b);

	ok1(!lstack_empty(&s));
	ok1(lstack_top(&s, struct stacker, sl) == &a);

	stacker = lstack_pop(&s, struct stacker, sl);
	ok1(stacker == &a);

	ok1(lstack_empty(&s));
	ok1(lstack_top(&s, struct stacker, sl) == NULL);

	ok1(lstack_pop(&s, struct stacker, sl) == NULL);

	/* This exits depending on whether all tests passed */
	return exit_status();
}