summaryrefslogtreecommitdiffstats
path: root/src/test/test_xlist.cc
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
commit19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch)
tree42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/test/test_xlist.cc
parentInitial commit. (diff)
downloadceph-upstream/16.2.11+ds.tar.xz
ceph-upstream/16.2.11+ds.zip
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/test_xlist.cc')
-rw-r--r--src/test/test_xlist.cc118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/test/test_xlist.cc b/src/test/test_xlist.cc
new file mode 100644
index 000000000..35b62edcf
--- /dev/null
+++ b/src/test/test_xlist.cc
@@ -0,0 +1,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: