Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
array.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
38#include <gtest/gtest.h>
39
40#include <tpl_array.H>
41#include <ah-unique.H>
42
43#include <array>
44#include <cstdint>
45#include <initializer_list>
46#include <iterator>
47#include <numeric>
48#include <sstream>
49#include <stdexcept>
50#include <string>
51#include <type_traits>
52#include <utility>
53#include <vector>
54
55using namespace Aleph;
56
57namespace {
58
59// A single integer argument used to mean capacity, or one element when a
60// variadic constructor won the overload resolution. It is now rejected.
61static_assert(not std::is_constructible_v<Array<int>, int>);
62static_assert(not std::is_constructible_v<Array<int>, size_t>);
63static_assert(not std::is_constructible_v<Array<int>, long>);
64static_assert(not std::is_constructible_v<Array<int>, unsigned>);
65static_assert(not std::is_constructible_v<Array<int>, double>);
66static_assert(not std::is_constructible_v<Array<int>, bool>);
67static_assert(not std::is_constructible_v<Array<size_t>, size_t>);
68static_assert(not std::is_constructible_v<Array<double>, int>);
69static_assert(not std::is_convertible_v<int, Array<int>>);
70static_assert(not std::is_convertible_v<size_t, Array<int>>);
71
72// The other constructors are unaffected.
73static_assert(std::is_default_constructible_v<Array<int>>);
74static_assert(std::is_copy_constructible_v<Array<int>>);
75static_assert(std::is_nothrow_move_constructible_v<Array<int>>);
76static_assert(std::is_constructible_v<Array<int>, size_t, int>);
77static_assert(std::is_constructible_v<Array<int>, std::initializer_list<int>>);
78
79TEST(ArrayCtors, FactoriesAndBracesSayWhatIsMeant)
80{
81 const Array<int> by_default;
82 EXPECT_TRUE(by_default.is_empty());
83 EXPECT_GE(by_default.capacity(), 32u);
84
85 auto reserved = Array<int>::create_reserved(100);
86 EXPECT_TRUE(reserved.is_empty());
87 EXPECT_GE(reserved.capacity(), 100u);
88 reserved.append(5);
89 EXPECT_EQ(reserved[0], 5);
90
91 auto small = Array<int>::create_reserved(0);
92 EXPECT_TRUE(small.is_empty());
93 small.append(1);
94 small.append(2);
95 EXPECT_EQ(small.size(), 2u);
96
97 auto slots = Array<int>::create(3);
98 EXPECT_EQ(slots.size(), 3u);
99
100 const Array<int> item{10};
101 ASSERT_EQ(item.size(), 1u);
102 EXPECT_EQ(item[0], 10);
103
104 // A parenthesized braced list is still an element list.
105 const Array<uint64_t> braced_in_parens({0});
106 ASSERT_EQ(braced_in_parens.size(), 1u);
107 EXPECT_EQ(braced_in_parens[0], 0u);
108
109 const Array<int> repeated(10, 7);
110 ASSERT_EQ(repeated.size(), 10u);
111 for (int value : repeated)
112 EXPECT_EQ(value, 7);
113 const Array<size_t> repeated_size(size_t{3}, size_t{8});
114 ASSERT_EQ(repeated_size.size(), 3u);
115 EXPECT_EQ(repeated_size[2], 8u);
116}
117
118TEST(ArrayCtors, InputRangesSupportSinglePassIteratorsAndSentinels)
119{
120 std::istringstream input("3 5 8");
121 Array<int> streamed{std::istream_iterator<int>(input),
122 std::istream_iterator<int>()};
123 ASSERT_EQ(streamed.size(), 3u);
124 EXPECT_EQ(streamed[2], 8);
125
126 int values[] = {11, 13, 17};
127 Array<int> counted(std::counted_iterator(values, 3), std::default_sentinel);
128 EXPECT_EQ(to_stdvector(counted), (std::vector<int>{11, 13, 17}));
129}
130
131TEST(ArrayCopyMove, MovedFromArraysRemainEmptyAndReusable)
132{
133 Array<int> original = {1, 2, 3};
134 Array<int> moved(std::move(original));
135 EXPECT_TRUE(original.is_empty());
136 EXPECT_EQ(original.begin(), original.end());
137 Array<int> empty_copy(original);
138 EXPECT_TRUE(empty_copy.is_empty());
139 Array<int> moved_twice(std::move(original));
140 EXPECT_TRUE(moved_twice.is_empty());
141 EXPECT_THROW(original[0], std::out_of_range);
142 EXPECT_THROW(original.remove_last(), std::underflow_error);
143
144 original.append(9);
145 original.insert(8);
146 original.reserve(64);
147 original.putn(1);
148 original[2] = 10;
149 EXPECT_EQ(to_stdvector(original), (std::vector<int>{8, 9, 10}));
150 EXPECT_EQ(to_stdvector(moved), (std::vector<int>{1, 2, 3}));
151 moved_twice.insert(42);
152 EXPECT_EQ(moved_twice[0], 42);
153}
154
155TEST(ArrayBasics, DefaultConstructionAndBase)
156{
157 Array<int> arr;
158 EXPECT_TRUE(arr.is_empty());
159 EXPECT_EQ(arr.size(), 0u);
160 EXPECT_THROW(arr.base(), std::underflow_error);
161
162 const Array<int> empty_const;
163 EXPECT_THROW(empty_const.base(), std::underflow_error);
164
165 arr.append(10);
166 arr.append(20);
167 EXPECT_FALSE(arr.is_empty());
168 EXPECT_EQ(arr.size(), 2u);
169 EXPECT_EQ(arr.base(), 10);
170 EXPECT_EQ(arr.get_first(), 10);
171 EXPECT_EQ(arr.get_last(), 20);
172
173 const auto & carr = arr;
174 EXPECT_EQ(carr.get_first(), 10);
175 EXPECT_EQ(carr.get_last(), 20);
176}
177
178TEST(ArrayFunctional, ToArrayPreservesOrder)
179{
180 Array<int> src = {5, 7, 9, 11};
181
182 auto copy = src.to_array();
183
184 ASSERT_EQ(copy.size(), src.size());
185 for (size_t i = 0; i < copy.size(); ++i)
186 EXPECT_EQ(copy(i), src(i));
187
188 // original remains unchanged
189 EXPECT_EQ(src.size(), 4u);
190}
191
192TEST(ArrayModifiers, InsertAppendAndRemove)
193{
194 Array<int> arr;
195 arr.append(1);
196 arr.append(2);
197 arr.insert(-1);
198
199 ASSERT_EQ(arr.size(), 3u);
200 EXPECT_EQ(arr.get_first(), -1);
201 EXPECT_EQ(arr.get_last(), 2);
202
203 EXPECT_EQ(arr.remove_first(), -1);
204 EXPECT_EQ(arr.remove_last(), 2);
205 EXPECT_EQ(arr.size(), 1u);
206 EXPECT_EQ(arr.base(), 1);
207
208 arr.empty();
209 EXPECT_TRUE(arr.is_empty());
210}
211
212TEST(ArrayCopyMove, CopyAndMoveSemantics)
213{
214 Array<int> original = {1, 2, 3, 4};
215 Array<int> copy(original);
216 ASSERT_EQ(copy.size(), original.size());
217 copy[0] = 100;
218 EXPECT_EQ(original[0], 1);
219
220 Array<int> assigned;
221 assigned = copy;
222 EXPECT_EQ(assigned.size(), copy.size());
223 EXPECT_EQ(assigned[0], 100);
224
225 Array<int> moved(std::move(copy));
226 EXPECT_EQ(moved.size(), 4u);
227 EXPECT_EQ(moved[0], 100);
228
229 Array<int> move_assigned;
230 move_assigned.append(999);
231 move_assigned = std::move(moved);
232 EXPECT_EQ(move_assigned.size(), 4u);
233 EXPECT_EQ(move_assigned[3], 4);
234}
235
236TEST(ArrayCapacity, ReservePutnAndSwap)
237{
238 Array<int> arr;
239 const auto initial_cap = arr.capacity();
240 arr.reserve(initial_cap + 50);
241 EXPECT_GE(arr.capacity(), initial_cap + 50);
242
243 arr.putn(5);
244 ASSERT_EQ(arr.size(), 5u);
245 for (size_t i = 0; i < arr.size(); ++i)
246 arr[i] = static_cast<int>(i * 10);
247
248 Array<int> other;
249 other.append(-1);
250 arr.swap(other);
251 EXPECT_EQ(arr.size(), 1u);
252 EXPECT_EQ(arr[0], -1);
253 EXPECT_EQ(other.size(), 5u);
254 EXPECT_EQ(other[2], 20);
255}
256
257TEST(ArrayAccessors, BoundsCheckingAndConstVariants)
258{
260 arr.append("hello");
261 arr.append("world");
262
263 EXPECT_EQ(arr[0], "hello");
264 EXPECT_EQ(arr(1), "world");
265 EXPECT_THROW(arr[2], std::out_of_range);
266
267 const Array<std::string> carr = arr;
268 EXPECT_EQ(carr[0], "hello");
269 EXPECT_EQ(carr(1), "world");
270 EXPECT_THROW(carr[3], std::out_of_range);
271}
272
273TEST(ArrayReverse, ReverseAndReverseInPlaceAliases)
274{
275 Array<int> arr;
276 for (int i = 1; i <= 5; ++i)
277 arr.append(i);
278
279 const std::array<int, 5> ascending = {1, 2, 3, 4, 5};
280 const std::array<int, 5> descending = {5, 4, 3, 2, 1};
281
282 arr.reverse();
283 for (size_t i = 0; i < descending.size(); ++i)
284 EXPECT_EQ(arr[i], descending[i]) << "reverse() should mutate in place";
285
286 const Array<int> &carr = arr;
287 const auto copy = carr.reverse();
288 for (size_t i = 0; i < ascending.size(); ++i)
289 EXPECT_EQ(copy[i], ascending[i]) << "const reverse() should return new copy";
290
291 arr.reverse_in_place();
292 for (size_t i = 0; i < ascending.size(); ++i)
293 EXPECT_EQ(arr[i], ascending[i]) << "reverse_in_place() alias should behave like reverse()";
294
295 const auto copy_rev = carr.rev();
296 for (size_t i = 0; i < descending.size(); ++i)
297 EXPECT_EQ(copy_rev[i], descending[i]) << "const rev() should return reversed copy";
298}
299
300TEST(ArrayReverse, NonConstRevReversesInPlaceAndReturnsReference)
301{
302 Array<int> arr = {1, 2, 3};
303
304 Array<int> &alias = arr.rev();
305
306 EXPECT_EQ(&alias, &arr);
307 EXPECT_EQ(arr[0], 3);
308 EXPECT_EQ(arr[1], 2);
309 EXPECT_EQ(arr[2], 1);
310}
311
312struct MoveOnlyOp
313{
314 bool *called;
315 explicit MoveOnlyOp(bool *c) : called(c) {}
316 MoveOnlyOp(const MoveOnlyOp &) = delete;
317 MoveOnlyOp & operator=(const MoveOnlyOp &) = delete;
318 MoveOnlyOp(MoveOnlyOp &&) = default;
319 MoveOnlyOp & operator=(MoveOnlyOp &&) = default;
320 bool operator()(int)
321 {
322 *called = true;
323 return true;
324 }
325};
326
327TEST(ArrayTraverse, TraversalVariants)
328{
329 Array<int> arr = {1, 2, 3, 4};
330
331 int sum = 0;
332 auto accumulate = [&sum](int value)
333 {
334 sum += value;
335 return true;
336 };
337 EXPECT_TRUE(arr.traverse(accumulate));
338 EXPECT_EQ(sum, 10);
339
340 int visited = 0;
341 auto stop_at_three = [&visited](int value)
342 {
343 ++visited;
344 return value < 3;
345 };
346 EXPECT_FALSE(arr.traverse(stop_at_three));
347 EXPECT_EQ(visited, 3);
348
349 bool called = false;
350 EXPECT_TRUE(arr.traverse(MoveOnlyOp(&called)));
351 EXPECT_TRUE(called);
352}
353
354TEST(ArrayTraverse, ConstElementsCannotBeModified)
355{
356 Array<int> arr = {1, 2, 3};
357 const Array<int> &const_arr = arr;
358
359 auto increment = [](int &value)
360 {
361 ++value;
362 return true;
363 };
364 int sum = 0;
365 auto accumulate_const = [&sum](const int &value)
366 {
367 sum += value;
368 return true;
369 };
370
371 EXPECT_TRUE(arr.traverse(increment));
372 EXPECT_EQ(arr[0], 2);
373 EXPECT_EQ(arr[1], 3);
374 EXPECT_EQ(arr[2], 4);
375 EXPECT_TRUE(const_arr.traverse(accumulate_const));
376 EXPECT_EQ(sum, 9);
377 EXPECT_TRUE(const_arr.traverse([](const int &value) { return value > 0; }));
378 EXPECT_EQ(arr[0], 2);
379 EXPECT_EQ(arr[1], 3);
380 EXPECT_EQ(arr[2], 4);
381}
382
383template <class Container, class Operation>
384concept CanTraverse = requires(Container &container, Operation &operation)
385{
386 container.traverse(operation);
387};
388
389struct MutableArrayOperation
390{
391 bool operator () (int &) const { return true; }
392};
393
394static_assert(CanTraverse<Array<int>, MutableArrayOperation>);
395static_assert(not CanTraverse<const Array<int>, MutableArrayOperation>);
396
397TEST(ArrayAlgorithms, InPlaceUnique)
398{
399 Array<int> arr = {1, 2, 1, 3, 2, 4, 4};
400
401 in_place_unique(arr);
402
403 ASSERT_EQ(arr.size(), 4u);
404 EXPECT_EQ(arr[0], 1);
405 EXPECT_EQ(arr[1], 2);
406 EXPECT_EQ(arr[2], 3);
407 EXPECT_EQ(arr[3], 4);
408}
409
410TEST(ArrayIterators, IteratorCoversAllElements)
411{
412 Array<int> arr = {0, 1, 2, 3};
413 Array<int>::Iterator it(arr);
414
415 int expected = 0;
416 for (; it.has_curr(); it.next())
417 {
418 EXPECT_EQ(it.get_curr(), expected);
419 ++expected;
420 }
421 EXPECT_EQ(expected, 4);
422}
423
424TEST(ArrayUtilities, BuildArrayAndStdVector)
425{
426 auto arr = build_array<int>(5, 4, 3, 2, 1);
427 EXPECT_EQ(arr.size(), 5u);
428 EXPECT_EQ(arr[0], 5);
429 EXPECT_EQ(arr[4], 1);
430
431 const auto vec = to_stdvector(arr);
432 ASSERT_EQ(vec.size(), arr.size());
433 for (size_t i = 0; i < vec.size(); ++i)
434 EXPECT_EQ(vec[i], arr(i));
435}
436
437namespace
438{
439struct DefaultInit
440{
441 int v;
442 DefaultInit() : v(123) {}
443 explicit DefaultInit(int x) : v(x) {}
444 bool operator==(const DefaultInit &o) const { return v == o.v; }
445};
446}
447
448TEST(ArrayCtors, ValueConstructorInitializesAllElements_POD)
449{
450 const size_t n = 8;
451 const int value = 42;
452 Array<int> arr(n, value);
453 ASSERT_EQ(arr.size(), n);
454 for (size_t i = 0; i < n; ++i)
455 EXPECT_EQ(arr[i], value);
456}
457
458TEST(ArrayCtors, ValueConstructorInitializesAllElements_NonPOD)
459{
460 const size_t n = 6;
461 const std::string value = "abc";
463 ASSERT_EQ(arr.size(), n);
464 for (size_t i = 0; i < n; ++i)
465 EXPECT_EQ(arr[i], value);
466}
467
468TEST(ArrayFactory, CreateYieldsLogicalSizeAndRequiresAssignBeforeReadForPOD)
469{
470 const size_t n = 10;
471 auto arr = Array<int>::create(n);
472 static_assert(std::is_trivially_default_constructible_v<int>);
473 ASSERT_EQ(arr.size(), n);
474
475 for (size_t i = 0; i < arr.size(); ++i)
476 arr[i] = static_cast<int>(i * 3);
477 for (size_t i = 0; i < arr.size(); ++i)
478 EXPECT_EQ(arr[i], static_cast<int>(i * 3));
479}
480
481TEST(ArrayFactory, CreateDefaultConstructsClassTypes)
482{
483 const size_t n = 7;
484 auto arr = Array<DefaultInit>::create(n);
485 static_assert(!std::is_trivially_default_constructible_v<DefaultInit>);
486 ASSERT_EQ(arr.size(), n);
487 for (size_t i = 0; i < n; ++i)
488 EXPECT_EQ(arr[i].v, 123);
489
490 arr[0] = DefaultInit(7);
491 EXPECT_EQ(arr[0].v, 7);
492}
493
494TEST(ArrayFactory, PutnAndAppendPopulateBothPODAndNonPOD)
495{
496 Array<int> pod;
497 pod.putn(3);
498 pod[0] = 1;
499 pod[1] = 2;
500 pod[2] = 3;
501 pod.append(4);
502 ASSERT_EQ(pod.size(), 4u);
503 EXPECT_EQ(pod[3], 4);
504
505 Array<std::string> nonpod;
506 nonpod.putn(2);
507 nonpod[0] = "x";
508 nonpod[1] = "y";
509 nonpod.append("z");
510 ASSERT_EQ(nonpod.size(), 3u);
511 EXPECT_EQ(nonpod[2], "z");
512}
513
514TEST(ArraySearch, Contains)
515{
516 Array<int> arr = {10, 20, 30, 40};
517 EXPECT_TRUE(arr.contains(20));
518 EXPECT_TRUE(arr.contains(40));
519 EXPECT_FALSE(arr.contains(50));
520
521 EXPECT_TRUE(arr.contains_if([](int x) { return x > 25; }));
522 EXPECT_FALSE(arr.contains_if([](int x) { return x > 100; }));
523}
524
525TEST(ArraySearch, ContainsEmpty)
526{
527 Array<int> empty;
528 EXPECT_FALSE(empty.contains(10));
529 EXPECT_FALSE(empty.contains_if([](int) { return true; }));
530}
531
532
534template <class T>
535void expect_items(const Array<T> &a, std::initializer_list<T> expected)
536{
537 ASSERT_EQ(a.size(), expected.size());
538 size_t i = 0;
539 for (const T &item : expected)
540 {
541 EXPECT_EQ(a[i], item) << "at position " << i;
542 ++i;
543 }
544}
545
547std::string long_string(const char c)
548{
549 return std::string(40, c);
550}
551
552// The argument of append() and insert() may be an element of the array
553// itself. Growth used to free it before reading it, and the gap opened by
554// insert() used to overwrite it.
555
556TEST(ArraySelfInsertion, AppendOwnElement)
557{
558 auto a = Array<int>::create_reserved(4);
559 for (int i : {10, 20, 30, 40})
560 a.append(i);
561 ASSERT_EQ(a.size(), a.capacity());
562
563 EXPECT_EQ(a.append(a[0]), 10);
564 expect_items(a, {10, 20, 30, 40, 10});
565 EXPECT_EQ(a.append(a[3]), 40); // with free capacity
566 expect_items(a, {10, 20, 30, 40, 10, 40});
567}
568
569TEST(ArraySelfInsertion, AppendOwnMovedElement)
570{
572 for (char c : {'a', 'b', 'c', 'd'})
573 a.append(long_string(c));
574 ASSERT_EQ(a.size(), a.capacity());
575
576 EXPECT_EQ(a.append(std::move(a[0])), long_string('a'));
577 ASSERT_EQ(a.size(), 5u);
578 EXPECT_EQ(a[3], long_string('d'));
579 EXPECT_EQ(a[4], long_string('a'));
580}
581
582TEST(ArraySelfInsertion, InsertOwnElement)
583{
584 auto a = Array<int>::create_reserved(4);
585 for (int i : {10, 20, 30})
586 a.append(i);
587
588 EXPECT_EQ(a.insert(a[1]), 20);
589 expect_items(a, {20, 10, 20, 30});
590 ASSERT_EQ(a.size(), a.capacity());
591 EXPECT_EQ(a.insert(a[3]), 30);
592 expect_items(a, {30, 20, 10, 20, 30});
593}
594
595TEST(ArraySelfInsertion, InsertOwnMovedElement)
596{
598 for (char c : {'a', 'b', 'c'})
599 a.append(long_string(c));
600
601 EXPECT_EQ(a.insert(std::move(a[2])), long_string('c'));
602 EXPECT_EQ(a[1], long_string('a'));
603 ASSERT_EQ(a.size(), a.capacity());
604 EXPECT_EQ(a.insert(std::move(a[2])), long_string('b'));
605 ASSERT_EQ(a.size(), 5u);
606 EXPECT_EQ(a[1], long_string('c'));
607 EXPECT_EQ(a[2], long_string('a'));
608}
609
610
611// get_first() and get_last() were noexcept, so the underflow_error thrown
612// on an empty array called std::terminate.
613static_assert(not noexcept(std::declval<Array<int> &>().get_first()));
614static_assert(not noexcept(std::declval<const Array<int> &>().get_first()));
615static_assert(not noexcept(std::declval<Array<int> &>().get_last()));
616static_assert(not noexcept(std::declval<const Array<int> &>().get_last()));
617
618TEST(ArrayAccess, GetFirstAndGetLastOfEmptyArrayThrow)
619{
620 Array<int> a;
621 const Array<int> &ca = a;
622 EXPECT_THROW((void) a.get_first(), std::underflow_error);
623 EXPECT_THROW((void) ca.get_first(), std::underflow_error);
624 EXPECT_THROW((void) a.get_last(), std::underflow_error);
625 EXPECT_THROW((void) ca.get_last(), std::underflow_error);
626
627 a.append(10);
628 a.append(20);
629 EXPECT_EQ(ca.get_first(), 10);
630 EXPECT_EQ(ca.get_last(), 20);
631 a.get_first() = 1;
632 a.get_last() = 2;
633 expect_items(a, {1, 2});
634}
635
636
638struct Copy_Move_Counted
639{
640 static inline size_t copies = 0;
641 static inline size_t moves = 0;
642 static inline size_t move_constructions = 0;
643 int value = 0;
644
645 Copy_Move_Counted() = default;
646 explicit Copy_Move_Counted(int v) : value(v) {}
647 Copy_Move_Counted(const Copy_Move_Counted & o) : value(o.value) { ++copies; }
648 Copy_Move_Counted(Copy_Move_Counted && o) noexcept : value(o.value)
649 {
650 ++moves;
651 ++move_constructions;
652 }
653 Copy_Move_Counted & operator = (const Copy_Move_Counted & o)
654 {
655 value = o.value;
656 ++copies;
657 return *this;
658 }
659 Copy_Move_Counted & operator = (Copy_Move_Counted && o) noexcept
660 {
661 value = o.value;
662 ++moves;
663 return *this;
664 }
665};
666
667TEST(ArrayAppend, ConstantConcatenationCopiesEachItemOnce)
668{
669 // append(a) const returned ret.append(a), a reference, so the whole
670 // result was copied a second time.
672 for (int i = 0; i < 5; ++i)
673 a.append(Copy_Move_Counted(i));
674 for (int i = 0; i < 3; ++i)
675 b.append(Copy_Move_Counted(10 + i));
676
677 Copy_Move_Counted::copies = 0;
678 const Array<Copy_Move_Counted> & ca = a;
679 const Array<Copy_Move_Counted> joined = ca.append(b);
680 EXPECT_EQ(joined.size(), 8u);
681 EXPECT_EQ(joined[7].value, 12);
682 EXPECT_LE(Copy_Move_Counted::copies, 5u + 3u); // the 5 items of a and the 3 of b
683}
684
685TEST(ArrayRemoveFirst, MovesEachItemOnce)
686{
687 // remove_first() moved the vacated last slot out once more, into a
688 // temporary that was discarded: one move construction too many.
690 for (int i = 0; i < 20; ++i) // enough items for the array not to contract
691 a.append(Copy_Move_Counted(i));
692 const size_t capacity = a.capacity();
693 Copy_Move_Counted::moves = 0;
694 Copy_Move_Counted::move_constructions = 0;
695 const Copy_Move_Counted first = a.remove_first();
696 ASSERT_EQ(a.capacity(), capacity);
697 EXPECT_EQ(first.value, 0);
698 EXPECT_EQ(a.size(), 19u);
699 EXPECT_EQ(a[0].value, 1);
700 EXPECT_EQ(a[18].value, 19);
701 EXPECT_EQ(Copy_Move_Counted::move_constructions, 1u); // only the returned item
702 EXPECT_EQ(Copy_Move_Counted::moves, 1u + 19u); // and the 19 shifts
703}
704
705} // namespace
bool operator==(const Time &l, const Time &r)
Definition ah-time.H:134
Deduplicate sequential Aleph containers in-place.
static string long_string(const char c)
A string too long for the small-string buffer.
size_t size_t int32_t value
Definition ca-c-api.h:116
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
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
void empty() noexcept
Empties the container.
Definition tpl_array.H:426
constexpr bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_array.H:444
T & base()
Return a reference to the first element of array.
Definition tpl_array.H:408
T & insert(const T &data)
insert a copy of data at the beginning of the array.
Definition tpl_array.H:368
Array & rev()
Reverse this array in place.
Definition tpl_array.H:585
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:313
Array & reverse_in_place()
Alias for reverse().
Definition tpl_array.H:572
T & get_last()
Return a modifiable reference to the last element.
Definition tpl_array.H:483
bool traverse(Op &op)
Traverse mutable elements from first to last.
Definition tpl_array.H:617
static Array create(const size_t n)
Create an array with n logical elements.
Definition tpl_array.H:245
Array & reverse()
Reverse the order of items in this array, in place.
Definition tpl_array.H:540
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
constexpr size_t capacity() const noexcept
Return the internal capacity.
Definition tpl_array.H:456
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:402
void putn(const size_t n)
Reserve n additional logical slots in the array without value-initializing them.
Definition tpl_array.H:392
T & get_first()
Return a modifiable reference to the first element.
Definition tpl_array.H:465
Array to_array() const
Copy to Aleph::Array (requires copyable elements).
Definition tpl_array.H:690
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
Minimal std::expected-style result type for C++20.
bool contains(const Type &item) const
Test if an item is present in the container using equality.
Definition ah-dry.H:1074
bool contains_if(Operation &&operation) const noexcept(const_search_is_noexcept< Operation >())
Test if an item satisfying a criterion is present in the container.
Definition ah-dry.H:1060
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
#define TEST(name)
Main namespace for Aleph-w library functions.
void in_place_unique(Container &c, Compare cmp={})
Remove duplicates in-place preserving first occurrence order.
Definition ah-unique.H:74
DynList< T > repeated(const Container< T > &c)
Return elements that appear more than once in the container.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
T accumulate(Itor beg, Itor end, T initValue)
Accumulate values in a range.
Definition ahAlgo.H:1493
std::vector< typename Container::Item_Type > to_stdvector(const Container &c)
Definition tpl_array.H:729
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Iterator on the items of an array.
Definition tpl_array.H:701
Dynamic array container with automatic resizing.