38# include <gtest/gtest.h>
72using namespace testing;
78 static constexpr size_t N = 1000;
83 for (
size_t i = 0; i <
N; ++i)
97 TypeParam c = this->c;
98 EXPECT_EQ(c.size(),
N);
100 EXPECT_TRUE(c.traverse([&
l] (
auto &
k) { l.append(k); return true; }));
101 EXPECT_TRUE(
zip_all([] (
auto t) {
return get<0>(t) == get<1>(t); },
102 this->item_list,
sort(
l)));
108 TypeParam c = this->c;
109 EXPECT_EQ(c.size(),
N);
112 EXPECT_TRUE(
zip_all([] (
auto t) {
return get<0>(t) == get<1>(t); },
113 this->item_list,
sort(
l)));
119 TypeParam c = this->c;
120 EXPECT_EQ(c.size(),
N);
121 auto ptr = c.find_ptr([
N] (
auto &
k) {
return k == int(
N); });
122 EXPECT_EQ(ptr,
nullptr);
123 this->item_list.for_each([&c] (
auto &
k)
125 auto ptr = c.find_ptr([
k] (
auto i) {
return k == i; });
126 ASSERT_NE(ptr,
nullptr);
134 TypeParam c = this->c;
135 EXPECT_EQ(c.size(),
N);
137 auto idx = c.find_index([
N] (
auto &
k) {
return k == int(
N); });
140 this->item_list.for_each([&c] (
auto &
k)
142 auto idx = c.find_index([
k] (
auto i) {
return k == i; });
143 ASSERT_EQ(c.nth(idx),
k);
150 TypeParam c = this->c;
151 EXPECT_EQ(c.size(),
N);
153 auto t = c.find_item([
N] (
auto &
k) {
return k == int(
N); });
154 ASSERT_FALSE(get<0>(t));
156 this->item_list.for_each([&c] (
auto &
k)
158 auto t = c.find_item([
k] (
auto i) {
return k == i; });
159 EXPECT_TRUE(get<0>(t));
160 ASSERT_EQ(get<1>(t),
k);
169 const std::vector<int> v = c.to_vector();
172 ASSERT_EQ(
l.
size(), c.size());
173 ASSERT_EQ(v.size(), c.size());
174 ASSERT_EQ(
l2.
size(), c.size());
179 EXPECT_EQ(x, v[idx++]);
183 for (
auto & item : c)
185 ASSERT_EQ(item, itl.get_curr_ne());
188 auto it = c.get_it();
191 EXPECT_EQ(it.get_curr_ne(),
l.
get_last());
195 EXPECT_EQ(it.get_curr_ne(),
l.
get_last());
204 auto ptr = c.find_ptr([
N] (
auto i) {
return i ==
N; });
205 EXPECT_EQ(c.size(),
N + 1);
206 ASSERT_NE(ptr,
nullptr);
209 c.nappend(
N + 1,
N + 2,
N + 3);
210 EXPECT_EQ(c.size(),
N + 4);
212 ptr = c.find_ptr([
N] (
auto i) {
return i ==
N + 1; });
213 ASSERT_NE(ptr,
nullptr);
214 EXPECT_EQ(*ptr,
N + 1);
216 ptr = c.find_ptr([
N] (
auto i) {
return i ==
N + 2; });
217 ASSERT_NE(ptr,
nullptr);
218 EXPECT_EQ(*ptr,
N + 2);
220 ptr = c.find_ptr([
N] (
auto i) {
return i ==
N + 3; });
221 ASSERT_NE(ptr,
nullptr);
222 EXPECT_EQ(*ptr,
N + 3);
232 size_t before = c.size();
233 size_t added = c.nappend(
N,
N,
N + 1, 0);
234 EXPECT_EQ(added, c.size() - before);
235 EXPECT_GE(added, 2u);
236 EXPECT_LE(added, 4u);
239 added = c.ninsert(
N + 2,
N + 2,
N + 3, 1);
240 EXPECT_EQ(added, c.size() - before);
241 EXPECT_GE(added, 2u);
242 EXPECT_LE(added, 4u);
245 EXPECT_EQ(c.nappend(), 0u);
246 EXPECT_EQ(c.ninsert(), 0u);
247 EXPECT_EQ(c.size(), before);
256 auto ptr = c.find_ptr([
N] (
auto i) {
return i ==
N; });
257 EXPECT_EQ(c.size(),
N + 1);
258 ASSERT_NE(ptr,
nullptr);
261 c.ninsert(
N + 1,
N + 2,
N + 3);
262 EXPECT_EQ(c.size(),
N + 4);
264 ptr = c.find_ptr([
N] (
auto i) {
return i ==
N + 1; });
265 ASSERT_NE(ptr,
nullptr);
266 EXPECT_EQ(*ptr,
N + 1);
268 ptr = c.find_ptr([
N] (
auto i) {
return i ==
N + 2; });
269 ASSERT_NE(ptr,
nullptr);
270 EXPECT_EQ(*ptr,
N + 2);
272 ptr = c.find_ptr([
N] (
auto i) {
return i ==
N + 3; });
273 ASSERT_NE(ptr,
nullptr);
274 EXPECT_EQ(*ptr,
N + 3);
282 ASSERT_TRUE(c.all([&
tbl] (
auto i)
284 const bool ret = tbl.contains(i);
288 EXPECT_EQ(
tbl.size(),
N);
296 auto &
l = this->item_list;
297 EXPECT_TRUE(
l.
all([&c] (
auto & i)
298 { return c.exists([i] (auto k) { return i == k; }); }));
299 EXPECT_FALSE(c.exists([
N] (
auto i) { return i == N; }));
305 auto &
l = this->item_list;
306 auto fct = [] (
int i) {
return i + 1; };
308 all([] (
auto & p) {
return p.first == p.second; }));
310 { return i < 7; }, fct))),
312 { return i < 7; }, fct))).
313 all([] (
auto & p) {
return p.first == p.second; }));
319 auto &
l = this->item_list;
320 auto fct = [] (
int i) {
return i + 1; };
322 all([] (
auto & p) {
return p.first == p.second; }));
324 { return i < 7; }, fct))),
326 { return i < 7; }, fct))).
327 all([] (
auto & p) {
return p.first == p.second; }));
334 auto sum = c.foldl(0, [] (
auto & a,
auto & i) {
return a + i; });
335 EXPECT_EQ(
sum,
N*(
N-1)/2);
340 auto fct = [] (
int a,
int i) {
return a + i; };
342 auto sum = c.filter([] (
auto i) {
return i < 8; }).
foldl(0, fct);
345 auto l = c.
ptr_filter([] (
auto & i) {
return i < 8; });
346 sum =
l.
foldl(0, [] (
auto a,
auto ptr) {
return a + *ptr; });
350 auto total =
N*(
N-1)/2;
351 auto p = c.partition([] (
auto & i) {
return i < 8; });
352 auto S = p.first.foldl(0, fct) + p.second.foldl(0, fct);
355 auto t = c.tpartition([] (
auto & i) {
return i < 8; });
356 S = get<0>(t).foldl(0, fct) + get<1>(t).foldl(0, fct);
364 EXPECT_EQ(
sort(c.to_dynlist()).take(8, 12),
365 build_dynlist<int>(8, 9, 10, 11, 12));
377 constexpr size_t largest = numeric_limits<size_t>::max();
378 const TypeParam & c = this->c;
379 EXPECT_THROW((
void) c.nth(this->N), out_of_range);
380 EXPECT_THROW((
void) c.nth(largest), out_of_range);
385 EXPECT_THROW((
void) empty.nth(0), out_of_range);
386 EXPECT_THROW((
void) empty.nth(largest), out_of_range);
387 EXPECT_THROW((
void) as_const(empty).nth(largest), out_of_range);
392 const TypeParam & c = this->c;
394 c.for_each([&order] (
int i) { order.push_back(i); });
395 ASSERT_EQ(order.size(), this->N);
397 constexpr size_t largest = numeric_limits<size_t>::max();
398 const size_t cases[][3] = { {0, 999, 1}, {0, 999, 2}, {1, 998, 3}, {5, 5, 7},
399 {0, 10, 4}, {990, largest, 3}, {0, 999, 1000},
400 {3, 900, largest}, {999, 999, 1}, {10, 5, 2},
401 {0, 4, 0}, {1000, 1005, 1}, {5000, 6000, 3},
402 {largest, largest, 1} };
403 for (
const auto & [i, j, step] : cases)
407 for (
size_t p = i; p <= j
and p < order.size(); )
415 c.take(i, j, step).for_each([&taken] (
int x) { taken.push_back(x); });
416 EXPECT_EQ(taken,
expected) <<
"take(" << i <<
", " << j <<
", " << step <<
")";
424 constexpr bool has_remove =
requires (TypeParam & c) { c.remove(); };
425 static_assert((
requires (TypeParam & c) { c.mutable_drop(
size_t{}); }) == has_remove);
426 if constexpr (has_remove)
428 TypeParam c = this->c;
429 const int eleventh = c.nth(10);
431 EXPECT_EQ(c.size(), this->N - 10);
432 EXPECT_EQ(c.nth(0), eleventh);
438static_assert(Aleph::dry_detail::const_traversal_is_noexcept<DynList<int>,
int>);
439static_assert(Aleph::dry_detail::const_traversal_is_noexcept<DynDlist<int>,
int>);
440static_assert(Aleph::dry_detail::const_traversal_is_noexcept<DynArray<int>,
int>);
441static_assert(Aleph::dry_detail::const_traversal_is_noexcept<ArrayQueue<int>,
int>);
442static_assert(Aleph::dry_detail::const_traversal_is_noexcept<ArrayStack<int>,
int>);
443static_assert(Aleph::dry_detail::const_traversal_is_noexcept<FixedStack<int>,
int>);
444static_assert(Aleph::dry_detail::const_traversal_is_noexcept<FixedQueue<int>,
int>);
445static_assert(Aleph::dry_detail::const_traversal_is_noexcept<DynListQueue<int>,
int>);
446static_assert(Aleph::dry_detail::const_traversal_is_noexcept<DynListStack<int>,
int>);
447static_assert(Aleph::dry_detail::const_traversal_is_noexcept<DynArrayHeap<int>,
int>);
448static_assert(
noexcept(declval<const DynList<int> &>().find_item(
Nothrow_Match{0})));
449static_assert(
noexcept(declval<const DynArray<int> &>().length()));
456 constexpr bool nothrow_traversal =
457 Aleph::dry_detail::const_traversal_is_noexcept<TypeParam, int>;
458 static_assert(
noexcept(declval<TypeParam &>().find_item(
Nothrow_Match{0})) == nothrow_traversal);
459 static_assert(
noexcept(declval<const TypeParam &>().find_item(
Nothrow_Match{0})) == nothrow_traversal);
460 const TypeParam & c = this->c;
483 static_assert(not
requires (
const C & c,
Modifying_Pred & op) { c.traverse(op); });
485 static_assert(not
requires (
const C & c,
Modifying_Pred & op) { c.all(op); });
486 static_assert(not
requires (
const C & c,
Modifying_Pred & op) { c.exists(op); });
487 static_assert(not
requires (
const C & c,
Modifying_Pred & op) { c.none(op); });
488 static_assert(not
requires (
const C & c,
Modifying_Pred & op) { c.count_if(op); });
489 static_assert(not
requires (
const C & c,
Modifying_Pred & op) { c.find_ptr(op); });
490 static_assert(not
requires (
const C & c,
Modifying_Pred & op) { c.find_index(op); });
491 static_assert(not
requires (
const C & c,
Modifying_Pred & op) { c.find_item(op); });
492 static_assert(not
requires (
const C & c,
Modifying_Pred & op) { c.find_opt(op); });
493 static_assert(not
requires (
const C & c,
Modifying_Pred & op) { c.contains_if(op); });
494 static_assert(not
requires (
const C & c,
Modifying_Pred & op) { c.filter(op); });
495 static_assert(not
requires (
const C & c,
Modifying_Act & op) { c.for_each(op); });
496 static_assert(not
requires (
const C & c,
Modifying_Act & op) { c.each(op); });
497 static_assert(not
requires (
const C & c,
Modifying_Act & op) { c.each(0, 1, op); });
500 auto reads = [] (
const int & x) {
return x >= 0; };
501 static_assert(
requires (
const C & c) { c.traverse(reads); c.all(reads); c.find_ptr(reads); });
506 const TypeParam & c = this->c;
508 c.for_each([&order] (
int i) { order.push_back(i); });
510 constexpr size_t largest = numeric_limits<size_t>::max();
511 const size_t cases[][2] = { {0, 1}, {3, 7}, {999, 1}, {1000, 1}, {5000, 2},
512 {0, largest}, {10, 0} };
513 for (
const auto & [pos, slice] : cases)
517 for (
size_t p = pos; p < order.size(); )
520 if (order.size() - p <= slice)
525 EXPECT_NO_THROW(c.each(pos, slice, [&visited] (
int x) { visited.push_back(x); }));
526 EXPECT_EQ(visited,
expected) <<
"each(" << pos <<
", " << slice <<
")";
530 const TypeParam empty;
532 EXPECT_NO_THROW(empty.each(0, 1, [&calls] (
int) { ++calls; }));
533 EXPECT_EQ(calls, 0u);
538 const TypeParam & c = this->c;
540 c.for_each([&order] (
int i) { order.push_back(i); });
541 const auto pred = [] (
int x) {
return x % 3 == 0; };
543 EXPECT_EQ(c.count_if(
pred),
size_t(
count_if(order.begin(), order.end(),
pred)));
544 EXPECT_TRUE(c.none([] (
int x) { return x < 0; }));
545 EXPECT_FALSE(c.none(
pred));
547 const auto found = c.find_opt([&order] (
int x) {
return x == order[17]; });
548 ASSERT_TRUE(found.has_value());
549 EXPECT_EQ(*found, order[17]);
550 EXPECT_FALSE(c.find_opt([] (
int x) { return x < 0; }).has_value());
553 const auto small = [&order] (
int x) {
return x != order[400]; };
555 c.take_while(small).for_each([&
prefix] (
int x) {
prefix.push_back(x); });
556 c.drop_while(small).for_each([&
suffix] (
int x) {
suffix.push_back(x); });
557 EXPECT_EQ(
prefix, vector<int>(order.begin(), order.begin() + 400));
558 EXPECT_EQ(
suffix, vector<int>(order.begin() + 400, order.end()));
562 c.flat_map([] (
int x) {
return DynList<int>({x, -x}); })
563 .
for_each([&doubled] (
int x) { doubled.push_back(x); });
564 ASSERT_EQ(doubled.size(), 2 * order.size());
565 EXPECT_EQ(doubled[2 * 5], order[5]);
566 EXPECT_EQ(doubled[2 * 5 + 1], -order[5]);
575 rebuilt.for_each([&again] (
int x) { again.push_back(x); });
576 EXPECT_EQ(again, order);
579 const auto total = c.reduce([] (
int a,
int x) {
return a + x; });
580 ASSERT_TRUE(total.has_value());
581 EXPECT_EQ(*total,
accumulate(order.begin(), order.end(), 0));
582 EXPECT_FALSE(TypeParam().reduce([] (
int a,
int x) {
return a + x; }).has_value());
590 const TypeParam & c = this->c;
591 vector<int> checked, unchecked;
592 for (
auto it = c.get_it(); it.has_curr(); it.next())
593 checked.push_back(it.get_curr());
594 for (
auto it = c.get_it(); it.has_curr(); it.next_ne())
595 unchecked.push_back(it.get_curr_ne());
596 EXPECT_EQ(checked, unchecked);
597 EXPECT_EQ(checked.size(), this->N);
601 find_index_nth, find_item, iterator_operations,
603 nappend_and_ninsert_count_the_items_added,
605 foldl, filter_ops, nth_out_of_range, take_with_step,
606 mutable_drop_needs_remove,
607 find_item_noexcept_for_nothrow_items,
608 queries_reject_modifying_operations,
609 each_with_position_and_slice,
610 functional_operations,
611 unchecked_iteration_matches_the_checked_one);
632 static constexpr size_t N = 10;
638 ptr_1 =
new C(range<int>(
N));
639 ptr_2 =
new C({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 });
655 auto ptr_1 = this->ptr_1;
656 auto ptr_2 = this->ptr_2;
657 auto ptr_3 = this->ptr_3;
658 EXPECT_EQ(ptr_1->size(),
N);
659 EXPECT_EQ(ptr_2->size(), 10);
660 EXPECT_EQ(ptr_3->size(), 10);
666 const auto r1 = range<int>(
N);
667 const auto r2 = build_dynlist<int>(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
668 const auto & r3 = r1;
689 EXPECT_FALSE(a1 == a3);
690 EXPECT_TRUE(a1 != a3);
694 EXPECT_TRUE(a1 == a1);
695 EXPECT_FALSE(a1 != a1);
699 EXPECT_TRUE(a1 == a2);
700 EXPECT_FALSE(a1 != a2);
704 EXPECT_FALSE(a1 == a4);
705 EXPECT_TRUE(a1 != a4);
709 EXPECT_FALSE(a1 == a5);
716 EXPECT_TRUE(d1 == d2);
717 EXPECT_FALSE(d1 == d3);
720TEST(StdMapCoexistence, map_method_with_std_map)
722 std::map<int, std::string> std_map;
725 std_map[3] =
"three";
729 auto mapped = aleph_list.
map([] (
int x) {
return x * 2; });
730 EXPECT_EQ(mapped.size(), 5);
732 EXPECT_EQ(std_map.size(), 3);
733 EXPECT_EQ(std_map[1],
"one");
735 auto filtered_mapped = aleph_list.
map_if(
736 [] (
int x) {
return x > 2; },
737 [] (
int x) {
return x * 3; }
739 EXPECT_EQ(filtered_mapped.size(), 3);
741 std::map<std::string, int> another_map;
742 another_map[
"a"] = 10;
743 another_map[
"b"] = 20;
745 EXPECT_EQ(another_map.size(), 2);
746 EXPECT_EQ(another_map[
"a"], 10);
748 auto result = mapped.foldl(0, [] (
int acc,
int val) {
return acc + val; });
749 EXPECT_EQ(result, 30);
757 static inline bool armed =
false;
760 Copy_Throws() noexcept = default;
761 explicit Copy_Throws(
int v) noexcept : value(v) {}
762 Copy_Throws(
const Copy_Throws & other) :
value(other.
value)
765 throw runtime_error(
"copy failed");
767 Copy_Throws & operator = (
const Copy_Throws &)
noexcept =
default;
771 struct Default_Throws
773 static inline bool armed =
false;
779 throw runtime_error(
"default construction failed");
781 explicit Default_Throws(
int v) noexcept :
value(v) {}
788 void check_find_item_propagates()
790 using T =
typename C::Item_Type;
791 auto match = [] (
const T & x)
noexcept {
return x.value == 2; };
792 auto never = [] (
const T &)
noexcept {
return false; };
793 static_assert(not
noexcept(declval<C &>().find_item(match)));
794 static_assert(not
noexcept(declval<const C &>().find_item(match)));
797 for (
int i = 0; i < 4; ++i)
802 if constexpr (is_same_v<T, Copy_Throws>)
804 EXPECT_THROW((
void) c.find_item(match), runtime_error);
805 EXPECT_THROW((
void) cc.find_item(match), runtime_error);
809 EXPECT_THROW((
void) c.find_item(never), runtime_error);
810 EXPECT_THROW((
void) cc.find_item(never), runtime_error);
814 const auto [found, item] = c.find_item(match);
816 EXPECT_EQ(item.value, 2);
822 check_find_item_propagates<Array<Copy_Throws>>();
823 check_find_item_propagates<DynList<Copy_Throws>>();
824 check_find_item_propagates<DynArray<Copy_Throws>>();
829 check_find_item_propagates<Array<Default_Throws>>();
830 check_find_item_propagates<DynList<Default_Throws>>();
831 check_find_item_propagates<DynArray<Default_Throws>>();
839 static inline size_t copies = 0;
842 Copy_Counted() =
default;
843 Copy_Counted(
const Copy_Counted & o) : items(o.items) { ++copies; }
844 Copy_Counted(Copy_Counted && o) noexcept : items(std::move(o.items)) {}
845 Copy_Counted & operator = (
const Copy_Counted & o)
851 Copy_Counted & operator = (Copy_Counted && o)
noexcept
853 items = std::move(o.items);
856 bool operator == (
const Copy_Counted & o)
const {
return items == o.items; }
860 constexpr bool foldl_without_operation =
requires (
const C & c) { c.foldl(0); };
862 template <
class C,
class Arg>
863 constexpr bool can_emplace =
requires (C & c, Arg a) { c.emplace(a); };
865 template <
class C,
class Op>
866 constexpr bool can_find_item =
requires (
const C & c, Op & op) { c.find_item(op); };
869 constexpr bool has_equality =
requires (
const C & a,
const C & b) { a == b; };
872 constexpr bool can_modify_each =
requires (C & c,
Modifying_Act & op) { c.mutable_for_each(op); };
877 explicit No_Default(
int v) :
value(v) {}
885 [[nodiscard]]
bool is_valid() const noexcept {
return valid; }
894 auto halves =
l.
maps([] (
int x) {
return x / 2.0; });
896 EXPECT_DOUBLE_EQ(halves.get_first(), 0.5);
897 EXPECT_DOUBLE_EQ(halves.get_last(), 1.5);
901 EXPECT_EQ(names.get_last(),
"3");
904 auto same =
l.
maps([] (
const int & x) ->
const int & {
return x; });
906 auto longs =
l.template maps<long>([] (
int x) {
return x; });
908 auto odd_halves =
l.
maps_if([] (
int x) {
return x % 2; }, [] (
int x) {
return x / 2.0; });
910 EXPECT_EQ(odd_halves.size(), 2u);
911 static_assert(is_same_v<
decltype(
l.
map([] (
int x) {
return x * 1.0f; })),
DynList<float>>);
912 static_assert(is_same_v<
decltype(
l.
map_if([] (
int) {
return true; }, [] (
int x) {
return x * 1.0f; })),
920 for (
int i = 0; i < 1000; ++i)
923 Copy_Counted::copies = 0;
924 const auto by_value =
l.
foldl(Copy_Counted(), [] (Copy_Counted acc,
int x)
926 acc.items.push_back(x);
929 EXPECT_EQ(by_value.items.size(), 1000u);
930 EXPECT_EQ(Copy_Counted::copies, 1u);
934 Copy_Counted::copies = 0;
935 const auto by_rvalue =
l.
foldl(Copy_Counted(), [] (Copy_Counted && acc,
int x) -> Copy_Counted &&
937 acc.items.push_back(x);
938 return std::move(acc);
940 EXPECT_EQ(by_rvalue.items.size(), 1000u);
941 EXPECT_EQ(Copy_Counted::copies, 1u);
944 const auto by_lvalue =
l.
foldl(Copy_Counted(), [] (Copy_Counted & acc,
int x) -> Copy_Counted &
946 acc.items.push_back(x);
949 EXPECT_EQ(by_lvalue.items.size(), 1000u);
951 EXPECT_EQ(
l.
fold(0, [] (
int a,
int x) { return a + x; }), 499500);
952 EXPECT_EQ(
l.
fold_left(0L, [] (
long a,
int x) { return a + x; }), 499500L);
959 static_assert(not foldl_without_operation<DynList<int>>);
960 static_assert(not foldl_without_operation<DynArray<int>>);
966 for (
int i = 0; i < 100; ++i)
969 c.items.push_back(i);
972 const auto even = [] (
const Copy_Counted & c) {
return c.items[0] % 2 == 0; };
974 Copy_Counted::copies = 0;
976 const size_t pair_copies = Copy_Counted::copies;
977 Copy_Counted::copies = 0;
979 EXPECT_EQ(pair_copies, 100u);
980 EXPECT_EQ(Copy_Counted::copies, pair_copies);
981 EXPECT_EQ(get<0>(t).
size(), p.first.size());
988 EXPECT_EQ(
l.
maps(&Item::key).get_last(), 3);
990 EXPECT_FALSE(
l.
all(&Item::valid));
991 EXPECT_TRUE(
l.
exists(&Item::valid));
992 EXPECT_EQ(
l.
count_if(&Item::is_valid), 2u);
993 EXPECT_EQ(
l.
find_ptr(&Item::is_valid)->key, 1);
995 EXPECT_EQ(
l.
partition(&Item::valid).second.size(), 1u);
1002 static_assert(not can_emplace<DynList<int *>,
const int *>);
1003 static_assert(not can_emplace<DynList<long>,
int *>);
1004 static_assert(can_emplace<DynList<int *>,
int *>);
1005 static_assert(can_emplace<DynList<string>,
const char *>);
1012 EXPECT_EQ(
l.
nappend(
string(
"a"),
string(
"b")), 2u);
1013 EXPECT_EQ(
l.
ninsert(
string(
"z")), 1u);
1014 EXPECT_EQ(
l.
size(), 5u);
1019 auto is_two = [] (
const No_Default & x) {
return x.value == 2; };
1020 static_assert(not can_find_item<DynList<No_Default>,
decltype(is_two)>);
1025 ASSERT_TRUE(found.has_value());
1026 EXPECT_EQ(found->value, 2);
1027 EXPECT_FALSE(
l.
find_opt([] (
const No_Default & x) { return x.value == 9; }).has_value());
1054 struct Recursive_Item
1059 bool operator == (
const Recursive_Item & o)
const {
return value == o.value; }
1063TEST(IncompleteItemType, nested_type_with_default_member_initializers)
1068 n.array.append(Nested_Items::Item());
1069 n.dynarray.append(Nested_Items::Item());
1070 n.list.append(Nested_Items::Item());
1072 auto is_two = [] (
const Nested_Items::Item & i) {
return i.value == 2; };
1073 EXPECT_TRUE(get<0>(n.array.find_item(is_two)));
1074 EXPECT_TRUE(get<0>(n.dynarray.find_item(is_two)));
1075 EXPECT_TRUE(get<0>(n.list.find_item(is_two)));
1078TEST(IncompleteItemType, recursive_type)
1083 r.children.emplace();
1084 r.children.append(Recursive_Item());
1086 auto is_two = [] (
const Recursive_Item & i) {
return i.value == 2; };
1087 EXPECT_TRUE(get<0>(
r.children.find_item(is_two)));
1088 EXPECT_TRUE(
r.children.find_opt(is_two).has_value());
1089 EXPECT_TRUE(
r.children.contains(Recursive_Item()));
1090 auto keep_first = [] (Recursive_Item a,
const Recursive_Item &) {
return a; };
1091 EXPECT_EQ(
r.children.reduce(keep_first)->value, 2);
1094TEST(EqualityMethods, sequences_compare_order_and_repetitions)
1103 EXPECT_FALSE(s1 == s2);
1104 EXPECT_FALSE(s3 == s4);
1105 EXPECT_TRUE(s1 != s2);
1110 EXPECT_FALSE(q1 == q2);
1112 EXPECT_TRUE(q1 == q2);
1118 EXPECT_TRUE(a1 == a2);
1120 EXPECT_FALSE(a1 == a2);
1122 f1.push(1); f2.
push(1);
1123 EXPECT_TRUE(f1 == f2);
1127 EXPECT_FALSE(aq1 == aq2);
1129 fq1.put(5); fq2.
put(5);
1130 EXPECT_TRUE(fq1 == fq2);
1133TEST(EqualityMethods, sorted_containers_count_repetitions)
1138 EXPECT_FALSE(t1 == t2);
1140 EXPECT_TRUE(t1 == t2);
1145 EXPECT_FALSE(i1 == i2);
1148 EXPECT_TRUE(i1 == i3);
1152TEST(EqualityMethods, hash_sets_keep_set_equality)
1155 for (
int i : {1, 2, 3})
1157 for (
int i : {3, 1, 2})
1159 EXPECT_TRUE(h1 == h2);
1161 EXPECT_FALSE(h1 == h2);
1164TEST(EqualityMethods, heaps_and_random_sets_have_no_equality)
1167 static_assert(not has_equality<ArrayHeap<int>>);
1168 static_assert(not has_equality<DynBinHeap<int>>);
1169 static_assert(not has_equality<DynArrayHeap<int>>);
1170 static_assert(not has_equality<Random_Set<int>>);
1171 static_assert(has_equality<DynListStack<int>>);
1172 static_assert(has_equality<ArrayQueue<int>>);
1175TEST(RandomSet, iterator_traverses_the_items)
1181 for (
int i = 0; i < 10; ++i)
1184 for (
const int x : q)
1188 for (
auto it = q.
get_it(); it.has_curr(); it.next_ne())
1191 EXPECT_EQ(q.
take(0, 9, 3).
size(), 4u);
1193 q.
each(1, 4, [&visited] (
int) { ++visited; });
1194 EXPECT_EQ(visited, 3u);
1197TEST(ReadOnlyContainers, offer_queries_but_no_modifying_members)
1202 static_assert(not can_modify_each<DynMatrix<int>>);
1203 static_assert(not can_modify_each<DynSkipList<int>>);
1204 static_assert(not can_modify_each<DynIntervalTree<int>>);
1205 static_assert(can_modify_each<DynList<int>>);
1209 for (
size_t i = 0; i < 2; ++i)
1210 for (
size_t j = 0; j < 3; ++j)
1211 m.write(i, j,
int(3 * i + j));
1212 EXPECT_EQ(
m.
nth(4), 4);
1213 EXPECT_EQ(*
m.
find_ptr([] (
int x) { return x == 5; }), 5);
1243 string &
find(
const int &
k)
1250 const string &
find(
const int &
k)
const {
return const_cast<Pair_Map *
>(
this)->
find(
k); }
1258 m.items_list.
append(make_pair(1,
string(
"one")));
1259 m.items_list.
append(make_pair(2,
string(
"two")));
1261 EXPECT_EQ(
m.
keys().get_last(), 2);
1262 EXPECT_EQ(
m.values().get_first(),
"one");
1263 EXPECT_EQ(
m.
items().size(), 2u);
1265 *
m.values_ptr().get_first() =
"uno";
1266 EXPECT_EQ(
m(1),
"uno");
1267 *
m.items_ptr().get_last().second =
"dos";
1268 EXPECT_EQ(
m(2),
"dos");
1270 const Pair_Map & cm =
m;
1272 EXPECT_EQ(*cm.items_ptr().get_last().second,
"dos");
1273 EXPECT_THROW(
m(3), domain_error);
bool traverse(const Container &c, Operation &op) noexcept(noexcept(c.traverse(op)))
Invoke c.traverse(op) as a free function (lvalue operation).
INSTANTIATE_TYPED_TEST_SUITE_P(traverses, Container, Ctypes)
REGISTER_TYPED_TEST_SUITE_P(Container, traverse, for_each, find_ptr, find_index_nth, find_item, iterator_operations, nappend, ninsert, nappend_and_ninsert_count_the_items_added, all, exists, maps, map_synonyms, foldl, filter_ops, nth_out_of_range, take_with_step, mutable_drop_needs_remove, find_item_noexcept_for_nothrow_items, queries_reject_modifying_operations, each_with_position_and_slice, functional_operations, unchecked_iteration_matches_the_checked_one)
Types< DynList< int >, DynDlist< int >, DynArray< int >, HashSet< int, ODhashTable >, HashSet< int, OLhashTable >, DynHashTable< int, LhashTable >, DynHashTable< int, LinearHashTable >, DynSetHash< int >, DynSetTree< int, Treap >, DynSetTree< int, Treap_Rk >, DynSetTree< int, Rand_Tree >, DynSetTree< int, Splay_Tree >, DynSetTree< int, Avl_Tree >, DynSetTree< int, Rb_Tree >, Array< int >, ArrayQueue< int >, ArrayStack< int >, DynListQueue< int >, DynListStack< int >, DynArrayHeap< int >, DynBinHeap< int >, FixedQueue< int >, FixedStack< int > > Ctypes
TYPED_TEST_SUITE_P(Container)
TYPED_TEST_P(Container, traverse)
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
String manipulation utilities.
Zip iterators and functional operations for multiple containers.
Functional programming utilities for Aleph-w containers.
High-level sorting functions for Aleph containers.
size_t size_t int32_t value
Queue implemented with a single dynamic array.
T & put(const T &item)
Copy and put an item in the queue.
Stack implemented with simple dynamic array and with bounds verification.
T & push(const T &data)
Push into stack a copy of data
Simple dynamic array with automatic resizing and functional operations.
Dynamic heap (priority queue) backed by DynArray.
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.
void remove(Key *key)
Removes a key from the hash table using its pointer.
Key * insert(const Key &key)
Inserts key into the hash set.
High-level interval tree with automatic memory management.
Key & append(const Key &iv)
Append an interval (alias for insert_dup).
Dynamic queue of elements of generic type T based on single linked list.
T & put(const T &data)
The type of element.
T get()
Remove the oldest item of the queue.
Dynamic stack of elements of generic type T based on a singly linked list.
T & push(const T &data)
Push an item by copy onto the top of the stack.
Doubly-linked list (defined in tpl_dynList.H).
T & get_first()
Return the first item of the list.
T & append(const T &item)
T & insert(const T &item)
T & get_last()
Return the last item of the list.
size_t size() const noexcept
Return the number of items of the list, in constant time.
Dynamic matrix with sparse storage.
Dynamic set backed by balanced binary search trees with automatic memory management.
size_t remove(const Key &key)
Removes a key from the dynamic set.
Key * insert_dup(const Key &key)
Very simple queue implemented with a contiguous array.
T & put(const T &item) noexcept
Put an item into the queue by copy.
T & push(const T &data) noexcept(std::is_nothrow_copy_assignable_v< T >)
Push a copy of data
Number Theoretic Transform over Z / MOD Z.
constexpr bool contains(const Key &key) const noexcept
Alias for has().
void put(const T &item)
Insert an item by copy.
Minimal std::expected-style result type for C++20.
Mixin providing equality comparison for sequence containers.
bool equal_to(const Container &r) const
Equality test between this and r.
Common methods to the Aleph-w ( ) containers.
std::pair< Aleph::DynList< T >, Aleph::DynList< T > > partition(Operation &op) const
Exclusive partition of container according to a filter criterion.
void emplace(Args &&... args)
Appends a new element into the container by constructing it from the given args.
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > maps(Operation &op) const
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > map(Operation &op) const
bool exists(Operation &op) const
Test for existence in the container of an element satisfying a criterion.
void each(Operation &operation) const
Alias of for_each().
Aleph::DynList< T > to_dynlist() const
Convert container to DynList.
T fold(const T &init, Operation &operation) const
Simplified version of foldl() where the folded type is the same type of elements stored in the contai...
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
std::tuple< Aleph::DynList< T >, Aleph::DynList< T > > tpartition(Operation &op) const
Exclusive partition of container according to a filter criterion.
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > map_if(Prop prop, Operation &op) const
bool all(Operation &operation) const
Check if all the elements of the container satisfy a condition.
Aleph::DynList< T > take(const size_t n) const
Return a list with the first n elements seen in the container during its traversal.
size_t nappend(Args &&... args)
Append n variadic items.
Acc foldl(const Acc &init, Op &op) const
Fold the elements of the container to a specific result.
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > maps_if(Prop prop, Operation &op) const
size_t ninsert(Args &&... args)
Insert n variadic items.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
size_t count_if(Operation &op) const
Count the elements that satisfy a criterion.
Acc fold_left(const Acc &init, Op &op) const
Alias for foldl().
Aleph::DynList< T > drop(const size_t n) const
Drop the first n elements seen in the container during its traversal.
void emplace_ins(Args &&... args)
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...
Common sequential searching methods on containers.
Type & nth(const size_t n)
Return the n-th item of the container.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
and std::copy_constructible< T > std::optional< Type > find_opt(Operation &operation) const
Copy of the first item satisfying a criterion, if any.
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< Container, Operation >::type, Type > Type * find_ptr(Operation &operation) noexcept(mutable_search_is_noexcept< Operation >())
Find a pointer to an item in the container according to a searching criterion.
size_t find_index(Operation &operation) const noexcept(const_search_is_noexcept< Operation >())
Find the position of an item in the container according to a searching criterion.
Common methods to mapping containers.
Key * append(const Key &key)
Alias for insert() (copy version).
DynList< Key > keys() const
Returns a list containing all keys in the table.
auto items() const
Alias for keys().
f(args...) is a valid call whose result is usable as a condition.
bool traverse(Node *root, Op op)
See infix_traverse().
MapOLhash< int, Foo > tbl
Singly linked list implementations with head-tail access.
Freq_Node * pred
Predecessor node in level-order traversal.
Main namespace for Aleph-w library functions.
DynList< typename Container::Item_Type > to_dynlist(const Container &c)
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
static void suffix(Node *root, DynList< Node * > &acc)
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > zip(const Container1 &a, const Container2 &b)
Zip two containers into a list of pairs.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
T foldl(const Container &container, const T &init, Operation operation)
Classic left fold (reduce).
std::decay_t< typename HeadC::Item_Type > T
bool exists(Container &container, Operation &operation)
Return true if at least one element satisfies a predicate.
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
T accumulate(Itor beg, Itor end, T initValue)
Accumulate values in a range.
static void prefix(Node *root, DynList< Node * > &acc)
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
and
Check uniqueness with explicit hash + equality functors.
bool zip_all(Op &&op, const Cs &...cs)
Return true if op returns true for all tuples and containers have equal length.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Operation for_each(Itor beg, const Itor &end, Operation op)
Apply an operation to each element in a range.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Itor::difference_type count_if(Itor beg, const Itor &end, Operation op)
Count elements satisfying a predicate.
Container::Item_Type * find_ptr(Container &container, Pred &pred)
Find the first element satisfying pred.
DynList< T > maps(const C &c, Op op)
Classic map operation.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Closed interval [low, high].
static constexpr size_t N
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation, typename Aleph::concepts_detail::traverse_arg< Container, Operation >::type, Container & >())
Traverse the container via its iterator and performs a conditioned operation on each item.
void operator()(int &x) const
Operations that need to modify the item: queries must reject them.
bool operator()(int &x) const
Matching criterion that cannot throw.
bool operator()(const int &i) const noexcept
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Fixed-capacity binary heap and heapsort algorithms.
Circular queue implementations backed by arrays.
Stack implementations backed by dynamic or fixed arrays.
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 matrix with lazy allocation.
Dynamic set implementations based on hash tables.
Dynamic set implementations based on balanced binary search trees.
Dynamic ordered set implemented with a Skip List.
Unified hash table interface.
Interval tree: augmented BST for overlap/stabbing queries.
Random access queue (bag) with O(1) random pop.