From 64e91d6eb7ec0765390b9e769ecca9cb7e4d7649 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 16 Sep 2023 22:16:01 +0200 Subject: [PATCH] test/util/TestIntrusiveForwardList: add constant_time_size test --- test/util/TestIntrusiveForwardList.cxx | 70 +++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/test/util/TestIntrusiveForwardList.cxx b/test/util/TestIntrusiveForwardList.cxx index 361dec95f..e242e1eda 100644 --- a/test/util/TestIntrusiveForwardList.cxx +++ b/test/util/TestIntrusiveForwardList.cxx @@ -32,7 +32,8 @@ TEST(IntrusiveForwardList, Basic) Item items[]{'a', 'b', 'c'}; - IntrusiveForwardList list; + using List = IntrusiveForwardList; + List list; ASSERT_EQ(ToString(list), ""); list.reverse(); ASSERT_EQ(ToString(list), ""); @@ -49,6 +50,73 @@ TEST(IntrusiveForwardList, Basic) ASSERT_EQ(ToString(list), "bc"); list.reverse(); ASSERT_EQ(ToString(list), "cb"); + + /* move constructor */ + auto list2 = std::move(list); + ASSERT_EQ(ToString(list2), "cb"); + ASSERT_EQ(ToString(list), ""); + + /* move operator */ + list = std::move(list2); + ASSERT_EQ(ToString(list), "cb"); + ASSERT_EQ(ToString(list2), ""); + + // insert_after() + List::insert_after(list.begin(), items[0]); + ASSERT_EQ(ToString(list), "cab"); +} + +TEST(IntrusiveForwardList, ConstantTimeSize) +{ + using Item = CharItem; + + Item items[]{'a', 'b', 'c'}; + + IntrusiveForwardList< + CharItem, IntrusiveForwardListBaseHookTraits, + IntrusiveForwardListOptions{.constant_time_size = true}> list; + ASSERT_EQ(ToString(list), ""); + ASSERT_EQ(list.size(), 0U); + + list.reverse(); + ASSERT_EQ(ToString(list), ""); + ASSERT_EQ(list.size(), 0U); + + for (auto &i : items) + list.push_front(i); + + ASSERT_EQ(ToString(list), "cba"); + ASSERT_EQ(list.size(), 3U); + + list.reverse(); + ASSERT_EQ(ToString(list), "abc"); + ASSERT_EQ(list.size(), 3U); + + list.pop_front(); + ASSERT_EQ(ToString(list), "bc"); + ASSERT_EQ(list.size(), 2U); + list.reverse(); + ASSERT_EQ(ToString(list), "cb"); + ASSERT_EQ(list.size(), 2U); + + /* move constructor */ + auto list2 = std::move(list); + ASSERT_EQ(ToString(list2), "cb"); + ASSERT_EQ(list2.size(), 2U); + ASSERT_EQ(ToString(list), ""); + ASSERT_EQ(list.size(), 0U); + + /* move operator */ + list = std::move(list2); + ASSERT_EQ(ToString(list), "cb"); + ASSERT_EQ(list.size(), 2U); + ASSERT_EQ(ToString(list2), ""); + ASSERT_EQ(list2.size(), 0U); + + // insert_after() + list.insert_after(list.begin(), items[0]); + ASSERT_EQ(ToString(list), "cab"); + ASSERT_EQ(list.size(), 3U); } TEST(IntrusiveForwardList, CacheLast)