1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
// Copyright (c) 2018-2025 Jean-Louis Leroy
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#define BOOST_TEST_MODULE static_list
#include <boost/test/unit_test.hpp>
#include "boost/openmethod/detail/static_list.hpp"
using boost::openmethod::detail::static_list;
struct value : static_list<value>::static_link {};
using test_iter = static_list<value>::iterator;
BOOST_TEST_DONT_PRINT_LOG_VALUE(test_iter);
BOOST_AUTO_TEST_CASE(test_list) {
static static_list<value> l;
static value a, b, c, d;
BOOST_TEST_REQUIRE(l.begin() == l.end());
BOOST_TEST_REQUIRE(l.empty());
l.push_back(a);
// a
BOOST_TEST_REQUIRE(&*l.begin() == &a);
BOOST_TEST_REQUIRE(!l.empty());
BOOST_TEST_REQUIRE(std::distance(l.begin(), l.end()) == 1);
static_list<value>::iterator iter;
l.push_back(b);
// a b
BOOST_TEST_REQUIRE(std::distance(l.begin(), l.end()) == 2);
// test iterator post-increment
iter = l.begin();
BOOST_TEST_REQUIRE(iter != l.end());
BOOST_TEST(&*iter++ == &a);
BOOST_TEST_REQUIRE(iter != l.end());
BOOST_TEST(&*iter++ == &b);
BOOST_TEST(iter == l.end());
// test iterator pre-increment
iter = l.begin();
++iter;
BOOST_TEST_REQUIRE(&*iter == &b);
l.push_back(c);
// a b c
BOOST_TEST_REQUIRE(std::distance(l.begin(), l.end()) == 3);
// test remove from back, the most common case
l.remove(c);
// a b
BOOST_TEST_REQUIRE(std::distance(l.begin(), l.end()) == 2);
iter = l.begin();
BOOST_TEST_REQUIRE(&*iter++ == &a);
BOOST_TEST_REQUIRE(&*iter++ == &b);
l.push_back(c);
// a b c
BOOST_TEST_REQUIRE(std::distance(l.begin(), l.end()) == 3);
l.push_back(d);
// a b c d
BOOST_TEST_REQUIRE(std::distance(l.begin(), l.end()) == 4);
// test remove from front
l.remove(a);
// b c d
BOOST_TEST_REQUIRE(std::distance(l.begin(), l.end()) == 3);
iter = l.begin();
BOOST_TEST_REQUIRE(&*iter++ == &b);
BOOST_TEST_REQUIRE(&*iter++ == &c);
BOOST_TEST_REQUIRE(&*iter++ == &d);
// test remove from middle
l.remove(b);
// c d
BOOST_TEST_REQUIRE(std::distance(l.begin(), l.end()) == 2);
iter = l.begin();
BOOST_TEST_REQUIRE(&*iter++ == &c);
BOOST_TEST_REQUIRE(&*iter++ == &d);
// test remove from end
l.remove(c);
// d
iter = l.begin();
BOOST_TEST_REQUIRE(std::distance(l.begin(), l.end()) == 1);
BOOST_TEST_REQUIRE(&*iter++ == &d);
// test remove from end, one item left
l.remove(d);
BOOST_TEST_REQUIRE(std::distance(l.begin(), l.end()) == 0);
}
struct static_value : static_list<static_value>::static_link {
explicit static_value(static_list<static_value>& reg) {
reg.push_back(*this);
}
};
// Check that static links and list work regardless of static initialization
// order.
using static_test_iter = static_list<static_value>::iterator;
BOOST_TEST_DONT_PRINT_LOG_VALUE(static_test_iter);
namespace link_link_list {
extern static_list<static_value> reg;
static_value a(reg);
static_value b(reg);
static_list<static_value> reg;
BOOST_AUTO_TEST_CASE(test_static_link_link_list) {
auto iter = reg.begin(), last = reg.end();
BOOST_TEST_REQUIRE(iter != last);
BOOST_TEST_REQUIRE(&*iter == &a);
BOOST_TEST_REQUIRE(++iter != last);
BOOST_TEST_REQUIRE(&*iter == &b);
BOOST_TEST_REQUIRE(++iter == last);
}
} // namespace link_link_list
namespace link_list_link {
extern static_list<static_value> reg;
static_value a(reg);
static_list<static_value> reg;
static_value b(reg);
BOOST_AUTO_TEST_CASE(test_static_link_list_link) {
auto iter = reg.begin(), last = reg.end();
BOOST_TEST_REQUIRE(iter != last);
BOOST_TEST_REQUIRE(&*iter == &a);
BOOST_TEST_REQUIRE(++iter != last);
BOOST_TEST_REQUIRE(&*iter == &b);
BOOST_TEST_REQUIRE(++iter == last);
}
} // namespace link_list_link
namespace list_link_link {
static_list<static_value> reg;
static_value a(reg);
static_value b(reg);
BOOST_AUTO_TEST_CASE(test_static_list_link_link) {
auto iter = reg.begin(), last = reg.end();
BOOST_TEST_REQUIRE(iter != last);
BOOST_TEST_REQUIRE(&*iter == &a);
BOOST_TEST_REQUIRE(++iter != last);
BOOST_TEST_REQUIRE(&*iter == &b);
BOOST_TEST_REQUIRE(++iter == last);
}
} // namespace list_link_link
|