15 #include "absl/algorithm/container.h"
18 #include <initializer_list>
25 #include <unordered_set>
30 #include "gmock/gmock.h"
31 #include "gtest/gtest.h"
32 #include "absl/base/casts.h"
33 #include "absl/base/macros.h"
34 #include "absl/memory/memory.h"
35 #include "absl/types/span.h"
53 std::unordered_set<int> container_ = {1, 2, 3};
54 std::list<int> sequence_ = {1, 2, 3};
55 std::vector<int> vector_ = {1, 2, 3};
56 int array_[3] = {1, 2, 3};
59 struct AccumulateCalls {
61 std::vector<int>
calls;
64 bool Predicate(
int value) {
return value < 3; }
65 bool BinPredicate(
int v1,
int v2) {
return v1 < v2; }
66 bool Equals(
int v1,
int v2) {
return v1 == v2; }
67 bool IsOdd(
int x) {
return x % 2 != 0; }
69 TEST_F(NonMutatingTest, Distance) {
79 TEST_F(NonMutatingTest, Distance_OverloadedBeginEnd) {
82 std::initializer_list<int>
a = {1, 2, 3};
83 std::valarray<int>
b = {1, 2, 3};
94 std::sort(
c.calls.begin(),
c.calls.end());
100 std::sort(
c2.calls.begin(),
c2.calls.end());
104 TEST_F(NonMutatingTest, FindReturnsCorrectType) {
112 TEST_F(NonMutatingTest, FindIfNot) {
116 TEST_F(NonMutatingTest, FindEnd) {
121 TEST_F(NonMutatingTest, FindEndWithPredicate) {
126 TEST_F(NonMutatingTest, FindFirstOf) {
131 TEST_F(NonMutatingTest, FindFirstOfWithPredicate) {
138 TEST_F(NonMutatingTest, AdjacentFindWithPredicate) {
146 const std::unordered_set<int>& const_container = container_;
150 TEST_F(NonMutatingTest, Mismatch) {
163 sequence_.back() = 5;
175 sequence_.pop_back();
188 constexpr
bool operator==(NoNotEquals)
const {
return true; }
189 constexpr
bool operator!=(NoNotEquals)
const =
delete;
191 std::vector<NoNotEquals>
first;
192 std::list<NoNotEquals>
second;
199 TEST_F(NonMutatingTest, MismatchWithPredicate) {
212 sequence_.front() = 0;
237 TEST_F(NonMutatingTest, Equal) {
244 std::vector<int> vector_plus = {1, 2, 3};
245 vector_plus.push_back(4);
251 TEST_F(NonMutatingTest, EqualWithPredicate) {
258 std::vector<int> vector_plus = {1, 2, 3};
259 vector_plus.push_back(4);
265 TEST_F(NonMutatingTest, IsPermutation) {
266 auto vector_permut_ = vector_;
267 std::next_permutation(vector_permut_.begin(), vector_permut_.end());
272 std::vector<int> vector_plus = {1, 2, 3};
273 vector_plus.push_back(4);
278 TEST_F(NonMutatingTest, IsPermutationWithPredicate) {
279 auto vector_permut_ = vector_;
280 std::next_permutation(vector_permut_.begin(), vector_permut_.end());
285 std::vector<int> vector_plus = {1, 2, 3};
286 vector_plus.push_back(4);
297 TEST_F(NonMutatingTest, SearchWithPredicate) {
304 TEST_F(NonMutatingTest, SearchNWithPredicate) {
308 TEST_F(NonMutatingTest, LowerBound) {
311 EXPECT_EQ(2, std::distance(sequence_.begin(),
i));
315 TEST_F(NonMutatingTest, LowerBoundWithPredicate) {
316 std::vector<int>
v(vector_);
317 std::sort(
v.begin(),
v.end(), std::greater<int>());
323 TEST_F(NonMutatingTest, UpperBound) {
326 EXPECT_EQ(1, std::distance(sequence_.begin(),
i));
330 TEST_F(NonMutatingTest, UpperBoundWithPredicate) {
331 std::vector<int>
v(vector_);
332 std::sort(
v.begin(),
v.end(), std::greater<int>());
338 TEST_F(NonMutatingTest, EqualRange) {
341 EXPECT_EQ(1, std::distance(sequence_.begin(),
p.first));
342 EXPECT_EQ(2, std::distance(sequence_.begin(),
p.second));
345 TEST_F(NonMutatingTest, EqualRangeArray) {
351 TEST_F(NonMutatingTest, EqualRangeWithPredicate) {
352 std::vector<int>
v(vector_);
353 std::sort(
v.begin(),
v.end(), std::greater<int>());
365 TEST_F(NonMutatingTest, BinarySearchWithPredicate) {
366 std::vector<int>
v(vector_);
367 std::sort(
v.begin(),
v.end(), std::greater<int>());
373 TEST_F(NonMutatingTest, MinElement) {
379 TEST_F(NonMutatingTest, MinElementWithPredicate) {
386 TEST_F(NonMutatingTest, MaxElement) {
392 TEST_F(NonMutatingTest, MaxElementWithPredicate) {
399 TEST_F(NonMutatingTest, LexicographicalCompare) {
411 TEST_F(NonMutatingTest, LexicographicalCopmareWithPredicate) {
413 std::greater<int>()));
423 std::vector<int>(
v), std::list<int>(sequence_), std::greater<int>()));
426 TEST_F(NonMutatingTest, Includes) {
427 std::set<int>
s(vector_.begin(), vector_.end());
432 TEST_F(NonMutatingTest, IncludesWithPredicate) {
433 std::vector<int>
v = {3, 2, 1};
434 std::set<int, std::greater<int>>
s(
v.begin(),
v.end());
441 std::list<int> list_ = {1, 2, 3};
445 TEST_F(NumericMutatingTest, Iota) {
447 std::list<int> expected{5, 6, 7};
451 TEST_F(NonMutatingTest, Accumulate) {
455 TEST_F(NonMutatingTest, AccumulateWithBinaryOp) {
460 TEST_F(NonMutatingTest, AccumulateLvalueInit) {
465 TEST_F(NonMutatingTest, AccumulateWithBinaryOpLvalueInit) {
471 TEST_F(NonMutatingTest, InnerProduct) {
473 1000 + 1 * 1 + 2 * 2 + 3 * 3);
476 TEST_F(NonMutatingTest, InnerProductWithBinaryOps) {
478 std::multiplies<int>(), std::plus<int>()),
479 10 * (1 + 1) * (2 + 2) * (3 + 3));
482 TEST_F(NonMutatingTest, InnerProductLvalueInit) {
485 1000 + 1 * 1 + 2 * 2 + 3 * 3);
488 TEST_F(NonMutatingTest, InnerProductWithBinaryOpsLvalueInit) {
491 std::multiplies<int>(), std::plus<int>()),
492 10 * (1 + 1) * (2 + 2) * (3 + 3));
495 TEST_F(NumericMutatingTest, AdjacentDifference) {
498 std::vector<int> expected{1, 2 - 1, 3 - 2, 1000};
502 TEST_F(NumericMutatingTest, AdjacentDifferenceWithBinaryOp) {
504 std::multiplies<int>());
506 std::vector<int> expected{1, 2 * 1, 3 * 2, 1000};
510 TEST_F(NumericMutatingTest, PartialSum) {
513 std::vector<int> expected{1, 1 + 2, 1 + 2 + 3, 1000};
517 TEST_F(NumericMutatingTest, PartialSumWithBinaryOp) {
519 std::multiplies<int>());
521 std::vector<int> expected{1, 1 * 2, 1 * 2 * 3, 1000};
525 TEST_F(NonMutatingTest, LinearSearch) {
531 const std::vector<int>&
v = vector_;
537 const std::vector<int>&
v = vector_;
542 TEST_F(NonMutatingTest, NoneOf) {
543 const std::vector<int>&
v = vector_;
548 TEST_F(NonMutatingTest, MinMaxElementLess) {
549 std::pair<std::vector<int>::const_iterator, std::vector<int>::const_iterator>
555 TEST_F(NonMutatingTest, MinMaxElementGreater) {
556 std::pair<std::vector<int>::const_iterator, std::vector<int>::const_iterator>
562 TEST_F(NonMutatingTest, MinMaxElementNoPredicate) {
563 std::pair<std::vector<int>::const_iterator, std::vector<int>::const_iterator>
571 std::list<int> sorted_ = {1, 2, 3, 4};
572 std::list<int> unsorted_ = {2, 4, 1, 3};
573 std::list<int> reversed_ = {4, 3, 2, 1};
576 TEST_F(SortingTest, IsSorted) {
582 TEST_F(SortingTest, IsSortedWithPredicate) {
588 TEST_F(SortingTest, IsSortedUntil) {
593 TEST_F(SortingTest, NthElement) {
594 std::vector<int> unsorted = {2, 4, 1, 3};
601 TEST(MutatingTest, IsPartitioned) {
610 TEST(MutatingTest, Partition) {
611 std::vector<int> actual = {1, 2, 3, 4, 5};
618 TEST(MutatingTest, StablePartition) {
619 std::vector<int> actual = {1, 2, 3, 4, 5};
624 TEST(MutatingTest, PartitionCopy) {
625 const std::vector<int> initial = {1, 2, 3, 4, 5};
626 std::vector<int> odds, evens;
628 back_inserter(evens), IsOdd);
635 TEST(MutatingTest, PartitionPoint) {
636 const std::vector<int> initial = {1, 3, 5, 2, 4};
641 TEST(MutatingTest, CopyMiddle) {
642 const std::vector<int> initial = {4, -1, -2, -3, 5};
643 const std::list<int>
input = {1, 2, 3};
644 const std::vector<int> expected = {4, 1, 2, 3, 5};
646 std::list<int>
test_list(initial.begin(), initial.end());
655 TEST(MutatingTest, CopyFrontInserter) {
656 const std::list<int> initial = {4, 5};
657 const std::list<int>
input = {1, 2, 3};
658 const std::list<int> expected = {3, 2, 1, 4, 5};
665 TEST(MutatingTest, CopyBackInserter) {
666 const std::vector<int> initial = {4, 5};
667 const std::list<int>
input = {1, 2, 3};
668 const std::vector<int> expected = {4, 5, 1, 2, 3};
670 std::list<int>
test_list(initial.begin(), initial.end());
679 TEST(MutatingTest, CopyN) {
680 const std::vector<int> initial = {1, 2, 3, 4, 5};
681 const std::vector<int> expected = {1, 2};
682 std::vector<int> actual;
687 TEST(MutatingTest, CopyIf) {
688 const std::list<int>
input = {1, 2, 3};
691 [](
int i) {
return i != 2; });
695 TEST(MutatingTest, CopyBackward) {
696 std::vector<int> actual = {1, 2, 3, 4, 5};
697 std::vector<int> expected = {1, 2, 1, 2, 3};
702 TEST(MutatingTest, Move) {
703 std::vector<std::unique_ptr<int>> src;
704 src.emplace_back(absl::make_unique<int>(1));
705 src.emplace_back(absl::make_unique<int>(2));
706 src.emplace_back(absl::make_unique<int>(3));
707 src.emplace_back(absl::make_unique<int>(4));
708 src.emplace_back(absl::make_unique<int>(5));
710 std::vector<std::unique_ptr<int>>
dest = {};
717 TEST(MutatingTest, MoveBackward) {
718 std::vector<std::unique_ptr<int>> actual;
719 actual.emplace_back(absl::make_unique<int>(1));
720 actual.emplace_back(absl::make_unique<int>(2));
721 actual.emplace_back(absl::make_unique<int>(3));
722 actual.emplace_back(absl::make_unique<int>(4));
723 actual.emplace_back(absl::make_unique<int>(5));
730 TEST(MutatingTest, MoveWithRvalue) {
731 auto MakeRValueSrc = [] {
732 std::vector<std::unique_ptr<int>> src;
733 src.emplace_back(absl::make_unique<int>(1));
734 src.emplace_back(absl::make_unique<int>(2));
735 src.emplace_back(absl::make_unique<int>(3));
739 std::vector<std::unique_ptr<int>>
dest = MakeRValueSrc();
745 TEST(MutatingTest, SwapRanges) {
746 std::vector<int> odds = {2, 4, 6};
747 std::vector<int> evens = {1, 3, 5};
762 TEST_F(NonMutatingTest, Transform) {
763 std::vector<int>
x{0, 2, 4},
y,
z;
767 EXPECT_EQ(std::vector<int>({0, -2, -4, 7}),
y);
791 const std::vector<int> initial = {1, 2, 3, 1, 4, 5};
792 const std::vector<int> expected = {4, 2, 3, 4, 4, 5};
798 std::list<int>
test_list(initial.begin(), initial.end());
803 TEST(MutatingTest, ReplaceIf) {
804 std::vector<int> actual = {1, 2, 3, 4, 5};
805 const std::vector<int> expected = {0, 2, 0, 4, 0};
811 TEST(MutatingTest, ReplaceCopy) {
812 const std::vector<int> initial = {1, 2, 3, 1, 4, 5};
813 const std::vector<int> expected = {4, 2, 3, 4, 4, 5};
815 std::vector<int> actual;
826 TEST(MutatingTest, SortWithPredicate) {
837 friend bool operator<(
const Element& e1,
const Element& e2) {
838 return e1.key < e2.key;
841 friend std::ostream&
operator<<(std::ostream&
o,
const Element& e) {
842 return o <<
"{" <<
e.key <<
", " <<
e.value <<
"}";
850 TEST(MutatingTest, StableSort) {
851 std::vector<Element>
test_vector = {{1, 1}, {2, 1}, {2, 0}, {1, 0}, {2, 2}};
854 ElementsAre(IsElement(1, 1), IsElement(1, 0), IsElement(2, 1),
855 IsElement(2, 0), IsElement(2, 2)));
858 TEST(MutatingTest, StableSortWithPredicate) {
859 std::vector<Element>
test_vector = {{1, 1}, {2, 1}, {2, 0}, {1, 0}, {2, 2}};
864 ElementsAre(IsElement(2, 1), IsElement(2, 0), IsElement(2, 2),
865 IsElement(1, 1), IsElement(1, 0)));
868 TEST(MutatingTest, ReplaceCopyIf) {
869 const std::vector<int> initial = {1, 2, 3, 4, 5};
870 const std::vector<int> expected = {0, 2, 0, 4, 0};
872 std::vector<int> actual;
877 TEST(MutatingTest, Fill) {
878 std::vector<int> actual(5);
883 TEST(MutatingTest, FillN) {
884 std::vector<int> actual(5, 0);
889 TEST(MutatingTest, Generate) {
890 std::vector<int> actual(5);
896 TEST(MutatingTest, GenerateN) {
897 std::vector<int> actual(5, 0);
903 TEST(MutatingTest, RemoveCopy) {
904 std::vector<int> actual;
909 TEST(MutatingTest, RemoveCopyIf) {
910 std::vector<int> actual;
916 TEST(MutatingTest, UniqueCopy) {
917 std::vector<int> actual;
919 back_inserter(actual));
923 TEST(MutatingTest, UniqueCopyWithPredicate) {
924 std::vector<int> actual;
926 back_inserter(actual),
927 [](
int x,
int y) {
return (
x < 0) == (
y < 0); });
941 TEST(MutatingTest, ReverseCopy) {
942 std::vector<int> actual;
948 std::vector<int> actual = {1, 2, 3, 4};
954 TEST(MutatingTest, RotateCopy) {
955 std::vector<int> initial = {1, 2, 3, 4};
956 std::vector<int> actual;
964 std::vector<int> actual = {1, 2, 3, 4, 5};
969 TEST(MutatingTest, PartialSort) {
970 std::vector<int> sequence{5, 3, 42, 0};
977 TEST(MutatingTest, PartialSortCopy) {
978 const std::vector<int> initial = {5, 3, 42, 0};
979 std::vector<int> actual(2);
987 std::vector<int> actual;
988 absl::c_merge(std::vector<int>{1, 3, 5}, std::vector<int>{2, 4},
989 back_inserter(actual));
993 TEST(MutatingTest, MergeWithComparator) {
994 std::vector<int> actual;
995 absl::c_merge(std::vector<int>{5, 3, 1}, std::vector<int>{4, 2},
996 back_inserter(actual), std::greater<int>());
1000 TEST(MutatingTest, InplaceMerge) {
1001 std::vector<int> actual = {1, 3, 5, 2, 4};
1006 TEST(MutatingTest, InplaceMergeWithComparator) {
1007 std::vector<int> actual = {5, 3, 1, 4, 2};
1014 std::vector<int>
a_ = {1, 2, 3};
1015 std::vector<int>
b_ = {1, 3, 5};
1017 std::vector<int> a_reversed_ = {3, 2, 1};
1018 std::vector<int> b_reversed_ = {5, 3, 1};
1021 TEST_F(SetOperationsTest, SetUnion) {
1022 std::vector<int> actual;
1027 TEST_F(SetOperationsTest, SetUnionWithComparator) {
1028 std::vector<int> actual;
1030 std::greater<int>());
1034 TEST_F(SetOperationsTest, SetIntersection) {
1035 std::vector<int> actual;
1040 TEST_F(SetOperationsTest, SetIntersectionWithComparator) {
1041 std::vector<int> actual;
1043 std::greater<int>());
1047 TEST_F(SetOperationsTest, SetDifference) {
1048 std::vector<int> actual;
1053 TEST_F(SetOperationsTest, SetDifferenceWithComparator) {
1054 std::vector<int> actual;
1056 std::greater<int>());
1060 TEST_F(SetOperationsTest, SetSymmetricDifference) {
1061 std::vector<int> actual;
1066 TEST_F(SetOperationsTest, SetSymmetricDifferenceWithComparator) {
1067 std::vector<int> actual;
1069 back_inserter(actual), std::greater<int>());
1073 TEST(HeapOperationsTest, WithoutComparator) {
1074 std::vector<int>
heap = {1, 2, 3};
1090 TEST(HeapOperationsTest, WithComparator) {
1091 using greater = std::greater<int>;
1092 std::vector<int>
heap = {3, 2, 1};
1108 TEST(MutatingTest, PermutationOperations) {
1109 std::vector<int> initial = {1, 2, 3, 4};
1110 std::vector<int> permuted = initial;
1116 std::vector<int> permuted2 = initial;