00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/algorithm/container.h"
00016
00017 #include <functional>
00018 #include <initializer_list>
00019 #include <iterator>
00020 #include <list>
00021 #include <memory>
00022 #include <ostream>
00023 #include <random>
00024 #include <set>
00025 #include <unordered_set>
00026 #include <utility>
00027 #include <valarray>
00028 #include <vector>
00029
00030 #include "gmock/gmock.h"
00031 #include "gtest/gtest.h"
00032 #include "absl/base/casts.h"
00033 #include "absl/base/macros.h"
00034 #include "absl/memory/memory.h"
00035 #include "absl/types/span.h"
00036
00037 namespace {
00038
00039 using ::testing::Each;
00040 using ::testing::ElementsAre;
00041 using ::testing::Gt;
00042 using ::testing::IsNull;
00043 using ::testing::Lt;
00044 using ::testing::Pointee;
00045 using ::testing::Truly;
00046 using ::testing::UnorderedElementsAre;
00047
00048
00049
00050
00051 class NonMutatingTest : public testing::Test {
00052 protected:
00053 std::unordered_set<int> container_ = {1, 2, 3};
00054 std::list<int> sequence_ = {1, 2, 3};
00055 std::vector<int> vector_ = {1, 2, 3};
00056 int array_[3] = {1, 2, 3};
00057 };
00058
00059 struct AccumulateCalls {
00060 void operator()(int value) {
00061 calls.push_back(value);
00062 }
00063 std::vector<int> calls;
00064 };
00065
00066 bool Predicate(int value) { return value < 3; }
00067 bool BinPredicate(int v1, int v2) { return v1 < v2; }
00068 bool Equals(int v1, int v2) { return v1 == v2; }
00069 bool IsOdd(int x) { return x % 2 != 0; }
00070
00071
00072 TEST_F(NonMutatingTest, Distance) {
00073 EXPECT_EQ(container_.size(), absl::c_distance(container_));
00074 EXPECT_EQ(sequence_.size(), absl::c_distance(sequence_));
00075 EXPECT_EQ(vector_.size(), absl::c_distance(vector_));
00076 EXPECT_EQ(ABSL_ARRAYSIZE(array_), absl::c_distance(array_));
00077
00078
00079 EXPECT_EQ(vector_.size(), absl::c_distance(std::vector<int>(vector_)));
00080 }
00081
00082 TEST_F(NonMutatingTest, Distance_OverloadedBeginEnd) {
00083
00084
00085 std::initializer_list<int> a = {1, 2, 3};
00086 std::valarray<int> b = {1, 2, 3};
00087 EXPECT_EQ(3, absl::c_distance(a));
00088 EXPECT_EQ(3, absl::c_distance(b));
00089
00090
00091
00092 }
00093
00094 TEST_F(NonMutatingTest, ForEach) {
00095 AccumulateCalls c = absl::c_for_each(container_, AccumulateCalls());
00096
00097 std::sort(c.calls.begin(), c.calls.end());
00098 EXPECT_EQ(vector_, c.calls);
00099
00100
00101 AccumulateCalls c2 =
00102 absl::c_for_each(std::unordered_set<int>(container_), AccumulateCalls());
00103 std::sort(c2.calls.begin(), c2.calls.end());
00104 EXPECT_EQ(vector_, c2.calls);
00105 }
00106
00107 TEST_F(NonMutatingTest, FindReturnsCorrectType) {
00108 auto it = absl::c_find(container_, 3);
00109 EXPECT_EQ(3, *it);
00110 absl::c_find(absl::implicit_cast<const std::list<int>&>(sequence_), 3);
00111 }
00112
00113 TEST_F(NonMutatingTest, FindIf) { absl::c_find_if(container_, Predicate); }
00114
00115 TEST_F(NonMutatingTest, FindIfNot) {
00116 absl::c_find_if_not(container_, Predicate);
00117 }
00118
00119 TEST_F(NonMutatingTest, FindEnd) {
00120 absl::c_find_end(sequence_, vector_);
00121 absl::c_find_end(vector_, sequence_);
00122 }
00123
00124 TEST_F(NonMutatingTest, FindEndWithPredicate) {
00125 absl::c_find_end(sequence_, vector_, BinPredicate);
00126 absl::c_find_end(vector_, sequence_, BinPredicate);
00127 }
00128
00129 TEST_F(NonMutatingTest, FindFirstOf) {
00130 absl::c_find_first_of(container_, sequence_);
00131 absl::c_find_first_of(sequence_, container_);
00132 }
00133
00134 TEST_F(NonMutatingTest, FindFirstOfWithPredicate) {
00135 absl::c_find_first_of(container_, sequence_, BinPredicate);
00136 absl::c_find_first_of(sequence_, container_, BinPredicate);
00137 }
00138
00139 TEST_F(NonMutatingTest, AdjacentFind) { absl::c_adjacent_find(sequence_); }
00140
00141 TEST_F(NonMutatingTest, AdjacentFindWithPredicate) {
00142 absl::c_adjacent_find(sequence_, BinPredicate);
00143 }
00144
00145 TEST_F(NonMutatingTest, Count) { EXPECT_EQ(1, absl::c_count(container_, 3)); }
00146
00147 TEST_F(NonMutatingTest, CountIf) {
00148 EXPECT_EQ(2, absl::c_count_if(container_, Predicate));
00149 const std::unordered_set<int>& const_container = container_;
00150 EXPECT_EQ(2, absl::c_count_if(const_container, Predicate));
00151 }
00152
00153 TEST_F(NonMutatingTest, Mismatch) {
00154 absl::c_mismatch(container_, sequence_);
00155 absl::c_mismatch(sequence_, container_);
00156 }
00157
00158 TEST_F(NonMutatingTest, MismatchWithPredicate) {
00159 absl::c_mismatch(container_, sequence_, BinPredicate);
00160 absl::c_mismatch(sequence_, container_, BinPredicate);
00161 }
00162
00163 TEST_F(NonMutatingTest, Equal) {
00164 EXPECT_TRUE(absl::c_equal(vector_, sequence_));
00165 EXPECT_TRUE(absl::c_equal(sequence_, vector_));
00166
00167
00168 std::vector<int> vector_plus = {1, 2, 3};
00169 vector_plus.push_back(4);
00170 EXPECT_FALSE(absl::c_equal(vector_plus, sequence_));
00171 EXPECT_FALSE(absl::c_equal(sequence_, vector_plus));
00172 }
00173
00174 TEST_F(NonMutatingTest, EqualWithPredicate) {
00175 EXPECT_TRUE(absl::c_equal(vector_, sequence_, Equals));
00176 EXPECT_TRUE(absl::c_equal(sequence_, vector_, Equals));
00177
00178
00179 std::vector<int> vector_plus = {1, 2, 3};
00180 vector_plus.push_back(4);
00181 EXPECT_FALSE(absl::c_equal(vector_plus, sequence_, Equals));
00182 EXPECT_FALSE(absl::c_equal(sequence_, vector_plus, Equals));
00183 }
00184
00185 TEST_F(NonMutatingTest, IsPermutation) {
00186 auto vector_permut_ = vector_;
00187 std::next_permutation(vector_permut_.begin(), vector_permut_.end());
00188 EXPECT_TRUE(absl::c_is_permutation(vector_permut_, sequence_));
00189 EXPECT_TRUE(absl::c_is_permutation(sequence_, vector_permut_));
00190
00191
00192 std::vector<int> vector_plus = {1, 2, 3};
00193 vector_plus.push_back(4);
00194 EXPECT_FALSE(absl::c_is_permutation(vector_plus, sequence_));
00195 EXPECT_FALSE(absl::c_is_permutation(sequence_, vector_plus));
00196 }
00197
00198 TEST_F(NonMutatingTest, IsPermutationWithPredicate) {
00199 auto vector_permut_ = vector_;
00200 std::next_permutation(vector_permut_.begin(), vector_permut_.end());
00201 EXPECT_TRUE(absl::c_is_permutation(vector_permut_, sequence_, Equals));
00202 EXPECT_TRUE(absl::c_is_permutation(sequence_, vector_permut_, Equals));
00203
00204
00205 std::vector<int> vector_plus = {1, 2, 3};
00206 vector_plus.push_back(4);
00207 EXPECT_FALSE(absl::c_is_permutation(vector_plus, sequence_, Equals));
00208 EXPECT_FALSE(absl::c_is_permutation(sequence_, vector_plus, Equals));
00209 }
00210
00211 TEST_F(NonMutatingTest, Search) {
00212 absl::c_search(sequence_, vector_);
00213 absl::c_search(vector_, sequence_);
00214 absl::c_search(array_, sequence_);
00215 }
00216
00217 TEST_F(NonMutatingTest, SearchWithPredicate) {
00218 absl::c_search(sequence_, vector_, BinPredicate);
00219 absl::c_search(vector_, sequence_, BinPredicate);
00220 }
00221
00222 TEST_F(NonMutatingTest, SearchN) { absl::c_search_n(sequence_, 3, 1); }
00223
00224 TEST_F(NonMutatingTest, SearchNWithPredicate) {
00225 absl::c_search_n(sequence_, 3, 1, BinPredicate);
00226 }
00227
00228 TEST_F(NonMutatingTest, LowerBound) {
00229 std::list<int>::iterator i = absl::c_lower_bound(sequence_, 3);
00230 ASSERT_TRUE(i != sequence_.end());
00231 EXPECT_EQ(2, std::distance(sequence_.begin(), i));
00232 EXPECT_EQ(3, *i);
00233 }
00234
00235 TEST_F(NonMutatingTest, LowerBoundWithPredicate) {
00236 std::vector<int> v(vector_);
00237 std::sort(v.begin(), v.end(), std::greater<int>());
00238 std::vector<int>::iterator i = absl::c_lower_bound(v, 3, std::greater<int>());
00239 EXPECT_TRUE(i == v.begin());
00240 EXPECT_EQ(3, *i);
00241 }
00242
00243 TEST_F(NonMutatingTest, UpperBound) {
00244 std::list<int>::iterator i = absl::c_upper_bound(sequence_, 1);
00245 ASSERT_TRUE(i != sequence_.end());
00246 EXPECT_EQ(1, std::distance(sequence_.begin(), i));
00247 EXPECT_EQ(2, *i);
00248 }
00249
00250 TEST_F(NonMutatingTest, UpperBoundWithPredicate) {
00251 std::vector<int> v(vector_);
00252 std::sort(v.begin(), v.end(), std::greater<int>());
00253 std::vector<int>::iterator i = absl::c_upper_bound(v, 1, std::greater<int>());
00254 EXPECT_EQ(3, i - v.begin());
00255 EXPECT_TRUE(i == v.end());
00256 }
00257
00258 TEST_F(NonMutatingTest, EqualRange) {
00259 std::pair<std::list<int>::iterator, std::list<int>::iterator> p =
00260 absl::c_equal_range(sequence_, 2);
00261 EXPECT_EQ(1, std::distance(sequence_.begin(), p.first));
00262 EXPECT_EQ(2, std::distance(sequence_.begin(), p.second));
00263 }
00264
00265 TEST_F(NonMutatingTest, EqualRangeArray) {
00266 auto p = absl::c_equal_range(array_, 2);
00267 EXPECT_EQ(1, std::distance(std::begin(array_), p.first));
00268 EXPECT_EQ(2, std::distance(std::begin(array_), p.second));
00269 }
00270
00271 TEST_F(NonMutatingTest, EqualRangeWithPredicate) {
00272 std::vector<int> v(vector_);
00273 std::sort(v.begin(), v.end(), std::greater<int>());
00274 std::pair<std::vector<int>::iterator, std::vector<int>::iterator> p =
00275 absl::c_equal_range(v, 2, std::greater<int>());
00276 EXPECT_EQ(1, std::distance(v.begin(), p.first));
00277 EXPECT_EQ(2, std::distance(v.begin(), p.second));
00278 }
00279
00280 TEST_F(NonMutatingTest, BinarySearch) {
00281 EXPECT_TRUE(absl::c_binary_search(vector_, 2));
00282 EXPECT_TRUE(absl::c_binary_search(std::vector<int>(vector_), 2));
00283 }
00284
00285 TEST_F(NonMutatingTest, BinarySearchWithPredicate) {
00286 std::vector<int> v(vector_);
00287 std::sort(v.begin(), v.end(), std::greater<int>());
00288 EXPECT_TRUE(absl::c_binary_search(v, 2, std::greater<int>()));
00289 EXPECT_TRUE(
00290 absl::c_binary_search(std::vector<int>(v), 2, std::greater<int>()));
00291 }
00292
00293 TEST_F(NonMutatingTest, MinElement) {
00294 std::list<int>::iterator i = absl::c_min_element(sequence_);
00295 ASSERT_TRUE(i != sequence_.end());
00296 EXPECT_EQ(*i, 1);
00297 }
00298
00299 TEST_F(NonMutatingTest, MinElementWithPredicate) {
00300 std::list<int>::iterator i =
00301 absl::c_min_element(sequence_, std::greater<int>());
00302 ASSERT_TRUE(i != sequence_.end());
00303 EXPECT_EQ(*i, 3);
00304 }
00305
00306 TEST_F(NonMutatingTest, MaxElement) {
00307 std::list<int>::iterator i = absl::c_max_element(sequence_);
00308 ASSERT_TRUE(i != sequence_.end());
00309 EXPECT_EQ(*i, 3);
00310 }
00311
00312 TEST_F(NonMutatingTest, MaxElementWithPredicate) {
00313 std::list<int>::iterator i =
00314 absl::c_max_element(sequence_, std::greater<int>());
00315 ASSERT_TRUE(i != sequence_.end());
00316 EXPECT_EQ(*i, 1);
00317 }
00318
00319 TEST_F(NonMutatingTest, LexicographicalCompare) {
00320 EXPECT_FALSE(absl::c_lexicographical_compare(sequence_, sequence_));
00321
00322 std::vector<int> v;
00323 v.push_back(1);
00324 v.push_back(2);
00325 v.push_back(4);
00326
00327 EXPECT_TRUE(absl::c_lexicographical_compare(sequence_, v));
00328 EXPECT_TRUE(absl::c_lexicographical_compare(std::list<int>(sequence_), v));
00329 }
00330
00331 TEST_F(NonMutatingTest, LexicographicalCopmareWithPredicate) {
00332 EXPECT_FALSE(absl::c_lexicographical_compare(sequence_, sequence_,
00333 std::greater<int>()));
00334
00335 std::vector<int> v;
00336 v.push_back(1);
00337 v.push_back(2);
00338 v.push_back(4);
00339
00340 EXPECT_TRUE(
00341 absl::c_lexicographical_compare(v, sequence_, std::greater<int>()));
00342 EXPECT_TRUE(absl::c_lexicographical_compare(
00343 std::vector<int>(v), std::list<int>(sequence_), std::greater<int>()));
00344 }
00345
00346 TEST_F(NonMutatingTest, Includes) {
00347 std::set<int> s(vector_.begin(), vector_.end());
00348 s.insert(4);
00349 EXPECT_TRUE(absl::c_includes(s, vector_));
00350 }
00351
00352 TEST_F(NonMutatingTest, IncludesWithPredicate) {
00353 std::vector<int> v = {3, 2, 1};
00354 std::set<int, std::greater<int>> s(v.begin(), v.end());
00355 s.insert(4);
00356 EXPECT_TRUE(absl::c_includes(s, v, std::greater<int>()));
00357 }
00358
00359 class NumericMutatingTest : public testing::Test {
00360 protected:
00361 std::list<int> list_ = {1, 2, 3};
00362 std::vector<int> output_;
00363 };
00364
00365 TEST_F(NumericMutatingTest, Iota) {
00366 absl::c_iota(list_, 5);
00367 std::list<int> expected{5, 6, 7};
00368 EXPECT_EQ(list_, expected);
00369 }
00370
00371 TEST_F(NonMutatingTest, Accumulate) {
00372 EXPECT_EQ(absl::c_accumulate(sequence_, 4), 1 + 2 + 3 + 4);
00373 }
00374
00375 TEST_F(NonMutatingTest, AccumulateWithBinaryOp) {
00376 EXPECT_EQ(absl::c_accumulate(sequence_, 4, std::multiplies<int>()),
00377 1 * 2 * 3 * 4);
00378 }
00379
00380 TEST_F(NonMutatingTest, AccumulateLvalueInit) {
00381 int lvalue = 4;
00382 EXPECT_EQ(absl::c_accumulate(sequence_, lvalue), 1 + 2 + 3 + 4);
00383 }
00384
00385 TEST_F(NonMutatingTest, AccumulateWithBinaryOpLvalueInit) {
00386 int lvalue = 4;
00387 EXPECT_EQ(absl::c_accumulate(sequence_, lvalue, std::multiplies<int>()),
00388 1 * 2 * 3 * 4);
00389 }
00390
00391 TEST_F(NonMutatingTest, InnerProduct) {
00392 EXPECT_EQ(absl::c_inner_product(sequence_, vector_, 1000),
00393 1000 + 1 * 1 + 2 * 2 + 3 * 3);
00394 }
00395
00396 TEST_F(NonMutatingTest, InnerProductWithBinaryOps) {
00397 EXPECT_EQ(absl::c_inner_product(sequence_, vector_, 10,
00398 std::multiplies<int>(), std::plus<int>()),
00399 10 * (1 + 1) * (2 + 2) * (3 + 3));
00400 }
00401
00402 TEST_F(NonMutatingTest, InnerProductLvalueInit) {
00403 int lvalue = 1000;
00404 EXPECT_EQ(absl::c_inner_product(sequence_, vector_, lvalue),
00405 1000 + 1 * 1 + 2 * 2 + 3 * 3);
00406 }
00407
00408 TEST_F(NonMutatingTest, InnerProductWithBinaryOpsLvalueInit) {
00409 int lvalue = 10;
00410 EXPECT_EQ(absl::c_inner_product(sequence_, vector_, lvalue,
00411 std::multiplies<int>(), std::plus<int>()),
00412 10 * (1 + 1) * (2 + 2) * (3 + 3));
00413 }
00414
00415 TEST_F(NumericMutatingTest, AdjacentDifference) {
00416 auto last = absl::c_adjacent_difference(list_, std::back_inserter(output_));
00417 *last = 1000;
00418 std::vector<int> expected{1, 2 - 1, 3 - 2, 1000};
00419 EXPECT_EQ(output_, expected);
00420 }
00421
00422 TEST_F(NumericMutatingTest, AdjacentDifferenceWithBinaryOp) {
00423 auto last = absl::c_adjacent_difference(list_, std::back_inserter(output_),
00424 std::multiplies<int>());
00425 *last = 1000;
00426 std::vector<int> expected{1, 2 * 1, 3 * 2, 1000};
00427 EXPECT_EQ(output_, expected);
00428 }
00429
00430 TEST_F(NumericMutatingTest, PartialSum) {
00431 auto last = absl::c_partial_sum(list_, std::back_inserter(output_));
00432 *last = 1000;
00433 std::vector<int> expected{1, 1 + 2, 1 + 2 + 3, 1000};
00434 EXPECT_EQ(output_, expected);
00435 }
00436
00437 TEST_F(NumericMutatingTest, PartialSumWithBinaryOp) {
00438 auto last = absl::c_partial_sum(list_, std::back_inserter(output_),
00439 std::multiplies<int>());
00440 *last = 1000;
00441 std::vector<int> expected{1, 1 * 2, 1 * 2 * 3, 1000};
00442 EXPECT_EQ(output_, expected);
00443 }
00444
00445 TEST_F(NonMutatingTest, LinearSearch) {
00446 EXPECT_TRUE(absl::c_linear_search(container_, 3));
00447 EXPECT_FALSE(absl::c_linear_search(container_, 4));
00448 }
00449
00450 TEST_F(NonMutatingTest, AllOf) {
00451 const std::vector<int>& v = vector_;
00452 EXPECT_FALSE(absl::c_all_of(v, [](int x) { return x > 1; }));
00453 EXPECT_TRUE(absl::c_all_of(v, [](int x) { return x > 0; }));
00454 }
00455
00456 TEST_F(NonMutatingTest, AnyOf) {
00457 const std::vector<int>& v = vector_;
00458 EXPECT_TRUE(absl::c_any_of(v, [](int x) { return x > 2; }));
00459 EXPECT_FALSE(absl::c_any_of(v, [](int x) { return x > 5; }));
00460 }
00461
00462 TEST_F(NonMutatingTest, NoneOf) {
00463 const std::vector<int>& v = vector_;
00464 EXPECT_FALSE(absl::c_none_of(v, [](int x) { return x > 2; }));
00465 EXPECT_TRUE(absl::c_none_of(v, [](int x) { return x > 5; }));
00466 }
00467
00468 TEST_F(NonMutatingTest, MinMaxElementLess) {
00469 std::pair<std::vector<int>::const_iterator, std::vector<int>::const_iterator>
00470 p = absl::c_minmax_element(vector_, std::less<int>());
00471 EXPECT_TRUE(p.first == vector_.begin());
00472 EXPECT_TRUE(p.second == vector_.begin() + 2);
00473 }
00474
00475 TEST_F(NonMutatingTest, MinMaxElementGreater) {
00476 std::pair<std::vector<int>::const_iterator, std::vector<int>::const_iterator>
00477 p = absl::c_minmax_element(vector_, std::greater<int>());
00478 EXPECT_TRUE(p.first == vector_.begin() + 2);
00479 EXPECT_TRUE(p.second == vector_.begin());
00480 }
00481
00482 TEST_F(NonMutatingTest, MinMaxElementNoPredicate) {
00483 std::pair<std::vector<int>::const_iterator, std::vector<int>::const_iterator>
00484 p = absl::c_minmax_element(vector_);
00485 EXPECT_TRUE(p.first == vector_.begin());
00486 EXPECT_TRUE(p.second == vector_.begin() + 2);
00487 }
00488
00489 class SortingTest : public testing::Test {
00490 protected:
00491 std::list<int> sorted_ = {1, 2, 3, 4};
00492 std::list<int> unsorted_ = {2, 4, 1, 3};
00493 std::list<int> reversed_ = {4, 3, 2, 1};
00494 };
00495
00496 TEST_F(SortingTest, IsSorted) {
00497 EXPECT_TRUE(absl::c_is_sorted(sorted_));
00498 EXPECT_FALSE(absl::c_is_sorted(unsorted_));
00499 EXPECT_FALSE(absl::c_is_sorted(reversed_));
00500 }
00501
00502 TEST_F(SortingTest, IsSortedWithPredicate) {
00503 EXPECT_FALSE(absl::c_is_sorted(sorted_, std::greater<int>()));
00504 EXPECT_FALSE(absl::c_is_sorted(unsorted_, std::greater<int>()));
00505 EXPECT_TRUE(absl::c_is_sorted(reversed_, std::greater<int>()));
00506 }
00507
00508 TEST_F(SortingTest, IsSortedUntil) {
00509 EXPECT_EQ(1, *absl::c_is_sorted_until(unsorted_));
00510 EXPECT_EQ(4, *absl::c_is_sorted_until(unsorted_, std::greater<int>()));
00511 }
00512
00513 TEST_F(SortingTest, NthElement) {
00514 std::vector<int> unsorted = {2, 4, 1, 3};
00515 absl::c_nth_element(unsorted, unsorted.begin() + 2);
00516 EXPECT_THAT(unsorted,
00517 ElementsAre(Lt(3), Lt(3), 3, Gt(3)));
00518 absl::c_nth_element(unsorted, unsorted.begin() + 2, std::greater<int>());
00519 EXPECT_THAT(unsorted,
00520 ElementsAre(Gt(2), Gt(2), 2, Lt(2)));
00521 }
00522
00523 TEST(MutatingTest, IsPartitioned) {
00524 EXPECT_TRUE(
00525 absl::c_is_partitioned(std::vector<int>{1, 3, 5, 2, 4, 6}, IsOdd));
00526 EXPECT_FALSE(
00527 absl::c_is_partitioned(std::vector<int>{1, 2, 3, 4, 5, 6}, IsOdd));
00528 EXPECT_FALSE(
00529 absl::c_is_partitioned(std::vector<int>{2, 4, 6, 1, 3, 5}, IsOdd));
00530 }
00531
00532 TEST(MutatingTest, Partition) {
00533 std::vector<int> actual = {1, 2, 3, 4, 5};
00534 absl::c_partition(actual, IsOdd);
00535 EXPECT_THAT(actual, Truly([](const std::vector<int>& c) {
00536 return absl::c_is_partitioned(c, IsOdd);
00537 }));
00538 }
00539
00540 TEST(MutatingTest, StablePartition) {
00541 std::vector<int> actual = {1, 2, 3, 4, 5};
00542 absl::c_stable_partition(actual, IsOdd);
00543 EXPECT_THAT(actual, ElementsAre(1, 3, 5, 2, 4));
00544 }
00545
00546 TEST(MutatingTest, PartitionCopy) {
00547 const std::vector<int> initial = {1, 2, 3, 4, 5};
00548 std::vector<int> odds, evens;
00549 auto ends = absl::c_partition_copy(initial, back_inserter(odds),
00550 back_inserter(evens), IsOdd);
00551 *ends.first = 7;
00552 *ends.second = 6;
00553 EXPECT_THAT(odds, ElementsAre(1, 3, 5, 7));
00554 EXPECT_THAT(evens, ElementsAre(2, 4, 6));
00555 }
00556
00557 TEST(MutatingTest, PartitionPoint) {
00558 const std::vector<int> initial = {1, 3, 5, 2, 4};
00559 auto middle = absl::c_partition_point(initial, IsOdd);
00560 EXPECT_EQ(2, *middle);
00561 }
00562
00563 TEST(MutatingTest, CopyMiddle) {
00564 const std::vector<int> initial = {4, -1, -2, -3, 5};
00565 const std::list<int> input = {1, 2, 3};
00566 const std::vector<int> expected = {4, 1, 2, 3, 5};
00567
00568 std::list<int> test_list(initial.begin(), initial.end());
00569 absl::c_copy(input, ++test_list.begin());
00570 EXPECT_EQ(std::list<int>(expected.begin(), expected.end()), test_list);
00571
00572 std::vector<int> test_vector = initial;
00573 absl::c_copy(input, test_vector.begin() + 1);
00574 EXPECT_EQ(expected, test_vector);
00575 }
00576
00577 TEST(MutatingTest, CopyFrontInserter) {
00578 const std::list<int> initial = {4, 5};
00579 const std::list<int> input = {1, 2, 3};
00580 const std::list<int> expected = {3, 2, 1, 4, 5};
00581
00582 std::list<int> test_list = initial;
00583 absl::c_copy(input, std::front_inserter(test_list));
00584 EXPECT_EQ(expected, test_list);
00585 }
00586
00587 TEST(MutatingTest, CopyBackInserter) {
00588 const std::vector<int> initial = {4, 5};
00589 const std::list<int> input = {1, 2, 3};
00590 const std::vector<int> expected = {4, 5, 1, 2, 3};
00591
00592 std::list<int> test_list(initial.begin(), initial.end());
00593 absl::c_copy(input, std::back_inserter(test_list));
00594 EXPECT_EQ(std::list<int>(expected.begin(), expected.end()), test_list);
00595
00596 std::vector<int> test_vector = initial;
00597 absl::c_copy(input, std::back_inserter(test_vector));
00598 EXPECT_EQ(expected, test_vector);
00599 }
00600
00601 TEST(MutatingTest, CopyN) {
00602 const std::vector<int> initial = {1, 2, 3, 4, 5};
00603 const std::vector<int> expected = {1, 2};
00604 std::vector<int> actual;
00605 absl::c_copy_n(initial, 2, back_inserter(actual));
00606 EXPECT_EQ(expected, actual);
00607 }
00608
00609 TEST(MutatingTest, CopyIf) {
00610 const std::list<int> input = {1, 2, 3};
00611 std::vector<int> output;
00612 absl::c_copy_if(input, std::back_inserter(output),
00613 [](int i) { return i != 2; });
00614 EXPECT_THAT(output, ElementsAre(1, 3));
00615 }
00616
00617 TEST(MutatingTest, CopyBackward) {
00618 std::vector<int> actual = {1, 2, 3, 4, 5};
00619 std::vector<int> expected = {1, 2, 1, 2, 3};
00620 absl::c_copy_backward(absl::MakeSpan(actual.data(), 3), actual.end());
00621 EXPECT_EQ(expected, actual);
00622 }
00623
00624 TEST(MutatingTest, Move) {
00625 std::vector<std::unique_ptr<int>> src;
00626 src.emplace_back(absl::make_unique<int>(1));
00627 src.emplace_back(absl::make_unique<int>(2));
00628 src.emplace_back(absl::make_unique<int>(3));
00629 src.emplace_back(absl::make_unique<int>(4));
00630 src.emplace_back(absl::make_unique<int>(5));
00631
00632 std::vector<std::unique_ptr<int>> dest = {};
00633 absl::c_move(src, std::back_inserter(dest));
00634 EXPECT_THAT(src, Each(IsNull()));
00635 EXPECT_THAT(dest, ElementsAre(Pointee(1), Pointee(2), Pointee(3), Pointee(4),
00636 Pointee(5)));
00637 }
00638
00639 TEST(MutatingTest, MoveWithRvalue) {
00640 auto MakeRValueSrc = [] {
00641 std::vector<std::unique_ptr<int>> src;
00642 src.emplace_back(absl::make_unique<int>(1));
00643 src.emplace_back(absl::make_unique<int>(2));
00644 src.emplace_back(absl::make_unique<int>(3));
00645 return src;
00646 };
00647
00648 std::vector<std::unique_ptr<int>> dest = MakeRValueSrc();
00649 absl::c_move(MakeRValueSrc(), std::back_inserter(dest));
00650 EXPECT_THAT(dest, ElementsAre(Pointee(1), Pointee(2), Pointee(3), Pointee(1),
00651 Pointee(2), Pointee(3)));
00652 }
00653
00654 TEST(MutatingTest, SwapRanges) {
00655 std::vector<int> odds = {2, 4, 6};
00656 std::vector<int> evens = {1, 3, 5};
00657 absl::c_swap_ranges(odds, evens);
00658 EXPECT_THAT(odds, ElementsAre(1, 3, 5));
00659 EXPECT_THAT(evens, ElementsAre(2, 4, 6));
00660 }
00661
00662 TEST_F(NonMutatingTest, Transform) {
00663 std::vector<int> x{0, 2, 4}, y, z;
00664 auto end = absl::c_transform(x, back_inserter(y), std::negate<int>());
00665 EXPECT_EQ(std::vector<int>({0, -2, -4}), y);
00666 *end = 7;
00667 EXPECT_EQ(std::vector<int>({0, -2, -4, 7}), y);
00668
00669 y = {1, 3, 0};
00670 end = absl::c_transform(x, y, back_inserter(z), std::plus<int>());
00671 EXPECT_EQ(std::vector<int>({1, 5, 4}), z);
00672 *end = 7;
00673 EXPECT_EQ(std::vector<int>({1, 5, 4, 7}), z);
00674 }
00675
00676 TEST(MutatingTest, Replace) {
00677 const std::vector<int> initial = {1, 2, 3, 1, 4, 5};
00678 const std::vector<int> expected = {4, 2, 3, 4, 4, 5};
00679
00680 std::vector<int> test_vector = initial;
00681 absl::c_replace(test_vector, 1, 4);
00682 EXPECT_EQ(expected, test_vector);
00683
00684 std::list<int> test_list(initial.begin(), initial.end());
00685 absl::c_replace(test_list, 1, 4);
00686 EXPECT_EQ(std::list<int>(expected.begin(), expected.end()), test_list);
00687 }
00688
00689 TEST(MutatingTest, ReplaceIf) {
00690 std::vector<int> actual = {1, 2, 3, 4, 5};
00691 const std::vector<int> expected = {0, 2, 0, 4, 0};
00692
00693 absl::c_replace_if(actual, IsOdd, 0);
00694 EXPECT_EQ(expected, actual);
00695 }
00696
00697 TEST(MutatingTest, ReplaceCopy) {
00698 const std::vector<int> initial = {1, 2, 3, 1, 4, 5};
00699 const std::vector<int> expected = {4, 2, 3, 4, 4, 5};
00700
00701 std::vector<int> actual;
00702 absl::c_replace_copy(initial, back_inserter(actual), 1, 4);
00703 EXPECT_EQ(expected, actual);
00704 }
00705
00706 TEST(MutatingTest, Sort) {
00707 std::vector<int> test_vector = {2, 3, 1, 4};
00708 absl::c_sort(test_vector);
00709 EXPECT_THAT(test_vector, ElementsAre(1, 2, 3, 4));
00710 }
00711
00712 TEST(MutatingTest, SortWithPredicate) {
00713 std::vector<int> test_vector = {2, 3, 1, 4};
00714 absl::c_sort(test_vector, std::greater<int>());
00715 EXPECT_THAT(test_vector, ElementsAre(4, 3, 2, 1));
00716 }
00717
00718
00719
00720 struct Element {
00721 int key;
00722 int value;
00723 friend bool operator<(const Element& e1, const Element& e2) {
00724 return e1.key < e2.key;
00725 }
00726
00727 friend std::ostream& operator<<(std::ostream& o, const Element& e) {
00728 return o << "{" << e.key << ", " << e.value << "}";
00729 }
00730 };
00731
00732 MATCHER_P2(IsElement, key, value, "") {
00733 return arg.key == key && arg.value == value;
00734 }
00735
00736 TEST(MutatingTest, StableSort) {
00737 std::vector<Element> test_vector = {{1, 1}, {2, 1}, {2, 0}, {1, 0}, {2, 2}};
00738 absl::c_stable_sort(test_vector);
00739 EXPECT_THAT(
00740 test_vector,
00741 ElementsAre(IsElement(1, 1), IsElement(1, 0), IsElement(2, 1),
00742 IsElement(2, 0), IsElement(2, 2)));
00743 }
00744
00745 TEST(MutatingTest, StableSortWithPredicate) {
00746 std::vector<Element> test_vector = {{1, 1}, {2, 1}, {2, 0}, {1, 0}, {2, 2}};
00747 absl::c_stable_sort(test_vector, [](const Element& e1, const Element& e2) {
00748 return e2 < e1;
00749 });
00750 EXPECT_THAT(
00751 test_vector,
00752 ElementsAre(IsElement(2, 1), IsElement(2, 0), IsElement(2, 2),
00753 IsElement(1, 1), IsElement(1, 0)));
00754 }
00755
00756 TEST(MutatingTest, ReplaceCopyIf) {
00757 const std::vector<int> initial = {1, 2, 3, 4, 5};
00758 const std::vector<int> expected = {0, 2, 0, 4, 0};
00759
00760 std::vector<int> actual;
00761 absl::c_replace_copy_if(initial, back_inserter(actual), IsOdd, 0);
00762 EXPECT_EQ(expected, actual);
00763 }
00764
00765 TEST(MutatingTest, Fill) {
00766 std::vector<int> actual(5);
00767 absl::c_fill(actual, 1);
00768 EXPECT_THAT(actual, ElementsAre(1, 1, 1, 1, 1));
00769 }
00770
00771 TEST(MutatingTest, FillN) {
00772 std::vector<int> actual(5, 0);
00773 absl::c_fill_n(actual, 2, 1);
00774 EXPECT_THAT(actual, ElementsAre(1, 1, 0, 0, 0));
00775 }
00776
00777 TEST(MutatingTest, Generate) {
00778 std::vector<int> actual(5);
00779 int x = 0;
00780 absl::c_generate(actual, [&x]() { return ++x; });
00781 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 4, 5));
00782 }
00783
00784 TEST(MutatingTest, GenerateN) {
00785 std::vector<int> actual(5, 0);
00786 int x = 0;
00787 absl::c_generate_n(actual, 3, [&x]() { return ++x; });
00788 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 0, 0));
00789 }
00790
00791 TEST(MutatingTest, RemoveCopy) {
00792 std::vector<int> actual;
00793 absl::c_remove_copy(std::vector<int>{1, 2, 3}, back_inserter(actual), 2);
00794 EXPECT_THAT(actual, ElementsAre(1, 3));
00795 }
00796
00797 TEST(MutatingTest, RemoveCopyIf) {
00798 std::vector<int> actual;
00799 absl::c_remove_copy_if(std::vector<int>{1, 2, 3}, back_inserter(actual),
00800 IsOdd);
00801 EXPECT_THAT(actual, ElementsAre(2));
00802 }
00803
00804 TEST(MutatingTest, UniqueCopy) {
00805 std::vector<int> actual;
00806 absl::c_unique_copy(std::vector<int>{1, 2, 2, 2, 3, 3, 2},
00807 back_inserter(actual));
00808 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 2));
00809 }
00810
00811 TEST(MutatingTest, UniqueCopyWithPredicate) {
00812 std::vector<int> actual;
00813 absl::c_unique_copy(std::vector<int>{1, 2, 3, -1, -2, -3, 1},
00814 back_inserter(actual),
00815 [](int x, int y) { return (x < 0) == (y < 0); });
00816 EXPECT_THAT(actual, ElementsAre(1, -1, 1));
00817 }
00818
00819 TEST(MutatingTest, Reverse) {
00820 std::vector<int> test_vector = {1, 2, 3, 4};
00821 absl::c_reverse(test_vector);
00822 EXPECT_THAT(test_vector, ElementsAre(4, 3, 2, 1));
00823
00824 std::list<int> test_list = {1, 2, 3, 4};
00825 absl::c_reverse(test_list);
00826 EXPECT_THAT(test_list, ElementsAre(4, 3, 2, 1));
00827 }
00828
00829 TEST(MutatingTest, ReverseCopy) {
00830 std::vector<int> actual;
00831 absl::c_reverse_copy(std::vector<int>{1, 2, 3, 4}, back_inserter(actual));
00832 EXPECT_THAT(actual, ElementsAre(4, 3, 2, 1));
00833 }
00834
00835 TEST(MutatingTest, Rotate) {
00836 std::vector<int> actual = {1, 2, 3, 4};
00837 auto it = absl::c_rotate(actual, actual.begin() + 2);
00838 EXPECT_THAT(actual, testing::ElementsAreArray({3, 4, 1, 2}));
00839 EXPECT_EQ(*it, 1);
00840 }
00841
00842 TEST(MutatingTest, RotateCopy) {
00843 std::vector<int> initial = {1, 2, 3, 4};
00844 std::vector<int> actual;
00845 auto end =
00846 absl::c_rotate_copy(initial, initial.begin() + 2, back_inserter(actual));
00847 *end = 5;
00848 EXPECT_THAT(actual, ElementsAre(3, 4, 1, 2, 5));
00849 }
00850
00851 TEST(MutatingTest, Shuffle) {
00852 std::vector<int> actual = {1, 2, 3, 4, 5};
00853 absl::c_shuffle(actual, std::random_device());
00854 EXPECT_THAT(actual, UnorderedElementsAre(1, 2, 3, 4, 5));
00855 }
00856
00857 TEST(MutatingTest, PartialSort) {
00858 std::vector<int> sequence{5, 3, 42, 0};
00859 absl::c_partial_sort(sequence, sequence.begin() + 2);
00860 EXPECT_THAT(absl::MakeSpan(sequence.data(), 2), ElementsAre(0, 3));
00861 absl::c_partial_sort(sequence, sequence.begin() + 2, std::greater<int>());
00862 EXPECT_THAT(absl::MakeSpan(sequence.data(), 2), ElementsAre(42, 5));
00863 }
00864
00865 TEST(MutatingTest, PartialSortCopy) {
00866 const std::vector<int> initial = {5, 3, 42, 0};
00867 std::vector<int> actual(2);
00868 absl::c_partial_sort_copy(initial, actual);
00869 EXPECT_THAT(actual, ElementsAre(0, 3));
00870 absl::c_partial_sort_copy(initial, actual, std::greater<int>());
00871 EXPECT_THAT(actual, ElementsAre(42, 5));
00872 }
00873
00874 TEST(MutatingTest, Merge) {
00875 std::vector<int> actual;
00876 absl::c_merge(std::vector<int>{1, 3, 5}, std::vector<int>{2, 4},
00877 back_inserter(actual));
00878 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 4, 5));
00879 }
00880
00881 TEST(MutatingTest, MergeWithComparator) {
00882 std::vector<int> actual;
00883 absl::c_merge(std::vector<int>{5, 3, 1}, std::vector<int>{4, 2},
00884 back_inserter(actual), std::greater<int>());
00885 EXPECT_THAT(actual, ElementsAre(5, 4, 3, 2, 1));
00886 }
00887
00888 TEST(MutatingTest, InplaceMerge) {
00889 std::vector<int> actual = {1, 3, 5, 2, 4};
00890 absl::c_inplace_merge(actual, actual.begin() + 3);
00891 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 4, 5));
00892 }
00893
00894 TEST(MutatingTest, InplaceMergeWithComparator) {
00895 std::vector<int> actual = {5, 3, 1, 4, 2};
00896 absl::c_inplace_merge(actual, actual.begin() + 3, std::greater<int>());
00897 EXPECT_THAT(actual, ElementsAre(5, 4, 3, 2, 1));
00898 }
00899
00900 class SetOperationsTest : public testing::Test {
00901 protected:
00902 std::vector<int> a_ = {1, 2, 3};
00903 std::vector<int> b_ = {1, 3, 5};
00904
00905 std::vector<int> a_reversed_ = {3, 2, 1};
00906 std::vector<int> b_reversed_ = {5, 3, 1};
00907 };
00908
00909 TEST_F(SetOperationsTest, SetUnion) {
00910 std::vector<int> actual;
00911 absl::c_set_union(a_, b_, back_inserter(actual));
00912 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 5));
00913 }
00914
00915 TEST_F(SetOperationsTest, SetUnionWithComparator) {
00916 std::vector<int> actual;
00917 absl::c_set_union(a_reversed_, b_reversed_, back_inserter(actual),
00918 std::greater<int>());
00919 EXPECT_THAT(actual, ElementsAre(5, 3, 2, 1));
00920 }
00921
00922 TEST_F(SetOperationsTest, SetIntersection) {
00923 std::vector<int> actual;
00924 absl::c_set_intersection(a_, b_, back_inserter(actual));
00925 EXPECT_THAT(actual, ElementsAre(1, 3));
00926 }
00927
00928 TEST_F(SetOperationsTest, SetIntersectionWithComparator) {
00929 std::vector<int> actual;
00930 absl::c_set_intersection(a_reversed_, b_reversed_, back_inserter(actual),
00931 std::greater<int>());
00932 EXPECT_THAT(actual, ElementsAre(3, 1));
00933 }
00934
00935 TEST_F(SetOperationsTest, SetDifference) {
00936 std::vector<int> actual;
00937 absl::c_set_difference(a_, b_, back_inserter(actual));
00938 EXPECT_THAT(actual, ElementsAre(2));
00939 }
00940
00941 TEST_F(SetOperationsTest, SetDifferenceWithComparator) {
00942 std::vector<int> actual;
00943 absl::c_set_difference(a_reversed_, b_reversed_, back_inserter(actual),
00944 std::greater<int>());
00945 EXPECT_THAT(actual, ElementsAre(2));
00946 }
00947
00948 TEST_F(SetOperationsTest, SetSymmetricDifference) {
00949 std::vector<int> actual;
00950 absl::c_set_symmetric_difference(a_, b_, back_inserter(actual));
00951 EXPECT_THAT(actual, ElementsAre(2, 5));
00952 }
00953
00954 TEST_F(SetOperationsTest, SetSymmetricDifferenceWithComparator) {
00955 std::vector<int> actual;
00956 absl::c_set_symmetric_difference(a_reversed_, b_reversed_,
00957 back_inserter(actual), std::greater<int>());
00958 EXPECT_THAT(actual, ElementsAre(5, 2));
00959 }
00960
00961 TEST(HeapOperationsTest, WithoutComparator) {
00962 std::vector<int> heap = {1, 2, 3};
00963 EXPECT_FALSE(absl::c_is_heap(heap));
00964 absl::c_make_heap(heap);
00965 EXPECT_TRUE(absl::c_is_heap(heap));
00966 heap.push_back(4);
00967 EXPECT_EQ(3, absl::c_is_heap_until(heap) - heap.begin());
00968 absl::c_push_heap(heap);
00969 EXPECT_EQ(4, heap[0]);
00970 absl::c_pop_heap(heap);
00971 EXPECT_EQ(4, heap[3]);
00972 absl::c_make_heap(heap);
00973 absl::c_sort_heap(heap);
00974 EXPECT_THAT(heap, ElementsAre(1, 2, 3, 4));
00975 EXPECT_FALSE(absl::c_is_heap(heap));
00976 }
00977
00978 TEST(HeapOperationsTest, WithComparator) {
00979 using greater = std::greater<int>;
00980 std::vector<int> heap = {3, 2, 1};
00981 EXPECT_FALSE(absl::c_is_heap(heap, greater()));
00982 absl::c_make_heap(heap, greater());
00983 EXPECT_TRUE(absl::c_is_heap(heap, greater()));
00984 heap.push_back(0);
00985 EXPECT_EQ(3, absl::c_is_heap_until(heap, greater()) - heap.begin());
00986 absl::c_push_heap(heap, greater());
00987 EXPECT_EQ(0, heap[0]);
00988 absl::c_pop_heap(heap, greater());
00989 EXPECT_EQ(0, heap[3]);
00990 absl::c_make_heap(heap, greater());
00991 absl::c_sort_heap(heap, greater());
00992 EXPECT_THAT(heap, ElementsAre(3, 2, 1, 0));
00993 EXPECT_FALSE(absl::c_is_heap(heap, greater()));
00994 }
00995
00996 TEST(MutatingTest, PermutationOperations) {
00997 std::vector<int> initial = {1, 2, 3, 4};
00998 std::vector<int> permuted = initial;
00999
01000 absl::c_next_permutation(permuted);
01001 EXPECT_TRUE(absl::c_is_permutation(initial, permuted));
01002 EXPECT_TRUE(absl::c_is_permutation(initial, permuted, std::equal_to<int>()));
01003
01004 std::vector<int> permuted2 = initial;
01005 absl::c_prev_permutation(permuted2, std::greater<int>());
01006 EXPECT_EQ(permuted, permuted2);
01007
01008 absl::c_prev_permutation(permuted);
01009 EXPECT_EQ(initial, permuted);
01010 }
01011
01012 }