Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
container_build_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / /
6 / ___ \| | __/ |_) | | | |_____\ V V /
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2026 Leandro Rabindranath Leon
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32
41#include <gtest/gtest.h>
42
43#include <memory>
44#include <stdexcept>
45#include <string>
46#include <type_traits>
47#include <utility>
48#include <vector>
49
50#include <ahFunctional.H>
51#include <al-domain.H>
52#include <htlist.H>
53#include <tpl_array.H>
54#include <tpl_arrayHeap.H>
55#include <tpl_arrayQueue.H>
56#include <tpl_arrayStack.H>
57#include <tpl_dynArray.H>
58#include <tpl_dynArrayHeap.H>
59#include <tpl_dynBinHeap.H>
60#include <tpl_dynDlist.H>
61#include <tpl_dynListQueue.H>
62#include <tpl_dynListStack.H>
63#include <tpl_dynMapOhash.H>
64#include <tpl_dynMapTree.H>
65#include <tpl_dynSetHash.H>
66#include <tpl_dynSetTree.H>
67#include <tpl_dynSkipList.H>
68#include <tpl_dynTreap.H>
69#include <tpl_dynarray_set.H>
70#include <tpl_hash.H>
71#include <tpl_odhash.H>
72#include <tpl_olhash.H>
73
74using namespace Aleph;
75
76namespace
77{
78 // The items of `c`, in traversal order.
79 template <class C>
80 std::vector<typename C::Item_Type> items_of(const C & c)
81 {
82 std::vector<typename C::Item_Type> v;
83 c.for_each([&v](const auto & item) { v.push_back(item); });
84 return v;
85 }
86
87 // Counts its copies and moves.
88 struct Counted
89 {
90 static inline int copies = 0;
91 static inline int moves = 0;
92
93 int value = 0;
94
95 Counted() = default;
96 Counted(const int v) : value(v) {}
97 Counted(const Counted & other) : value(other.value) { ++copies; }
98 Counted(Counted && other) noexcept : value(other.value) { ++moves; }
99
100 Counted & operator = (const Counted & other)
101 {
102 value = other.value;
103 ++copies;
104 return *this;
105 }
106
107 Counted & operator = (Counted && other) noexcept
108 {
109 value = other.value;
110 ++moves;
111 return *this;
112 }
113
114 static void reset() noexcept { copies = moves = 0; }
115 };
116
117 // Converts to int by throwing.
118 struct Throws_On_Conversion
119 {
120 operator int() const { throw std::runtime_error("conversion failed"); }
121 };
122
123 template <class C, class... Items>
124 concept Buildable = requires (Items &&... items)
125 {
126 C::build(std::forward<Items>(items)...);
127 };
128
129 template <class C, class... Items>
130 concept Can_Nappend = requires (C & c, Items &&... items)
131 {
132 c.nappend(std::forward<Items>(items)...);
133 };
134
135 template <class C, class... Items>
136 concept Can_Ninsert = requires (C & c, Items &&... items)
137 {
138 c.ninsert(std::forward<Items>(items)...);
139 };
140}
141
142// ---------------------------------------------------------------------------
143// The result type
144// ---------------------------------------------------------------------------
145
146TEST(ContainerBuild, ContainersBuildTheirOwnType)
147{
148 static_assert(std::is_same_v<decltype(DynList<int>::build(1)), DynList<int>>);
149 static_assert(std::is_same_v<decltype(DynDlist<int>::build(1)), DynDlist<int>>);
150 static_assert(std::is_same_v<decltype(Array<int>::build(1)), Array<int>>);
151 static_assert(std::is_same_v<decltype(DynArray<int>::build(1)), DynArray<int>>);
152 static_assert(std::is_same_v<decltype(ArrayStack<int>::build(1)), ArrayStack<int>>);
153 static_assert(std::is_same_v<decltype(ArrayQueue<int>::build(1)), ArrayQueue<int>>);
154 static_assert(std::is_same_v<decltype(DynSetTree<int>::build(1)), DynSetTree<int>>);
155 static_assert(std::is_same_v<decltype(DynSetHash<int>::build(1)), DynSetHash<int>>);
156 static_assert(std::is_same_v<decltype(OLhashTable<int>::build(1)), OLhashTable<int>>);
157 static_assert(std::is_same_v<decltype(DynBinHeap<int>::build(1)), DynBinHeap<int>>);
158}
159
160TEST(ContainerBuild, DerivedClassesBuildThemselves)
161{
162 // Without Derived_Build() they would inherit a build() of their base.
163 static_assert(std::is_same_v<decltype(DynSetBinTree<int>::build(1)), DynSetBinTree<int>>);
164 static_assert(std::is_same_v<decltype(DynSetAvlTree<int>::build(1)), DynSetAvlTree<int>>);
165 static_assert(std::is_same_v<decltype(DynSetSplayTree<int>::build(1)), DynSetSplayTree<int>>);
166 static_assert(std::is_same_v<decltype(DynSetSplayRkTree<int>::build(1)), DynSetSplayRkTree<int>>);
167 static_assert(std::is_same_v<decltype(DynSetRandTree<int>::build(1)), DynSetRandTree<int>>);
168 static_assert(std::is_same_v<decltype(DynSetTreap<int>::build(1)), DynSetTreap<int>>);
169 static_assert(std::is_same_v<decltype(DynSetTreapRk<int>::build(1)), DynSetTreapRk<int>>);
170 static_assert(std::is_same_v<decltype(DynSetAvlRkTree<int>::build(1)), DynSetAvlRkTree<int>>);
171 static_assert(std::is_same_v<decltype(DynSetRbTree<int>::build(1)), DynSetRbTree<int>>);
172 static_assert(std::is_same_v<decltype(DynSetTdRbTree<int>::build(1)), DynSetTdRbTree<int>>);
173 static_assert(std::is_same_v<decltype(DynSetRbRkTree<int>::build(1)), DynSetRbRkTree<int>>);
174 static_assert(std::is_same_v<decltype(DynSetTdRbRkTree<int>::build(1)), DynSetTdRbRkTree<int>>);
175 static_assert(std::is_same_v<decltype(DynSetHtdRbTree<int>::build(1)), DynSetHtdRbTree<int>>);
176 static_assert(std::is_same_v<decltype(DynSetHtdRbRkTree<int>::build(1)), DynSetHtdRbRkTree<int>>);
177 static_assert(std::is_same_v<decltype(DynSetLhash<int>::build(1)), DynSetLhash<int>>);
178 static_assert(std::is_same_v<decltype(DynSetLinHash<int>::build(1)), DynSetLinHash<int>>);
179 static_assert(std::is_same_v<decltype(DynArray_Set<int>::build(1)), DynArray_Set<int>>);
180 static_assert(std::is_same_v<decltype(HashSet<int>::build(1)), HashSet<int>>);
181
182 using P = std::pair<int, int>;
183 static_assert(std::is_same_v<decltype(DynMapTree<int, int>::build(P{})), DynMapTree<int, int>>);
184 static_assert(std::is_same_v<decltype(DynMapBinTree<int, int>::build(P{})), DynMapBinTree<int, int>>);
185 static_assert(std::is_same_v<decltype(DynMapAvlTree<int, int>::build(P{})), DynMapAvlTree<int, int>>);
186 static_assert(std::is_same_v<decltype(DynMapRbTree<int, int>::build(P{})), DynMapRbTree<int, int>>);
187 static_assert(std::is_same_v<decltype(DynMapRandTree<int, int>::build(P{})), DynMapRandTree<int, int>>);
188 static_assert(std::is_same_v<decltype(DynMapTreap<int, int>::build(P{})), DynMapTreap<int, int>>);
189 static_assert(std::is_same_v<decltype(DynMapTreapRk<int, int>::build(P{})), DynMapTreapRk<int, int>>);
190 static_assert(std::is_same_v<decltype(DynMapSplayTree<int, int>::build(P{})), DynMapSplayTree<int, int>>);
191 static_assert(std::is_same_v<decltype(DynTreapTree<int, int>::build(P{})), DynTreapTree<int, int>>);
192 static_assert(std::is_same_v<decltype(DynMapHash<int, int>::build(P{})), DynMapHash<int, int>>);
193 static_assert(std::is_same_v<decltype(MapOLhash<int, int>::build(P{})), MapOLhash<int, int>>);
194 static_assert(std::is_same_v<decltype(HashMap<int, int>::build(P{})), HashMap<int, int>>);
195 static_assert(std::is_same_v<decltype(MapODhash<int, int>::build(P{})), MapODhash<int, int>>);
196 static_assert(std::is_same_v<decltype(MapOpenHash<int, int>::build(P{})), MapOpenHash<int, int>>);
197 static_assert(std::is_same_v<decltype(AlDomain<int>::build(1)), AlDomain<int>>);
198
199 // And they hold what the base container would hold.
200 auto avl = DynSetAvlTree<int>::build(3, 1, 2, 1);
201 EXPECT_EQ(items_of(avl), (std::vector<int>{1, 2, 3}));
202 auto map = DynMapAvlTree<int, std::string>::build(std::pair{2, "two"}, std::pair{1, "one"});
203 EXPECT_EQ(map.size(), 2u);
204 EXPECT_EQ(map.find(1), "one");
205}
206
207// ---------------------------------------------------------------------------
208// The rules of each container, as with the braces
209// ---------------------------------------------------------------------------
210
211TEST(ContainerBuild, SequencesKeepTheArgumentOrder)
212{
213 EXPECT_EQ(items_of(DynList<int>::build(3, 1, 2)), (std::vector<int>{3, 1, 2}));
214 EXPECT_EQ(items_of(DynDlist<int>::build(3, 1, 2)), (std::vector<int>{3, 1, 2}));
215 EXPECT_EQ(items_of(Array<int>::build(3, 1, 2)), (std::vector<int>{3, 1, 2}));
216 EXPECT_EQ(items_of(DynArray<int>::build(3, 1, 2)), (std::vector<int>{3, 1, 2}));
217
218 // The same container as the braces.
219 EXPECT_EQ(items_of(DynList<int>::build(3, 1, 2)), items_of(DynList<int>{3, 1, 2}));
220 EXPECT_EQ(items_of(Array<int>::build(3, 1, 2)), items_of(Array<int>{3, 1, 2}));
221}
222
223TEST(ContainerBuild, ArrayBuildOfOneIntegerIsOneItem)
224{
225 // Unlike the removed Array<T>(n), it never means a capacity.
226 const auto a = Array<int>::build(10);
227 ASSERT_EQ(a.size(), 1u);
228 EXPECT_EQ(a[0], 10);
229
230 const auto reserved = Array<int>::create_reserved(10);
231 EXPECT_EQ(reserved.size(), 0u);
232}
233
234TEST(ContainerBuild, StacksEndWithTheLastItemOnTop)
235{
236 auto s = ArrayStack<int>::build(1, 2, 3);
237 EXPECT_EQ(s.top(), 3);
238 EXPECT_EQ(s.pop(), 3);
239 EXPECT_EQ(s.pop(), 2);
240 EXPECT_EQ(s.pop(), 1);
241
242 auto fs = FixedStack<int>::build(1, 2, 3);
243 EXPECT_EQ(fs.top(), 3);
244 EXPECT_EQ(fs.size(), 3u);
245
246 auto ls = DynListStack<int>::build(1, 2, 3);
247 EXPECT_EQ(ls.top(), 3);
248 EXPECT_EQ(ls.size(), 3u);
249}
250
251TEST(ContainerBuild, QueuesEndWithTheFirstItemInFront)
252{
253 auto q = ArrayQueue<int>::build(1, 2, 3);
254 EXPECT_EQ(q.front(), 1);
255 EXPECT_EQ(q.get(), 1);
256 EXPECT_EQ(q.get(), 2);
257 EXPECT_EQ(q.get(), 3);
258
259 auto fq = FixedQueue<int>::build(1, 2, 3);
260 EXPECT_EQ(fq.front(), 1);
261 EXPECT_EQ(fq.size(), 3u);
262
263 auto lq = DynListQueue<int>::build(1, 2, 3);
264 EXPECT_EQ(lq.get(), 1);
265 EXPECT_EQ(lq.get(), 2);
266 EXPECT_EQ(lq.get(), 3);
267}
268
269TEST(ContainerBuild, SetsKeepOneItemPerKey)
270{
271 auto tree = DynSetTree<int>::build(3, 1, 2, 1, 3);
272 EXPECT_EQ(items_of(tree), (std::vector<int>{1, 2, 3}));
273 EXPECT_EQ(items_of(tree), items_of(DynSetTree<int>{3, 1, 2, 1, 3}));
274
275 EXPECT_EQ(DynSetHash<int>::build(3, 1, 2, 1).size(), 3u);
276 EXPECT_EQ(DynSetLinHash<int>::build(3, 1, 2, 1).size(), 3u);
277 EXPECT_EQ(OLhashTable<int>::build(3, 1, 2, 1).size(), 3u);
278 EXPECT_EQ(ODhashTable<int>::build(3, 1, 2, 1).size(), 3u);
279 EXPECT_EQ(HashSet<int>::build(3, 1, 2, 1).size(), 3u);
280
281 // DynArray_Set allows repeated items.
282 EXPECT_EQ(DynArray_Set<int>::build(3, 1, 3).size(), 3u);
283
284 // A skip list too, for rvalue and for lvalue items: its append() of an
285 // rvalue used to throw for a key already in the set.
286 const auto skip = DynSkipList<int>::build(3, 1, 2, 1);
287 EXPECT_EQ(items_of(skip), (std::vector<int>{1, 2, 3}));
288 EXPECT_EQ(items_of(skip), items_of(DynSkipList<int>{3, 1, 2, 1}));
289 const int one = 1;
290 EXPECT_EQ(DynSkipList<int>::build(one, one).size(), 1u);
291}
292
293TEST(ContainerBuild, MapsKeepTheFirstOfTwoEqualKeys)
294{
295 auto tree = DynMapTree<int, std::string>::build(std::pair{1, "one"},
296 std::pair{2, "two"},
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");
301
302 auto hash = DynMapHash<int, std::string>::build(std::pair{1, "one"},
303 std::pair{1, "uno"});
304 EXPECT_EQ(hash.size(), 1u);
305 EXPECT_EQ(hash.find(1), "one");
306
307 auto open = MapOLhash<int, std::string>::build(std::pair{1, "one"},
308 std::pair{1, "uno"});
309 EXPECT_EQ(open.size(), 1u);
310 EXPECT_EQ(open.find(1), "one");
311}
312
313TEST(ContainerBuild, HeapsKeepEveryItem)
314{
315 auto bin = DynBinHeap<int>::build(3, 1, 2, 1);
316 EXPECT_EQ(bin.size(), 4u);
317 EXPECT_EQ(bin.top(), 1);
318
319 auto dyn = DynArrayHeap<int>::build(3, 1, 2, 1);
320 EXPECT_EQ(dyn.size(), 4u);
321 EXPECT_EQ(dyn.top(), 1);
322
323 auto fixed = ArrayHeap<int>::build(3, 1, 2, 1);
324 EXPECT_EQ(fixed.size(), 4u);
325 EXPECT_EQ(fixed.top(), 1);
326}
327
328TEST(ContainerBuild, NoItemsBuildsAnEmptyContainer)
329{
330 EXPECT_TRUE(DynList<int>::build().is_empty());
331 EXPECT_TRUE(DynSetTree<int>::build().is_empty());
332 EXPECT_TRUE((DynMapTree<int, int>::build().is_empty()));
333}
334
335// ---------------------------------------------------------------------------
336// What the braces cannot do
337// ---------------------------------------------------------------------------
338
339TEST(ContainerBuild, MoveOnlyItems)
340{
341 auto list = DynList<std::unique_ptr<int>>::build(std::make_unique<int>(1),
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);
346
347 auto array = Array<std::unique_ptr<int>>::build(std::make_unique<int>(7));
348 ASSERT_EQ(array.size(), 1u);
349 EXPECT_EQ(*array[0], 7);
350
351 auto dlist = DynDlist<std::unique_ptr<int>>::build(std::make_unique<int>(5));
352 EXPECT_EQ(*dlist.get_first(), 5);
353}
354
355TEST(ContainerBuild, RvaluesAreMovedAndLvaluesCopiedOnce)
356{
357 Counted::reset();
358 Counted lvalue(1);
359 auto list = DynList<Counted>::build(lvalue, Counted(2));
360 EXPECT_EQ(Counted::copies, 1); // only the lvalue
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);
365}
366
367TEST(ContainerBuild, ItemsConvertWithoutNarrowingErrors)
368{
369 // A braced list rejects the non-constant int as a narrowing conversion.
370 const int n = 1;
371 EXPECT_EQ(items_of(Array<double>::build(n, 2.5)), (std::vector<double>{1.0, 2.5}));
372 EXPECT_EQ(items_of(DynList<std::string>::build("a", std::string("b"))),
373 (std::vector<std::string>{"a", "b"}));
374}
375
376TEST(ContainerBuild, OnlyItemsAreAccepted)
377{
378 static_assert(Buildable<DynList<int>, int, long, short>);
379 static_assert(not Buildable<DynList<int>, std::string>);
380 // DynList::append(DynList &) concatenates a list: build() takes items.
381 static_assert(not Buildable<DynList<int>, DynList<int> &>);
382 // A map takes pairs: a bare key would get a default-constructed value.
383 static_assert(not Buildable<DynMapTree<int, std::string>, int>);
384 static_assert(Buildable<DynMapTree<int, std::string>, std::pair<int, const char *>>);
385 SUCCEED();
386}
387
388TEST(ContainerBuild, ThrowingItemLeavesNothingBehind)
389{
390 // The partially built container is destroyed; the sanitizer jobs check
391 // that nothing leaks.
392 EXPECT_THROW((void) DynList<int>::build(1, Throws_On_Conversion{}, 3), std::runtime_error);
393 EXPECT_THROW((void) DynSetTree<int>::build(1, Throws_On_Conversion{}), std::runtime_error);
394}
395
396// ---------------------------------------------------------------------------
397// The functions that append several items
398// ---------------------------------------------------------------------------
399
400TEST(ContainerBuild, NappendAndNinsertForwardTheirItems)
401{
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);
406 EXPECT_EQ(*list.get_first(), 0);
407 EXPECT_EQ(*list.get_last(), 2);
408}
409
410TEST(ContainerBuild, NappendKeepsTheArgumentOrderAndNinsertFollowsInsert)
411{
412 DynList<int> appended;
413 EXPECT_EQ(appended.nappend(1, 2, 3), 3u);
414 EXPECT_EQ(items_of(appended), (std::vector<int>{1, 2, 3}));
415
416 // DynList::insert() puts each item first: the last argument ends first.
417 DynList<int> inserted;
418 EXPECT_EQ(inserted.ninsert(1, 2, 3), 3u);
419 EXPECT_EQ(items_of(inserted), (std::vector<int>{3, 2, 1}));
420
421 // ArrayQueue::insert() is put(): the queue keeps the argument order.
422 ArrayQueue<int> queue;
423 EXPECT_EQ(queue.ninsert(1, 2, 3), 3u);
424 EXPECT_EQ(items_of(queue), (std::vector<int>{1, 2, 3}));
425}
426
427TEST(ContainerBuild, NappendAndNinsertCountTheItemsAdded)
428{
429 // They returned the number of arguments, also when a set refused some.
430 DynSetTree<int> tree;
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}));
434
435 DynSetHash<int> hash;
436 EXPECT_EQ(hash.nappend(5, 5, 5), 1u);
437 EXPECT_EQ(hash.ninsert(5, 6), 1u);
438 EXPECT_EQ(hash.size(), 2u);
439
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");
445
446 // ninsert() of a skip list counts the keys it adds. Its append() returns
447 // the key already in the set, so nappend() cannot tell them apart.
448 DynSkipList<int> skip;
449 EXPECT_EQ(skip.ninsert(1, 1, 2), 2u);
450 (void) skip.nappend(2, 3);
451 EXPECT_EQ(items_of(skip), (std::vector<int>{1, 2, 3}));
452
453 // The free functions follow the same rule.
454 DynSetTree<int> other;
455 EXPECT_EQ(append_in_container(other, 1, 1, 2), 2u);
456 EXPECT_EQ(insert_in_container(other, 2, 3), 1u);
457 EXPECT_EQ(items_of(other), (std::vector<int>{1, 2, 3}));
458
459 // A sequence adds every item.
460 DynList<int> list;
461 EXPECT_EQ(list.nappend(1, 1, 1), 3u);
462 EXPECT_EQ(append_in_container(list, 1, 1), 2u);
463 EXPECT_EQ(list.size(), 5u);
464}
465
466TEST(ContainerBuild, NappendAndNinsertKeepTheItemsAddedBeforeAThrow)
467{
468 DynList<int> appended;
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}));
471
472 DynList<int> inserted;
473 EXPECT_THROW((void) inserted.ninsert(1, Throws_On_Conversion{}, 3), std::runtime_error);
474 EXPECT_EQ(items_of(inserted), (std::vector<int>{1}));
475}
476
477TEST(ContainerBuild, NappendAndNinsertConvertAndMoveTheirItems)
478{
479 DynList<long> longs;
480 EXPECT_EQ(longs.nappend(1, short{2}, 3L), 3u);
481 EXPECT_EQ(items_of(longs), (std::vector<long>{1, 2, 3}));
482
483 DynList<std::string> strings;
484 EXPECT_EQ(strings.ninsert("b", std::string("a")), 2u);
485 EXPECT_EQ(items_of(strings), (std::vector<std::string>{"a", "b"}));
486
487 Counted::reset();
488 Counted lvalue(1);
489 DynList<Counted> counted;
490 EXPECT_EQ(counted.nappend(lvalue, Counted(2)), 2u);
491 EXPECT_EQ(Counted::copies, 1); // only the lvalue
492 EXPECT_EQ(counted.ninsert(lvalue, Counted(3)), 2u);
493 EXPECT_EQ(Counted::copies, 2);
494
495 // Only items: neither another type nor a whole list.
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> &>);
501}
502
503TEST(ContainerBuild, BuildFunctionsForwardTheirItems)
504{
505 auto list = build_dynlist<std::unique_ptr<int>>(std::make_unique<int>(1));
506 EXPECT_EQ(*list.get_first(), 1);
507
508 auto array = build_array<std::unique_ptr<int>>(std::make_unique<int>(2));
509 EXPECT_EQ(*array[0], 2);
510
511 auto set = build_container<DynSetAvlTree<int>>(3, 1, 2);
512 static_assert(std::is_same_v<decltype(set), DynSetAvlTree<int>>);
513 EXPECT_EQ(items_of(set), (std::vector<int>{1, 2, 3}));
514}
515
516TEST(ContainerBuild, AppendInContainerTreatsEveryArgumentAsAnItem)
517{
518 // The former recursive implementation took a leading size_t lvalue for
519 // its internal counter instead of appending it.
520 DynList<size_t> list;
521 size_t first = 7;
522 EXPECT_EQ(append_in_container(list, first, size_t{8}), 2u);
523 EXPECT_EQ(items_of(list), (std::vector<size_t>{7, 8}));
524 EXPECT_EQ(first, 7u);
525}
Functional programming utilities for Aleph-w containers.
Integer domain classes for sparse data structures.
size_t size_t int32_t value
Definition ca-c-api.h:116
Generic domain class based on hash set.
Definition al-domain.H:132
static AlDomain build(Items &&... items)
Definition al-domain.H:140
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.
Definition tpl_array.H:141
static Array create_reserved(const size_t cap)
Build an empty array with room for at least cap items.
Definition tpl_array.H:194
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).
Definition htlist.H:1231
T & get_first()
Return the first item of the list.
Definition htlist.H:1548
T & get_last()
Return the last item of the list.
Definition htlist.H:1529
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
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.
Fixed length stack.
Open addressing hash table with double hashing collision resolution.
Definition tpl_odhash.H:193
Open addressing hash table with linear probing collision resolution.
Definition tpl_olhash.H:175
size_t nappend(Args &&... args)
Append n variadic items.
Definition ah-dry.H:1379
size_t ninsert(Args &&... args)
Insert n variadic items.
Definition ah-dry.H:1353
static Container build(Items &&... items)
Build a container that holds the given items.
Definition ah-dry.H:1423
#define TEST(name)
pair< size_t, string > P
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)
Definition tpl_hash.H:114
static HashSet build(Items &&... items)
Definition tpl_hash.H:66
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)
static int moves
static int copies
static void reset()
Counted(int v=0)
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.
Dynamic treap alias.
Array-based dynamic set.
Unified hash table interface.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.