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
|
#include <algorithm>
#include <iterator>
#include <vector>
#include "include/xlist.h"
#include "gtest/gtest.h"
struct Item {
xlist<Item*>::item xitem;
int val;
explicit Item(int v) :
xitem(this),
val(v)
{}
};
class XlistTest : public testing::Test
{
protected:
typedef xlist<Item*> ItemList;
typedef std::vector<Item*> Items;
typedef std::vector<ItemList::item*> Refs;
Items items;
// for filling up an ItemList
Refs refs;
void SetUp() override {
for (int i = 0; i < 13; i++) {
items.push_back(new Item(i));
refs.push_back(&items.back()->xitem);
}
}
void TearDown() override {
for (Items::iterator i = items.begin(); i != items.end(); ++i) {
delete *i;
}
items.clear();
}
};
TEST_F(XlistTest, capability) {
ItemList list;
ASSERT_TRUE(list.empty());
ASSERT_EQ(0u, list.size());
std::copy(refs.begin(), refs.end(), std::back_inserter(list));
ASSERT_EQ((size_t)list.size(), refs.size());
list.clear();
ASSERT_TRUE(list.empty());
ASSERT_EQ(0u, list.size());
}
TEST_F(XlistTest, traverse) {
ItemList list;
std::copy(refs.begin(), refs.end(), std::back_inserter(list));
// advance until iterator::end()
size_t index = 0;
for (ItemList::iterator i = list.begin(); !i.end(); ++i) {
ASSERT_EQ(*i, items[index]);
index++;
}
// advance until i == v.end()
index = 0;
for (ItemList::iterator i = list.begin(); i != list.end(); ++i) {
ASSERT_EQ(*i, items[index]);
index++;
}
list.clear();
}
TEST_F(XlistTest, move_around) {
Item item1(42), item2(17);
ItemList list;
// only a single element in the list
list.push_back(&item1.xitem);
ASSERT_EQ(&item1, list.front());
ASSERT_EQ(&item1, list.back());
list.push_back(&item2.xitem);
ASSERT_EQ(&item1, list.front());
ASSERT_EQ(&item2, list.back());
// move item2 to the front
list.push_front(&item2.xitem);
ASSERT_EQ(&item2, list.front());
ASSERT_EQ(&item1, list.back());
// and move it back
list.push_back(&item2.xitem);
ASSERT_EQ(&item1, list.front());
ASSERT_EQ(&item2, list.back());
list.clear();
}
TEST_F(XlistTest, item_queries) {
Item item(42);
ItemList list;
list.push_back(&item.xitem);
ASSERT_TRUE(item.xitem.is_on_list());
ASSERT_EQ(&list, item.xitem.get_list());
ASSERT_TRUE(item.xitem.remove_myself());
ASSERT_FALSE(item.xitem.is_on_list());
ASSERT_TRUE(item.xitem.get_list() == NULL);
}
// Local Variables:
// compile-command: "cd .. ;
// make unittest_xlist &&
// ./unittest_xlist"
// End:
|