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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
// SPDX-License-Identifier: BSD-2-Clause
// Copyright CM4all GmbH
// author: Max Kellermann <max.kellermann@ionos.com>
#include "util/IntrusiveTreeSet.hxx"
#include <gtest/gtest.h>
#include <cstdlib> // for std::rand()
#include <deque>
#include <set>
#include <string>
namespace {
struct IntItem final : IntrusiveTreeSetHook<IntrusiveHookMode::TRACK> {
int value;
IntItem(int _value) noexcept:value(_value) {}
struct GetKey {
constexpr int operator()(const IntItem &item) const noexcept {
return item.value;
}
};
};
} // anonymous namespace
template<bool constant_time_size>
static void
TestBasic()
{
IntItem a{1}, b{2}, c{3}, d{4}, e{5}, f{1};
IntrusiveTreeSet<IntItem,
IntrusiveTreeSetOperators<IntItem, IntItem::GetKey>,
IntrusiveTreeSetBaseHookTraits<IntItem>,
IntrusiveTreeSetOptions{.constant_time_size=constant_time_size}> set;
EXPECT_EQ(set.size(), 0U);
EXPECT_TRUE(set.empty());
EXPECT_FALSE(a.is_linked());
EXPECT_FALSE(b.is_linked());
set.insert(b);
EXPECT_FALSE(a.is_linked());
EXPECT_TRUE(b.is_linked());
EXPECT_EQ(set.size(), 1U);
EXPECT_EQ(set.find(2), set.iterator_to(b));
EXPECT_EQ(&set.front(), &b);
set.insert(a);
EXPECT_EQ(&set.front(), &a);
EXPECT_TRUE(a.is_linked());
EXPECT_TRUE(b.is_linked());
set.insert(c);
EXPECT_EQ(&set.front(), &a);
EXPECT_EQ(set.size(), 3U);
EXPECT_NE(set.find(3), set.end());
EXPECT_EQ(set.find(3), set.iterator_to(c));
EXPECT_EQ(set.find(4), set.end());
EXPECT_TRUE(c.is_linked());
set.erase(set.iterator_to(c));
EXPECT_FALSE(c.is_linked());
EXPECT_EQ(set.size(), 2U);
EXPECT_EQ(set.find(3), set.end());
EXPECT_EQ(&set.front(), &a);
set.insert(c);
set.insert(d);
set.insert(e);
EXPECT_EQ(set.size(), 5U);
EXPECT_EQ(&set.front(), &a);
EXPECT_EQ(set.find(1), set.iterator_to(a));
EXPECT_EQ(set.find(2), set.iterator_to(b));
EXPECT_EQ(set.find(3), set.iterator_to(c));
EXPECT_EQ(set.find(4), set.iterator_to(d));
EXPECT_EQ(set.find(5), set.iterator_to(e));
EXPECT_TRUE(a.is_linked());
EXPECT_FALSE(f.is_linked());
set.erase(set.iterator_to(a));
EXPECT_FALSE(a.is_linked());
EXPECT_FALSE(f.is_linked());
EXPECT_EQ(set.find(1), set.end());
EXPECT_EQ(set.size(), 4U);
EXPECT_EQ(&set.front(), &b);
set.insert(f);
EXPECT_FALSE(a.is_linked());
EXPECT_TRUE(f.is_linked());
EXPECT_EQ(set.find(1), set.iterator_to(f));
EXPECT_EQ(set.size(), 5U);
EXPECT_EQ(&set.front(), &f);
set.pop_front();
EXPECT_FALSE(f.is_linked());
set.clear_and_dispose([](auto *i){ i->value = -1; });
EXPECT_EQ(set.size(), 0U);
EXPECT_TRUE(set.empty());
EXPECT_EQ(a.value, 1);
EXPECT_EQ(b.value, -1);
EXPECT_EQ(c.value, -1);
EXPECT_EQ(d.value, -1);
EXPECT_EQ(e.value, -1);
EXPECT_EQ(f.value, 1);
}
TEST(IntrusiveTreeSet, Basic)
{
TestBasic<false>();
TestBasic<true>();
}
template<int... values>
static constexpr auto
MakeIntItems(std::integer_sequence<int, values...>) noexcept
-> std::array<IntItem, sizeof...(values)>
{
return {values...};
}
TEST(IntrusiveTreeSet, RandomOrder)
{
auto items = MakeIntItems(std::make_integer_sequence<int, 32>());
IntrusiveTreeSet<IntItem,
IntrusiveTreeSetOperators<IntItem, IntItem::GetKey>> set;
set.insert(items[0]);
set.insert(items[5]);
set.insert(items[10]);
set.insert(items[15]);
set.insert(items[20]);
set.insert(items[25]);
set.insert(items[30]);
set.insert(items[1]);
set.insert(items[2]);
set.insert(items[3]);
set.insert(items[31]);
set.insert(items[4]);
set.insert(items[6]);
set.insert(items[7]);
set.insert(items[21]);
set.insert(items[22]);
set.insert(items[23]);
set.insert(items[24]);
set.insert(items[26]);
set.insert(items[8]);
set.insert(items[9]);
set.insert(items[11]);
set.insert(items[12]);
set.insert(items[13]);
set.insert(items[14]);
set.insert(items[27]);
set.insert(items[28]);
set.insert(items[29]);
set.insert(items[16]);
set.insert(items[17]);
set.insert(items[18]);
set.insert(items[19]);
EXPECT_EQ(set.size(), items.size());
for (const auto &i : items) {
EXPECT_TRUE(i.is_linked());
}
int expected = 0;
for (const auto &i : set) {
EXPECT_EQ(i.value, expected++);
}
for (std::size_t remove = 0; remove < items.size(); ++remove) {
EXPECT_TRUE(items[remove].is_linked());
set.pop_front();
EXPECT_FALSE(items[remove].is_linked());
#ifndef NDEBUG
set.Check();
#endif
expected = remove + 1;
for (const auto &i : set) {
EXPECT_EQ(i.value, expected++);
}
}
}
TEST(IntrusiveTreeSet, LargeRandom)
{
std::set<int> dedup;
std::deque<std::unique_ptr<IntItem>> items;
IntrusiveTreeSet<IntItem,
IntrusiveTreeSetOperators<IntItem, IntItem::GetKey>> set;
std::srand(42);
for (unsigned i = 0; i < 1024; ++i) {
/* prevent duplicates to avoid different ordering of
identical values in the std::deque and in the
IntrusiveTreeSet */
int value;
while (true) {
value = std::rand();
if (dedup.insert(value).second)
break;
}
items.emplace_back(std::make_unique<IntItem>(value));
set.insert(*items.back());
#ifndef NDEBUG
set.Check();
#endif
}
std::sort(items.begin(), items.end(), [](const auto &a, const auto &b){
return a->value < b->value;
});
std::size_t idx = 0;
for (const auto &i : set) {
EXPECT_EQ(i.value, items[idx++]->value);
}
while (!set.empty()) {
EXPECT_FALSE(items.empty());
EXPECT_TRUE(items.front()->is_linked());
EXPECT_EQ(items.front()->value, set.front().value);
// erase the front element
set.pop_front();
EXPECT_FALSE(items.front()->is_linked());
items.pop_front();
#ifndef NDEBUG
set.Check();
#endif
// erase a random element
const auto r = std::next(items.begin(), std::rand() % items.size());
EXPECT_TRUE((*r)->is_linked());
set.erase(set.iterator_to(**r));
EXPECT_FALSE((*r)->is_linked());
items.erase(r);
#ifndef NDEBUG
set.Check();
#endif
idx = 0;
for (const auto &i : set) {
EXPECT_EQ(i.value, items[idx++]->value);
}
}
EXPECT_TRUE(items.empty());
}
struct ZeroIntItem final : IntrusiveTreeSetHook<IntrusiveHookMode::TRACK> {
int value = 0;
struct GetKey {
constexpr int operator()(const ZeroIntItem &item) const noexcept {
return item.value;
}
};
};
/**
* Fill a tree with many all-zero values. This verifies the
* robustness of the RedBlackTree implementation for this corner case.
*/
TEST(IntrusiveTreeSet, Zero)
{
std::array<ZeroIntItem, 1024> items;
IntrusiveTreeSet<ZeroIntItem,
IntrusiveTreeSetOperators<ZeroIntItem, ZeroIntItem::GetKey>> set;
set.insert(items[0]);
set.insert(items[5]);
set.insert(items[10]);
set.insert(items[15]);
set.insert(items[20]);
set.insert(items[25]);
set.insert(items[30]);
set.insert(items[1]);
set.insert(items[2]);
set.insert(items[3]);
set.insert(items[31]);
set.insert(items[4]);
set.insert(items[6]);
set.insert(items[7]);
set.insert(items[21]);
set.insert(items[22]);
set.insert(items[23]);
set.insert(items[24]);
set.insert(items[26]);
set.insert(items[8]);
set.insert(items[9]);
set.insert(items[11]);
set.insert(items[12]);
set.insert(items[13]);
set.insert(items[14]);
set.insert(items[27]);
set.insert(items[28]);
set.insert(items[29]);
set.insert(items[16]);
set.insert(items[17]);
set.insert(items[18]);
set.insert(items[19]);
for (std::size_t i = 32; i < items.size(); ++i)
set.insert(items[i]);
EXPECT_EQ(set.size(), items.size());
for (const auto &i : items) {
EXPECT_TRUE(i.is_linked());
}
for (const auto &i : set) {
EXPECT_EQ(i.value, 0);
}
for (auto &i : items) {
EXPECT_TRUE(i.is_linked());
set.erase(set.iterator_to(i));
EXPECT_FALSE(i.is_linked());
#ifndef NDEBUG
set.Check();
#endif
}
EXPECT_TRUE(set.empty());
}
|