]> git.ozlabs.org Git - ccan/blob - ccan/list/test/run-prepend_list.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / list / test / run-prepend_list.c
1 #include <ccan/list/list.h>
2 #include <ccan/tap/tap.h>
3 #include <ccan/list/list.c>
4 #include <stdarg.h>
5
6 static bool list_expect(struct list_head *h, ...)
7 {
8         va_list ap;
9         struct list_node *n = &h->n, *expected;
10
11         va_start(ap, h);
12         while ((expected = va_arg(ap, struct list_node *)) != NULL) {
13                 n = n->next;
14                 if (n != expected)
15                         return false;
16         }
17         return (n->next == &h->n);
18 }
19
20 int main(void)
21 {
22         struct list_head h1, h2;
23         struct list_node n[4];
24
25         plan_tests(40);
26
27         list_head_init(&h1);
28         list_head_init(&h2);
29
30         /* Append an empty list to an empty list. */
31         list_append_list(&h1, &h2);
32         ok1(list_empty(&h1));
33         ok1(list_empty(&h2));
34         ok1(list_check(&h1, NULL));
35         ok1(list_check(&h2, NULL));
36
37         /* Prepend an empty list to an empty list. */
38         list_prepend_list(&h1, &h2);
39         ok1(list_empty(&h1));
40         ok1(list_empty(&h2));
41         ok1(list_check(&h1, NULL));
42         ok1(list_check(&h2, NULL));
43
44         /* Append an empty list to a non-empty list */
45         list_add(&h1, &n[0]);
46         list_append_list(&h1, &h2);
47         ok1(list_empty(&h2));
48         ok1(list_check(&h1, NULL));
49         ok1(list_check(&h2, NULL));
50         ok1(list_expect(&h1, &n[0], NULL));
51
52         /* Prepend an empty list to a non-empty list */
53         list_prepend_list(&h1, &h2);
54         ok1(list_empty(&h2));
55         ok1(list_check(&h1, NULL));
56         ok1(list_check(&h2, NULL));
57         ok1(list_expect(&h1, &n[0], NULL));
58
59         /* Append a non-empty list to an empty list. */
60         list_append_list(&h2, &h1);
61         ok1(list_empty(&h1));
62         ok1(list_check(&h1, NULL));
63         ok1(list_check(&h2, NULL));
64         ok1(list_expect(&h2, &n[0], NULL));
65
66         /* Prepend a non-empty list to an empty list. */
67         list_prepend_list(&h1, &h2);
68         ok1(list_empty(&h2));
69         ok1(list_check(&h1, NULL));
70         ok1(list_check(&h2, NULL));
71         ok1(list_expect(&h1, &n[0], NULL));
72
73         /* Prepend a non-empty list to non-empty list. */
74         list_add(&h2, &n[1]);
75         list_prepend_list(&h1, &h2);
76         ok1(list_empty(&h2));
77         ok1(list_check(&h1, NULL));
78         ok1(list_check(&h2, NULL));
79         ok1(list_expect(&h1, &n[1], &n[0], NULL));
80
81         /* Append a non-empty list to non-empty list. */
82         list_add(&h2, &n[2]);
83         list_append_list(&h1, &h2);
84         ok1(list_empty(&h2));
85         ok1(list_check(&h1, NULL));
86         ok1(list_check(&h2, NULL));
87         ok1(list_expect(&h1, &n[1], &n[0], &n[2], NULL));
88
89         /* Prepend a 2-entry list to a 2-entry list. */
90         list_del_from(&h1, &n[2]);
91         list_add(&h2, &n[2]);
92         list_add_tail(&h2, &n[3]);
93         list_prepend_list(&h1, &h2);
94         ok1(list_empty(&h2));
95         ok1(list_check(&h1, NULL));
96         ok1(list_check(&h2, NULL));
97         ok1(list_expect(&h1, &n[2], &n[3], &n[1], &n[0], NULL));
98
99         /* Append a 2-entry list to a 2-entry list. */
100         list_del_from(&h1, &n[2]);
101         list_del_from(&h1, &n[3]);
102         list_add(&h2, &n[2]);
103         list_add_tail(&h2, &n[3]);
104         list_append_list(&h1, &h2);
105         ok1(list_empty(&h2));
106         ok1(list_check(&h1, NULL));
107         ok1(list_check(&h2, NULL));
108         ok1(list_expect(&h1, &n[1], &n[0], &n[2], &n[3], NULL));
109
110         return exit_status();
111 }