48#include <gtest/gtest.h>
58struct Throwing_Move_Key
60 static inline int moves_left = -1;
64 Throwing_Move_Key() =
default;
65 Throwing_Move_Key(
const int v) :
value(v) {}
66 Throwing_Move_Key(
const Throwing_Move_Key &) =
default;
67 Throwing_Move_Key(Throwing_Move_Key &&) =
default;
68 Throwing_Move_Key &operator = (
const Throwing_Move_Key &) =
default;
69 Throwing_Move_Key &operator = (Throwing_Move_Key &&other)
72 throw std::runtime_error(
"move assignment");
79 bool operator < (
const Throwing_Move_Key &other)
const noexcept
81 return value < other.value;
86struct Throwing_Copy_Key
88 static inline bool fail =
false;
92 Throwing_Copy_Key() =
default;
93 Throwing_Copy_Key(
const int v) :
value(v) {}
94 Throwing_Copy_Key(
const Throwing_Copy_Key &other) :
value(other.
value)
97 throw std::runtime_error(
"copy");
99 Throwing_Copy_Key(Throwing_Copy_Key &&) noexcept = default;
100 Throwing_Copy_Key &operator = (const Throwing_Copy_Key &other)
103 throw std::runtime_error(
"copy assignment");
107 Throwing_Copy_Key &operator = (Throwing_Copy_Key &&) noexcept = default;
109 bool operator < (const Throwing_Copy_Key &other) const noexcept
111 return value < other.value;
118struct Failing_Default_Key
120 static inline bool fail =
false;
124 Failing_Default_Key()
127 throw std::bad_alloc();
129 Failing_Default_Key(
const int v) :
value(v) {}
130 Failing_Default_Key(
const Failing_Default_Key &) =
default;
131 Failing_Default_Key(Failing_Default_Key &&other) :
value(other.
value) {}
132 Failing_Default_Key &operator = (
const Failing_Default_Key &) =
default;
133 Failing_Default_Key &operator = (Failing_Default_Key &&other)
139 bool operator < (
const Failing_Default_Key &other)
const noexcept
141 return value < other.value;
146::testing::AssertionResult is_strictly_sorted(
const Set &s)
148 for (
size_t i = 1; i < s.size(); ++i)
149 if (not (s.nth(i - 1) < s.nth(i)))
150 return ::testing::AssertionFailure()
151 <<
"keys " << s.nth(i - 1).value <<
" and " << s.nth(i).value
152 <<
" at " << i - 1 <<
" are not strictly increasing";
153 return ::testing::AssertionSuccess();
161 EXPECT_EQ(s.
size(), 0u);
163 EXPECT_EQ(s.
find(42), s.
end());
165 EXPECT_EQ(s.
count(42), 0u);
171 EXPECT_TRUE(s.
insert(5).second);
172 EXPECT_TRUE(s.
insert(1).second);
173 EXPECT_TRUE(s.
insert(3).second);
174 EXPECT_FALSE(s.
insert(3).second);
176 ASSERT_EQ(s.
size(), 3u);
177 EXPECT_EQ(s.
nth(0), 1);
178 EXPECT_EQ(s.
nth(1), 3);
179 EXPECT_EQ(s.
nth(2), 5);
182 auto [it, inserted] = s.
insert(1);
183 EXPECT_FALSE(inserted);
185 EXPECT_EQ(it, s.
begin());
191 ASSERT_EQ(s.
size(), 3u);
192 EXPECT_EQ(s.
nth(0), 1);
193 EXPECT_EQ(s.
nth(1), 3);
194 EXPECT_EQ(s.
nth(2), 5);
199 const std::vector<int> v = {9, 7, 7, 8, 1};
201 ASSERT_EQ(s.
size(), 4u);
202 EXPECT_TRUE(std::is_sorted(s.
begin(), s.
end()));
220 ASSERT_EQ(hi - lo, 1);
231 EXPECT_EQ(s.
erase(3), 1u);
232 EXPECT_EQ(s.
erase(3), 0u);
233 EXPECT_EQ(s.
size(), 4u);
237 ASSERT_NE(it, s.
end());
240 EXPECT_EQ(s.
size(), 3u);
243 EXPECT_THROW((
void) s.
erase(s.
end()), std::out_of_range);
251 EXPECT_EQ(s.
min(), 3);
252 EXPECT_EQ(s.
max(), 9);
253 EXPECT_THROW((
void) s.
nth(3), std::out_of_range);
256 EXPECT_THROW((
void) e.
get_first(), std::underflow_error);
257 EXPECT_THROW((
void) e.
get_last(), std::underflow_error);
274 EXPECT_TRUE(s.
traverse([&
sum] (
const int &x) { sum += x; return true; }));
279 EXPECT_FALSE(s.
traverse([&visited] (
const int &x)
284 EXPECT_EQ(visited, 2);
301 EXPECT_EQ(assigned, s);
304 move_assigned = std::move(assigned);
305 EXPECT_EQ(move_assigned, s);
311 EXPECT_EQ(s.
nth(0), 5);
312 EXPECT_EQ(s.
nth(1), 3);
313 EXPECT_EQ(s.
nth(2), 1);
315 EXPECT_FALSE(s.
insert(5).second);
323 EXPECT_GE(cap, 1000u);
324 for (
int i = 0; i < 1000; ++i)
327 EXPECT_EQ(s.
size(), 1000u);
333 const int *p = s.
data();
334 ASSERT_EQ(s.
size(), 4u);
337 EXPECT_EQ(s.
begin(), p);
338 EXPECT_EQ(s.
end(), p + 4);
346 EXPECT_EQ(a.
size(), 1u);
348 EXPECT_EQ(b.
size(), 2u);
356 std::mt19937
rng(20260702);
357 std::uniform_int_distribution<int> key_dist(0, 200);
358 std::uniform_int_distribution<int> op_dist(0, 2);
363 for (
int step = 0; step < 4000; ++step)
365 const int k = key_dist(
rng);
366 switch (op_dist(
rng))
369 EXPECT_EQ(fs.
insert(
k).second, ref.insert(
k).second);
372 EXPECT_EQ(fs.
erase(
k), ref.erase(
k));
380 ASSERT_EQ(fs.
size(), ref.size());
382 for (
const int k : ref)
383 EXPECT_EQ(fs.
nth(i++),
k);
389 for (
const int k : {1, 3, 5, 7})
390 s.
insert(Throwing_Copy_Key(
k));
391 const Throwing_Copy_Key first(0), middle(4);
393 Throwing_Copy_Key::fail =
true;
394 EXPECT_THROW(s.
insert(first), std::runtime_error);
395 EXPECT_THROW(s.
insert(middle), std::runtime_error);
396 Throwing_Copy_Key::fail =
false;
398 ASSERT_EQ(s.
size(), 4u);
399 EXPECT_TRUE(is_strictly_sorted(s));
402 EXPECT_TRUE(s.
contains(Throwing_Copy_Key(5)));
410 const auto build = []
413 for (
const int k : {1, 3, 5, 7, 9})
414 s.
insert(Throwing_Move_Key(
k));
418 enum class Operation { insert_first, insert_middle, erase_first, reserve };
419 for (
const auto operation : {Operation::insert_first, Operation::insert_middle,
420 Operation::erase_first, Operation::reserve})
421 for (
int budget = 0; budget < 12; ++budget)
423 SCOPED_TRACE(testing::Message() <<
"operation " <<
static_cast<int>(operation)
424 <<
", moves allowed " << budget);
427 Throwing_Move_Key::moves_left = budget;
432 case Operation::insert_first: s.insert(Throwing_Move_Key(0));
break;
433 case Operation::insert_middle: s.insert(Throwing_Move_Key(4));
break;
434 case Operation::erase_first: s.erase(Throwing_Move_Key(1));
break;
435 case Operation::reserve: s.reserve(4 * s.capacity());
break;
438 catch (
const std::runtime_error &)
442 Throwing_Move_Key::moves_left = -1;
444 EXPECT_TRUE(is_strictly_sorted(s));
446 EXPECT_TRUE(s.is_empty());
447 else if (operation == Operation::erase_first)
448 EXPECT_EQ(s.size(), 4u);
449 else if (operation == Operation::reserve)
450 EXPECT_EQ(s.size(), 5u);
452 EXPECT_EQ(s.size(), 6u);
455 s.insert(Throwing_Move_Key(100));
456 EXPECT_TRUE(s.contains(Throwing_Move_Key(100)));
457 EXPECT_TRUE(is_strictly_sorted(s));
462 Throwing_Move_Key::moves_left = 11;
463 EXPECT_NO_THROW(s.insert(Throwing_Move_Key(0)));
464 Throwing_Move_Key::moves_left = -1;
472 using Key = Failing_Default_Key;
473 ASSERT_FALSE(std::is_nothrow_move_assignable_v<Key>);
476 for (
int k = 0;
k < 4; ++
k)
480 const auto holds_the_four_keys = [&s]
484 for (
int k = 0;
k < 4; ++
k)
485 if (s.
nth(
k).value !=
k)
491 EXPECT_THROW(s.
reserve(std::numeric_limits<size_t>::max()), std::overflow_error);
492 EXPECT_TRUE(holds_the_four_keys());
496 EXPECT_THROW(s.
reserve(64), std::bad_alloc);
497 EXPECT_THROW(s.
insert(Key(9)), std::bad_alloc);
499 EXPECT_TRUE(holds_the_four_keys());
503 EXPECT_EQ(s.
size(), 5u);
506 EXPECT_TRUE(is_strictly_sorted(s));
515 for (
int budget = 0; not
completed; ++budget)
517 ASSERT_LT(budget, 64);
518 SCOPED_TRACE(testing::Message() <<
"moves allowed " << budget);
520 for (
int k = 1;
k <= 4; ++
k)
521 s.
insert(Throwing_Move_Key(
k));
524 Throwing_Move_Key::moves_left = budget;
527 s.
insert(Throwing_Move_Key(0));
530 catch (
const std::runtime_error &)
534 Throwing_Move_Key::moves_left = -1;
536 EXPECT_TRUE(is_strictly_sorted(s));
539 EXPECT_EQ(s.
size(), 5u);
540 EXPECT_GT(budget, 4);
542 s.
insert(Throwing_Move_Key(100));
543 EXPECT_TRUE(s.
contains(Throwing_Move_Key(100)));
544 EXPECT_TRUE(is_strictly_sorted(s));
bool operator<(const Time &l, const Time &r)
size_t size_t int32_t value
Ordered set stored as a sorted contiguous array.
size_t capacity() const noexcept
Return the capacity of the backing array. O(1).
const Key & min() const
Smallest key (checked). Alias of get_first().
const_iterator upper_bound(const Key &k) const
First element greater than k.
size_t erase(const Key &k)
Remove the key equivalent to k, if present.
const_iterator end() const noexcept
Iterator past the greatest key. O(1).
const_iterator find(const Key &k) const
Find a key.
void empty() noexcept
Remove all keys (Aleph convention).
size_t count(const Key &k) const
Count occurrences of a key (0 or 1).
std::pair< const_iterator, const_iterator > equal_range(const Key &k) const
Range of elements equivalent to k.
void clear() noexcept
Remove all keys. Alias of empty(). Capacity is kept.
const Key & max() const
Greatest key (checked). Alias of get_last().
bool traverse(Operation operation) const
Traverse keys in sorted order while operation returns true.
const Key * data() const noexcept
Pointer to the underlying sorted, contiguous storage. O(1).
void reserve(size_t cap)
Reserve capacity for at least cap keys.
const_iterator lower_bound(const Key &k) const
First element not less than k.
bool contains(const Key &k) const
Test membership.
size_t size() const noexcept
Return the number of stored keys. O(1).
bool is_empty() const noexcept
Return true if the set holds no keys. O(1).
const Key & get_first() const
Smallest key (checked).
const Key & nth(size_t i) const
Positional access to the i-th smallest key (checked).
std::pair< const_iterator, bool > insert(const Key &k)
Insert a copy of k if no equivalent key exists.
const Key & get_last() const
Greatest key (checked).
void swap(FlatSet &s) noexcept(std::is_nothrow_swappable_v< Compare >)
Swap contents with s in O(1).
const_iterator begin() const noexcept
Iterator to the smallest key. O(1).
bool completed() const noexcept
Return true if all underlying iterators are finished.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
and
Check uniqueness with explicit hash + equality functors.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Sorted-array set (Aleph::FlatSet), a cache-friendly ordered set.