-
Notifications
You must be signed in to change notification settings - Fork 0
/
slist_test.cc
145 lines (118 loc) · 3.26 KB
/
slist_test.cc
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <gtest/gtest.h>
#include <zda/slist.h>
typedef struct int_entry {
int val;
zda_slist_node_t node;
} int_entry_t;
static zda_inline int_entry_t *new_int_entry(int v)
{
int_entry_t *entry = (int_entry_t *)malloc(sizeof(int_entry));
if (!entry) abort();
entry->val = v;
return entry;
}
#define get_int_entry(node) zda_slist_entry(node, int_entry)
TEST(slist_test, insert)
{
zda_slist_header_t list;
zda_slist_header_init(&list);
for (int i = 0; i < 10; ++i) {
zda_slist_push_front(&list, &new_int_entry(i)->node);
}
int i = 0;
zda_slist_iterate(&list)
{
printf(" i = %d\n", i);
auto x = 9 - i;
EXPECT_EQ(get_int_entry(pos)->val, x) << x;
++i;
}
zda_slist_destroy(&list, int_entry_t);
}
static void prepare_slist(zda_slist_header_t *header, int n)
{
for (int i = 0; i < n; ++i) {
zda_slist_push_front(header, &new_int_entry(i)->node);
}
}
int int_entry_cmp(zda_slist_node_t const *node, void const *key)
{
return zda_slist_entry(node, int_entry_t const)->val == *(int const *)key;
}
TEST(slist_test, search)
{
zda_slist_header_t header;
zda_slist_header_init(&header);
prepare_slist(&header, 10);
for (int i = 0; i < 10; ++i) {
auto node = zda_slist_search(&header, &i, int_entry_cmp);
ASSERT_TRUE(node);
EXPECT_EQ(get_int_entry(node)->val, i);
}
zda_slist_destroy(&header, int_entry_t);
}
TEST(slist_test, remove)
{
zda_slist_header_t header;
zda_slist_header_init(&header);
prepare_slist(&header, 10);
for (int i = 0; i < 10; ++i) {
auto front_node = zda_slist_front(&header);
auto entry = get_int_entry(front_node);
ASSERT_TRUE(front_node);
EXPECT_EQ(entry->val, 9 - i);
printf("remove entry: %d\n", entry->val);
zda_slist_pop_front(&header);
free(entry);
}
zda_slist_destroy(&header, int_entry_t);
}
static void prepare_slist2(zda_slist2_t *slist, int n)
{
for (int i = 0; i < n; ++i) {
zda_slist_push_front2(slist, &new_int_entry(i)->node);
}
}
TEST(slist2_test, insert)
{
zda_slist2_t slist;
zda_slist_header_init2(&slist);
prepare_slist2(&slist, 100);
int i = 99;
zda_slist_iterate2(&slist)
{
auto entry = zda_slist_entry(pos, int_entry);
EXPECT_EQ(entry->val, i);
--i;
}
zda_slist_destroy2(&slist, int_entry_t);
}
TEST(slist2_test, pop)
{
zda_slist_header2_t slist;
zda_slist_header_init2(&slist);
prepare_slist2(&slist, 100);
int i = 99;
while (!zda_slist_is_empty2(&slist)) {
auto node = zda_slist_front2(&slist);
auto entry = zda_slist_entry(node, int_entry_t);
EXPECT_EQ(i, entry->val);
zda_slist_pop_front2(&slist);
free(entry);
--i;
}
zda_slist_destroy2(&slist, int_entry_t);
}
TEST(slist2_test, search)
{
zda_slist_header2_t slist;
zda_slist_header_init2(&slist);
prepare_slist2(&slist, 100);
for (int i = 0; i < 100; ++i) {
auto node = zda_slist_search2(&slist, &i, int_entry_cmp);
ASSERT_TRUE(node);
auto entry = zda_slist_entry(node, int_entry_t);
EXPECT_EQ(entry->val, i);
}
zda_slist_destroy2(&slist, int_entry_t);
}