38#include <gtest/gtest.h>
56TEST(DynArrayBasics, construction_and_size)
60 EXPECT_EQ(arr.
size(), 0u);
63 EXPECT_EQ(arr.
size(), 1u);
65 EXPECT_EQ(arr.
access(0), 42);
68 EXPECT_EQ(arr.
size(), 2u);
69 EXPECT_EQ(arr.
access(1), 7);
72TEST(DynArrayFunctional, ToArrayPreservesOrder)
75 for (
int v : {3, 6, 9})
81 for (
size_t i = 0; i <
copy.size(); ++i)
82 EXPECT_EQ(
copy(i), src(i));
85TEST(DynArrayBasics, default_values_and_touch)
91 ASSERT_EQ(arr.
size(), 4u);
92 for (
size_t i = 0; i < arr.
size(); ++i)
93 EXPECT_EQ(arr.
access(i), 123);
96 EXPECT_EQ(arr.
access(2), 77);
98 auto & ref = arr.
touch(10);
100 EXPECT_EQ(arr.
size(), 11u);
101 EXPECT_EQ(arr.
access(10), 99);
104TEST(DynArrayAlgorithms, InPlaceUnique)
117 ASSERT_EQ(arr.
size(), 4u);
118 EXPECT_EQ(arr.
access(0), 1);
119 EXPECT_EQ(arr.
access(1), 2);
120 EXPECT_EQ(arr.
access(2), 3);
121 EXPECT_EQ(arr.
access(3), 4);
124TEST(DynArrayErrors, stack_operations_on_empty)
128 EXPECT_THROW(arr.
pop(), std::underflow_error);
129 EXPECT_THROW(arr.
top(), std::underflow_error);
130 EXPECT_THROW(arr.
get_first(), std::underflow_error);
131 EXPECT_THROW(arr.
get_last(), std::underflow_error);
134 EXPECT_THROW(arr.
remove(dummy), std::underflow_error);
137TEST(DynArrayReserve, invalid_range_throws)
140 EXPECT_THROW(arr.
reserve(5, 4), std::domain_error);
143TEST(DynArrayReserve, reserve_and_access)
147 ASSERT_EQ(arr.
size(), 5u);
148 for (
size_t i = 0; i < arr.
size(); ++i)
149 arr.
access(i) =
static_cast<int>(i * 2);
150 for (
size_t i = 0; i < arr.
size(); ++i)
151 EXPECT_EQ(arr.
access(i),
static_cast<int>(i * 2));
159TEST(DynArrayReserve, an_index_past_the_limit_is_refused)
161 constexpr size_t max = numeric_limits<size_t>::max();
163 for (
const size_t i : {limit, limit + 1, size_t(1) << 61,
max - 1,
max})
169 EXPECT_THROW(arr.
reserve(i ==
max ?
max : i + 1), length_error);
170 EXPECT_THROW(arr.
reserve(1, i), length_error);
171 EXPECT_THROW((
void) arr.
touch(i), length_error);
172 EXPECT_THROW(arr[i] = 7, length_error);
173 EXPECT_THROW(arr.
adjust(i ==
max ?
max : i + 1), length_error);
174 ASSERT_EQ(arr.
size(), 2u);
175 EXPECT_EQ(arr(0), 5);
176 EXPECT_EQ(arr(1), 6);
179 EXPECT_EQ(arr.
size(), 3u);
180 EXPECT_EQ(arr(2), 7);
182 EXPECT_THROW(
DynArray<int> too_big(limit + 1), length_error);
185TEST(DynArrayIterator, get_it_position)
188 for (
int i = 0; i < 6; ++i)
192 ASSERT_TRUE(it.has_curr());
193 EXPECT_EQ(it.get_curr(), 3);
195 EXPECT_EQ(it.get_curr(), 4);
197 const auto & carr = arr;
198 auto cit = carr.
get_it(5);
199 EXPECT_EQ(cit.get_curr(), 5);
200 EXPECT_THROW(carr.get_it(6), std::out_of_range);
203TEST(DynArrayIterator, IsLastRequiresCurrentItem)
206 EXPECT_FALSE(singular.
is_last());
209 EXPECT_FALSE(singular.
is_last());
214 EXPECT_FALSE(it.has_curr());
215 EXPECT_FALSE(it.is_last());
219 EXPECT_TRUE(it.has_curr());
220 EXPECT_TRUE(it.is_last());
222 EXPECT_FALSE(it.is_last());
225TEST(DynArrayIterator, CheckedAccessRejectsInvalidPositions)
228 EXPECT_THROW(singular.
get_curr(), std::overflow_error);
229 EXPECT_THROW(singular.
next(), std::overflow_error);
231 EXPECT_THROW(singular.
get_curr(), std::underflow_error);
232 EXPECT_THROW(singular.
next(), std::overflow_error);
236 EXPECT_THROW(it.get_curr(), std::overflow_error);
237 EXPECT_THROW(it.next(), std::overflow_error);
239 EXPECT_THROW(it.get_curr(), std::underflow_error);
243 EXPECT_EQ(it.get_curr(), 42);
245 EXPECT_THROW(it.get_curr(), std::overflow_error);
246 EXPECT_THROW(it.next(), std::overflow_error);
248 EXPECT_THROW(it.get_curr(), std::underflow_error);
250 EXPECT_EQ(it.get_curr(), 42);
253TEST(DynArrayReserve, adjust_and_cut)
257 EXPECT_EQ(arr.
size(), 10u);
259 EXPECT_EQ(arr.
size(), 3u);
265 EXPECT_EQ(arr.
size(), 2u);
269 EXPECT_EQ(arr.
size(), 0u);
276TEST(DynArrayReserve, reserve_invalid_order)
279 EXPECT_THROW(arr.
reserve(4, 3), std::domain_error);
280 EXPECT_EQ(arr.
size(), 0u);
283TEST(DynArrayReserve, reserve_touch_consistency)
287 ASSERT_EQ(arr.
size(), 6u);
289 EXPECT_EQ(arr.
size(), 21u);
291 EXPECT_EQ(arr.
size(), 6u);
297struct Fails_After_Budget
299 static inline long budget = -1;
305 throw std::bad_alloc();
314 ~Budget_Guard() { Fails_After_Budget::budget = -1; }
317TEST(DynArrayReserve, reserve_fills_missing_blocks_of_existing_segment)
325 EXPECT_EQ(arr.
size(), 16u);
326 for (
size_t i = 0; i <= 15; ++i)
327 EXPECT_TRUE(arr.
exist(i)) <<
"entry " << i;
328 EXPECT_EQ(arr.
access(0), 7);
331TEST(DynArrayReserve, failed_reserve_releases_only_what_it_allocated)
337 ASSERT_EQ(block, 4u);
344 Fails_After_Budget::budget =
static_cast<long>(4 *
block);
345 EXPECT_THROW(arr.
reserve(0, 47), std::bad_alloc);
346 Fails_After_Budget::budget = -1;
349 EXPECT_EQ(arr.
size(), 1u);
350 for (
size_t i = 0; i <
block; ++i)
351 EXPECT_TRUE(arr.
exist(i)) <<
"pre-existing entry " << i;
352 for (
size_t i = block; i <= 47; ++i)
353 EXPECT_FALSE(arr.
exist(i)) <<
"entry " << i <<
" was not rolled back";
357 EXPECT_EQ(arr.
size(), 48u);
358 for (
size_t i = 0; i <= 47; ++i)
359 EXPECT_TRUE(arr.
exist(i));
362TEST(DynArrayReserve, failure_on_first_block_releases_new_segment)
367 Fails_After_Budget::budget = 0;
368 EXPECT_THROW(arr.
reserve(20, 40), std::bad_alloc);
369 Fails_After_Budget::budget = -1;
372 EXPECT_EQ(arr.
size(), 0u);
373 for (
size_t i = 0; i <= 47; ++i)
374 EXPECT_FALSE(arr.
exist(i));
377TEST(DynArrayReserve, reserve_of_existing_range_constructs_nothing)
384 Fails_After_Budget::budget = 0;
385 EXPECT_NO_THROW(arr.
reserve(0, 47));
386 EXPECT_NO_THROW(arr.
reserve(5, 30));
388 EXPECT_EQ(arr.
size(), 48u);
391TEST(DynArrayQueueStack, push_pop_fifo_lifo)
394 for (
int i = 0; i < 5; ++i)
402 EXPECT_EQ(arr.
pop(), 4);
403 EXPECT_EQ(arr.
size(), 5u);
408static_assert(std::is_constructible_v<DynArray<int>,
size_t>);
409static_assert(not std::is_convertible_v<int, DynArray<int>>);
410static_assert(not std::is_convertible_v<size_t, DynArray<int>>);
411static_assert(std::is_constructible_v<DynArray<int>, size_t,
int>);
414struct With_Dyn_Array_Member
420TEST(DynArrayCtors, DefaultConstructorIsNotExplicit)
425 const With_Dyn_Array_Member aggregate{7};
426 EXPECT_EQ(aggregate.key, 7);
427 EXPECT_TRUE(aggregate.values.is_empty());
430TEST(DynArrayCtors, LazySizeIsCoveredByTheDirectory)
432 const size_t n =
size_t{1} << 30;
434 EXPECT_EQ(lazy.size(), n);
435 EXPECT_GE(lazy.max_size(), lazy.size());
436 EXPECT_EQ(lazy.get_num_blocks(), 0u);
437 EXPECT_FALSE(lazy.exist(n - 1));
443TEST(DynArrayCtors, CountValueAllocatesEveryEntry)
446 ASSERT_EQ(sevens.size(), 5u);
448 sevens.for_each([&sum] (
int x) {
sum += x; });
450 for (
size_t i = 0; i < sevens.size(); ++i)
452 EXPECT_TRUE(sevens.exist(i));
453 EXPECT_EQ(sevens.access(i), 7);
457 ASSERT_EQ(same_type.size(), 3u);
458 EXPECT_EQ(same_type.access(2), 8u);
461 ASSERT_EQ(strings.size(), 2u);
462 EXPECT_EQ(strings.access(1),
"ab");
465 EXPECT_TRUE(
none.is_empty());
468TEST(DynArrayCopy, CopyKeepsTheLazySizeAndItsOwnDefaultValue)
472 EXPECT_EQ(lazy_copy.size(), 10u);
473 EXPECT_EQ(lazy_copy.get_num_blocks(), 0u);
480 EXPECT_EQ(
copy.access(2), 5);
481 EXPECT_EQ(
copy.touch(5000), 42);
484TEST(DynArrayCopy, CopyAssignmentReplacesEverything)
487 for (
int i = 1; i <= 10; ++i)
493 EXPECT_EQ(dst.
size(), 10u);
494 EXPECT_FALSE(dst.
exist(0));
495 EXPECT_EQ(dst.
touch(5000), 42);
499 EXPECT_EQ(dst.
size(), 5001u);
500 EXPECT_EQ(dst.
access(5000), 42);
507 static inline long live = 0;
508 static inline long budget = -1;
511 Copy_Bomb() noexcept { ++live; }
512 Copy_Bomb(
const Copy_Bomb & other) noexcept :
value(other.value) { ++live; }
513 Copy_Bomb(Copy_Bomb && other) noexcept :
value(other.value) { ++live; }
514 ~Copy_Bomb() { --live; }
516 Copy_Bomb & operator = (
const Copy_Bomb & other)
518 if (budget >= 0
and budget-- == 0)
519 throw std::runtime_error(
"Copy_Bomb: copy budget exhausted");
524 Copy_Bomb & operator = (Copy_Bomb && other)
noexcept
534TEST(DynArrayCopy, ThrowingCopyLeaksNothing)
537 src.
touch(10).value = 1;
538 src.
touch(9000).value = 2;
539 src.
touch(1100000).value = 3;
542 const long live_before = Copy_Bomb::live;
550 Copy_Bomb::budget = at;
552 <<
"copy did not throw at assignment " << at;
553 Copy_Bomb::budget = -1;
554 EXPECT_EQ(Copy_Bomb::live, live_before)
555 <<
"elements leaked when throwing at assignment " << at;
560 dst.
touch(3).value = 7;
561 const long live_with_dst = Copy_Bomb::live;
562 Copy_Bomb::budget =
block + 3;
563 EXPECT_THROW(dst = src, std::runtime_error);
564 Copy_Bomb::budget = -1;
565 EXPECT_EQ(Copy_Bomb::live, live_with_dst);
566 ASSERT_EQ(dst.
size(), 4u);
567 EXPECT_EQ(dst.
read(3).value, 7);
575TEST(DynArrayBlocks, ABlockThatCannotBeFilledLeavesTheArrayAsItWas)
579 ~Unlimited() { Copy_Bomb::budget = -1; }
583 const Writer writers[] = {
593 for (
const Writer write : writers)
597 for (
const size_t first : {
size_t{0},
size_t{4},
size_t{16}})
598 for (
long at = 0; at < 4; ++at)
600 SCOPED_TRACE(testing::Message() <<
"writer " <<
w <<
", entry " << first <<
", copy " << at);
606 const long live = Copy_Bomb::live;
608 Copy_Bomb::budget = at;
609 EXPECT_THROW(write(a, first), std::runtime_error);
610 Copy_Bomb::budget = -1;
613 EXPECT_EQ(Copy_Bomb::live, live);
614 EXPECT_FALSE(a.
exist(first));
618 for (
size_t i = first + 1; i < first + 4; ++i)
619 EXPECT_EQ(a.
access(i).value, 7) <<
"entry " << i;
627static_assert(std::is_same_v<
decltype(std::declval<const DynArray<int> &>()[0]),
630TEST(DynArrayLazyReads, ReadingUnwrittenEntriesAllocatesNothing)
635 EXPECT_EQ(lazy.read(3), 0);
636 EXPECT_EQ(view[3], 0);
639 lazy.for_each([&sum] (
int x) {
sum += x; });
640 for (
const int x : view)
645 EXPECT_EQ(lazy.to_array().size(), 10u);
646 EXPECT_EQ(lazy.get_num_blocks(), 0u);
648 EXPECT_THROW((
void) lazy.read(10), std::out_of_range);
649 EXPECT_THROW((
void) view[10], std::out_of_range);
652TEST(DynArrayLazyReads, ReadsUseTheDefaultValue)
655 lazy.set_default_initial_value(7);
656 EXPECT_EQ(lazy.read(4), 7);
659 lazy.for_each([&sum] (
int x) {
sum += x; });
661 EXPECT_EQ(lazy.get_num_blocks(), 0u);
664TEST(DynArrayLazyReads, TraversalVisitsGapsBetweenWrittenBlocks)
667 sparse.
touch(10000) = 1;
668 ASSERT_EQ(sparse.
size(), 10001u);
672 sparse.
for_each([&] (
int x) { ++visited;
sum += x; });
673 EXPECT_EQ(visited, 10001u);
677TEST(DynArrayLazyReads, ModifiableAccessAllocatesTheBlock)
681 EXPECT_EQ(lazy.read(3), 1);
682 EXPECT_EQ(lazy.get_num_blocks(), 1u);
686 EXPECT_EQ(other.read(0), 0);
689 EXPECT_EQ(queue.get_last(), 0);
690 queue.get_first() = 4;
691 EXPECT_EQ(queue.read(0), 4);
692 EXPECT_EQ(queue.top(), 0);
695TEST(DynArrayLazyReads, WritesThroughModifiableTraversalsReachTheArray)
702 lazy.traverse([] (
int & x) { x = 5;
return true; });
703 EXPECT_EQ(lazy.read(0), 5);
704 EXPECT_EQ(lazy.read(2), 5);
705 EXPECT_EQ(lazy.get_num_blocks(), 1u);
708 for (
auto & x : ranged)
710 EXPECT_EQ(ranged.read(1), 6);
713 mutated.mutable_for_each([] (
int & x) { x = 7; });
714 EXPECT_EQ(mutated.read(2), 7);
717 for (
auto it = iterated.get_it(); it.has_curr(); it.next_ne())
718 it.get_curr_ne() = 8;
719 EXPECT_EQ(iterated.read(0), 8);
722 sliced.each(1, 2, [] (
int & x) { x = 9; });
723 EXPECT_EQ(sliced.read(0), 0);
724 EXPECT_EQ(sliced.read(1), 9);
725 EXPECT_EQ(sliced.read(3), 9);
728 written.traverse([] (
int & x) { x = 5;
return true; });
729 EXPECT_EQ(written.read(2), 5);
732TEST(DynArrayLazyReads, ConstantTraversalsNeverAllocate)
735 lazy.set_default_initial_value(4);
739 clazy.
traverse([&sum] (
const int & x) {
sum += x;
return true; });
740 for (
const auto & x : clazy)
742 for (
auto it = clazy.
get_it(); it.has_curr(); it.next_ne())
743 sum += it.get_curr_ne();
744 lazy.for_each([&sum] (
int x) {
sum += x; });
745 clazy.
each(0, 1, [&sum] (
int x) {
sum += x; });
746 EXPECT_EQ(sum, 4 * 3 * 5);
747 EXPECT_EQ(lazy.get_num_blocks(), 0u);
750TEST(DynArrayLazyReads, StackAndQueueOperationsOnLazyEntries)
754 ASSERT_EQ(lazy.size(), 4u);
755 EXPECT_EQ(lazy.read(0), 9);
756 EXPECT_EQ(lazy.read(3), 0);
759 EXPECT_EQ(popped.pop(), 0);
760 EXPECT_EQ(popped.size(), 2u);
763TEST(DynArrayLazyReads, CutDropsTheEntriesItRemoves)
766 for (
int i = 1; i <= 10; ++i)
772 int expected[] = {1, 2, 3, 4, 5, 0, 0, 0, 0, 99};
773 ASSERT_EQ(arr.
size(), 10u);
774 for (
size_t i = 0; i < arr.
size(); ++i)
775 EXPECT_EQ(arr.
read(i),
expected[i]) <<
"at index " << i;
778TEST(DynArrayMovedFrom, StaysEmptyAndReusable)
783 ASSERT_EQ(target.read(0), 1);
785 EXPECT_TRUE(source.is_empty());
786 EXPECT_FALSE(source.exist(0));
787 EXPECT_EQ(source.test(0),
nullptr);
791 source.for_each([&visited] (
int) { ++visited; });
792 EXPECT_EQ(visited, 0u);
794 EXPECT_THROW((
void) source.read(0), std::out_of_range);
797 EXPECT_EQ(source.read(0), 2);
801 EXPECT_EQ(target.size(), 5u);
802 EXPECT_EQ(target.read(4), 7);
806 EXPECT_EQ(by_index.size(), 3u);
807 by_index = by_reserve;
808 EXPECT_EQ(by_index.read(0), 1);
816void expect_an_array_that_never_wrote(
DynArray<int> & a,
const size_t dim,
const char * how)
823 EXPECT_FALSE(c.
exist(0));
824 EXPECT_EQ(c.
test(0),
nullptr);
825 EXPECT_THROW((
void) c.
read(
dim), std::out_of_range);
828 EXPECT_EQ(c.
read(0), 0);
833 c.
for_each([&visited] (
const int & item)
838 EXPECT_EQ(visited,
dim);
839 EXPECT_EQ(c.
get_num_blocks(), 0u) <<
"a reader allocated a block";
843 EXPECT_EQ(
copy.get_num_blocks(), 0u);
850 EXPECT_EQ(c.
read(0), 0);
851 EXPECT_EQ(
copy.size(),
dim) <<
"the copy shares nothing with the array";
854TEST(DynArrayNeverWritten, EveryWayOfBuildingOneLeavesItUsable)
857 expect_an_array_that_never_wrote(by_default, 0,
"built by default");
860 expect_an_array_that_never_wrote(empty, 0,
"built with size 0");
863 expect_an_array_that_never_wrote(lazy, 5,
"built with a size");
867 const size_t big = by_default.
max_size() + 3;
869 EXPECT_EQ(large.size(), big);
870 EXPECT_GE(large.max_size(), big);
871 EXPECT_EQ(std::as_const(large).read(big - 1), 0);
873 EXPECT_EQ(large.get_num_blocks(), 0u);
874 large.touch(big - 1) = 9;
875 EXPECT_EQ(std::as_const(large).read(big - 1), 9);
876 EXPECT_EQ(std::as_const(large).read(0), 0);
877 EXPECT_EQ(large.get_num_blocks(), 1u);
880 expect_an_array_that_never_wrote(with_geometry, 0,
"built with a geometry");
884 expect_an_array_that_never_wrote(copied, 5,
"copied from one that never wrote");
889 expect_an_array_that_never_wrote(assigned, 5,
"assigned one that never wrote");
893 expect_an_array_that_never_wrote(taken, 5,
"moved from one that never wrote");
894 expect_an_array_that_never_wrote(moved_from, 0,
"that was moved from without having written");
898 expect_an_array_that_never_wrote(cut_down, 2,
"cut without having written");
901 expect_an_array_that_never_wrote(emptied, 0,
"emptied without having written");
905TEST(DynArrayNeverWritten, EachWriterMakesTheDirectory)
910 EXPECT_EQ(by_append.
size(), 2u);
911 EXPECT_EQ(by_append.
read(1), 2);
915 EXPECT_EQ(by_index.
size(), 4u);
916 EXPECT_EQ(by_index.
read(3), 4);
917 EXPECT_EQ(by_index.
read(0), 0);
921 EXPECT_EQ(by_reserve.
size(), 10u);
922 EXPECT_TRUE(by_reserve.
exist(9));
924 EXPECT_EQ(by_reserve.
read(9), 5);
928 EXPECT_EQ(by_push.
top(), 6);
929 EXPECT_EQ(by_push.
pop(), 6);
934 const size_t index = far.
max_size() + 7;
935 far.
touch(index) = 8;
936 EXPECT_EQ(far.
size(), index + 1);
937 EXPECT_EQ(far.
read(index), 8);
938 EXPECT_EQ(far.
read(0), 0);
943 EXPECT_EQ(std::as_const(with_default).read(2), 42);
944 with_default.touch(1) = 1;
945 EXPECT_EQ(with_default.read(0), 42);
946 EXPECT_EQ(with_default.read(1), 1);
951TEST(DynArrayNeverWritten, SwapsWithOneThatWrote)
954 for (
int i = 0; i < 10; ++i)
958 EXPECT_EQ(never.size(), 10u);
959 EXPECT_EQ(never.read(9), 9);
960 expect_an_array_that_never_wrote(written, 3,
"after the swap");
964 expect_an_array_that_never_wrote(a, 2,
"of two that never wrote, the first");
965 expect_an_array_that_never_wrote(b, 0,
"of two that never wrote, the second");
971 return std::string(40, c);
985 throw std::bad_alloc();
997TEST(DynArrayInsert, OwnEntryKeepsItsValue)
1000 for (
int i : {10, 20, 30})
1004 ASSERT_EQ(a.
size(), 4u);
1005 const int expected[] = {20, 10, 20, 30};
1006 for (
size_t i = 0; i < 4; ++i)
1010TEST(DynArrayInsert, OwnMovedEntryKeepsItsValue)
1013 for (
char c : {
'a',
'b',
'c'})
1017 ASSERT_EQ(a.
size(), 4u);
1022TEST(DynArrayInsert, FailedAllocationLeavesTheArrayAndTheArgumentUntouched)
1025 for (
char c : {
'a',
'b',
'c',
'd'})
1031 EXPECT_THROW(a.
insert(x), std::bad_alloc);
1032 EXPECT_THROW(a.
insert(std::move(x)), std::bad_alloc);
1036 ASSERT_EQ(a.
size(), 4u);
1037 for (
size_t i = 0; i < 4; ++i)
1049TEST(DynArrayGeometry, OversizedGeometryThrowsOverflowError)
1052 EXPECT_THROW((
DynArray<int>(40, 20, 10)), std::overflow_error);
1054 std::overflow_error);
1058 EXPECT_EQ(d + s + b, 63u);
1064struct Counted_Default
1066 static inline long defaults = 0;
1067 static inline bool failing =
false;
1073 throw std::bad_alloc();
1076 explicit Counted_Default(
int v) noexcept :
value(v) {}
1079TEST(DynArrayIterator, CreatingIteratorsConstructsNoItem)
1085 static_assert(std::is_nothrow_default_constructible_v<DynArray<Counted_Default>::Iterator>);
1087 a.
append(Counted_Default(1));
1088 a.
append(Counted_Default(2));
1090 const long before = Counted_Default::defaults;
1091 Counted_Default::failing =
true;
1094 EXPECT_TRUE(it.has_curr());
1095 EXPECT_EQ(it.get_curr().value, 1);
1099 for (
const auto & item : a)
1103 Counted_Default::failing =
false;
1104 EXPECT_EQ(Counted_Default::defaults, before);
1107TEST(DynArrayIterator, UnwrittenEntryOnConstantAndModifiableArrays)
1117 const int & first = cit.
get_curr();
1118 EXPECT_EQ(first, 7);
1120 EXPECT_EQ(&cit.get_curr(), &first);
1121 EXPECT_EQ(a.
test(0),
nullptr);
1127 EXPECT_EQ(&entry, a.
test(0));
1129 EXPECT_EQ(a.
read(0), 100);
1131 EXPECT_EQ(it.get_curr(), 7);
1132 EXPECT_EQ(a.
size(), 10u);
1150TEST(DynArrayLocate, NthOfAnUnwrittenEntryIsTheEntry)
1155 EXPECT_EQ(ca.
nth(1), 7);
1156 EXPECT_EQ(ca.
nth_ne(5), 7);
1157 EXPECT_EQ(a.
test(1),
nullptr);
1158 EXPECT_EQ(a.
test(5),
nullptr);
1159 EXPECT_THROW((
void) ca.
nth(10), std::out_of_range);
1162 EXPECT_EQ(&x, a.
test(3));
1165 EXPECT_EQ(a.
read(3), 30);
1168 EXPECT_EQ(&
y, a.
test(6));
1170 EXPECT_THROW((
void) a.
nth(10), std::out_of_range);
1171 EXPECT_EQ(a.
size(), 10u);
1174TEST(DynArrayLocate, FindPtrOfAnUnwrittenEntryPointsIntoTheArray)
1178 auto is_seven = [] (
int i) {
return i == 7; };
1180 const int * c = ca.
find_ptr(is_seven);
1181 ASSERT_NE(c,
nullptr);
1183 EXPECT_EQ(a.
test(0),
nullptr);
1186 ASSERT_NE(p,
nullptr);
1187 EXPECT_EQ(p, a.
test(0));
1189 EXPECT_EQ(a.
read(0), 1);
1191 EXPECT_EQ(a.
find_ptr([] (
int i) { return i == 42; }),
nullptr);
1192 EXPECT_EQ(ca.
find_ptr([] (
int i) { return i == 42; }),
nullptr);
1193 EXPECT_EQ(*a.
find_ptr([] (
int i) { return i == 0; }), 0);
1197template <
class A,
class Op>
1198constexpr bool can_find_item =
requires (A & a, Op & op) { a.find_item(op); };
1201constexpr auto is_seven_criterion = [] (
const int & i) {
return i == 7; };
1203TEST(DynArrayLocate, FindItemOfAnUnwrittenEntryCopiesTheDefaultValue)
1206 const auto [found, item] = a.
find_item([] (
int i) {
return i == 7; });
1209 EXPECT_EQ(a.
test(0),
nullptr);
1213 auto scribble = [] (
int & i) {
const bool match = i == 7; i = -1;
return match; };
1214 static_assert(not can_find_item<DynArray<int>,
decltype(scribble)>);
1215 static_assert(can_find_item<DynArray<int>,
decltype(is_seven_criterion)>);
1217 const auto [missing, dflt] = std::as_const(a).find_item([] (
int i) {
return i == 42; });
1218 EXPECT_FALSE(missing);
1226TEST(DynArrayLocate, AddressesOfUnwrittenEntriesStayValid)
1233 const auto ptrs = ca.
ptr_filter([] (
int x) {
return x == 7; });
1234 ASSERT_EQ(ptrs.size(), 9u);
1236 ptrs.for_each([&ca, &a, &i] (
const int * p)
1239 EXPECT_EQ(p, i < 8 ? &ca.
read(0) : a.
test(8));
1245 ASSERT_NE(lo,
nullptr);
1246 ASSERT_NE(hi,
nullptr);
1255 EXPECT_EQ(sum, 9 * 7 + 0);
1256 EXPECT_EQ(a.
test(0),
nullptr);
1259 auto seven = [] (
int x) {
return x == 7; };
1261 ASSERT_NE(p,
nullptr);
1262 EXPECT_EQ(p, a.
test(0));
1264 EXPECT_EQ(mutable_ptrs.get_first(), a.
test(0));
1265 *mutable_ptrs.get_last() = 42;
1266 EXPECT_EQ(a.
read(9), 42);
1274 Throwing_Copy() =
default;
1275 Throwing_Copy(
const Throwing_Copy & o) :
value(o.
value) {}
1276 Throwing_Copy(Throwing_Copy && o) :
value(o.
value) {}
1277 Throwing_Copy & operator = (
const Throwing_Copy & o) {
value = o.value;
return *
this; }
1278 Throwing_Copy & operator = (Throwing_Copy && o) {
value = o.value;
return *
this; }
1281TEST(DynArrayNoexcept, DependsOnT)
1283 static_assert(std::is_nothrow_move_constructible_v<DynArray<int>>);
1284 static_assert(std::is_nothrow_move_assignable_v<DynArray<int>>);
1285 static_assert(std::is_nothrow_swappable_v<DynArray<int>>);
1286 static_assert(
noexcept(std::declval<DynArray<int> &>().set_default_initial_value(0)));
1288 static_assert(not std::is_nothrow_move_constructible_v<DynArray<Throwing_Copy>>);
1289 static_assert(not std::is_nothrow_move_assignable_v<DynArray<Throwing_Copy>>);
1290 static_assert(not
noexcept(std::declval<DynArray<Throwing_Copy> &>().
swap(
1292 static_assert(not
noexcept(std::declval<DynArray<Throwing_Copy> &>().set_default_initial_value(
1293 std::declval<const Throwing_Copy &>())));
1296 a.
append(Throwing_Copy());
1298 EXPECT_EQ(b.
size(), 1u);
1304struct Assignment_Throws
1306 static inline int assignments_left = -1;
1310 Assignment_Throws() =
default;
1311 Assignment_Throws(
const int v) :
value(v) {}
1312 Assignment_Throws(
const Assignment_Throws &) =
default;
1313 Assignment_Throws & operator = (
const Assignment_Throws & o)
1315 if (assignments_left == 0)
1316 throw std::runtime_error(
"assignment");
1317 if (assignments_left > 0)
1324TEST(DynArrayCut, a_cut_that_fails_releases_nothing_and_keeps_the_dimension)
1327 for (
int i = 0; i < 12; ++i)
1328 a.
touch(i) = Assignment_Throws(i + 1);
1332 Assignment_Throws::assignments_left = 1;
1333 EXPECT_THROW(a.
cut(6), std::runtime_error);
1334 Assignment_Throws::assignments_left = -1;
1336 EXPECT_EQ(a.
size(), 12u);
1337 for (
size_t i = 0; i < 12; ++i)
1338 ASSERT_TRUE(a.
exist(i)) << i;
1339 for (
int i = 0; i < 6; ++i)
1340 EXPECT_EQ(a.
access(i).value, i + 1);
1341 for (
int i = 8; i < 12; ++i)
1342 EXPECT_EQ(a.
access(i).value, i + 1);
1346 EXPECT_EQ(a.
size(), 6u);
1347 EXPECT_FALSE(a.
exist(8));
1348 for (
int i = 0; i < 6; ++i)
1349 EXPECT_EQ(a.
access(i).value, i + 1);
1351 EXPECT_EQ(a.
size(), 12u);
1352 for (
size_t i = 6; i < 12; ++i)
1353 EXPECT_EQ(a.
read(i).value, 0) << i;
1356 Assignment_Throws::assignments_left = 0;
1357 EXPECT_THROW(a.
cut(10), std::runtime_error);
1358 Assignment_Throws::assignments_left = -1;
1359 EXPECT_EQ(a.
size(), 12u);
1360 EXPECT_TRUE(a.
exist(11));
1367TEST(DynArrayCut, a_pop_or_a_remove_that_fails_keeps_the_dimension)
1369 const auto make = [] (
const int n)
1372 for (
int i = 0; i < n; ++i)
1373 a.
touch(i) = Assignment_Throws(i + 1);
1377 for (
int n = 5; n <= 12; ++n)
1381 const bool resets = (n - 1) % 4 != 0;
1382 const auto size_after =
static_cast<size_t>(resets ? n : n - 1);
1385 Assignment_Throws::assignments_left = 0;
1387 EXPECT_THROW((
void) a.
pop(), std::runtime_error) << n;
1389 EXPECT_EQ(a.
pop().value, n);
1390 Assignment_Throws::assignments_left = -1;
1392 ASSERT_EQ(a.
size(), size_after) << n;
1393 for (
size_t i = 0; i < a.
size(); ++i)
1395 ASSERT_TRUE(a.
exist(i)) << n <<
" " << i;
1396 EXPECT_EQ(a.
access(i).value,
static_cast<int>(i) + 1) << n;
1402 Assignment_Throws::assignments_left = 2;
1404 EXPECT_THROW(b.
remove(b.
access(0)), std::runtime_error) << n;
1407 Assignment_Throws::assignments_left = -1;
1409 ASSERT_EQ(b.
size(), size_after) << n;
1410 for (
size_t i = 0; i < b.
size(); ++i)
1411 ASSERT_TRUE(b.
exist(i)) << n <<
" " << i;
1412 EXPECT_EQ(b.
access(0).value, n);
1414 EXPECT_EQ(b.
access(b.
size() - 1).value, 1) << n;
Deduplicate sequential Aleph containers in-place.
Functional programming utilities for Aleph-w containers.
static string long_string(const char c)
A string too long for the small-string buffer.
size_t size_t int32_t value
Iterator on the items of array.
void reset_last() noexcept
Reset the iterator to the last item.
bool is_last() const noexcept
Check whether the current item is the last item.
void next()
Advance one position, from the last item to the end if needed.
bool has_curr() const noexcept
Check whether the iterator refers to an item.
T & get_curr() const
Return the current item.
void each(Operation &operation) const
Alias of for_each(); see FunctionalMethods::for_each().
void set_default_initial_value(const T &value) noexcept(std::is_nothrow_copy_assignable_v< T >)
Set the default value.
T * test(const size_t i) const noexcept
Test if the i-th entry es writable,.
size_t get_block_size() const noexcept
Return the block size.
void swap(DynArray< T > &array) noexcept(std::is_nothrow_swappable_v< T >)
Swap in constant time array with this
T & top()
Return a modifiable reference to the last item of stack.
void push(const T &data)
See append(const T & data).
void adjust(const size_t dim)
Set a new dimension.
Iterator get_it()
Iterator on the modifiable array, positioned on entry 0.
void cut(const size_t new_dim=0)
Cut the array to a new dimension; that is, it reduces the dimension of array and frees the remaining ...
const T & read(const size_t i) const
Read entry i without allocating memory.
Array< T > to_array() const
Copy contents into Aleph::Array (requires copyable elements).
T & insert(const T &item)
Insert a copy of item at the beginning of the array.
size_t get_num_blocks() const noexcept
Return the number of blocks consumed by the array.
void clear() noexcept
Empties the container.
T * find_ptr(Operation &operation)
Return a pointer to the first entry satisfying a criterion.
T & touch(const size_t i)
Touch the entry i.
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) noexcept
Fast access without checking allocation and bound_min_clock checking.
T & nth(const size_t n)
Return a modifiable reference to entry n.
T & get_first()
Return a modifiable reference to the first item of array (as if this was a queue)
T & nth_ne(const size_t n)
Return a modifiable reference to entry n without checking n.
T pop()
Remove the last item of array (as if this was a stack)
static void compute_sizes(const size_t n, size_t &d, size_t &s, size_t &b)
Given a dimension n, it proposes values for the directory, segment and block sizes.
size_t max_size() const noexcept
Return the maximum allowed dimension (or the maximum number of elements that could have the array tre...
bool traverse(Operation &operation) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
Traverse a constant array and execute a conditioned operation, which must have the signature:
bool exist(const size_t i) const
Return true if the i-th entry is accessible.
T & get_last()
Return a modifiable reference to the last item of array (as if this was a queue)
T & append()
Allocate a new entry to the end of array.
bool is_empty() const noexcept
Return true if the array is empty.
void empty() noexcept
Empty the array.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
void remove(T &item)
Given a valid reference to an item in the array, it removes it and decrease the dimension.
Minimal std::expected-style result type for C++20.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Aleph::DynList< const T * > ptr_filter(Operation &operation) const
Filter the elements of a container according to a matching criterion and return a pointer to the matc...
bool contains(const Type &item) const
Test if an item is present in the container using equality.
and std::default_initializable< T > and std::copy_constructible< T > std::tuple< bool, Type > find_item(Operation &operation) const noexcept(find_item_is_noexcept< Operation >())
Safe sequential searching of an item matching a criterion.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_dim_function > > dim(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Compiler_SSA_Block & block(Compiler_SSA_Function &function, const Compiler_SSA_Block_Id id)
constexpr size_t Unlimited
Alias for Search_Unlimited.
Main namespace for Aleph-w library functions.
void in_place_unique(Container &c, Compare cmp={})
Remove duplicates in-place preserving first occurrence order.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
const Container::Item_Type * max_ptr(const Container &container, Cmp cmp=Cmp())
Find the maximum element in a container.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
and
Check uniqueness with explicit hash + equality functors.
and PredicateWith< Cmp &, const typename Container::Item_Type &, functional_detail::get_curr_cref_t< Container > > std::pair< const typename Container::Item_Type *, const typename Container::Item_Type * > minmax_ptr(const Container &container, Cmp cmp=Cmp())
Find both min and max elements in a single pass.
DynList< typename Container::Item_Type * > pointers_list(Container &c)
Create a list of pointers to items in a container.
Container::Item_Type * find_ptr(Container &container, Pred &pred)
Find the first element satisfying pred.
bool none(const Container &container, Operation &operation)
Return true if no element satisfies operation.
const Container::Item_Type * min_ptr(const Container &container, Cmp cmp=Cmp())
Find the minimum element in a container.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Element whose default construction, that of every slot of a new buffer, can fail as an allocation doe...
static int constructions_left
Failing_Slot()
Fail at the configured default construction.
Lazy and scalable dynamic array implementation.