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
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* author: Max Kellermann <mk@cm4all.com>
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
#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<std::size_t size>
|
|
|
|
static auto MakeArray(const char (&src)[size]) noexcept {
|
|
|
|
std::array<CharItem, size - 1> result;
|
|
|
|
for (std::size_t i = 0; i < result.size(); ++i)
|
|
|
|
result[i].ch = src[i];
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|