2021-03-02 18:00:41 +01:00
|
|
|
/*
|
2022-11-10 15:57:12 +01:00
|
|
|
* Copyright 2020-2022 Max Kellermann <max.kellermann@gmail.com>
|
2021-03-02 18:00:41 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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 {
|
|
|
|
|
|
|
|
template<typename Hook>
|
|
|
|
struct CharItem final : Hook {
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
constexpr CharItem(char _ch) noexcept:ch(_ch) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Hook>
|
|
|
|
static std::string
|
|
|
|
ToString(const IntrusiveList<CharItem<Hook>> &list,
|
|
|
|
typename IntrusiveList<CharItem<Hook>>::const_iterator it,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Hook>
|
|
|
|
static std::string
|
|
|
|
ToStringReverse(const IntrusiveList<CharItem<Hook>> &list,
|
|
|
|
typename IntrusiveList<CharItem<Hook>>::const_iterator it,
|
|
|
|
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-11 16:16:11 +01:00
|
|
|
using Item = CharItem<IntrusiveListHook>;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IntrusiveList, SafeLink)
|
|
|
|
{
|
2022-11-11 16:16:11 +01:00
|
|
|
using Item = CharItem<SafeLinkIntrusiveListHook>;
|
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-11 16:16:11 +01:00
|
|
|
using Item = CharItem<AutoUnlinkIntrusiveListHook>;
|
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
|
|
|
|
|
|
|
TEST(IntrusiveList, Merge)
|
|
|
|
{
|
|
|
|
using Item = CharItem<IntrusiveListHook>;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
using Item = CharItem<IntrusiveListHook>;
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|