50#include <gtest/gtest.h>
62 static inline int moves_left = -1;
66 Throwing_Move() =
default;
67 Throwing_Move(
const int v) :
value(v) {}
68 Throwing_Move(
const Throwing_Move &) =
default;
69 Throwing_Move(Throwing_Move &&) =
default;
70 Throwing_Move &operator = (
const Throwing_Move &) =
default;
71 Throwing_Move &operator = (Throwing_Move &&other)
74 throw std::runtime_error(
"move assignment");
85 static inline bool fail =
false;
89 Throwing_Copy() =
default;
90 Throwing_Copy(
const int v) :
value(v) {}
91 Throwing_Copy(
const Throwing_Copy &other) :
value(other.
value)
94 throw std::runtime_error(
"copy");
96 Throwing_Copy(Throwing_Copy &&) noexcept = default;
97 Throwing_Copy &operator = (const Throwing_Copy &other)
100 throw std::runtime_error(
"copy assignment");
104 Throwing_Copy &operator = (Throwing_Copy &&) noexcept = default;
110template <
bool Nothrow_Move>
111struct Failing_Default
113 static inline bool fail =
false;
120 throw std::bad_alloc();
122 Failing_Default(
const int v) :
value(v) {}
123 Failing_Default(
const Failing_Default &) =
default;
124 Failing_Default(Failing_Default &&other)
noexcept(Nothrow_Move) :
value(other.
value) {}
125 Failing_Default &operator = (
const Failing_Default &) =
default;
126 Failing_Default &operator = (Failing_Default &&other)
noexcept(Nothrow_Move)
136::testing::AssertionResult is_consistent(
const Map &
m)
138 for (
size_t i = 0; i <
m.
size(); ++i)
140 if (i > 0
and not (
m.nth_key(i - 1) <
m.nth_key(i)))
141 return ::testing::AssertionFailure()
142 <<
"keys " <<
m.nth_key(i - 1) <<
" and " <<
m.nth_key(i)
143 <<
" at " << i - 1 <<
" are not strictly increasing";
144 if (
m.nth_value(i).value != 10 *
m.nth_key(i))
145 return ::testing::AssertionFailure()
146 <<
"key " <<
m.nth_key(i) <<
" holds " <<
m.nth_value(i).value;
148 return ::testing::AssertionSuccess();
151class Pair_Input_Iterator
153 const std::vector<std::pair<int, std::string>> *items_ =
nullptr;
158 using iterator_category = std::input_iterator_tag;
159 using value_type = std::pair<int, std::string>;
160 using difference_type = std::ptrdiff_t;
161 using pointer = void;
162 using reference =
const value_type &;
164 Pair_Input_Iterator() =
default;
166 Pair_Input_Iterator(
const std::vector<std::pair<int, std::string>> &items,
168 : items_(&items), pos_(pos) {}
170 reference
operator*() const noexcept {
return (*items_)[pos_]; }
172 Pair_Input_Iterator &operator++() noexcept
178 Pair_Input_Iterator operator++(
int)
noexcept
180 Pair_Input_Iterator ret = *
this;
185 bool operator==(
const Pair_Input_Iterator &it)
const noexcept
187 return items_ == it.items_
and pos_ == it.pos_;
190 bool operator!=(
const Pair_Input_Iterator &it)
const noexcept
192 return not (*
this == it);
201 EXPECT_EQ(
m.
size(), 0u);
213 ASSERT_EQ(
m.
size(), 3u);
216 EXPECT_EQ(
m.at(
"a"), 11);
219 EXPECT_EQ(
m[
"zz"], 0);
220 EXPECT_EQ(
m.
size(), 4u);
223 EXPECT_EQ(
m.nth_key(0),
"a");
224 EXPECT_EQ(
m.nth_key(1),
"b");
225 EXPECT_EQ(
m.nth_key(2),
"c");
226 EXPECT_EQ(
m.nth_key(3),
"zz");
232 EXPECT_EQ(
m.at(
"a"), 1);
233 EXPECT_THROW((
void)
m.at(
"nope"), std::out_of_range);
236 EXPECT_EQ(cm.
at(
"a"), 1);
237 EXPECT_THROW((
void) cm.
at(
"nope"), std::out_of_range);
243 {3,
"THREE"}, {2,
"two"}};
244 ASSERT_EQ(
m.
size(), 3u);
245 EXPECT_EQ(
m.at(3),
"three");
246 EXPECT_EQ(
m.nth_key(0), 1);
247 EXPECT_EQ(
m.nth_key(2), 3);
252 const std::vector<std::pair<int, std::string>> items =
253 {{3,
"three"}, {1,
"one"}, {2,
"two"}};
256 Pair_Input_Iterator(items, items.size()));
258 ASSERT_EQ(
m.
size(), 3u);
259 EXPECT_EQ(
m.at(1),
"one");
260 EXPECT_EQ(
m.at(2),
"two");
261 EXPECT_EQ(
m.at(3),
"three");
276 EXPECT_EQ(from_it, src);
277 EXPECT_EQ(from_cit, src);
278 ASSERT_EQ(from_tail.
size(), 2u);
279 EXPECT_FALSE(from_tail.
contains(1));
280 EXPECT_EQ(from_tail.
at(3),
"three");
286 EXPECT_TRUE(
m.
insert(1, 100).second);
287 EXPECT_FALSE(
m.
insert(1, 999).second);
288 EXPECT_EQ(
m.at(1), 100);
290 EXPECT_FALSE(
m.insert_or_assign(1, 999).second);
291 EXPECT_EQ(
m.at(1), 999);
293 EXPECT_TRUE(
m.insert_or_assign(2, 200).second);
294 EXPECT_EQ(
m.at(2), 200);
297 EXPECT_TRUE(
m.
insert(std::pair<int, int>(3, 300)).second);
298 EXPECT_EQ(
m.at(3), 300);
305 EXPECT_EQ(
m.erase(2), 1u);
306 EXPECT_EQ(
m.erase(2), 0u);
307 EXPECT_EQ(
m.
size(), 3u);
310 ASSERT_NE(it,
m.
end());
312 EXPECT_EQ((*it).first, 4);
313 EXPECT_EQ(
m.
size(), 2u);
315 EXPECT_THROW((
void)
m.erase(
m.
end()), std::out_of_range);
322 std::vector<std::string> ks;
324 for (
auto [
k, v] :
m)
329 EXPECT_EQ(ks, (std::vector<std::string>{
"a",
"b",
"c"}));
333 for (
auto [
k, v] :
m)
335 EXPECT_EQ(
m.at(
"a"), 10);
336 EXPECT_EQ(
m.at(
"b"), 20);
337 EXPECT_EQ(
m.at(
"c"), 30);
340 auto it =
m.
find(
"b");
341 ASSERT_NE(it,
m.
end());
342 EXPECT_EQ(it->first,
"b");
344 EXPECT_EQ(
m.at(
"b"), 7);
352 EXPECT_EQ(
m.
end() - it, 4);
353 EXPECT_EQ((it + 2)->first, 3);
354 EXPECT_EQ(it[3].second, 40);
357 EXPECT_EQ(it->first, 1);
358 EXPECT_TRUE(it <
m.
end());
362 EXPECT_EQ(cit->second, 10);
369 EXPECT_EQ(
m.lower_bound(20)->first, 20);
370 EXPECT_EQ(
m.lower_bound(25)->first, 30);
371 EXPECT_EQ(
m.upper_bound(20)->first, 30);
372 EXPECT_EQ(
m.lower_bound(31),
m.
end());
374 auto [lo, hi] =
m.equal_range(20);
375 EXPECT_EQ(hi - lo, 1);
376 auto [lo2, hi2] =
m.equal_range(15);
385 auto vs =
m.values();
386 ASSERT_EQ(ks.size(), 3u);
387 ASSERT_EQ(vs.size(), 3u);
388 EXPECT_EQ(ks.get_first(),
"a");
389 EXPECT_EQ(ks.get_last(),
"c");
390 EXPECT_EQ(vs.get_first(), 1);
391 EXPECT_EQ(vs.get_last(), 3);
398 EXPECT_TRUE(
m.
traverse([] (
const int &,
int &v) { v += 100; return true; }));
399 EXPECT_EQ(
m.at(1), 101);
403 EXPECT_FALSE(cm.
traverse([&visited] (
const int &
k,
const int &)
408 EXPECT_EQ(visited, 2);
425 EXPECT_EQ(
m.
size(), 1u);
426 EXPECT_EQ(
m.at(
"q"), 9);
427 EXPECT_EQ(other.
size(), 2u);
433 = {{1,
"one"}, {5,
"five"}, {3,
"three"}};
434 EXPECT_EQ(
m.nth_key(0), 5);
435 EXPECT_EQ(
m.nth_key(1), 3);
436 EXPECT_EQ(
m.nth_key(2), 1);
437 EXPECT_EQ(
m.at(3),
"three");
443 const int *kp =
m.keys_data();
444 const int *vp =
m.values_data();
445 for (
size_t i = 0; i <
m.
size(); ++i)
446 EXPECT_EQ(vp[i], kp[i] * 10);
453 std::mt19937
rng(20260702);
454 std::uniform_int_distribution<int> key_dist(0, 150);
455 std::uniform_int_distribution<int> val_dist(0, 1000000);
456 std::uniform_int_distribution<int> op_dist(0, 3);
459 std::map<int, int> ref;
461 for (
int step = 0; step < 4000; ++step)
463 const int k = key_dist(
rng);
464 const int v = val_dist(
rng);
465 switch (op_dist(
rng))
472 EXPECT_EQ(fm.
insert(
k, v).second, ref.insert({k, v}).second);
475 EXPECT_EQ(fm.
erase(
k), ref.erase(
k));
479 const auto it = ref.find(
k);
483 EXPECT_EQ(fm.
at(
k), it->second);
489 ASSERT_EQ(fm.
size(), ref.size());
491 for (
const auto &[
k, v] : ref)
502 const std::string thirty(40,
'c');
503 m.
insert(1, std::string(40,
'a'));
505 m.
insert(5, std::string(40,
'e'));
509 EXPECT_TRUE(
m.
insert(0,
m.at(3)).second);
510 EXPECT_EQ(
m.at(0), thirty);
511 EXPECT_EQ(
m.at(3), thirty);
518 EXPECT_EQ(
m.at(
next), thirty);
522 EXPECT_TRUE(
m.insert_or_assign(-1,
m.at(3)).second);
523 EXPECT_EQ(
m.at(-1), thirty);
527 EXPECT_TRUE(
m.
insert(-2, std::move(
m.at(0))).second);
528 EXPECT_EQ(
m.at(-2), thirty);
539 EXPECT_TRUE(
m.
insert(
m.at(1), 7).second);
540 EXPECT_EQ(
m.at(10), 7);
543 EXPECT_EQ(
m.at(30), 8);
549 EXPECT_EQ(
m.at(50), 9);
550 EXPECT_EQ(
m.at(5), 50);
556 for (
const int k : {1, 3, 5, 7})
558 const Throwing_Copy
value(0);
560 Throwing_Copy::fail =
true;
563 EXPECT_THROW(
m.insert_or_assign(9,
value), std::runtime_error);
564 Throwing_Copy::fail =
false;
566 ASSERT_EQ(
m.
size(), 4u);
567 EXPECT_TRUE(is_consistent(
m));
576 for (
const int k : {2, 4, 6, 8, 10})
577 source.insert(
k, Throwing_Copy(10 *
k));
579 target.
insert(1, Throwing_Copy(10));
581 Throwing_Copy::fail =
true;
582 EXPECT_THROW(target = source, std::runtime_error);
583 Throwing_Copy::fail =
false;
585 ASSERT_EQ(target.
size(), 1u);
586 EXPECT_EQ(target.
nth_key(0), 1);
587 EXPECT_EQ(target.
nth_value(0).value, 10);
590 ASSERT_EQ(target.
size(), 5u);
591 EXPECT_TRUE(is_consistent(target));
600 const auto build = []
603 for (
const int k : {1, 3, 5, 7, 9})
608 enum class Operation { insert_first, insert_middle, erase_first, reserve };
609 for (
const auto operation : {Operation::insert_first, Operation::insert_middle,
610 Operation::erase_first, Operation::reserve})
611 for (
int budget = 0; budget < 12; ++budget)
613 SCOPED_TRACE(testing::Message() <<
"operation " <<
static_cast<int>(operation)
614 <<
", moves allowed " << budget);
617 Throwing_Move::moves_left = budget;
622 case Operation::insert_first:
m.
insert(0, Throwing_Move(0));
break;
623 case Operation::insert_middle:
m.
insert(4, Throwing_Move(40));
break;
624 case Operation::erase_first:
m.erase(1);
break;
625 case Operation::reserve:
m.reserve(4 *
m.
capacity());
break;
628 catch (
const std::runtime_error &)
632 Throwing_Move::moves_left = -1;
634 EXPECT_TRUE(is_consistent(
m));
637 else if (operation == Operation::erase_first)
638 EXPECT_EQ(
m.
size(), 4u);
639 else if (operation == Operation::reserve)
640 EXPECT_EQ(
m.
size(), 5u);
642 EXPECT_EQ(
m.
size(), 6u);
645 m.
insert(100, Throwing_Move(1000));
647 EXPECT_TRUE(is_consistent(
m));
652 Throwing_Move::moves_left = 11;
653 EXPECT_NO_THROW(
m.
insert(0, Throwing_Move(0)));
654 Throwing_Move::moves_left = -1;
662 using Value = Failing_Default<false>;
663 ASSERT_FALSE(std::is_nothrow_move_assignable_v<Value>);
666 for (
int k = 0;
k < 4; ++
k)
671 EXPECT_THROW(
m.reserve(std::numeric_limits<size_t>::max()), std::overflow_error);
672 EXPECT_EQ(
m.
size(), 4u);
673 EXPECT_TRUE(is_consistent(
m));
677 EXPECT_THROW(
m.reserve(64), std::bad_alloc);
679 EXPECT_THROW(
m.insert_or_assign(9,
Value(90)), std::bad_alloc);
681 EXPECT_EQ(
m.
size(), 4u);
682 EXPECT_TRUE(is_consistent(
m));
687 EXPECT_EQ(
m.
size(), 5u);
689 EXPECT_TRUE(is_consistent(
m));
698 for (
int budget = 0; not
completed; ++budget)
700 ASSERT_LT(budget, 64);
701 SCOPED_TRACE(testing::Message() <<
"moves allowed " << budget);
703 for (
int k = 1;
k <= 4; ++
k)
707 Throwing_Move::moves_left = budget;
713 catch (
const std::runtime_error &)
717 Throwing_Move::moves_left = -1;
719 EXPECT_TRUE(is_consistent(
m));
722 EXPECT_EQ(
m.
size(), 5u);
723 EXPECT_GT(budget, 4);
725 m.
insert(100, Throwing_Move(1000));
727 EXPECT_TRUE(is_consistent(
m));
735 for (
const bool nothrow_move : {
true,
false})
737 SCOPED_TRACE(testing::Message() <<
"nothrow move " << nothrow_move);
741 source.insert(1,
Value(10));
743 ASSERT_EQ(source.capacity(), 0u);
746 EXPECT_THROW(source.insert(2,
Value(20)), std::bad_alloc);
748 ASSERT_TRUE(source.is_empty());
749 EXPECT_TRUE(source.begin() == source.end());
751 source.insert(2,
Value(20));
752 EXPECT_EQ(source.size(), 1u);
753 EXPECT_TRUE(is_consistent(source));
754 EXPECT_EQ(taken.
size(), 1u);
755 EXPECT_TRUE(is_consistent(taken));
758 check(Failing_Default<true>(0));
760 check(Failing_Default<false>(0));
bool operator==(const Time &l, const Time &r)
size_t size_t int32_t value
Random-access proxy iterator over (key, value) entries.
Ordered map stored as two parallel sorted contiguous arrays.
const Key & nth_key(size_t i) const
Key of the i-th entry in sorted order (checked).
std::pair< iterator, bool > insert(const Key &k, const T &v)
Insert (k, v) if k is not mapped.
T & nth_value(size_t i)
Value of the i-th entry in sorted order (checked).
bool contains(const Key &k) const
Test whether key k is mapped.
size_t erase(const Key &k)
Remove the entry with key k, if present.
T & at(const Key &k)
Checked access to the value mapped to k.
bool traverse(Operation operation)
Traverse entries in key order while operation returns true.
iterator begin() noexcept
Iterator to the entry with the smallest key. O(1).
size_t size() const noexcept
Return the number of stored entries. O(1).
iterator end() noexcept
Iterator past the entry with the greatest key. O(1).
void swap(ODhashTable &other) noexcept
Swaps this table with other in constant time.
constexpr bool contains(const Key &key) const noexcept
Alias for has().
constexpr size_t size() const noexcept
Returns the number of entries in the table.
constexpr size_t capacity() const noexcept
Returns the current capacity of the table.
constexpr bool is_empty() const noexcept
Checks if the table is empty.
DynList< Key > keys() const
Returns a list containing all keys in the table.
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Key & find(const Key &key)
Finds a key and returns a reference to it.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
bool operator!=(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
bool completed() const noexcept
Return true if all underlying iterators are finished.
@ Value
Regular runtime payload stored as Interpreter_Value.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
and
Check uniqueness with explicit hash + equality functors.
Matrix< Trow, Tcol, NumType > operator*(const NumType &scalar, const Matrix< Trow, Tcol, NumType > &m)
Scalar-matrix multiplication (scalar * matrix).
void next()
Advance all underlying iterators (bounds-checked).
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation, typename Aleph::concepts_detail::traverse_arg< Container, Operation >::type, Container & >())
Traverse the container via its iterator and performs a conditioned operation on each item.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Sorted-array map (Aleph::FlatMap), a cache-friendly ordered map.