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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#include "run_tests.h"
#include "dl_list.h"
static struct elem {
int i;
const char *name;
} elems[] = {
{ 1, "Hello" },
{ 2, " " },
{ 3, "World" },
};
static struct elem test_elems[] = {
{ 1, "Hello" },
{ 3, "Another" },
{ 1, "Hello" },
{ 2, " " },
{ 3, "World" },
};
static void printer_func(const void *e_in) {
const struct elem *e = (const struct elem *)e_in;
INFO((stderr, "ELEM: %d, `%s'\n", e->i, e->name));
}
static Boolean test_equality(int verbose,
struct dl_list *list,
const struct elem *elems,
size_t list_len,
size_t elem_cnt)
{
size_t i;
struct dl_list *ptr = NULL;
if (list_len != elem_cnt) {
if (verbose) {
ERROR((stderr, "List len %ld != %ld\n", (long) list_len, (long) elem_cnt));
}
return False;
}
for (i = 0, ptr = list; i < list_len; i++, ptr = ptr->next) {
const struct elem *curr = (const struct elem *)ptr->item;
if (verbose) {
INFO((stderr, "Checking: `%s' - `%s'\n", elems[i].name, curr->name));
}
if (strcmp(test_elems[i].name, curr->name) != 0) {
return False;
}
if (elems[i].i != curr->i) {
return False;
}
}
return True;
}
static Boolean dl_list_test1(int verbosity) {
struct dl_list *testlist = NULL;
size_t list_len;
static struct elem e1 = { 3, "Another" };
static struct elem e2 = { 1, "Hello" };
testlist = dl_list_insert(testlist, &elems[0]);
testlist = dl_list_insert(testlist, &elems[1]);
testlist = dl_list_insert(testlist, &elems[2]);
testlist = dl_list_head(testlist);
if (verbosity) {
testlist = dl_list_head(testlist);
dl_list_apply(testlist, printer_func);
}
list_len = dl_list_len(testlist);
test_equality(verbosity, testlist, elems, list_len, 3);
/* test pushing */
testlist = dl_list_push_front(testlist, &e1);
testlist = dl_list_push_front(testlist, &e2);
testlist = dl_list_head(testlist);
list_len = dl_list_len(testlist);
test_equality(verbosity, testlist, test_elems, list_len, 5);
/* test truncating */
testlist = dl_list_truncate(testlist);
testlist = dl_list_truncate(testlist);
return True;
}
void register_all_from_test_dl_list(void) {
register_test(dl_list_test1, "dl_list construction");
}
|