38#include <gtest/gtest.h>
45#include <initializer_list>
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>>);
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>>);
79TEST(ArrayCtors, FactoriesAndBracesSayWhatIsMeant)
83 EXPECT_GE(by_default.
capacity(), 32u);
86 EXPECT_TRUE(reserved.is_empty());
87 EXPECT_GE(reserved.capacity(), 100u);
89 EXPECT_EQ(reserved[0], 5);
92 EXPECT_TRUE(small.is_empty());
95 EXPECT_EQ(small.size(), 2u);
98 EXPECT_EQ(slots.size(), 3u);
101 ASSERT_EQ(item.size(), 1u);
102 EXPECT_EQ(item[0], 10);
106 ASSERT_EQ(braced_in_parens.size(), 1u);
107 EXPECT_EQ(braced_in_parens[0], 0u);
114 ASSERT_EQ(repeated_size.size(), 3u);
115 EXPECT_EQ(repeated_size[2], 8u);
118TEST(ArrayCtors, InputRangesSupportSinglePassIteratorsAndSentinels)
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);
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}));
131TEST(ArrayCopyMove, MovedFromArraysRemainEmptyAndReusable)
136 EXPECT_EQ(original.
begin(), original.
end());
138 EXPECT_TRUE(empty_copy.is_empty());
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);
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);
155TEST(ArrayBasics, DefaultConstructionAndBase)
159 EXPECT_EQ(arr.
size(), 0u);
160 EXPECT_THROW(arr.
base(), std::underflow_error);
163 EXPECT_THROW(empty_const.
base(), std::underflow_error);
168 EXPECT_EQ(arr.
size(), 2u);
169 EXPECT_EQ(arr.
base(), 10);
173 const auto & carr = arr;
174 EXPECT_EQ(carr.get_first(), 10);
175 EXPECT_EQ(carr.get_last(), 20);
178TEST(ArrayFunctional, ToArrayPreservesOrder)
185 for (
size_t i = 0; i <
copy.size(); ++i)
186 EXPECT_EQ(
copy(i), src(i));
189 EXPECT_EQ(src.
size(), 4u);
192TEST(ArrayModifiers, InsertAppendAndRemove)
199 ASSERT_EQ(arr.
size(), 3u);
205 EXPECT_EQ(arr.
size(), 1u);
206 EXPECT_EQ(arr.
base(), 1);
212TEST(ArrayCopyMove, CopyAndMoveSemantics)
216 ASSERT_EQ(
copy.size(), original.
size());
218 EXPECT_EQ(original[0], 1);
222 EXPECT_EQ(assigned.
size(),
copy.size());
223 EXPECT_EQ(assigned[0], 100);
226 EXPECT_EQ(moved.size(), 4u);
227 EXPECT_EQ(moved[0], 100);
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);
236TEST(ArrayCapacity, ReservePutnAndSwap)
239 const auto initial_cap = arr.
capacity();
241 EXPECT_GE(arr.
capacity(), initial_cap + 50);
244 ASSERT_EQ(arr.
size(), 5u);
245 for (
size_t i = 0; i < arr.
size(); ++i)
246 arr[i] =
static_cast<int>(i * 10);
251 EXPECT_EQ(arr.
size(), 1u);
252 EXPECT_EQ(arr[0], -1);
253 EXPECT_EQ(other.
size(), 5u);
254 EXPECT_EQ(other[2], 20);
257TEST(ArrayAccessors, BoundsCheckingAndConstVariants)
263 EXPECT_EQ(arr[0],
"hello");
264 EXPECT_EQ(arr(1),
"world");
265 EXPECT_THROW(arr[2], std::out_of_range);
268 EXPECT_EQ(carr[0],
"hello");
269 EXPECT_EQ(carr(1),
"world");
270 EXPECT_THROW(carr[3], std::out_of_range);
273TEST(ArrayReverse, ReverseAndReverseInPlaceAliases)
276 for (
int i = 1; i <= 5; ++i)
279 const std::array<int, 5> ascending = {1, 2, 3, 4, 5};
280 const std::array<int, 5> descending = {5, 4, 3, 2, 1};
283 for (
size_t i = 0; i < descending.size(); ++i)
284 EXPECT_EQ(arr[i], descending[i]) <<
"reverse() should mutate in place";
288 for (
size_t i = 0; i < ascending.size(); ++i)
289 EXPECT_EQ(
copy[i], ascending[i]) <<
"const reverse() should return new copy";
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()";
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";
300TEST(ArrayReverse, NonConstRevReversesInPlaceAndReturnsReference)
306 EXPECT_EQ(&alias, &arr);
307 EXPECT_EQ(arr[0], 3);
308 EXPECT_EQ(arr[1], 2);
309 EXPECT_EQ(arr[2], 1);
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;
327TEST(ArrayTraverse, TraversalVariants)
341 auto stop_at_three = [&visited](
int value)
346 EXPECT_FALSE(arr.
traverse(stop_at_three));
347 EXPECT_EQ(visited, 3);
350 EXPECT_TRUE(arr.
traverse(MoveOnlyOp(&called)));
354TEST(ArrayTraverse, ConstElementsCannotBeModified)
359 auto increment = [](
int &
value)
365 auto accumulate_const = [&
sum](
const int &
value)
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));
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);
383template <
class Container,
class Operation>
386 container.traverse(operation);
389struct MutableArrayOperation
391 bool operator () (
int &)
const {
return true; }
394static_assert(CanTraverse<Array<int>, MutableArrayOperation>);
395static_assert(not CanTraverse<const Array<int>, MutableArrayOperation>);
397TEST(ArrayAlgorithms, InPlaceUnique)
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);
410TEST(ArrayIterators, IteratorCoversAllElements)
416 for (; it.has_curr(); it.next())
424TEST(ArrayUtilities, BuildArrayAndStdVector)
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);
432 ASSERT_EQ(vec.size(), arr.
size());
433 for (
size_t i = 0; i < vec.size(); ++i)
434 EXPECT_EQ(vec[i], arr(i));
442 DefaultInit() : v(123) {}
443 explicit DefaultInit(
int x) : v(x) {}
444 bool operator==(
const DefaultInit &o)
const {
return v == o.v; }
448TEST(ArrayCtors, ValueConstructorInitializesAllElements_POD)
451 const int value = 42;
453 ASSERT_EQ(arr.
size(), n);
454 for (
size_t i = 0; i < n; ++i)
455 EXPECT_EQ(arr[i],
value);
458TEST(ArrayCtors, ValueConstructorInitializesAllElements_NonPOD)
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);
468TEST(ArrayFactory, CreateYieldsLogicalSizeAndRequiresAssignBeforeReadForPOD)
472 static_assert(std::is_trivially_default_constructible_v<int>);
473 ASSERT_EQ(arr.
size(), n);
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));
481TEST(ArrayFactory, CreateDefaultConstructsClassTypes)
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);
490 arr[0] = DefaultInit(7);
491 EXPECT_EQ(arr[0].v, 7);
494TEST(ArrayFactory, PutnAndAppendPopulateBothPODAndNonPOD)
502 ASSERT_EQ(pod.
size(), 4u);
503 EXPECT_EQ(pod[3], 4);
510 ASSERT_EQ(nonpod.
size(), 3u);
511 EXPECT_EQ(nonpod[2],
"z");
514TEST(ArraySearch, Contains)
521 EXPECT_TRUE(arr.
contains_if([](
int x) { return x > 25; }));
522 EXPECT_FALSE(arr.
contains_if([](
int x) { return x > 100; }));
525TEST(ArraySearch, ContainsEmpty)
529 EXPECT_FALSE(empty.
contains_if([](
int) { return true; }));
541 EXPECT_EQ(a[i], item) <<
"at position " << i;
549 return std::string(40, c);
556TEST(ArraySelfInsertion, AppendOwnElement)
559 for (
int i : {10, 20, 30, 40})
561 ASSERT_EQ(a.size(), a.capacity());
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);
566 expect_items(a, {10, 20, 30, 40, 10, 40});
569TEST(ArraySelfInsertion, AppendOwnMovedElement)
572 for (
char c : {
'a',
'b',
'c',
'd'})
574 ASSERT_EQ(a.size(), a.capacity());
576 EXPECT_EQ(a.append(std::move(a[0])),
long_string(
'a'));
577 ASSERT_EQ(a.size(), 5u);
582TEST(ArraySelfInsertion, InsertOwnElement)
585 for (
int i : {10, 20, 30})
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});
595TEST(ArraySelfInsertion, InsertOwnMovedElement)
598 for (
char c : {
'a',
'b',
'c'})
601 EXPECT_EQ(a.insert(std::move(a[2])),
long_string(
'c'));
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);
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()));
618TEST(ArrayAccess, GetFirstAndGetLastOfEmptyArrayThrow)
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);
633 expect_items(a, {1, 2});
638struct Copy_Move_Counted
640 static inline size_t copies = 0;
641 static inline size_t moves = 0;
642 static inline size_t move_constructions = 0;
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)
651 ++move_constructions;
653 Copy_Move_Counted & operator = (
const Copy_Move_Counted & o)
659 Copy_Move_Counted & operator = (Copy_Move_Counted && o)
noexcept
667TEST(ArrayAppend, ConstantConcatenationCopiesEachItemOnce)
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));
677 Copy_Move_Counted::copies = 0;
680 EXPECT_EQ(joined.
size(), 8u);
681 EXPECT_EQ(joined[7].
value, 12);
682 EXPECT_LE(Copy_Move_Counted::copies, 5u + 3u);
685TEST(ArrayRemoveFirst, MovesEachItemOnce)
690 for (
int i = 0; i < 20; ++i)
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;
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);
702 EXPECT_EQ(Copy_Move_Counted::moves, 1u + 19u);
bool operator==(const Time &l, const Time &r)
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
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.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
void empty() noexcept
Empties the container.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
T & base()
Return a reference to the first element of array.
T & insert(const T &data)
insert a copy of data at the beginning of the array.
Array & rev()
Reverse this array in place.
void swap(Array &s) noexcept
Swap this with s
Array & reverse_in_place()
Alias for reverse().
T & get_last()
Return a modifiable reference to the last element.
bool traverse(Op &op)
Traverse mutable elements from first to last.
static Array create(const size_t n)
Create an array with n logical elements.
Array & reverse()
Reverse the order of items in this array, in place.
T & append(const T &data)
Append a copy of data
constexpr size_t capacity() const noexcept
Return the internal capacity.
void reserve(size_t cap)
Reserves cap cells into the array.
void putn(const size_t n)
Reserve n additional logical slots in the array without value-initializing them.
T & get_first()
Return a modifiable reference to the first element.
Array to_array() const
Copy to Aleph::Array (requires copyable elements).
size_t size() const noexcept
Return the number of items of the list, in constant time.
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.
bool contains_if(Operation &&operation) const noexcept(const_search_is_noexcept< Operation >())
Test if an item satisfying a criterion is present in the container.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
Main namespace for Aleph-w library functions.
void in_place_unique(Container &c, Compare cmp={})
Remove duplicates in-place preserving first occurrence order.
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.
std::decay_t< typename HeadC::Item_Type > T
T accumulate(Itor beg, Itor end, T initValue)
Accumulate values in a range.
std::vector< typename Container::Item_Type > to_stdvector(const Container &c)
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Iterator on the items of an array.
Dynamic array container with automatic resizing.