2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2021-03-02 18:00:41 +01:00
|
|
|
|
|
|
|
#include "util/IntrusiveList.hxx"
|
2022-11-10 11:31:19 +01:00
|
|
|
#include "util/SortList.hxx"
|
2021-03-02 18:00:41 +01:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-11-12 08:52:58 +01:00
|
|
|
template<IntrusiveHookMode mode>
|
|
|
|
struct CharItem final : IntrusiveListHook<mode> {
|
2022-11-11 16:16:11 +01:00
|
|
|
char ch;
|
|
|
|
|
|
|
|
constexpr CharItem(char _ch) noexcept:ch(_ch) {}
|
|
|
|
};
|
|
|
|
|
2022-11-12 08:52:58 +01:00
|
|
|
template<IntrusiveHookMode mode>
|
2022-11-11 16:16:11 +01:00
|
|
|
static std::string
|
2022-11-12 08:52:58 +01:00
|
|
|
ToString(const IntrusiveList<CharItem<mode>> &list,
|
|
|
|
typename IntrusiveList<CharItem<mode>>::const_iterator it,
|
2022-11-11 16:16:11 +01:00
|
|
|
std::size_t n) noexcept
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
for (std::size_t i = 0; i < n; ++i, ++it)
|
|
|
|
result.push_back(it == list.end() ? '_' : it->ch);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-11-12 08:52:58 +01:00
|
|
|
template<IntrusiveHookMode mode>
|
2022-11-11 16:16:11 +01:00
|
|
|
static std::string
|
2022-11-12 08:52:58 +01:00
|
|
|
ToStringReverse(const IntrusiveList<CharItem<mode>> &list,
|
|
|
|
typename IntrusiveList<CharItem<mode>>::const_iterator it,
|
2022-11-11 16:16:11 +01:00
|
|
|
std::size_t n) noexcept
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
for (std::size_t i = 0; i < n; ++i, --it)
|
|
|
|
result.push_back(it == list.end() ? '_' : it->ch);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2021-03-02 18:00:41 +01:00
|
|
|
TEST(IntrusiveList, Basic)
|
|
|
|
{
|
2022-11-12 08:52:58 +01:00
|
|
|
using Item = CharItem<IntrusiveHookMode::NORMAL>;
|
2021-03-02 18:00:41 +01:00
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
Item items[]{'a', 'b', 'c'};
|
2021-03-02 18:00:41 +01:00
|
|
|
|
|
|
|
IntrusiveList<Item> list;
|
2022-11-11 16:16:11 +01:00
|
|
|
for (auto &i : items)
|
|
|
|
list.push_back(i);
|
|
|
|
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 5), "abc_a");
|
|
|
|
ASSERT_EQ(ToStringReverse(list, list.begin(), 5), "a_cba");
|
|
|
|
|
|
|
|
items[1].unlink();
|
2021-03-02 18:00:41 +01:00
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 4), "ac_a");
|
|
|
|
ASSERT_EQ(ToStringReverse(list, list.begin(), 4), "a_ca");
|
2022-11-10 15:57:12 +01:00
|
|
|
|
|
|
|
IntrusiveList<Item> other_list;
|
2022-11-11 16:16:11 +01:00
|
|
|
Item other_items[]{'d', 'e', 'f', 'g'};
|
|
|
|
for (auto &i : other_items)
|
|
|
|
other_list.push_back(i);
|
2022-11-10 15:57:12 +01:00
|
|
|
|
|
|
|
list.splice(std::next(list.begin()), other_list,
|
2022-11-11 16:16:11 +01:00
|
|
|
other_list.iterator_to(other_items[1]),
|
|
|
|
other_list.iterator_to(other_items[3]), 2);
|
|
|
|
|
|
|
|
ASSERT_EQ(ToString(other_list, other_list.begin(), 4), "dg_d");
|
|
|
|
ASSERT_EQ(ToStringReverse(other_list, other_list.begin(), 4), "d_gd");
|
|
|
|
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 6), "aefc_a");
|
|
|
|
ASSERT_EQ(ToStringReverse(list, list.begin(), 6), "a_cfea");
|
2021-03-02 18:00:41 +01:00
|
|
|
}
|
|
|
|
|
2022-11-12 08:52:58 +01:00
|
|
|
TEST(IntrusiveList, Track)
|
2021-03-02 18:00:41 +01:00
|
|
|
{
|
2022-11-12 08:52:58 +01:00
|
|
|
using Item = CharItem<IntrusiveHookMode::TRACK>;
|
2021-03-02 18:00:41 +01:00
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
Item items[]{'a', 'b', 'c'};
|
2021-03-02 18:00:41 +01:00
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
for (const auto &i : items)
|
|
|
|
ASSERT_FALSE(i.is_linked());
|
2021-03-02 18:00:41 +01:00
|
|
|
|
|
|
|
IntrusiveList<Item> list;
|
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
list.push_back(items[1]);
|
|
|
|
list.push_back(items[2]);
|
|
|
|
list.push_front(items[0]);
|
2021-03-02 18:00:41 +01:00
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
for (const auto &i : items)
|
|
|
|
ASSERT_TRUE(i.is_linked());
|
2021-03-02 18:00:41 +01:00
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 5), "abc_a");
|
|
|
|
ASSERT_EQ(ToStringReverse(list, list.begin(), 5), "a_cba");
|
2021-03-02 18:00:41 +01:00
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
items[1].unlink();
|
2021-03-02 18:00:41 +01:00
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
ASSERT_TRUE(items[0].is_linked());
|
|
|
|
ASSERT_FALSE(items[1].is_linked());
|
|
|
|
ASSERT_TRUE(items[2].is_linked());
|
2021-03-02 18:00:41 +01:00
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 4), "ac_a");
|
|
|
|
ASSERT_EQ(ToStringReverse(list, list.begin(), 4), "a_ca");
|
2021-03-02 18:00:41 +01:00
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
list.erase(list.iterator_to(items[0]));
|
2021-03-02 18:00:41 +01:00
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
ASSERT_FALSE(items[0].is_linked());
|
|
|
|
ASSERT_FALSE(items[1].is_linked());
|
|
|
|
ASSERT_TRUE(items[2].is_linked());
|
2021-03-02 18:00:41 +01:00
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 3), "c_c");
|
|
|
|
ASSERT_EQ(ToStringReverse(list, list.begin(), 3), "c_c");
|
2021-03-02 18:00:41 +01:00
|
|
|
|
|
|
|
list.clear();
|
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
ASSERT_FALSE(items[0].is_linked());
|
|
|
|
ASSERT_FALSE(items[1].is_linked());
|
|
|
|
ASSERT_FALSE(items[2].is_linked());
|
|
|
|
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 2), "__");
|
|
|
|
ASSERT_EQ(ToStringReverse(list, list.begin(), 2), "__");
|
2021-03-02 18:00:41 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
IntrusiveList<Item> list2;
|
2022-11-11 16:16:11 +01:00
|
|
|
list2.push_back(items[0]);
|
|
|
|
ASSERT_TRUE(items[0].is_linked());
|
2021-03-02 18:00:41 +01:00
|
|
|
}
|
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
ASSERT_FALSE(items[0].is_linked());
|
2021-03-02 18:00:41 +01:00
|
|
|
}
|
2021-03-02 18:13:55 +01:00
|
|
|
|
|
|
|
TEST(IntrusiveList, AutoUnlink)
|
|
|
|
{
|
2022-11-12 08:52:58 +01:00
|
|
|
using Item = CharItem<IntrusiveHookMode::AUTO_UNLINK>;
|
2021-03-02 18:13:55 +01:00
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
Item a{'a'};
|
2021-03-02 18:13:55 +01:00
|
|
|
ASSERT_FALSE(a.is_linked());
|
|
|
|
|
|
|
|
IntrusiveList<Item> list;
|
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
Item b{'b'};
|
2021-03-02 18:13:55 +01:00
|
|
|
ASSERT_FALSE(b.is_linked());
|
|
|
|
|
|
|
|
{
|
2022-11-11 16:16:11 +01:00
|
|
|
Item c{'c'};
|
2021-03-02 18:13:55 +01:00
|
|
|
|
|
|
|
list.push_back(a);
|
|
|
|
list.push_back(b);
|
|
|
|
list.push_back(c);
|
|
|
|
|
|
|
|
ASSERT_TRUE(a.is_linked());
|
|
|
|
ASSERT_TRUE(b.is_linked());
|
|
|
|
ASSERT_TRUE(c.is_linked());
|
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 5), "abc_a");
|
2021-03-02 18:13:55 +01:00
|
|
|
}
|
|
|
|
|
2022-11-11 16:16:11 +01:00
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 5), "ab_ab");
|
2021-03-02 18:13:55 +01:00
|
|
|
|
|
|
|
ASSERT_TRUE(a.is_linked());
|
|
|
|
ASSERT_TRUE(b.is_linked());
|
|
|
|
}
|
2022-11-10 11:31:19 +01:00
|
|
|
|
2023-10-13 18:00:28 +02:00
|
|
|
TEST(IntrusiveList, Tag)
|
|
|
|
{
|
|
|
|
struct A {};
|
|
|
|
struct B {};
|
|
|
|
|
|
|
|
struct TaggedItem
|
|
|
|
: public IntrusiveListHook<IntrusiveHookMode::NORMAL, A>,
|
|
|
|
public IntrusiveListHook<IntrusiveHookMode::NORMAL, B> {};
|
|
|
|
|
|
|
|
TaggedItem one, two;
|
|
|
|
|
|
|
|
IntrusiveList<TaggedItem, IntrusiveListBaseHookTraits<TaggedItem, A>> a;
|
|
|
|
IntrusiveList<TaggedItem, IntrusiveListBaseHookTraits<TaggedItem, B>> b;
|
|
|
|
|
|
|
|
EXPECT_TRUE(a.empty());
|
|
|
|
EXPECT_TRUE(b.empty());
|
|
|
|
|
|
|
|
a.push_back(one);
|
|
|
|
a.push_back(two);
|
|
|
|
|
|
|
|
EXPECT_FALSE(a.empty());
|
|
|
|
EXPECT_TRUE(b.empty());
|
|
|
|
|
|
|
|
b.push_back(one);
|
|
|
|
|
|
|
|
EXPECT_FALSE(a.empty());
|
|
|
|
EXPECT_FALSE(b.empty());
|
|
|
|
|
|
|
|
a.clear();
|
|
|
|
|
|
|
|
EXPECT_TRUE(a.empty());
|
|
|
|
EXPECT_FALSE(b.empty());
|
|
|
|
|
|
|
|
a.push_back(two);
|
|
|
|
a.push_back(one);
|
|
|
|
|
|
|
|
EXPECT_FALSE(a.empty());
|
|
|
|
EXPECT_FALSE(b.empty());
|
|
|
|
|
|
|
|
b.erase(b.iterator_to(one));
|
|
|
|
|
|
|
|
EXPECT_FALSE(a.empty());
|
|
|
|
EXPECT_TRUE(b.empty());
|
|
|
|
}
|
|
|
|
|
2022-11-10 11:31:19 +01:00
|
|
|
TEST(IntrusiveList, Merge)
|
|
|
|
{
|
2022-11-12 08:52:58 +01:00
|
|
|
using Item = CharItem<IntrusiveHookMode::NORMAL>;
|
2022-11-10 11:31:19 +01:00
|
|
|
|
|
|
|
const auto predicate = [](const Item &a, const Item &b){
|
|
|
|
return a.ch < b.ch;
|
|
|
|
};
|
|
|
|
|
|
|
|
Item items[]{'c', 'k', 'u'};
|
|
|
|
|
|
|
|
IntrusiveList<Item> list;
|
|
|
|
for (auto &i : items)
|
|
|
|
list.push_back(i);
|
|
|
|
|
|
|
|
IntrusiveList<Item> other_list;
|
|
|
|
Item other_items[]{'a', 'b', 'g', 'm', 'n', 'x', 'y', 'z'};
|
|
|
|
for (auto &i : other_items)
|
|
|
|
other_list.push_back(i);
|
|
|
|
|
|
|
|
MergeList(list, other_list, predicate);
|
|
|
|
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 13), "abcgkmnuxyz_a");
|
|
|
|
ASSERT_TRUE(other_list.empty());
|
|
|
|
|
|
|
|
Item more_items[]{'a', 'o', 'p', 'q'};
|
|
|
|
for (auto &i : more_items)
|
|
|
|
other_list.push_back(i);
|
|
|
|
|
|
|
|
MergeList(list, other_list, predicate);
|
|
|
|
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 17), "aabcgkmnopquxyz_a");
|
|
|
|
ASSERT_EQ(&*list.begin(), &other_items[0]);
|
|
|
|
ASSERT_EQ(&*std::next(list.begin()), &more_items[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IntrusiveList, Sort)
|
|
|
|
{
|
2022-11-12 08:52:58 +01:00
|
|
|
using Item = CharItem<IntrusiveHookMode::NORMAL>;
|
2022-11-10 11:31:19 +01:00
|
|
|
|
|
|
|
const auto predicate = [](const Item &a, const Item &b){
|
|
|
|
return a.ch < b.ch;
|
|
|
|
};
|
|
|
|
|
|
|
|
Item items[]{'z', 'a', 'b', 'q', 'b', 'c', 't', 'm', 'y'};
|
|
|
|
|
|
|
|
IntrusiveList<Item> list;
|
|
|
|
SortList(list, predicate);
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 2), "__");
|
|
|
|
|
|
|
|
list.push_back(items[0]);
|
|
|
|
SortList(list, predicate);
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 3), "z_z");
|
|
|
|
|
|
|
|
list.push_back(items[1]);
|
|
|
|
SortList(list, predicate);
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 4), "az_a");
|
|
|
|
SortList(list, predicate);
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 4), "az_a");
|
|
|
|
|
|
|
|
list.clear();
|
|
|
|
for (auto &i : items)
|
|
|
|
list.push_back(i);
|
|
|
|
|
|
|
|
SortList(list, predicate);
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 11), "abbcmqtyz_a");
|
|
|
|
ASSERT_EQ(&*std::next(list.begin(), 1), &items[2]);
|
|
|
|
ASSERT_EQ(&*std::next(list.begin(), 2), &items[4]);
|
|
|
|
}
|
2022-11-13 08:51:18 +01:00
|
|
|
|
|
|
|
#include "util/IntrusiveSortedList.hxx"
|
|
|
|
|
|
|
|
TEST(IntrusiveSortedList, Basic)
|
|
|
|
{
|
|
|
|
using Item = CharItem<IntrusiveHookMode::NORMAL>;
|
|
|
|
|
|
|
|
struct Compare {
|
|
|
|
constexpr bool operator()(const Item &a, const Item &b) noexcept {
|
|
|
|
return a.ch < b.ch;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Item items[]{'z', 'a', 'b', 'q', 'b', 'c', 't', 'm', 'y'};
|
|
|
|
|
|
|
|
IntrusiveSortedList<Item, Compare> list;
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 2), "__");
|
|
|
|
|
|
|
|
list.insert(items[0]);
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 3), "z_z");
|
|
|
|
|
|
|
|
list.insert(items[1]);
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 4), "az_a");
|
|
|
|
|
|
|
|
list.insert(items[2]);
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 5), "abz_a");
|
|
|
|
|
|
|
|
list.insert(items[3]);
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 6), "abqz_a");
|
|
|
|
|
|
|
|
list.insert(items[4]);
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 7), "abbqz_a");
|
|
|
|
|
|
|
|
list.insert(items[5]);
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 8), "abbcqz_a");
|
|
|
|
|
|
|
|
list.insert(items[6]);
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 9), "abbcqtz_a");
|
|
|
|
|
|
|
|
list.insert(items[7]);
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 10), "abbcmqtz_a");
|
|
|
|
|
|
|
|
list.insert(items[8]);
|
|
|
|
ASSERT_EQ(ToString(list, list.begin(), 11), "abbcmqtyz_a");
|
|
|
|
}
|