53# include <type_traits>
56# include <gtest/gtest.h>
57# include <gtest/gtest-spi.h>
76 struct Injected_Failure :
public std::runtime_error
78 using std::runtime_error::runtime_error;
85 static inline long countdown = -1;
86 static inline long fired = 0;
87 static inline long done = 0;
88 static inline long defaults = 0;
89 static inline long alive = 0;
91 static void hit(
const char * operation)
97 throw Injected_Failure(operation);
107 explicit Failing_From(
const long k)
noexcept
109 Failures::countdown =
k;
114 ~Failing_From() { Failures::countdown = -1; }
116 Failing_From(
const Failing_From &) =
delete;
117 Failing_From & operator = (
const Failing_From &) =
delete;
126 Copy_Fails() noexcept { ++Failures::alive; }
128 Copy_Fails(
const long v) noexcept :
value(v) { ++Failures::alive; }
130 Copy_Fails(
const Copy_Fails & item) :
value(item.
value)
132 Failures::hit(
"copy");
136 Copy_Fails & operator = (
const Copy_Fails & item)
138 Failures::hit(
"copy assignment");
143 ~Copy_Fails() { --Failures::alive; }
145 bool operator < (
const Copy_Fails & item)
const noexcept {
return value < item.value; }
147 bool operator == (
const Copy_Fails & item)
const noexcept {
return value == item.value; }
155 Move_Fails() noexcept { ++Failures::alive; }
157 Move_Fails(
const long v) noexcept :
value(v) { ++Failures::alive; }
159 Move_Fails(
const Move_Fails & item) noexcept :
value(item.value) { ++Failures::alive; }
161 Move_Fails(Move_Fails && item) :
value(item.
value)
163 Failures::hit(
"move");
167 Move_Fails & operator = (
const Move_Fails & item)
noexcept
173 Move_Fails & operator = (Move_Fails && item)
175 Failures::hit(
"move assignment");
180 ~Move_Fails() { --Failures::alive; }
182 bool operator < (
const Move_Fails & item)
const noexcept {
return value < item.value; }
184 bool operator == (
const Move_Fails & item)
const noexcept {
return value == item.value; }
194 ++Failures::defaults;
195 Failures::hit(
"default construction");
199 Default_Fails(
const long v) noexcept :
value(v) { ++Failures::alive; }
201 Default_Fails(
const Default_Fails & item) noexcept :
value(item.value) { ++Failures::alive; }
203 Default_Fails & operator = (
const Default_Fails & item)
noexcept =
default;
205 ~Default_Fails() { --Failures::alive; }
207 bool operator < (
const Default_Fails & item)
const noexcept {
return value < item.value; }
209 bool operator == (
const Default_Fails & item)
const noexcept {
return value == item.value; }
212 long value_of(
const long v)
noexcept {
return v; }
214 long value_of(
const Copy_Fails & item)
noexcept {
return item.value; }
216 size_t hash_of(
const Copy_Fails & item)
noexcept
218 return static_cast<size_t>(item.value) * 2654435761u;
222 constexpr long Max_Failure_Point = 100000;
231 template <
class Fixture,
class Op,
class Val
id>
232 void try_every_failure(
const std::string & what,
const bool lets_through,
235 for (
long k = 0;
k < Max_Failure_Point; ++
k)
237 SCOPED_TRACE(what +
", failing from the operation " + std::to_string(
k));
238 const long alive = Failures::alive;
244 Failing_From failing(
k);
249 catch (
const Injected_Failure &)
253 fired = Failures::fired;
257 EXPECT_EQ(threw, fired > 0) <<
"a failure did not reach the caller";
259 EXPECT_FALSE(threw) <<
"a failure reached the caller";
260 valid(fixture, threw);
262 EXPECT_EQ(Failures::alive, alive) <<
"items leaked or destroyed twice";
266 ADD_FAILURE() << what <<
" made no operation that can fail: nothing was tested";
270 ADD_FAILURE() << what <<
" made more than " << Max_Failure_Point <<
" operations";
277 TEST(Failure_Harness, AnOperationThatCannotFailFailsTheTest)
280 EXPECT_NONFATAL_FAILURE(try_every_failure<Nothing>(
"doing nothing",
true,
282 [] (Nothing &, bool) {}),
283 "nothing was tested");
291 template <
template <
class>
class N,
class C>
297 using Control_Data = C;
301 template <
class Node,
class Key>
305 Node * node =
nullptr;
307 Node_Fixture() =
default;
308 Node_Fixture(
const Node_Fixture &) =
delete;
309 Node_Fixture & operator = (
const Node_Fixture &) =
delete;
310 ~Node_Fixture() {
delete node; }
313 template <
class Kind>
314 struct Node_Construction :
public ::testing::Test
319 static void check_from_rvalue(
const std::string & what)
322 using Fixture = Node_Fixture<Node, Key>;
323 if constexpr (std::is_nothrow_constructible_v<Node, Key &&>)
324 ADD_FAILURE() << what <<
" is noexcept, but the key may throw";
326 try_every_failure<Fixture>(what,
true,
327 [] (Fixture & f) { f.node =
new Node(std::move(f.key)); },
328 [] (Fixture & f,
const bool threw)
330 EXPECT_EQ(f.key.value, 7);
331 EXPECT_EQ(f.node ==
nullptr, threw);
332 if (f.node !=
nullptr)
334 EXPECT_EQ(
KEY(f.node).value, 7);
340 using Node_Kinds = ::testing::Types<Node_Kind<BinNode, Empty_Node>,
341 Node_Kind<BinNodeVtl, Empty_Node>,
342 Node_Kind<AvlNode, AvlNode_Data>,
343 Node_Kind<AvlNodeVtl, AvlNode_Data>,
344 Node_Kind<AvlNodeRk, AvlNodeRk_Data>,
345 Node_Kind<RbNode, RbNode_Data>,
346 Node_Kind<RbNodeVtl, RbNode_Data>,
347 Node_Kind<RbNodeRk, RbNodeRk_Data>,
348 Node_Kind<TreapNode, TreapNode_Data>,
349 Node_Kind<Treap_Rk_Node, TreapRkNode_Data>,
350 Node_Kind<BinNodeXt, BinNodeXt_Data>,
351 Node_Kind<RandNode, BinNodeXt_Data>,
352 Node_Kind<BinHeapNode, BinHeapNode_Data>>;
355 TYPED_TEST(Node_Construction, FromAnRvalueLetsTheFailureThrough)
357 this->
template check_from_rvalue<Copy_Fails>(
"Node(Key &&) with a key that is copied");
358 this->
template check_from_rvalue<Move_Fails>(
"Node(Key &&) with a key that is moved");
362 TYPED_TEST(Node_Construction, FromControlDataLetsTheFailureThrough)
365 using Control_Data =
typename TypeParam::Control_Data;
366 using Fixture = Node_Fixture<Node, long>;
367 if constexpr (std::is_nothrow_constructible_v<Node, const Control_Data &>)
368 ADD_FAILURE() <<
"Node(const Control_Data &) is noexcept, but the key may throw";
370 try_every_failure<Fixture>(
"Node(const Control_Data &)",
true,
371 [] (Fixture & f) { f.node =
new Node(Control_Data()); },
372 [] (Fixture & f,
const bool threw)
374 EXPECT_EQ(f.node ==
nullptr, threw);
380 static_assert(std::is_nothrow_constructible_v<AvlNode<int>,
int &&>);
381 static_assert(std::is_nothrow_constructible_v<RbNode<std::string>, std::string &&>);
382 static_assert(std::is_nothrow_constructible_v<BinNode<std::string>,
const Empty_Node &>);
397 for (
long v = 1; v <= 3; ++v)
406 Dnode_List(
const Dnode_List &) =
delete;
407 Dnode_List & operator = (
const Dnode_List &) =
delete;
413 if (moved !=
nullptr)
422 values.
append(p->get_data().value);
423 values_ok = values.
size() == 3
and values(0) == 1
and values(1) == 2
and values(2) == 3;
428 TEST(Dnode_Construction, ByDefaultLetsTheFailureThrough)
431 using Fixture = Node_Fixture<Node, long>;
432 if constexpr (std::is_nothrow_default_constructible_v<Node>)
433 ADD_FAILURE() <<
"Dnode() is noexcept, but the item may throw";
435 try_every_failure<Fixture>(
"Dnode()",
true,
436 [] (Fixture & f) { f.node =
new Node; },
437 [] (Fixture & f,
const bool threw)
439 EXPECT_EQ(f.node ==
nullptr, threw);
445 TEST(Dnode_Construction, ByMoveLetsTheFailureThroughAndKeepsTheList)
447 if constexpr (std::is_nothrow_move_constructible_v<Dnode<Move_Fails>>)
448 ADD_FAILURE() <<
"Dnode(Dnode &&) is noexcept, but the item may throw";
450 try_every_failure<Dnode_List>(
"Dnode(Dnode &&)",
true,
455 [] (Dnode_List &
l,
const bool threw)
457 bool values_ok =
false;
459 EXPECT_TRUE(values_ok);
460 EXPECT_EQ(
l.moved ==
nullptr, threw);
461 EXPECT_EQ(second, threw ?
l.middle :
l.moved);
469 static_assert(std::is_nothrow_default_constructible_v<Dnode<std::string>>);
470 static_assert(std::is_nothrow_move_constructible_v<Dnode<std::string>>);
471 static_assert(std::is_nothrow_move_constructible_v<Dnode<int>>);
483 for (
long v = 1; v <= 3; ++v)
484 c.
append(
typename C::Item_Type(v));
488 template <
class Tuple,
size_t...
I>
489 bool items_are(
const Tuple & t,
const long v, std::index_sequence<I...>)
491 return (...
and (value_of(std::get<I>(t)) == v));
495 template <
bool Enumerated,
class... Cs>
503 std::tuple<Cs...> containers{one_two_three<Cs>()...};
504 Iterator it = std::apply([] (
const Cs & ... c) {
return Iterator(c...); }, containers);
506 Fixture() { it.next_ne(); }
510 static void on_the_second_items(
const Iterator & it)
512 ASSERT_TRUE(it.has_curr());
513 const auto t = it.get_curr_ne();
514 EXPECT_TRUE(items_are(t, 2, std::make_index_sequence<
sizeof...(Cs)>()));
515 if constexpr (Enumerated)
517 EXPECT_EQ(std::get<
sizeof...(Cs)>(t), 1u);
523 struct Zip_Current :
public ::testing::Test {};
525 using Zips = ::testing::Types<Zip_Of<false, DynList<Copy_Fails>>,
530 Zip_Of<true, DynList<Copy_Fails>>,
537 TYPED_TEST(Zip_Current, LetsTheFailureThroughAndKeepsThePosition)
539 using Iterator =
typename TypeParam::Iterator;
540 using Fixture =
typename TypeParam::Fixture;
541 if constexpr (
noexcept(std::declval<const Iterator &>().get_curr_ne()))
542 ADD_FAILURE() <<
"get_curr_ne() is noexcept, but copying an item may throw";
544 try_every_failure<Fixture>(
"get_curr_ne()",
true,
545 [] (Fixture & f) { (void) f.it.get_curr_ne(); },
546 [] (Fixture & f, bool)
548 TypeParam::on_the_second_items(f.it);
552 static_assert(
noexcept(std::declval<const ZipIterator<DynList<int>,
DynList<long>> &>()
554 static_assert(
noexcept(std::declval<const ZipIterator<DynList<int>> &>().
get_curr_ne()));
555 static_assert(
noexcept(std::declval<const EnumZipIterator<DynList<int>,
DynList<double>> &>()
560 static_assert(not
noexcept(std::declval<const ZipIterator<DynList<int>,
DynArray<long>> &>()
562 static_assert(not
noexcept(std::declval<const EnumZipIterator<DynArray<int>> &>()
573 constexpr long Keys = 300;
581 for (
long k = 0;
k < Keys; ++
k)
585 Grown_Table(
const Grown_Table &) =
delete;
586 Grown_Table & operator = (
const Grown_Table &) =
delete;
588 ~Grown_Table() {
delete table; }
591 void empty_and_usable()
593 ASSERT_NE(table,
nullptr);
595 EXPECT_EQ(table->size(), 0u);
596 for (
long k = 0;
k < Keys; ++
k)
597 EXPECT_FALSE(table->
contains(Copy_Fails(
k))) <<
"key " <<
k;
598 for (
long k = 0;
k < Keys; ++
k)
599 EXPECT_NE(table->
insert(Copy_Fails(
k)),
nullptr) <<
"key " <<
k;
600 EXPECT_EQ(table->size(),
static_cast<size_t>(Keys));
601 for (
long k = 0;
k < Keys; ++
k)
602 EXPECT_TRUE(table->
contains(Copy_Fails(
k))) <<
"key " <<
k;
606 TEST(Linear_Hash_Table, GrowsInTheseTests)
609 EXPECT_GT(t.table->capacity(), 101u);
615 template <
class Op,
class Val
id>
616 void expect_no_operation_of_a_key(
const char * what, Op op, Valid valid)
619 const long alive = Failures::alive;
625 Failing_From failing(0);
630 catch (
const Injected_Failure &)
634 done = Failures::done;
636 EXPECT_FALSE(threw) <<
"a failure reached the caller";
637 EXPECT_EQ(done, 0) <<
"it copied a key";
640 EXPECT_EQ(Failures::alive, alive) <<
"keys leaked or destroyed twice";
646 TEST(Linear_Hash_Table, ItBuildsNoKeyForItsLists)
648 const long alive = Failures::alive;
651 EXPECT_EQ(Failures::alive, alive) <<
"an empty table holds keys";
652 for (
long k = 0;
k < Keys; ++
k)
654 EXPECT_GT(table.capacity(), 101u);
655 EXPECT_EQ(Failures::alive, alive + Keys) <<
"the lists hold keys of their own";
657 EXPECT_EQ(Failures::alive, alive);
659 EXPECT_EQ(Failures::alive, alive);
662 TEST(Linear_Hash_Table, EmptyCopiesNoKey)
664 expect_no_operation_of_a_key(
"empty()",
665 [] (Grown_Table & t) { t.table->empty(); },
666 [] (Grown_Table & t) { t.empty_and_usable(); });
669 TEST(Linear_Hash_Table, TheDestructorCopiesNoKey)
671 expect_no_operation_of_a_key(
"~DynSetLinHash()",
677 [] (Grown_Table & t) { EXPECT_EQ(t.table,
nullptr); });
681 TEST(Linear_Hash_Table, RemoveCopiesNoKey)
683 expect_no_operation_of_a_key(
"remove()",
686 for (
long k = Keys - 1;
k >= 0; --
k)
687 t.table->remove(t.table->search(Copy_Fails(
k)));
691 EXPECT_EQ(t.table->capacity(), 101u);
692 t.empty_and_usable();
699 TEST(Linear_Hash_Table, ItsMovesCopyNoKey)
701 static_assert(std::is_nothrow_move_constructible_v<DynSetLinHash<Copy_Fails>>);
702 static_assert(std::is_nothrow_move_assignable_v<DynSetLinHash<Copy_Fails>>);
703 expect_no_operation_of_a_key(
"the move constructor",
707 EXPECT_EQ(taken.size(),
static_cast<size_t>(Keys));
708 EXPECT_TRUE(t.table->is_empty());
711 [] (Grown_Table & t) { t.empty_and_usable(); });
712 expect_no_operation_of_a_key(
"the move assignment",
716 taken = std::move(*t.table);
717 EXPECT_EQ(taken.size(),
static_cast<size_t>(Keys));
718 EXPECT_TRUE(t.table->is_empty());
720 [] (Grown_Table & t) { t.empty_and_usable(); });
725 TEST(Linear_Hash_Map, SearchCopiesNothing)
728 for (
long k = 0;
k < Keys;
k += 2)
729 (
void) map.insert(Copy_Fails(
k), Default_Fails(
k));
730 const auto & const_map = map;
732 for (
long k = -1;
k <= Keys; ++
k)
734 const Copy_Fails key(
k);
735 const bool in =
k >= 0
and k < Keys
and k % 2 == 0;
737 auto * p = map.search(key);
738 const auto * q = const_map.search(key);
739 const bool has = map.has(key);
740 const bool contains = map.contains(key);
741 EXPECT_EQ(Failures::done, 0) <<
"searching for " <<
k <<
" copied a key or built a Data";
742 EXPECT_EQ(p !=
nullptr, in) <<
"key " <<
k;
743 EXPECT_EQ(q, p) <<
"key " <<
k;
744 EXPECT_EQ(has, in) <<
"key " <<
k;
748 EXPECT_EQ(p->second.value,
k);
765 size_t other_hash_of(
const Copy_Fails & item)
noexcept
767 return static_cast<size_t>(item.value) * 40503u + 17;
772 size_t pair_hash_of(
const std::pair<Copy_Fails, Default_Fails> & p)
noexcept
774 return other_hash_of(p.first);
777 size_t second_hash_of(
const Copy_Fails & item)
noexcept
779 return static_cast<size_t>(item.value) * 3 + 11;
786 Map map{101, hash_of};
790 map.set_hash_fct(pair_hash_of);
791 for (
long k = 0;
k < Keys;
k += 2)
792 (
void) map.insert(Copy_Fails(
k), Default_Fails(
k));
795 void holds_its_pairs()
797 for (
long k = -1;
k <= Keys; ++
k)
799 const Copy_Fails key(
k);
800 const auto * p = map.search(key);
801 EXPECT_EQ(p !=
nullptr,
k >= 0
and k < Keys
and k % 2 == 0) <<
"key " <<
k;
803 EXPECT_EQ(p->second.value,
k);
809 void search_by_pairs_lets_the_failure_through(
const std::string & name)
811 static_assert(not
noexcept(std::declval<Map &>().search(std::declval<const Copy_Fails &>())));
812 for (
const long key : {4L, 5L})
813 try_every_failure<Map_By_Pairs<Map>>(name +
"::search() by pairs",
true,
814 [key] (Map_By_Pairs<Map> & t)
816 const Copy_Fails
k(key);
817 (void) t.map.search(
k);
819 [] (Map_By_Pairs<Map> & t, bool) { t.holds_its_pairs(); });
822 TEST(Hash_Map_By_Pairs, SearchLetsTheFailureThrough)
824 search_by_pairs_lets_the_failure_through<DynMapLinHash<Copy_Fails, Default_Fails>>(
"DynMapLinHash");
825 search_by_pairs_lets_the_failure_through<DynMapHash<Copy_Fails, Default_Fails>>(
"DynMapHash");
829 void search_by_keys_copies_nothing()
831 Map map(101, hash_of);
832 map.set_hash_fct(pair_hash_of);
833 map.set_hash_fct(other_hash_of);
834 for (
long k = 0;
k < Keys;
k += 2)
835 (
void) map.insert(Copy_Fails(
k), Default_Fails(
k));
837 for (
long k = -1;
k <= Keys; ++
k)
839 const Copy_Fails key(
k);
841 const auto * p = map.search(key);
842 const bool has = map.has(key);
843 EXPECT_EQ(Failures::done, 0) <<
"searching for " <<
k <<
" copied a key or built a Data";
844 EXPECT_EQ(p !=
nullptr,
k >= 0
and k < Keys
and k % 2 == 0) <<
"key " <<
k;
845 EXPECT_EQ(has, p !=
nullptr) <<
"key " <<
k;
849 TEST(Hash_Map_By_Keys, SearchCopiesNothingAfterAFunctionOfPairs)
851 search_by_keys_copies_nothing<DynMapLinHash<Copy_Fails, Default_Fails>>();
852 search_by_keys_copies_nothing<DynMapHash<Copy_Fails, Default_Fails>>();
856 template <
class Table>
859 Table table = make();
863 if constexpr (std::is_same_v<Table, ODhashTable<Copy_Fails>>)
864 return Table(101, hash_of, second_hash_of);
866 return Table(101, hash_of);
871 for (
long k = 0;
k < Keys; ++
k)
872 (
void) table.
insert(Copy_Fails(
k));
875 void holds_its_keys()
877 EXPECT_EQ(table.
size(),
static_cast<size_t>(Keys));
878 for (
long k = -1;
k <= Keys; ++
k)
879 EXPECT_EQ(table.
search(Copy_Fails(
k)) !=
nullptr,
k >= 0
and k < Keys) <<
"key " <<
k;
884 template <
class Hash_Fct>
885 bool is_the_function(
const Hash_Fct & fct,
size_t (*fct_ptr)(
const Copy_Fails &))
888 const auto * target = fct.template target<size_t (*)(const Copy_Fails &)>();
889 return target !=
nullptr and *target == fct_ptr;
894 TEST(Open_Hash_Table, SetHashFctKeepsItsFunctionWhenACopyFails)
896 using OL = Open_Table<OLhashTable<Copy_Fails>>;
897 using OD = Open_Table<ODhashTable<Copy_Fails>>;
898 try_every_failure<OL>(
"OLhashTable::set_hash_fct()",
true,
899 [] (OL & t) { t.table.set_hash_fct(other_hash_of); },
900 [] (OL & t,
const bool threw)
903 EXPECT_TRUE(is_the_function(t.table.get_hash_fct(),
904 threw ? hash_of : other_hash_of));
906 try_every_failure<OD>(
"ODhashTable::set_hash_fct()",
true,
907 [] (OD & t) { t.table.set_hash_fct(other_hash_of); },
908 [] (OD & t,
const bool threw)
911 EXPECT_TRUE(is_the_function(t.table.get_hash_fct(),
912 threw ? hash_of : other_hash_of));
913 EXPECT_TRUE(is_the_function(t.table.get_second_hash_fct(),
916 try_every_failure<OD>(
"ODhashTable::set_second_hash_fct()",
true,
917 [] (OD & t) { t.table.set_second_hash_fct(other_hash_of); },
918 [] (OD & t,
const bool threw)
921 EXPECT_TRUE(is_the_function(t.table.get_hash_fct(), hash_of));
922 EXPECT_TRUE(is_the_function(t.table.get_second_hash_fct(),
923 threw ? second_hash_of : other_hash_of));
935 struct Open_Map_With_Pairs
937 Map map{101, hash_of, second_hash_of};
939 Open_Map_With_Pairs()
941 for (
long k = 0;
k < Keys;
k += 2)
942 (
void) map.insert(Copy_Fails(
k), Default_Fails(
k));
945 void holds_its_pairs()
947 for (
long k = -1;
k <= Keys; ++
k)
949 const Copy_Fails key(
k);
950 const auto * p = map.search(key);
951 EXPECT_EQ(p !=
nullptr,
k >= 0
and k < Keys
and k % 2 == 0) <<
"key " <<
k;
953 EXPECT_EQ(p->second.value,
k);
959 void open_map_search_lets_the_failure_through(
const std::string & name)
961 using Fixture = Open_Map_With_Pairs<Map>;
962 static_assert(not
noexcept(std::declval<Map &>().search(std::declval<const Copy_Fails &>())));
963 static_assert(not
noexcept(std::declval<const Map &>().has(std::declval<const Copy_Fails &>())));
964 static_assert(not
noexcept(std::declval<const Map &>().contains(std::declval<Copy_Fails>())));
965 for (
const long key : {4L, 5L})
967 try_every_failure<Fixture>(name +
"::search()",
true,
970 const Copy_Fails
k(key);
971 (void) t.map.search(
k);
973 [] (Fixture & t, bool) { t.holds_its_pairs(); });
974 try_every_failure<Fixture>(name +
"::has()",
true,
977 const Copy_Fails
k(key);
980 [] (Fixture & t, bool) { t.holds_its_pairs(); });
984 TEST(Open_Hash_Map, SearchLetsTheFailureThrough)
986 open_map_search_lets_the_failure_through<MapOLhash<Copy_Fails, Default_Fails>>(
"MapOLhash");
987 open_map_search_lets_the_failure_through<MapODhash<Copy_Fails, Default_Fails>>(
"MapODhash");
988 static_assert(
noexcept(std::declval<MapOLhash<int, int> &>().
search(1)));
989 static_assert(
noexcept(std::declval<const MapODhash<int, int> &>().has(1)));
1003 template <
template <
typename,
class>
class T,
bool Splays =
false>
1006 template <
class Key,
class Data>
1009 static constexpr bool splays = Splays;
1012 constexpr long Pairs = 100;
1014 bool in_the_map(
const long k)
noexcept
1016 return k >= 0
and k < 2 * Pairs
and k % 2 == 0;
1021 template <
class Map>
1028 for (
long i = 0; i < Pairs; ++i)
1030 const long k = 2 * ((i * 37) % Pairs);
1031 (void) map.
insert(Copy_Fails(
k),
typename Map::Value_Type(
k));
1036 template <
class Kind>
1037 struct Map_Search :
public ::testing::Test {};
1039 using Tree_Kinds = ::testing::Types<Tree_Kind<BinTree>, Tree_Kind<Avl_Tree>,
1040 Tree_Kind<Avl_Tree_Rk>, Tree_Kind<Rb_Tree>,
1041 Tree_Kind<Rb_Tree_Rk>, Tree_Kind<TdRbTree>,
1042 Tree_Kind<TdRbTreeRk>, Tree_Kind<HtdRbTree>,
1043 Tree_Kind<HtdRbTreeRk>, Tree_Kind<Treap>,
1044 Tree_Kind<Treap_Rk>, Tree_Kind<Rand_Tree>,
1045 Tree_Kind<Splay_Tree, true>,
1046 Tree_Kind<Splay_Tree_Rk, true>>;
1052 using Map =
typename TypeParam::template Map<Copy_Fails, Default_Fails>;
1055 const auto & const_map = f.map;
1056 for (
long k = -1;
k <= 2 * Pairs; ++
k)
1058 const Copy_Fails key(
k);
1059 const bool in = in_the_map(
k);
1061 auto * p = map.search(key);
1062 const auto * q = const_map.search(key);
1063 auto *
r = map.search(Copy_Fails(
k));
1064 const auto * s = const_map.search(Copy_Fails(
k));
1067 const bool has = const_map.has(key);
1068 const bool has_rvalue = const_map.has(Copy_Fails(
k));
1069 const bool contains = const_map.contains(key);
1070 const bool contains_rvalue = const_map.contains(Copy_Fails(
k));
1073 EXPECT_EQ(map.find(key).value,
k);
1074 EXPECT_EQ(const_map.find(key).value,
k);
1078 EXPECT_THROW((
void) map.find(key), std::domain_error) <<
"key " <<
k;
1079 EXPECT_THROW((
void) const_map.find(key), std::domain_error) <<
"key " <<
k;
1081 if constexpr (not TypeParam::splays)
1083 EXPECT_EQ(Failures::done, 0) <<
"searching for " <<
k <<
" copied a key or built a Data";
1085 EXPECT_EQ(p !=
nullptr, in) <<
"key " <<
k;
1086 EXPECT_TRUE(q == p
and r == p
and s == p) <<
"key " <<
k;
1087 EXPECT_EQ(has, in) <<
"key " <<
k;
1088 EXPECT_EQ(has_rvalue, in) <<
"key " <<
k;
1090 EXPECT_EQ(contains_rvalue, in) <<
"key " <<
k;
1093 EXPECT_EQ(p->second.value,
k);
1096 EXPECT_TRUE(map.verify());
1097 EXPECT_EQ(map.size(),
static_cast<size_t>(Pairs));
1106 TYPED_TEST(Map_Search, SearchLetsTheFailureThrough)
1108 using Map =
typename TypeParam::template Map<Copy_Fails, long>;
1109 using Fixture = Map_Fixture<Map>;
1110 if constexpr (not TypeParam::splays)
1111 GTEST_SKIP() <<
"the search of this tree builds no pair";
1112 else if constexpr (
noexcept(std::declval<Map &>().search(Copy_Fails(0))))
1113 ADD_FAILURE() <<
"search() is noexcept, but it copies a key that may throw";
1115 for (
const long k : {0L, 1L, 98L})
1116 try_every_failure<Fixture>(
"search(" + std::to_string(
k) +
")",
true,
1117 [
k] (Fixture & f) { (void) f.map.search(Copy_Fails(
k)); },
1118 [] (Fixture & f, bool)
1120 EXPECT_TRUE(f.map.verify());
1121 EXPECT_EQ(f.map.size(),
static_cast<size_t>(Pairs));
1126 TYPED_TEST(Map_Search, RemoveKeyLetsTheFailureThrough)
1128 using Map =
typename TypeParam::template Map<Copy_Fails, long>;
1129 using Fixture = Map_Fixture<Map>;
1130 for (
const long k : {0L, 98L, 99L})
1132 bool refused =
false;
1133 try_every_failure<Fixture>(
"remove_key(" + std::to_string(
k) +
")",
true,
1134 [
k, &refused] (Fixture & f)
1139 f.map.remove_key(Copy_Fails(
k));
1141 catch (
const std::domain_error &)
1146 [
k, &refused] (Fixture & f,
const bool threw)
1149 EXPECT_EQ(refused, not in_the_map(
k))
1150 <<
"std::domain_error, for a key not in the map alone";
1151 const bool removed = in_the_map(
k)
and not threw;
1152 EXPECT_EQ(f.map.has(Copy_Fails(
k)),
1153 in_the_map(
k)
and not removed);
1154 EXPECT_EQ(f.map.size(),
1155 static_cast<size_t>(removed ? Pairs - 1 : Pairs));
1156 EXPECT_TRUE(f.map.verify());
1161 static_assert(
noexcept(std::declval<const DynMapTree<int, int> &>().
search(1)));
1162 static_assert(
noexcept(std::declval<DynMapTree<long, Array<int>,
Rb_Tree> &>().has(1L)));
1163 static_assert(
noexcept(std::declval<DynMapTree<long, int, Splay_Tree> &>().has(1L)));
1172 struct Removal_Fixture
1179 for (
long k = 0;
k < 10; ++
k)
1181 (void) map.
insert(
k, Copy_Fails(
k));
1182 (void) ranked.
insert(Copy_Fails(
k));
1188 TEST(Set_Removal, DelFreesTheNodeWhenItsCopyFails)
1190 try_every_failure<Removal_Fixture>(
"remove(5)",
true,
1191 [] (Removal_Fixture & f)
1193 (void) f.map.remove(5);
1195 [] (Removal_Fixture & f, bool)
1197 const bool removed = not f.map.contains(5);
1198 EXPECT_EQ(f.map.size(), removed ? 9u : 10u);
1199 EXPECT_TRUE(f.map.verify());
1203 TEST(Set_Removal, RemovePosFreesTheNodeWhenItsCopyFails)
1205 try_every_failure<Removal_Fixture>(
"remove_pos(3)",
true,
1206 [] (Removal_Fixture & f)
1208 EXPECT_EQ(f.ranked.remove_pos(3).value, 3);
1210 [] (Removal_Fixture & f, bool)
1212 EXPECT_FALSE(f.ranked.contains(Copy_Fails(3)));
1213 EXPECT_EQ(f.ranked.size(), 9u);
1214 EXPECT_TRUE(f.ranked.verify());
1225 struct Reading_Fails
1229 long items[3] = {1, 2, 3};
1235 const Reading_Fails * container;
1238 Iterator(
const Reading_Fails & c) noexcept : container(&c) {}
1240 bool has_curr() const noexcept {
return i < 3; }
1244 Failures::hit(
"reading");
1245 return container->items[i];
1250 void next_ne() noexcept { ++i; }
1258 TEST(Zip_Reading, LetsTheFailureOfTheReadingThrough)
1262 EXPECT_FALSE(
noexcept(std::declval<const Zip &>().
get_curr_ne()));
1263 EXPECT_FALSE(
noexcept(std::declval<const Enum_Zip &>().
get_curr_ne()));
1266 const Reading_Fails container;
1267 const auto list = one_two_three<DynList<long>>();
1268 const Zip
zip(container, list);
1269 const Enum_Zip enum_zip(container, list);
1271 Failing_From failing(0);
1272 EXPECT_THROW((
void)
zip.get_curr_ne(), Injected_Failure);
1273 EXPECT_THROW((
void) enum_zip.get_curr_ne(), Injected_Failure);
1275 EXPECT_EQ(std::get<0>(
zip.get_curr_ne()), 1);
1276 EXPECT_EQ(std::get<1>(enum_zip.get_curr_ne()), 1);
1281 struct Copy_From_Mutable_Fails
1285 Copy_From_Mutable_Fails(
const long v) noexcept :
value(v) {}
1287 Copy_From_Mutable_Fails(
const Copy_From_Mutable_Fails & item) noexcept
1288 :
value(item.value) {}
1290 Copy_From_Mutable_Fails(Copy_From_Mutable_Fails & item) :
value(item.
value)
1292 Failures::hit(
"copy from an item that is not constant");
1295 Copy_From_Mutable_Fails &
1296 operator = (
const Copy_From_Mutable_Fails &)
noexcept =
default;
1302 TEST(Zip_Current_Copies, CopiesFromConstantItems)
1305 const C a = one_two_three<C>(), b = one_two_three<C>(), c = one_two_three<C>();
1308 Failing_From failing(0);
1309 const auto t =
zip.get_curr_ne();
1310 const auto u = enum_zip.get_curr_ne();
1311 EXPECT_EQ(Failures::fired, 0);
1312 EXPECT_EQ(std::get<0>(t).
value + std::get<1>(t).
value + std::get<2>(t).
value, 3);
1313 EXPECT_EQ(std::get<2>(u).
value, 1);
1318 struct Mutable_Copy_Only
1322 Mutable_Copy_Only(
const long v) noexcept :
value(v) {}
1324 Mutable_Copy_Only(Mutable_Copy_Only & item) :
value(item.
value)
1326 Failures::hit(
"copy from an item that is not constant");
1329 Mutable_Copy_Only(
const Mutable_Copy_Only &) =
delete;
1331 Mutable_Copy_Only(Mutable_Copy_Only && item) noexcept :
value(item.value) {}
1335 struct By_Value_Items
1345 Iterator(
const By_Value_Items &)
noexcept {}
1347 bool has_curr() const noexcept {
return i < 3; }
1349 Mutable_Copy_Only
get_curr_ne() const noexcept {
return Mutable_Copy_Only(i + 1); }
1353 void next_ne() noexcept { ++i; }
1362 TEST(Zip_Current_Copies, CopiesAnItemThatCopiesOnlyFromOneNotConstant)
1367 EXPECT_FALSE(
noexcept(std::declval<const Zip &>().
get_curr_ne()));
1368 EXPECT_FALSE(
noexcept(std::declval<const Enum_Zip &>().
get_curr_ne()));
1370 const By_Value_Items items;
1371 const auto list = one_two_three<List>();
1373 for (Zip it(items, list); it.has_curr(); it.next_ne())
1374 sum += std::get<0>(it.get_curr_ne()).value * std::get<1>(it.get_curr_ne());
1375 for (Enum_Zip it(items, list); it.has_curr(); it.next_ne())
1376 sum += std::get<0>(it.get_curr_ne()).value +
static_cast<long>(std::get<2>(it.get_curr_ne()));
1377 EXPECT_EQ(sum, (1 + 4 + 9) + (1 + 3 + 5));
1379 const Zip
zip(items, list);
1380 const Enum_Zip enum_zip(items, list);
1382 Failing_From failing(0);
1383 EXPECT_THROW((
void)
zip.get_curr_ne(), Injected_Failure);
1384 EXPECT_THROW((
void) enum_zip.get_curr_ne(), Injected_Failure);
1386 EXPECT_EQ(std::get<0>(
zip.get_curr_ne()).value, 1);
1396 Failing_Base() { Failures::hit(
"default construction of the base"); }
1398 Failing_Base(
const Failing_Base &) { Failures::hit(
"copy of the base"); }
1400 Failing_Base & operator = (
const Failing_Base &)
noexcept =
default;
1402 void reset() noexcept {}
1410 TEST(Node_Construction_Base, LetsTheFailureOfTheBaseThrough)
1412 using Node = Failing_Base_Node<long>;
1413 EXPECT_FALSE((std::is_nothrow_constructible_v<Node, long &&>));
1414 EXPECT_FALSE((std::is_nothrow_constructible_v<Node, const Failing_Base &>));
1416 const Failing_Base base;
1418 Failing_From failing(0);
1419 EXPECT_THROW((
void)
Node(7L), Injected_Failure);
1422 Failing_From failing(0);
1423 EXPECT_THROW((
void)
Node(base), Injected_Failure);
1427 EXPECT_TRUE((std::is_nothrow_constructible_v<
AvlNode<long>,
long &&>));
bool operator<(const Time &l, const Time &r)
Zip iterators and functional operations for multiple containers.
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
TYPED_TEST(ArcHeapLifecycle, ClearNullsTheHandles)
WeightedDigraph::Node Node
size_t size_t int32_t value
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
Node for binary search tree.
constexpr bool is_empty() const noexcept
Return true if this (as header node) is empty.
void append(Dlink *node) noexcept
Insert node before this.
Node belonging to a double circular linked list with header node.
Dnode< T > *& get_next() const noexcept
Return the next node to this
Dnode< T > * remove_first_ne() noexcept
Remove the first node and return its address.
bool contains(const Key &key) const noexcept
Return true if the key exists in the table.
Key * insert(const Key &key)
Inserts key into the hash set.
bool is_empty() const noexcept
Checks if the container is empty.
Doubly-linked list (defined in tpl_dynList.H).
Generic key-value map implemented on top of a binary search tree.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
Generic linear hash table.
const size_t & size() const noexcept
Returns the number of elements in the table.
Bucket * insert(Bucket *bucket)
Insert bucket in the table.
Bucket * search(const Key &key) const noexcept
Search for key in the linear hash table.
constexpr bool is_empty() const noexcept
Number Theoretic Transform over Z / MOD Z.
Declare AvlNode type with 40-byte pool allocation.
Iterator that traverses multiple containers with enumeration index.
Iterator that traverses multiple Aleph containers in lockstep.
#define DECLARE_BINNODE(Name, height, Control_Data)
Specify tree node for a binary tree.
Singly linked list implementations with head-tail access.
LinearHashTable< int > Table
@ I
Infected (and infectious).
Main namespace for Aleph-w library functions.
T Item_Type
Type of elements from the first container.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
auto get_curr_ne() const noexcept(zip_detail::nothrow_curr_copy< Aleph::const_iterator_t< HeadC >, T > and zip_detail::nothrow_item_copy< T > and zip_detail::nothrow_tail_curr< ZipIterator< TailC... > >)
Return the current tuple (unchecked variant).
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > zip(const Container1 &a, const Container2 &b)
Zip two containers into a list of pairs.
std::decay_t< typename HeadC::Item_Type > T
bool contains(const std::string_view &str, const std::string_view &substr)
Check if substr appears inside str.
auto get_curr() const
Return the current tuple (bounds-checked).
and
Check uniqueness with explicit hash + equality functors.
bool has_curr() const noexcept
Return true if all underlying iterators are positioned on a valid item.
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
void next()
Advance all underlying iterators (bounds-checked).
void next_ne() noexcept(noexcept(std::declval< Aleph::const_iterator_t< HeadC > & >().next_ne()) and noexcept(std::declval< ZipIterator< TailC... > & >().next_ne()))
Advance all underlying iterators (unchecked variant).
@ Tuple
Tuple type such as (Int, Bool).
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Red-black tree with nodes without virtual destructor.
Dynamic array container with automatic resizing.
Binary heap implementation using tree structure.
Doubly linked list node with typed data.
Lazy and scalable dynamic array implementation.
Dynamic map with open hashing.
Dynamic key-value map based on balanced binary search trees.
Dynamic set implementations based on hash tables.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.