41#include <gtest/gtest.h>
80 std::vector<typename C::Item_Type> items_of(
const C & c)
82 std::vector<typename C::Item_Type> v;
83 c.for_each([&v](
const auto & item) { v.push_back(item); });
90 static inline int copies = 0;
91 static inline int moves = 0;
98 Counted(Counted && other) noexcept :
value(other.value) { ++moves; }
100 Counted & operator = (
const Counted & other)
107 Counted & operator = (Counted && other)
noexcept
114 static void reset() noexcept { copies = moves = 0; }
118 struct Throws_On_Conversion
120 operator int()
const {
throw std::runtime_error(
"conversion failed"); }
123 template <
class C,
class... Items>
126 C::build(std::forward<Items>(items)...);
129 template <
class C,
class... Items>
132 c.nappend(std::forward<Items>(items)...);
135 template <
class C,
class... Items>
138 c.ninsert(std::forward<Items>(items)...);
146TEST(ContainerBuild, ContainersBuildTheirOwnType)
160TEST(ContainerBuild, DerivedClassesBuildThemselves)
182 using P = std::pair<int, int>;
201 EXPECT_EQ(items_of(avl), (std::vector<int>{1, 2, 3}));
203 EXPECT_EQ(map.size(), 2u);
204 EXPECT_EQ(map.find(1),
"one");
211TEST(ContainerBuild, SequencesKeepTheArgumentOrder)
223TEST(ContainerBuild, ArrayBuildOfOneIntegerIsOneItem)
227 ASSERT_EQ(a.size(), 1u);
231 EXPECT_EQ(reserved.size(), 0u);
234TEST(ContainerBuild, StacksEndWithTheLastItemOnTop)
237 EXPECT_EQ(s.top(), 3);
238 EXPECT_EQ(s.pop(), 3);
239 EXPECT_EQ(s.pop(), 2);
240 EXPECT_EQ(s.pop(), 1);
243 EXPECT_EQ(fs.top(), 3);
244 EXPECT_EQ(fs.size(), 3u);
247 EXPECT_EQ(ls.top(), 3);
248 EXPECT_EQ(ls.size(), 3u);
251TEST(ContainerBuild, QueuesEndWithTheFirstItemInFront)
254 EXPECT_EQ(q.front(), 1);
255 EXPECT_EQ(q.get(), 1);
256 EXPECT_EQ(q.get(), 2);
257 EXPECT_EQ(q.get(), 3);
260 EXPECT_EQ(fq.front(), 1);
261 EXPECT_EQ(fq.size(), 3u);
264 EXPECT_EQ(lq.get(), 1);
265 EXPECT_EQ(lq.get(), 2);
266 EXPECT_EQ(lq.get(), 3);
269TEST(ContainerBuild, SetsKeepOneItemPerKey)
272 EXPECT_EQ(items_of(tree), (std::vector<int>{1, 2, 3}));
287 EXPECT_EQ(items_of(skip), (std::vector<int>{1, 2, 3}));
293TEST(ContainerBuild, MapsKeepTheFirstOfTwoEqualKeys)
297 std::pair{1,
"uno"});
298 EXPECT_EQ(tree.size(), 2u);
299 EXPECT_EQ(tree.find(1),
"one");
300 EXPECT_EQ(tree.find(2),
"two");
303 std::pair{1,
"uno"});
304 EXPECT_EQ(hash.size(), 1u);
305 EXPECT_EQ(hash.find(1),
"one");
308 std::pair{1,
"uno"});
309 EXPECT_EQ(open.size(), 1u);
310 EXPECT_EQ(open.find(1),
"one");
313TEST(ContainerBuild, HeapsKeepEveryItem)
316 EXPECT_EQ(bin.size(), 4u);
317 EXPECT_EQ(bin.top(), 1);
320 EXPECT_EQ(dyn.size(), 4u);
321 EXPECT_EQ(dyn.top(), 1);
324 EXPECT_EQ(fixed.size(), 4u);
325 EXPECT_EQ(fixed.top(), 1);
328TEST(ContainerBuild, NoItemsBuildsAnEmptyContainer)
339TEST(ContainerBuild, MoveOnlyItems)
342 std::make_unique<int>(2));
343 ASSERT_EQ(list.size(), 2u);
344 EXPECT_EQ(*list.get_first(), 1);
345 EXPECT_EQ(*list.get_last(), 2);
348 ASSERT_EQ(array.size(), 1u);
349 EXPECT_EQ(*array[0], 7);
352 EXPECT_EQ(*dlist.get_first(), 5);
355TEST(ContainerBuild, RvaluesAreMovedAndLvaluesCopiedOnce)
360 EXPECT_EQ(Counted::copies, 1);
361 EXPECT_GE(Counted::moves, 1);
362 ASSERT_EQ(list.size(), 2u);
363 EXPECT_EQ(list.get_first().value, 1);
364 EXPECT_EQ(list.get_last().value, 2);
367TEST(ContainerBuild, ItemsConvertWithoutNarrowingErrors)
373 (std::vector<std::string>{
"a",
"b"}));
376TEST(ContainerBuild, OnlyItemsAreAccepted)
378 static_assert(Buildable<DynList<int>, int, long,
short>);
379 static_assert(not Buildable<DynList<int>, std::string>);
381 static_assert(not Buildable<DynList<int>,
DynList<int> &>);
383 static_assert(not Buildable<DynMapTree<int, std::string>,
int>);
384 static_assert(Buildable<DynMapTree<int, std::string>, std::pair<int, const char *>>);
388TEST(ContainerBuild, ThrowingItemLeavesNothingBehind)
392 EXPECT_THROW((
void)
DynList<int>::build(1, Throws_On_Conversion{}, 3), std::runtime_error);
400TEST(ContainerBuild, NappendAndNinsertForwardTheirItems)
403 EXPECT_EQ(list.
nappend(std::make_unique<int>(1), std::make_unique<int>(2)), 2u);
404 EXPECT_EQ(list.
ninsert(std::make_unique<int>(0)), 1u);
405 ASSERT_EQ(list.
size(), 3u);
410TEST(ContainerBuild, NappendKeepsTheArgumentOrderAndNinsertFollowsInsert)
413 EXPECT_EQ(appended.
nappend(1, 2, 3), 3u);
414 EXPECT_EQ(items_of(appended), (std::vector<int>{1, 2, 3}));
418 EXPECT_EQ(inserted.
ninsert(1, 2, 3), 3u);
419 EXPECT_EQ(items_of(inserted), (std::vector<int>{3, 2, 1}));
423 EXPECT_EQ(queue.
ninsert(1, 2, 3), 3u);
424 EXPECT_EQ(items_of(queue), (std::vector<int>{1, 2, 3}));
427TEST(ContainerBuild, NappendAndNinsertCountTheItemsAdded)
431 EXPECT_EQ(tree.
ninsert(1, 1, 2), 2u);
432 EXPECT_EQ(tree.
nappend(2, 3, 3), 1u);
433 EXPECT_EQ(items_of(tree), (std::vector<int>{1, 2, 3}));
436 EXPECT_EQ(hash.
nappend(5, 5, 5), 1u);
437 EXPECT_EQ(hash.
ninsert(5, 6), 1u);
438 EXPECT_EQ(hash.size(), 2u);
440 using Pair = std::pair<int, std::string>;
442 EXPECT_EQ(map.
nappend(Pair{1,
"one"}, Pair{1,
"uno"}, Pair{2,
"two"}), 2u);
443 EXPECT_EQ(map.
size(), 2u);
444 EXPECT_EQ(map.
find(1),
"one");
449 EXPECT_EQ(skip.
ninsert(1, 1, 2), 2u);
451 EXPECT_EQ(items_of(skip), (std::vector<int>{1, 2, 3}));
457 EXPECT_EQ(items_of(other), (std::vector<int>{1, 2, 3}));
461 EXPECT_EQ(list.
nappend(1, 1, 1), 3u);
463 EXPECT_EQ(list.
size(), 5u);
466TEST(ContainerBuild, NappendAndNinsertKeepTheItemsAddedBeforeAThrow)
469 EXPECT_THROW((
void) appended.
nappend(1, 2, Throws_On_Conversion{}, 4), std::runtime_error);
470 EXPECT_EQ(items_of(appended), (std::vector<int>{1, 2}));
473 EXPECT_THROW((
void) inserted.
ninsert(1, Throws_On_Conversion{}, 3), std::runtime_error);
474 EXPECT_EQ(items_of(inserted), (std::vector<int>{1}));
477TEST(ContainerBuild, NappendAndNinsertConvertAndMoveTheirItems)
480 EXPECT_EQ(longs.
nappend(1,
short{2}, 3L), 3u);
481 EXPECT_EQ(items_of(longs), (std::vector<long>{1, 2, 3}));
484 EXPECT_EQ(strings.
ninsert(
"b", std::string(
"a")), 2u);
485 EXPECT_EQ(items_of(strings), (std::vector<std::string>{
"a",
"b"}));
490 EXPECT_EQ(counted.
nappend(lvalue, Counted(2)), 2u);
491 EXPECT_EQ(Counted::copies, 1);
492 EXPECT_EQ(counted.
ninsert(lvalue, Counted(3)), 2u);
493 EXPECT_EQ(Counted::copies, 2);
496 static_assert(Can_Nappend<DynList<int>, int,
long>);
497 static_assert(Can_Ninsert<DynList<int>, int,
long>);
498 static_assert(not Can_Nappend<DynList<int>, std::string>);
499 static_assert(not Can_Ninsert<DynList<int>, std::string>);
500 static_assert(not Can_Nappend<DynList<int>,
DynList<int> &>);
503TEST(ContainerBuild, BuildFunctionsForwardTheirItems)
505 auto list = build_dynlist<std::unique_ptr<int>>(std::make_unique<int>(1));
506 EXPECT_EQ(*list.get_first(), 1);
508 auto array = build_array<std::unique_ptr<int>>(std::make_unique<int>(2));
509 EXPECT_EQ(*array[0], 2);
511 auto set = build_container<DynSetAvlTree<int>>(3, 1, 2);
513 EXPECT_EQ(items_of(set), (std::vector<int>{1, 2, 3}));
516TEST(ContainerBuild, AppendInContainerTreatsEveryArgumentAsAnItem)
523 EXPECT_EQ(items_of(list), (std::vector<size_t>{7, 8}));
524 EXPECT_EQ(first, 7u);
Functional programming utilities for Aleph-w containers.
Integer domain classes for sparse data structures.
size_t size_t int32_t value
Generic domain class based on hash set.
static AlDomain build(Items &&... items)
Fixed-capacity binary heap backed by a raw array.
Queue implemented with a single dynamic array.
Stack implemented with simple dynamic array and with bounds verification.
Simple dynamic array with automatic resizing and functional operations.
static Array create_reserved(const size_t cap)
Build an empty array with room for at least cap items.
Dynamic heap (priority queue) backed by DynArray.
Set-like container backed by a dynamic array.
static DynArray_Set build(Items &&... items)
Dynamic heap of elements of type T ordered by a comparison functor.
Dynamic doubly linked list with O(1) size and bidirectional access.
Self-adjusting dynamic hash table.
Dynamic queue of elements of generic type T based on single linked list.
Dynamic stack of elements of generic type T based on a singly linked list.
Doubly-linked list (defined in tpl_dynList.H).
T & get_first()
Return the first item of the list.
T & get_last()
Return the last item of the list.
size_t size() const noexcept
Return the number of items of the list, in constant time.
Dynamic map implemented with an AVL tree.
static DynMapAvlTree build(Items &&... items)
Dynamic map implemented with a classic binary search tree.
static DynMapBinTree build(Items &&... items)
static DynMapHashTable build(Items &&... items)
Dynamic map implemented with a randomized BST.
static DynMapRandTree build(Items &&... items)
Dynamic map implemented with a red-black tree.
static DynMapRbTree build(Items &&... items)
Dynamic map implemented with a splay tree.
static DynMapSplayTree build(Items &&... items)
Dynamic map implemented with a ranked treap.
static DynMapTreapRk build(Items &&... items)
Dynamic map implemented with a treap.
static DynMapTreap build(Items &&... items)
Generic key-value map implemented on top of a binary search tree.
static DynMapTree build(Items &&... items)
Data & find(const Key &key)
Find the value associated with key.
Dynamic set implemented using extended AVL binary search trees with rank support of type Avl_Tree_Rk<...
static DynSetAvlRkTree build(Items &&... items)
Dynamic set implemented using AVL binary search trees of type Avl_Tree<Key>.
static DynSetAvlTree build(Items &&... items)
Dynamic set implemented using binary search trees of type BinTree<Key>.
static DynSetBinTree build(Items &&... items)
Dynamic set implemented using Hybrid Red-Black trees with rank support of type HtdRbTreeRk<Key>.
static DynSetHtdRbRkTree build(Items &&... items)
Dynamic set implemented using Hybrid Top-Down/Bottom-Up Red-Black trees of type HtdRbTree<Key>.
static DynSetHtdRbTree build(Items &&... items)
Dynamic set implemented using randomized binary search trees of type Rand_Tree<Key>.
static DynSetRandTree build(Items &&... items)
Dynamic set implemented using extended Red-Black binary search trees with rank support of type Rb_Tre...
static DynSetRbRkTree build(Items &&... items)
Dynamic set implemented using Red-Black binary search trees of type Rb_Tree<Key> (bottom-up implement...
static DynSetRbTree build(Items &&... items)
Dynamic set implemented using splay trees with rank support of type Splay_Tree_Rk<Key>.
static DynSetSplayRkTree build(Items &&... items)
Dynamic set implemented using splay binary search trees of type Splay_Tree<Key>.
static DynSetSplayTree build(Items &&... items)
Dynamic set implemented using Top-Down Red-Black binary search trees with rank support of type TdRbTr...
static DynSetTdRbRkTree build(Items &&... items)
Dynamic set implemented using Top-Down Red-Black binary search trees of type TdRbTree<Key>.
static DynSetTdRbTree build(Items &&... items)
Dynamic set implemented using extended treap binary search trees with rank support of type Treap_Rk<K...
static DynSetTreapRk build(Items &&... items)
Dynamic set implemented using randomized treap binary search trees of type Treap<Key>.
static DynSetTreap build(Items &&... items)
Dynamic set backed by balanced binary search trees with automatic memory management.
const size_t & size() const
Returns the cardinality of the set.
Dynamic ordered set implemented with a Skip List.
Very simple queue implemented with a contiguous array.
Open addressing hash table with double hashing collision resolution.
Open addressing hash table with linear probing collision resolution.
size_t nappend(Args &&... args)
Append n variadic items.
size_t ninsert(Args &&... args)
Insert n variadic items.
static Container build(Items &&... items)
Build a container that holds the given items.
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
size_t append_in_container(C &c, Args &&... args)
Append multiple items into a container.
size_t insert_in_container(C &c, Args &&... args)
Insert multiple items into a container.
Hash-based dynamic set (defined in tpl_dynSetHash.H).
static DynSetLhash build(Items &&... items)
static DynSetLinHash build(Items &&... items)
Dynamic mapping implemented with treap trees.
static DynTreapTree build(Items &&... items)
static HashMap build(Items &&... items)
static HashSet build(Items &&... items)
Open addressing hash map using double hashing.
Open addressing hash map using linear probing.
Open addressing hash map for key-value pairs.
static MapOpenHash build(Items &&... items)
Fixed-capacity binary heap and heapsort algorithms.
Circular queue implementations backed by arrays.
Stack implementations backed by dynamic or fixed arrays.
Dynamic array container with automatic resizing.
Array-based dynamic binary heap.
Lazy and scalable dynamic array implementation.
Dynamic binary heap with node-based storage.
Dynamic doubly linked list implementation.
Dynamic queue implementation based on linked lists.
Dynamic stack implementation based on linked lists.
Dynamic map with open hashing.
Dynamic key-value map based on balanced binary search trees.
Dynamic set implementations based on hash tables.
Dynamic set implementations based on balanced binary search trees.
Dynamic ordered set implemented with a Skip List.
Unified hash table interface.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.