]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/test_xlist.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / test / test_xlist.cc
CommitLineData
7c673cae
FG
1#include <algorithm>
2#include <iterator>
3#include <vector>
4#include "include/xlist.h"
5
6#include "gtest/gtest.h"
7
8
9struct Item {
10 xlist<Item*>::item xitem;
11 int val;
12
13 explicit Item(int v) :
14 xitem(this),
15 val(v)
16 {}
17};
18
19class XlistTest : public testing::Test
20{
21protected:
22 typedef xlist<Item*> ItemList;
23 typedef std::vector<Item*> Items;
24 typedef std::vector<ItemList::item*> Refs;
25 Items items;
26 // for filling up an ItemList
27 Refs refs;
28
29 void SetUp() override {
30 for (int i = 0; i < 13; i++) {
31 items.push_back(new Item(i));
32 refs.push_back(&items.back()->xitem);
33 }
34 }
35 void TearDown() override {
36 for (Items::iterator i = items.begin(); i != items.end(); ++i) {
37 delete *i;
38 }
39 items.clear();
40 }
41};
42
43TEST_F(XlistTest, capability) {
44 ItemList list;
45 ASSERT_TRUE(list.empty());
11fdf7f2 46 ASSERT_EQ(0u, list.size());
7c673cae
FG
47
48 std::copy(refs.begin(), refs.end(), std::back_inserter(list));
49 ASSERT_EQ((size_t)list.size(), refs.size());
50
51 list.clear();
52 ASSERT_TRUE(list.empty());
11fdf7f2 53 ASSERT_EQ(0u, list.size());
7c673cae
FG
54}
55
56TEST_F(XlistTest, traverse) {
57 ItemList list;
58 std::copy(refs.begin(), refs.end(), std::back_inserter(list));
59
60 // advance until iterator::end()
61 size_t index = 0;
62 for (ItemList::iterator i = list.begin(); !i.end(); ++i) {
63 ASSERT_EQ(*i, items[index]);
64 index++;
65 }
66 // advance until i == v.end()
67 index = 0;
68 for (ItemList::iterator i = list.begin(); i != list.end(); ++i) {
69 ASSERT_EQ(*i, items[index]);
70 index++;
71 }
72 list.clear();
73}
74
75TEST_F(XlistTest, move_around) {
76 Item item1(42), item2(17);
77 ItemList list;
78
79 // only a single element in the list
80 list.push_back(&item1.xitem);
81 ASSERT_EQ(&item1, list.front());
82 ASSERT_EQ(&item1, list.back());
83
84 list.push_back(&item2.xitem);
85 ASSERT_EQ(&item1, list.front());
86 ASSERT_EQ(&item2, list.back());
87
88 // move item2 to the front
89 list.push_front(&item2.xitem);
90 ASSERT_EQ(&item2, list.front());
91 ASSERT_EQ(&item1, list.back());
92
93 // and move it back
94 list.push_back(&item2.xitem);
95 ASSERT_EQ(&item1, list.front());
96 ASSERT_EQ(&item2, list.back());
97
98 list.clear();
99}
100
101TEST_F(XlistTest, item_queries) {
102 Item item(42);
103 ItemList list;
104 list.push_back(&item.xitem);
105
106 ASSERT_TRUE(item.xitem.is_on_list());
107 ASSERT_EQ(&list, item.xitem.get_list());
108
109 ASSERT_TRUE(item.xitem.remove_myself());
110 ASSERT_FALSE(item.xitem.is_on_list());
111 ASSERT_TRUE(item.xitem.get_list() == NULL);
112}
113
114// Local Variables:
115// compile-command: "cd .. ;
116// make unittest_xlist &&
117// ./unittest_xlist"
118// End: