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