test/util/TestIntrusiveForwardList: add constant_time_size test

This commit is contained in:
Max Kellermann 2023-09-16 22:16:01 +02:00
parent 6f7dce6dca
commit 64e91d6eb7

View File

@ -32,7 +32,8 @@ TEST(IntrusiveForwardList, Basic)
Item items[]{'a', 'b', 'c'};
IntrusiveForwardList<CharItem> list;
using List = IntrusiveForwardList<CharItem>;
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<CharItem>,
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)