Debian Codegraph @

texk/xdvik/tests/test_dl_list.c

raw
1 #include "run_tests.h"
2 #include "dl_list.h"
3
4 static struct elem {
5 int i;
6 const char *name;
7 } elems[] = {
8 { 1, "Hello" },
9 { 2, " " },
10 { 3, "World" },
11 };
12
13 static struct elem test_elems[] = {
14 { 1, "Hello" },
15 { 3, "Another" },
16 { 1, "Hello" },
17 { 2, " " },
18 { 3, "World" },
19 };
20
21 static void printer_func(const void *e_in) {
22 const struct elem *e = (const struct elem *)e_in;
23 INFO((stderr, "ELEM: %d, `%s'\n", e->i, e->name));
24 }
25
26 static Boolean test_equality(int verbose,
27 struct dl_list *list,
28 const struct elem *elems,
29 size_t list_len,
30 size_t elem_cnt)
31 {
32 size_t i;
33 struct dl_list *ptr = NULL;
34
35 if (list_len != elem_cnt) {
36 if (verbose) {
37 ERROR((stderr, "List len %ld != %ld\n", (long) list_len, (long) elem_cnt));
38 }
39 return False;
40 }
41
42 for (i = 0, ptr = list; i < list_len; i++, ptr = ptr->next) {
43 const struct elem *curr = (const struct elem *)ptr->item;
44
45 if (verbose) {
46 INFO((stderr, "Checking: `%s' - `%s'\n", elems[i].name, curr->name));
47 }
48 if (strcmp(test_elems[i].name, curr->name) != 0) {
49 return False;
50 }
51 if (elems[i].i != curr->i) {
52 return False;
53 }
54 }
55 return True;
56 }
57
58 static Boolean dl_list_test1(int verbosity) {
59 struct dl_list *testlist = NULL;
60 size_t list_len;
61 static struct elem e1 = { 3, "Another" };
62 static struct elem e2 = { 1, "Hello" };
63
64 testlist = dl_list_insert(testlist, &elems[0]);
65 testlist = dl_list_insert(testlist, &elems[1]);
66 testlist = dl_list_insert(testlist, &elems[2]);
67 testlist = dl_list_head(testlist);
68
69 if (verbosity) {
70 testlist = dl_list_head(testlist);
71 dl_list_apply(testlist, printer_func);
72 }
73
74 list_len = dl_list_len(testlist);
75
76 test_equality(verbosity, testlist, elems, list_len, 3);
77
78 /* test pushing */
79 testlist = dl_list_push_front(testlist, &e1);
80 testlist = dl_list_push_front(testlist, &e2);
81 testlist = dl_list_head(testlist);
82 list_len = dl_list_len(testlist);
83
84 test_equality(verbosity, testlist, test_elems, list_len, 5);
85
86 /* test truncating */
87 testlist = dl_list_truncate(testlist);
88 testlist = dl_list_truncate(testlist);
89
90
91 return True;
92 }
93
94 void register_all_from_test_dl_list(void) {
95 register_test(dl_list_test1, "dl_list construction");
96 }