]> git.ozlabs.org Git - ccan/blob - ccan/lstack/test/run.c
lstack: Linked list stack implementation
[ccan] / ccan / lstack / test / run.c
1 #include "config.h"
2
3 #include <ccan/lstack/lstack.h>
4 #include <ccan/tap/tap.h>
5
6 struct stacker {
7         const char *name;
8         struct lstack_link sl;
9 };
10
11 int main(void)
12 {
13         LSTACK(s);
14         struct stacker a = { "Alice" };
15         struct stacker b = { "Bob" };
16         struct stacker c = { "Carol" };
17         struct stacker *stacker;
18
19         /* This is how many tests you plan to run */
20         plan_tests(18);
21
22         ok1(lstack_empty(&s));
23         ok1(lstack_top(&s, struct stacker, sl) == NULL);
24
25         lstack_push(&s, &a, sl);
26
27         ok1(!lstack_empty(&s));
28         ok1(lstack_top(&s, struct stacker, sl) == &a);
29
30         lstack_push(&s, &b, sl);
31
32         ok1(!lstack_empty(&s));
33         ok1(lstack_top(&s, struct stacker, sl) == &b);
34
35         lstack_push(&s, &c, sl);
36
37         ok1(!lstack_empty(&s));
38         ok1(lstack_top(&s, struct stacker, sl) == &c);
39
40         stacker = lstack_pop(&s, struct stacker, sl);
41         ok1(stacker == &c);
42
43         ok1(!lstack_empty(&s));
44         ok1(lstack_top(&s, struct stacker, sl) == &b);
45
46         stacker = lstack_pop(&s, struct stacker, sl);
47         ok1(stacker == &b);
48
49         ok1(!lstack_empty(&s));
50         ok1(lstack_top(&s, struct stacker, sl) == &a);
51
52         stacker = lstack_pop(&s, struct stacker, sl);
53         ok1(stacker == &a);
54
55         ok1(lstack_empty(&s));
56         ok1(lstack_top(&s, struct stacker, sl) == NULL);
57
58         ok1(lstack_pop(&s, struct stacker, sl) == NULL);
59
60         /* This exits depending on whether all tests passed */
61         return exit_status();
62 }