8#include <gtest/gtest.h>
24 vector<int> v = {1, 2, 3, 4, 5};
30TEST(ForEach, ModifyElements)
32 vector<int> v = {1, 2, 3};
53TEST(CountIf, CountEvenNumbers)
55 vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8};
57 [](
int x) { return x % 2 == 0; });
63 vector<int> v = {1, 3, 5, 7, 9};
65 [](
int x) { return x % 2 == 0; });
71 vector<int> v = {2, 4, 6, 8};
73 [](
int x) { return x % 2 == 0; });
79 vector<int> v = {1, 2, 3, 2, 4, 2, 5};
86 vector<int> v = {1, 2, 3, 4, 5};
95TEST(MinElement, FindMinimum)
97 vector<int> v = {5, 2, 8, 1, 9};
107 EXPECT_EQ(it, empty.end());
112 vector<int> v = {5, 2, 8, 1, 9};
124 vector<int> v = {1, 3, 5, 4, 7, 8};
126 [](
int x) { return x % 2 == 0; });
127 EXPECT_NE(it, v.end());
133 vector<int> v = {1, 3, 5, 7, 9};
135 [](
int x) { return x % 2 == 0; });
136 EXPECT_EQ(it, v.end());
141 vector<int> v = {1, 2, 3, 4, 5};
143 EXPECT_NE(it, v.end());
149 vector<int> v = {1, 2, 3, 4, 5};
151 EXPECT_EQ(it, v.end());
160 vector<int> v = {1, 2, 2, 2, 3, 4};
162 EXPECT_NE(it, v.end());
166TEST(SearchN, NotEnoughConsecutive)
168 vector<int> v = {1, 2, 2, 3, 2, 2, 4};
170 EXPECT_EQ(it, v.end());
175 vector<int> v = {1, 2, 3};
177 EXPECT_EQ(it, v.end());
184TEST(AdjacentFind, FindDuplicate)
186 vector<int> v = {1, 2, 3, 3, 4, 5};
188 EXPECT_NE(it, v.end());
194 vector<int> v = {1, 2, 3, 4, 5};
196 EXPECT_EQ(it, v.end());
203 EXPECT_EQ(it, empty.end());
212 vector<int> v1 = {1, 2, 3, 4, 5};
213 vector<int> v2 = {1, 2, 3, 4, 5};
214 EXPECT_TRUE(
Aleph::equal(v1.begin(), v1.end(), v2.begin()));
219 vector<int> v1 = {1, 2, 3, 4, 5};
220 vector<int> v2 = {1, 2, 3, 4, 6};
221 EXPECT_FALSE(
Aleph::equal(v1.begin(), v1.end(), v2.begin()));
230 vector<int> v1 = {1, 2, 3};
231 vector<int> v2 = {1, 2, 4};
233 v1.begin(), v1.end(), v2.begin(), v2.end()));
238 vector<int> v1 = {1, 2, 4};
239 vector<int> v2 = {1, 2, 3};
241 v1.begin(), v1.end(), v2.begin(), v2.end()));
244TEST(LexCompare, PrefixLessThan)
246 vector<int> v1 = {1, 2};
247 vector<int> v2 = {1, 2, 3};
249 v1.begin(), v1.end(), v2.begin(), v2.end()));
258 vector<int> src = {1, 2, 3};
259 vector<int> dst(3, 0);
263 EXPECT_EQ(dst[0], 1);
264 EXPECT_EQ(dst[1], 2);
265 EXPECT_EQ(dst[2], 3);
268TEST(CopyBackward, BasicCopyBackward)
270 vector<int> src = {1, 2, 3};
271 vector<int> dst(3, 0);
275 EXPECT_EQ(dst[0], 1);
276 EXPECT_EQ(dst[1], 2);
277 EXPECT_EQ(dst[2], 3);
286 vector<int> src = {1, 2, 3};
287 vector<int> dst(3, 0);
290 [](
int x) { return x * 2; });
292 EXPECT_EQ(dst[0], 2);
293 EXPECT_EQ(dst[1], 4);
294 EXPECT_EQ(dst[2], 6);
297TEST(Transform, BinaryTransform)
299 vector<int> src1 = {1, 2, 3};
300 vector<int> src2 = {10, 20, 30};
301 vector<int> dst(3, 0);
304 [](
int a,
int b) { return a + b; });
306 EXPECT_EQ(dst[0], 11);
307 EXPECT_EQ(dst[1], 22);
308 EXPECT_EQ(dst[2], 33);
317 vector<int> v1 = {1, 2, 3};
318 vector<int> v2 = {4, 5, 6};
336 vector<int> v = {1, 2, 3};
347 vector<int> v = {1, 2, 3, 4, 5};
362TEST(Generate, GenerateSequence)
374TEST(GenerateN, GenerateNElements)
391TEST(ReplaceIf, ReplaceEvenNumbers)
393 vector<int> v = {1, 2, 3, 4, 5};
396 [](
int x) { return x % 2 == 0; }, 0);
407 vector<int> v = {1, 2, 1, 3, 1};
422TEST(ReplaceCopyIf, BasicReplaceCopy)
424 vector<int> src = {1, 2, 3, 4, 5};
425 vector<int> dst(5, 0);
428 [](
int x) { return x % 2 == 0; }, 0);
430 EXPECT_EQ(dst[0], 1);
431 EXPECT_EQ(dst[1], 0);
432 EXPECT_EQ(dst[2], 3);
433 EXPECT_EQ(dst[3], 0);
434 EXPECT_EQ(dst[4], 5);
443 vector<int> v = {1, 2, 3, 4, 5};
454TEST(Reverse, ReverseSingleElement)
456 vector<int> v = {42};
473TEST(ReverseCopy, BasicReverseCopy)
475 vector<int> src = {1, 2, 3, 4, 5};
476 vector<int> dst(5, 0);
480 EXPECT_EQ(dst[0], 5);
481 EXPECT_EQ(dst[1], 4);
482 EXPECT_EQ(dst[2], 3);
483 EXPECT_EQ(dst[3], 2);
484 EXPECT_EQ(dst[4], 1);
493 vector<int> v = {1, 2, 3, 4, 5};
510 vector<int> v = {1, 2, 4, 5, 6};
512 EXPECT_NE(it, v.end());
516TEST(LowerBound, ValueNotPresent)
518 vector<int> v = {1, 2, 4, 5, 6};
520 EXPECT_NE(it, v.end());
526 vector<int> v = {1, 2, 4, 5, 6};
528 EXPECT_NE(it, v.end());
538 vector<int> v = {1, 2, 3, 4, 5};
542TEST(BinarySearch, ValueMissing)
544 vector<int> v = {1, 2, 3, 4, 5};
554 vector<int> v = {1, 2, 2, 2, 3, 4};
557 EXPECT_EQ(*
range.first, 2);
558 EXPECT_EQ(*
range.second, 3);
567 vector<int> v1 = {1, 2, 3, 4, 5, 6, 7};
568 vector<int> v2 = {2, 4, 6};
569 EXPECT_TRUE(
Aleph::includes(v1.begin(), v1.end(), v2.begin(), v2.end()));
572TEST(Includes, SubsetNotIncluded)
574 vector<int> v1 = {1, 2, 3, 4, 5};
575 vector<int> v2 = {2, 4, 8};
576 EXPECT_FALSE(
Aleph::includes(v1.begin(), v1.end(), v2.begin(), v2.end()));
581 vector<int> v1 = {1, 2, 3};
583 EXPECT_TRUE(
Aleph::includes(v1.begin(), v1.end(), v2.begin(), v2.end()));
592 vector<int> v1 = {1, 3, 5};
593 vector<int> v2 = {2, 4, 6};
594 vector<int> result(6, 0);
596 Aleph::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), result.begin());
598 EXPECT_EQ(result[0], 1);
599 EXPECT_EQ(result[1], 2);
600 EXPECT_EQ(result[2], 3);
601 EXPECT_EQ(result[3], 4);
602 EXPECT_EQ(result[4], 5);
603 EXPECT_EQ(result[5], 6);
608 vector<int> v1 = {1, 2, 3};
610 vector<int> result(3, 0);
612 Aleph::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), result.begin());
614 EXPECT_EQ(result[0], 1);
615 EXPECT_EQ(result[1], 2);
616 EXPECT_EQ(result[2], 3);
625 vector<int> v = {1, 2, 3, 4, 5};
630TEST(Accumulate, ProductElements)
632 vector<int> v = {1, 2, 3, 4, 5};
634 [](
int a,
int b) { return a * b; });
638TEST(Accumulate, WithInitialValue)
640 vector<int> v = {1, 2, 3};
658 vector<int> v1 = {1, 2, 3};
659 vector<int> v2 = {4, 5, 6};
662 EXPECT_EQ(result, 32);
665TEST(InnerProduct, CustomOperations)
667 vector<int> v1 = {1, 2, 3};
668 vector<int> v2 = {1, 1, 1};
672 [](
int a,
int b) { return a + b; },
673 [](
int a,
int b) { return a - b; });
674 EXPECT_EQ(result, 3);
683 vector<int> src = {1, 2, 3, 4, 5};
684 vector<int> dst(5, 0);
688 EXPECT_EQ(dst[0], 1);
689 EXPECT_EQ(dst[1], 3);
690 EXPECT_EQ(dst[2], 6);
691 EXPECT_EQ(dst[3], 10);
692 EXPECT_EQ(dst[4], 15);
695TEST(PartialSum, CustomOperation)
697 vector<int> src = {1, 2, 3, 4};
698 vector<int> dst(4, 0);
701 [](
int a,
int b) { return a * b; });
703 EXPECT_EQ(dst[0], 1);
704 EXPECT_EQ(dst[1], 2);
705 EXPECT_EQ(dst[2], 6);
706 EXPECT_EQ(dst[3], 24);
715 EXPECT_EQ(it, dst.begin());
722TEST(AdjacentDifference, ComputeDifferences)
724 vector<int> src = {1, 3, 6, 10, 15};
725 vector<int> dst(5, 0);
729 EXPECT_EQ(dst[0], 1);
730 EXPECT_EQ(dst[1], 2);
731 EXPECT_EQ(dst[2], 3);
732 EXPECT_EQ(dst[3], 4);
733 EXPECT_EQ(dst[4], 5);
736TEST(AdjacentDifference, CustomOperation)
738 vector<int> src = {1, 2, 4, 8};
739 vector<int> dst(4, 0);
742 [](
int a,
int b) { return a / b; });
744 EXPECT_EQ(dst[0], 1);
745 EXPECT_EQ(dst[1], 2);
746 EXPECT_EQ(dst[2], 2);
747 EXPECT_EQ(dst[3], 2);
756 vector<int> v = {1, 1, 2, 2, 2, 3, 3};
763 EXPECT_EQ(std::distance(v.begin(), it), 3);
768 vector<int> v = {1, 2, 3, 4};
771 EXPECT_EQ(it, v.end());
778TEST(RemoveIf, RemoveEvenNumbers)
780 vector<int> v = {1, 2, 3, 4, 5, 6};
783 [](
int x) { return x % 2 == 0; });
788 EXPECT_EQ(std::distance(v.begin(), it), 3);
793 vector<int> v = {1, 2, 1, 3, 1};
799 EXPECT_EQ(std::distance(v.begin(), it), 2);
808 vector<int> src = {1, 2, 3, 4, 5, 6};
813 std::back_inserter(dst),
814 [](
int x) { return x % 2 == 0; });
817 ASSERT_EQ(dst.size(), 3);
818 EXPECT_EQ(dst[0], 1);
819 EXPECT_EQ(dst[1], 3);
820 EXPECT_EQ(dst[2], 5);
829 vector<int> src = {1, 1, 2, 2, 3, 3};
834 ASSERT_EQ(dst.size(), 3);
835 EXPECT_EQ(dst[0], 1);
836 EXPECT_EQ(dst[1], 2);
837 EXPECT_EQ(dst[2], 3);
846 vector<int> v = {42};
862 for (
int i = 0; i <
N; ++i)
867 [](
int x) { return x % 2 == 0; });
872 EXPECT_EQ(
sum, (
long)(
N - 1) *
N / 2);
881 vector<int> v1 = {1, 2, 3, 4, 5};
882 vector<int> v2 = {1, 2, 3, 9, 5};
886 EXPECT_EQ(*result.first, 4);
887 EXPECT_EQ(*result.second, 9);
892 vector<int> v1 = {1, 2, 3};
893 vector<int> v2 = {1, 2, 3};
897 EXPECT_EQ(result.first, v1.end());
906 vector<int> v = {1, 2, 3, 4, 5, 6};
907 vector<int> sub = {3, 4, 5};
909 auto it =
Aleph::search(v.begin(), v.end(), sub.begin(), sub.end());
911 EXPECT_NE(it, v.end());
917 vector<int> v = {1, 2, 3, 4, 5};
918 vector<int> sub = {3, 5, 4};
920 auto it =
Aleph::search(v.begin(), v.end(), sub.begin(), sub.end());
922 EXPECT_EQ(it, v.end());
Aleph-w implementation of STL-like algorithms.
Number Theoretic Transform over Z / MOD Z.
User-facing facade exported by the umbrella search header.
Main namespace for Aleph-w library functions.
Itor lower_bound(Itor beg, Itor end, const T &value)
Find lower bound in a sorted range.
std::pair< Itor1, Itor2 > mismatch(Itor1 beg, const Itor1 &end, Itor2 cmpBeg, BinaryPredicate op=BinaryPredicate())
Find the first mismatching elements in two ranges.
void reverse(Itor beg, Itor end)
Reverse elements in a range.
Itor unique(Itor __first, Itor __last, BinaryPredicate __binary_pred=BinaryPredicate())
Remove consecutive duplicates in place.
Itor2 partial_sum(Itor1 sourceBeg, Itor1 sourceEnd, Itor2 destBeg)
Compute partial sums of a range.
Itor2 copy_backward(const Itor1 &sourceBeg, Itor1 sourceEnd, Itor2 destEnd)
Copy elements backward from one range to another.
void generate(Itor beg, const Itor &end, Func op)
Generate values for a range.
T inner_product(Itor1 beg1, Itor1 end1, Itor2 beg2, T initValue)
Compute inner product of two ranges.
void replace_if(Itor beg, const Itor &end, UnaryPredicate op, const T &value)
Replace elements satisfying a predicate.
Out_Itor unique_copy(In_Itor __first, In_Itor __last, Out_Itor __result)
Copy unique elements.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
bool lexicographical_compare(Itor1 beg1, const Itor1 &end1, Itor2 beg2, const Itor2 &end2, Comp op=Comp())
Lexicographical comparison of two ranges.
void fill(Itor beg, const Itor &end, const T &value)
Fill a range with a value.
Out_Itor remove_copy_if(In_Itor __first, const In_Itor &__last, Out_Itor __result, Predicate __pred)
Copy elements not satisfying a predicate.
Itor2 adjacent_difference(Itor1 sourceBeg, Itor1 sourceEnd, Itor2 destBeg)
Compute adjacent differences.
Itor adjacent_find(Itor beg, const Itor &end, BinaryPredicate op=BinaryPredicate())
Find first pair of adjacent equal elements.
T accumulate(Itor beg, Itor end, T initValue)
Accumulate values in a range.
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
Itor2 swap_ranges(Itor1 beg1, const Itor1 &end1, Itor2 beg2)
Swap elements between two ranges.
Itor min_element(Itor beg, const Itor &end, CompFunc op=CompFunc())
Find the minimum element in a range.
Itor2 replace_copy_if(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg, UnaryPredicate op, const T &value)
Copy and replace elements satisfying a predicate.
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
Itor upper_bound(Itor beg, Itor end, const T &value)
Find upper bound in a sorted range.
Fw_Itor remove(Fw_Itor __first, const Fw_Itor &__last, const T &__value)
Remove elements equal to a value.
Itor search_n(Itor beg, const Itor &end, Size count, const T &value, BinaryPredicate op=BinaryPredicate())
Find n consecutive elements equal to a value.
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
bool equal(Itor1 beg, const Itor1 &end, Itor2 cmpBeg, BinaryPredicate op=BinaryPredicate())
Test if two ranges are equal.
Itor2 transform(Itor1 sourceBeg, Itor1 sourceEnd, Itor2 destBeg, UnaryFunc op)
Transform elements using a unary operation.
Operation for_each(Itor beg, const Itor &end, Operation op)
Apply an operation to each element in a range.
void fill_n(Itor beg, Size num, const T &value)
Fill n elements with a value.
Fw_Itor remove_if(Fw_Itor __first, const Fw_Itor &__last, Predicate __pred)
Remove elements satisfying a predicate.
Itor3 merge(Itor1 source1Beg, Itor1 source1End, Itor2 source2Beg, Itor2 source2End, Itor3 destBeg)
Merge two sorted ranges.
bool binary_search(Itor beg, Itor end, const T &value)
Binary search for a value.
Itor::difference_type count_if(Itor beg, const Itor &end, Operation op)
Count elements satisfying a predicate.
void generate_n(Itor beg, Size num, Func op)
Generate values for n elements.
bool includes(Itor1 beg, Itor1 end, Itor2 searchBeg, Itor2 searchEnd)
Test if one sorted range includes another.
std::pair< Itor, Itor > equal_range(Itor beg, Itor end, const T &value)
Find equal range in a sorted sequence.
Itor2 reverse_copy(Itor1 sourceBeg, Itor1 sourceEnd, Itor2 destBeg)
Copy elements in reverse order.
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
Itor max_element(const Itor &beg, const Itor &end, CompFunc op=CompFunc())
Find the maximum element in a range.
void replace(Itor beg, const Itor &end, const T &old_value, const T &new_value)
Replace elements equal to a value.
Itor find_if(Itor beg, const Itor &end, UnaryPredicate op)
Find the first element satisfying a predicate.
void rotate(Itor beg, Itor pos, Itor end)
Rotate elements in a range.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Dynamic array container with automatic resizing.
Dynamic doubly linked list implementation.