xref: /aosp_15_r20/external/abseil-cpp/absl/algorithm/container_test.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/algorithm/container.h"
16 
17 #include <algorithm>
18 #include <array>
19 #include <functional>
20 #include <initializer_list>
21 #include <iterator>
22 #include <list>
23 #include <memory>
24 #include <ostream>
25 #include <random>
26 #include <set>
27 #include <unordered_set>
28 #include <utility>
29 #include <valarray>
30 #include <vector>
31 
32 #include "gmock/gmock.h"
33 #include "gtest/gtest.h"
34 #include "absl/base/casts.h"
35 #include "absl/base/config.h"
36 #include "absl/base/macros.h"
37 #include "absl/memory/memory.h"
38 #include "absl/types/span.h"
39 
40 namespace {
41 
42 using ::testing::Each;
43 using ::testing::ElementsAre;
44 using ::testing::Gt;
45 using ::testing::IsNull;
46 using ::testing::IsSubsetOf;
47 using ::testing::Lt;
48 using ::testing::Pointee;
49 using ::testing::SizeIs;
50 using ::testing::Truly;
51 using ::testing::UnorderedElementsAre;
52 
53 // Most of these tests just check that the code compiles, not that it
54 // does the right thing. That's fine since the functions just forward
55 // to the STL implementation.
56 class NonMutatingTest : public testing::Test {
57  protected:
58   std::unordered_set<int> container_ = {1, 2, 3};
59   std::list<int> sequence_ = {1, 2, 3};
60   std::vector<int> vector_ = {1, 2, 3};
61   int array_[3] = {1, 2, 3};
62 };
63 
64 struct AccumulateCalls {
operator ()__anon8c2d03130111::AccumulateCalls65   void operator()(int value) { calls.push_back(value); }
66   std::vector<int> calls;
67 };
68 
Predicate(int value)69 bool Predicate(int value) { return value < 3; }
BinPredicate(int v1,int v2)70 bool BinPredicate(int v1, int v2) { return v1 < v2; }
Equals(int v1,int v2)71 bool Equals(int v1, int v2) { return v1 == v2; }
IsOdd(int x)72 bool IsOdd(int x) { return x % 2 != 0; }
73 
TEST_F(NonMutatingTest,Distance)74 TEST_F(NonMutatingTest, Distance) {
75   EXPECT_EQ(container_.size(),
76             static_cast<size_t>(absl::c_distance(container_)));
77   EXPECT_EQ(sequence_.size(), static_cast<size_t>(absl::c_distance(sequence_)));
78   EXPECT_EQ(vector_.size(), static_cast<size_t>(absl::c_distance(vector_)));
79   EXPECT_EQ(ABSL_ARRAYSIZE(array_),
80             static_cast<size_t>(absl::c_distance(array_)));
81 
82   // Works with a temporary argument.
83   EXPECT_EQ(vector_.size(),
84             static_cast<size_t>(absl::c_distance(std::vector<int>(vector_))));
85 }
86 
TEST_F(NonMutatingTest,Distance_OverloadedBeginEnd)87 TEST_F(NonMutatingTest, Distance_OverloadedBeginEnd) {
88   // Works with classes which have custom ADL-selected overloads of std::begin
89   // and std::end.
90   std::initializer_list<int> a = {1, 2, 3};
91   std::valarray<int> b = {1, 2, 3};
92   EXPECT_EQ(3, absl::c_distance(a));
93   EXPECT_EQ(3, absl::c_distance(b));
94 
95   // It is assumed that other c_* functions use the same mechanism for
96   // ADL-selecting begin/end overloads.
97 }
98 
TEST_F(NonMutatingTest,ForEach)99 TEST_F(NonMutatingTest, ForEach) {
100   AccumulateCalls c = absl::c_for_each(container_, AccumulateCalls());
101   // Don't rely on the unordered_set's order.
102   std::sort(c.calls.begin(), c.calls.end());
103   EXPECT_EQ(vector_, c.calls);
104 
105   // Works with temporary container, too.
106   AccumulateCalls c2 =
107       absl::c_for_each(std::unordered_set<int>(container_), AccumulateCalls());
108   std::sort(c2.calls.begin(), c2.calls.end());
109   EXPECT_EQ(vector_, c2.calls);
110 }
111 
TEST_F(NonMutatingTest,FindReturnsCorrectType)112 TEST_F(NonMutatingTest, FindReturnsCorrectType) {
113   auto it = absl::c_find(container_, 3);
114   EXPECT_EQ(3, *it);
115   absl::c_find(absl::implicit_cast<const std::list<int>&>(sequence_), 3);
116 }
117 
TEST_F(NonMutatingTest,Contains)118 TEST_F(NonMutatingTest, Contains) {
119   EXPECT_TRUE(absl::c_contains(container_, 3));
120   EXPECT_FALSE(absl::c_contains(container_, 4));
121 }
122 
TEST_F(NonMutatingTest,FindIf)123 TEST_F(NonMutatingTest, FindIf) { absl::c_find_if(container_, Predicate); }
124 
TEST_F(NonMutatingTest,FindIfNot)125 TEST_F(NonMutatingTest, FindIfNot) {
126   absl::c_find_if_not(container_, Predicate);
127 }
128 
TEST_F(NonMutatingTest,FindEnd)129 TEST_F(NonMutatingTest, FindEnd) {
130   absl::c_find_end(sequence_, vector_);
131   absl::c_find_end(vector_, sequence_);
132 }
133 
TEST_F(NonMutatingTest,FindEndWithPredicate)134 TEST_F(NonMutatingTest, FindEndWithPredicate) {
135   absl::c_find_end(sequence_, vector_, BinPredicate);
136   absl::c_find_end(vector_, sequence_, BinPredicate);
137 }
138 
TEST_F(NonMutatingTest,FindFirstOf)139 TEST_F(NonMutatingTest, FindFirstOf) {
140   absl::c_find_first_of(container_, sequence_);
141   absl::c_find_first_of(sequence_, container_);
142 }
143 
TEST_F(NonMutatingTest,FindFirstOfWithPredicate)144 TEST_F(NonMutatingTest, FindFirstOfWithPredicate) {
145   absl::c_find_first_of(container_, sequence_, BinPredicate);
146   absl::c_find_first_of(sequence_, container_, BinPredicate);
147 }
148 
TEST_F(NonMutatingTest,AdjacentFind)149 TEST_F(NonMutatingTest, AdjacentFind) { absl::c_adjacent_find(sequence_); }
150 
TEST_F(NonMutatingTest,AdjacentFindWithPredicate)151 TEST_F(NonMutatingTest, AdjacentFindWithPredicate) {
152   absl::c_adjacent_find(sequence_, BinPredicate);
153 }
154 
TEST_F(NonMutatingTest,Count)155 TEST_F(NonMutatingTest, Count) { EXPECT_EQ(1, absl::c_count(container_, 3)); }
156 
TEST_F(NonMutatingTest,CountIf)157 TEST_F(NonMutatingTest, CountIf) {
158   EXPECT_EQ(2, absl::c_count_if(container_, Predicate));
159   const std::unordered_set<int>& const_container = container_;
160   EXPECT_EQ(2, absl::c_count_if(const_container, Predicate));
161 }
162 
TEST_F(NonMutatingTest,Mismatch)163 TEST_F(NonMutatingTest, Mismatch) {
164   // Testing necessary as absl::c_mismatch executes logic.
165   {
166     auto result = absl::c_mismatch(vector_, sequence_);
167     EXPECT_EQ(result.first, vector_.end());
168     EXPECT_EQ(result.second, sequence_.end());
169   }
170   {
171     auto result = absl::c_mismatch(sequence_, vector_);
172     EXPECT_EQ(result.first, sequence_.end());
173     EXPECT_EQ(result.second, vector_.end());
174   }
175 
176   sequence_.back() = 5;
177   {
178     auto result = absl::c_mismatch(vector_, sequence_);
179     EXPECT_EQ(result.first, std::prev(vector_.end()));
180     EXPECT_EQ(result.second, std::prev(sequence_.end()));
181   }
182   {
183     auto result = absl::c_mismatch(sequence_, vector_);
184     EXPECT_EQ(result.first, std::prev(sequence_.end()));
185     EXPECT_EQ(result.second, std::prev(vector_.end()));
186   }
187 
188   sequence_.pop_back();
189   {
190     auto result = absl::c_mismatch(vector_, sequence_);
191     EXPECT_EQ(result.first, std::prev(vector_.end()));
192     EXPECT_EQ(result.second, sequence_.end());
193   }
194   {
195     auto result = absl::c_mismatch(sequence_, vector_);
196     EXPECT_EQ(result.first, sequence_.end());
197     EXPECT_EQ(result.second, std::prev(vector_.end()));
198   }
199   {
200     struct NoNotEquals {
201       constexpr bool operator==(NoNotEquals) const { return true; }
202       constexpr bool operator!=(NoNotEquals) const = delete;
203     };
204     std::vector<NoNotEquals> first;
205     std::list<NoNotEquals> second;
206 
207     // Check this still compiles.
208     absl::c_mismatch(first, second);
209   }
210 }
211 
TEST_F(NonMutatingTest,MismatchWithPredicate)212 TEST_F(NonMutatingTest, MismatchWithPredicate) {
213   // Testing necessary as absl::c_mismatch executes logic.
214   {
215     auto result = absl::c_mismatch(vector_, sequence_, BinPredicate);
216     EXPECT_EQ(result.first, vector_.begin());
217     EXPECT_EQ(result.second, sequence_.begin());
218   }
219   {
220     auto result = absl::c_mismatch(sequence_, vector_, BinPredicate);
221     EXPECT_EQ(result.first, sequence_.begin());
222     EXPECT_EQ(result.second, vector_.begin());
223   }
224 
225   sequence_.front() = 0;
226   {
227     auto result = absl::c_mismatch(vector_, sequence_, BinPredicate);
228     EXPECT_EQ(result.first, vector_.begin());
229     EXPECT_EQ(result.second, sequence_.begin());
230   }
231   {
232     auto result = absl::c_mismatch(sequence_, vector_, BinPredicate);
233     EXPECT_EQ(result.first, std::next(sequence_.begin()));
234     EXPECT_EQ(result.second, std::next(vector_.begin()));
235   }
236 
237   sequence_.clear();
238   {
239     auto result = absl::c_mismatch(vector_, sequence_, BinPredicate);
240     EXPECT_EQ(result.first, vector_.begin());
241     EXPECT_EQ(result.second, sequence_.end());
242   }
243   {
244     auto result = absl::c_mismatch(sequence_, vector_, BinPredicate);
245     EXPECT_EQ(result.first, sequence_.end());
246     EXPECT_EQ(result.second, vector_.begin());
247   }
248 }
249 
TEST_F(NonMutatingTest,Equal)250 TEST_F(NonMutatingTest, Equal) {
251   EXPECT_TRUE(absl::c_equal(vector_, sequence_));
252   EXPECT_TRUE(absl::c_equal(sequence_, vector_));
253   EXPECT_TRUE(absl::c_equal(sequence_, array_));
254   EXPECT_TRUE(absl::c_equal(array_, vector_));
255 
256   // Test that behavior appropriately differs from that of equal().
257   std::vector<int> vector_plus = {1, 2, 3};
258   vector_plus.push_back(4);
259   EXPECT_FALSE(absl::c_equal(vector_plus, sequence_));
260   EXPECT_FALSE(absl::c_equal(sequence_, vector_plus));
261   EXPECT_FALSE(absl::c_equal(array_, vector_plus));
262 }
263 
TEST_F(NonMutatingTest,EqualWithPredicate)264 TEST_F(NonMutatingTest, EqualWithPredicate) {
265   EXPECT_TRUE(absl::c_equal(vector_, sequence_, Equals));
266   EXPECT_TRUE(absl::c_equal(sequence_, vector_, Equals));
267   EXPECT_TRUE(absl::c_equal(array_, sequence_, Equals));
268   EXPECT_TRUE(absl::c_equal(vector_, array_, Equals));
269 
270   // Test that behavior appropriately differs from that of equal().
271   std::vector<int> vector_plus = {1, 2, 3};
272   vector_plus.push_back(4);
273   EXPECT_FALSE(absl::c_equal(vector_plus, sequence_, Equals));
274   EXPECT_FALSE(absl::c_equal(sequence_, vector_plus, Equals));
275   EXPECT_FALSE(absl::c_equal(vector_plus, array_, Equals));
276 }
277 
TEST_F(NonMutatingTest,IsPermutation)278 TEST_F(NonMutatingTest, IsPermutation) {
279   auto vector_permut_ = vector_;
280   std::next_permutation(vector_permut_.begin(), vector_permut_.end());
281   EXPECT_TRUE(absl::c_is_permutation(vector_permut_, sequence_));
282   EXPECT_TRUE(absl::c_is_permutation(sequence_, vector_permut_));
283 
284   // Test that behavior appropriately differs from that of is_permutation().
285   std::vector<int> vector_plus = {1, 2, 3};
286   vector_plus.push_back(4);
287   EXPECT_FALSE(absl::c_is_permutation(vector_plus, sequence_));
288   EXPECT_FALSE(absl::c_is_permutation(sequence_, vector_plus));
289 }
290 
TEST_F(NonMutatingTest,IsPermutationWithPredicate)291 TEST_F(NonMutatingTest, IsPermutationWithPredicate) {
292   auto vector_permut_ = vector_;
293   std::next_permutation(vector_permut_.begin(), vector_permut_.end());
294   EXPECT_TRUE(absl::c_is_permutation(vector_permut_, sequence_, Equals));
295   EXPECT_TRUE(absl::c_is_permutation(sequence_, vector_permut_, Equals));
296 
297   // Test that behavior appropriately differs from that of is_permutation().
298   std::vector<int> vector_plus = {1, 2, 3};
299   vector_plus.push_back(4);
300   EXPECT_FALSE(absl::c_is_permutation(vector_plus, sequence_, Equals));
301   EXPECT_FALSE(absl::c_is_permutation(sequence_, vector_plus, Equals));
302 }
303 
TEST_F(NonMutatingTest,Search)304 TEST_F(NonMutatingTest, Search) {
305   absl::c_search(sequence_, vector_);
306   absl::c_search(vector_, sequence_);
307   absl::c_search(array_, sequence_);
308 }
309 
TEST_F(NonMutatingTest,SearchWithPredicate)310 TEST_F(NonMutatingTest, SearchWithPredicate) {
311   absl::c_search(sequence_, vector_, BinPredicate);
312   absl::c_search(vector_, sequence_, BinPredicate);
313 }
314 
TEST_F(NonMutatingTest,ContainsSubrange)315 TEST_F(NonMutatingTest, ContainsSubrange) {
316   EXPECT_TRUE(absl::c_contains_subrange(sequence_, vector_));
317   EXPECT_TRUE(absl::c_contains_subrange(vector_, sequence_));
318   EXPECT_TRUE(absl::c_contains_subrange(array_, sequence_));
319 }
320 
TEST_F(NonMutatingTest,ContainsSubrangeWithPredicate)321 TEST_F(NonMutatingTest, ContainsSubrangeWithPredicate) {
322   EXPECT_TRUE(absl::c_contains_subrange(sequence_, vector_, Equals));
323   EXPECT_TRUE(absl::c_contains_subrange(vector_, sequence_, Equals));
324 }
325 
TEST_F(NonMutatingTest,SearchN)326 TEST_F(NonMutatingTest, SearchN) { absl::c_search_n(sequence_, 3, 1); }
327 
TEST_F(NonMutatingTest,SearchNWithPredicate)328 TEST_F(NonMutatingTest, SearchNWithPredicate) {
329   absl::c_search_n(sequence_, 3, 1, BinPredicate);
330 }
331 
TEST_F(NonMutatingTest,LowerBound)332 TEST_F(NonMutatingTest, LowerBound) {
333   std::list<int>::iterator i = absl::c_lower_bound(sequence_, 3);
334   ASSERT_TRUE(i != sequence_.end());
335   EXPECT_EQ(2, std::distance(sequence_.begin(), i));
336   EXPECT_EQ(3, *i);
337 }
338 
TEST_F(NonMutatingTest,LowerBoundWithPredicate)339 TEST_F(NonMutatingTest, LowerBoundWithPredicate) {
340   std::vector<int> v(vector_);
341   std::sort(v.begin(), v.end(), std::greater<int>());
342   std::vector<int>::iterator i = absl::c_lower_bound(v, 3, std::greater<int>());
343   EXPECT_TRUE(i == v.begin());
344   EXPECT_EQ(3, *i);
345 }
346 
TEST_F(NonMutatingTest,UpperBound)347 TEST_F(NonMutatingTest, UpperBound) {
348   std::list<int>::iterator i = absl::c_upper_bound(sequence_, 1);
349   ASSERT_TRUE(i != sequence_.end());
350   EXPECT_EQ(1, std::distance(sequence_.begin(), i));
351   EXPECT_EQ(2, *i);
352 }
353 
TEST_F(NonMutatingTest,UpperBoundWithPredicate)354 TEST_F(NonMutatingTest, UpperBoundWithPredicate) {
355   std::vector<int> v(vector_);
356   std::sort(v.begin(), v.end(), std::greater<int>());
357   std::vector<int>::iterator i = absl::c_upper_bound(v, 1, std::greater<int>());
358   EXPECT_EQ(3, i - v.begin());
359   EXPECT_TRUE(i == v.end());
360 }
361 
TEST_F(NonMutatingTest,EqualRange)362 TEST_F(NonMutatingTest, EqualRange) {
363   std::pair<std::list<int>::iterator, std::list<int>::iterator> p =
364       absl::c_equal_range(sequence_, 2);
365   EXPECT_EQ(1, std::distance(sequence_.begin(), p.first));
366   EXPECT_EQ(2, std::distance(sequence_.begin(), p.second));
367 }
368 
TEST_F(NonMutatingTest,EqualRangeArray)369 TEST_F(NonMutatingTest, EqualRangeArray) {
370   auto p = absl::c_equal_range(array_, 2);
371   EXPECT_EQ(1, std::distance(std::begin(array_), p.first));
372   EXPECT_EQ(2, std::distance(std::begin(array_), p.second));
373 }
374 
TEST_F(NonMutatingTest,EqualRangeWithPredicate)375 TEST_F(NonMutatingTest, EqualRangeWithPredicate) {
376   std::vector<int> v(vector_);
377   std::sort(v.begin(), v.end(), std::greater<int>());
378   std::pair<std::vector<int>::iterator, std::vector<int>::iterator> p =
379       absl::c_equal_range(v, 2, std::greater<int>());
380   EXPECT_EQ(1, std::distance(v.begin(), p.first));
381   EXPECT_EQ(2, std::distance(v.begin(), p.second));
382 }
383 
TEST_F(NonMutatingTest,BinarySearch)384 TEST_F(NonMutatingTest, BinarySearch) {
385   EXPECT_TRUE(absl::c_binary_search(vector_, 2));
386   EXPECT_TRUE(absl::c_binary_search(std::vector<int>(vector_), 2));
387 }
388 
TEST_F(NonMutatingTest,BinarySearchWithPredicate)389 TEST_F(NonMutatingTest, BinarySearchWithPredicate) {
390   std::vector<int> v(vector_);
391   std::sort(v.begin(), v.end(), std::greater<int>());
392   EXPECT_TRUE(absl::c_binary_search(v, 2, std::greater<int>()));
393   EXPECT_TRUE(
394       absl::c_binary_search(std::vector<int>(v), 2, std::greater<int>()));
395 }
396 
TEST_F(NonMutatingTest,MinElement)397 TEST_F(NonMutatingTest, MinElement) {
398   std::list<int>::iterator i = absl::c_min_element(sequence_);
399   ASSERT_TRUE(i != sequence_.end());
400   EXPECT_EQ(*i, 1);
401 }
402 
TEST_F(NonMutatingTest,MinElementWithPredicate)403 TEST_F(NonMutatingTest, MinElementWithPredicate) {
404   std::list<int>::iterator i =
405       absl::c_min_element(sequence_, std::greater<int>());
406   ASSERT_TRUE(i != sequence_.end());
407   EXPECT_EQ(*i, 3);
408 }
409 
TEST_F(NonMutatingTest,MaxElement)410 TEST_F(NonMutatingTest, MaxElement) {
411   std::list<int>::iterator i = absl::c_max_element(sequence_);
412   ASSERT_TRUE(i != sequence_.end());
413   EXPECT_EQ(*i, 3);
414 }
415 
TEST_F(NonMutatingTest,MaxElementWithPredicate)416 TEST_F(NonMutatingTest, MaxElementWithPredicate) {
417   std::list<int>::iterator i =
418       absl::c_max_element(sequence_, std::greater<int>());
419   ASSERT_TRUE(i != sequence_.end());
420   EXPECT_EQ(*i, 1);
421 }
422 
TEST_F(NonMutatingTest,LexicographicalCompare)423 TEST_F(NonMutatingTest, LexicographicalCompare) {
424   EXPECT_FALSE(absl::c_lexicographical_compare(sequence_, sequence_));
425 
426   std::vector<int> v;
427   v.push_back(1);
428   v.push_back(2);
429   v.push_back(4);
430 
431   EXPECT_TRUE(absl::c_lexicographical_compare(sequence_, v));
432   EXPECT_TRUE(absl::c_lexicographical_compare(std::list<int>(sequence_), v));
433 }
434 
TEST_F(NonMutatingTest,LexicographicalCopmareWithPredicate)435 TEST_F(NonMutatingTest, LexicographicalCopmareWithPredicate) {
436   EXPECT_FALSE(absl::c_lexicographical_compare(sequence_, sequence_,
437                                                std::greater<int>()));
438 
439   std::vector<int> v;
440   v.push_back(1);
441   v.push_back(2);
442   v.push_back(4);
443 
444   EXPECT_TRUE(
445       absl::c_lexicographical_compare(v, sequence_, std::greater<int>()));
446   EXPECT_TRUE(absl::c_lexicographical_compare(
447       std::vector<int>(v), std::list<int>(sequence_), std::greater<int>()));
448 }
449 
TEST_F(NonMutatingTest,Includes)450 TEST_F(NonMutatingTest, Includes) {
451   std::set<int> s(vector_.begin(), vector_.end());
452   s.insert(4);
453   EXPECT_TRUE(absl::c_includes(s, vector_));
454 }
455 
TEST_F(NonMutatingTest,IncludesWithPredicate)456 TEST_F(NonMutatingTest, IncludesWithPredicate) {
457   std::vector<int> v = {3, 2, 1};
458   std::set<int, std::greater<int>> s(v.begin(), v.end());
459   s.insert(4);
460   EXPECT_TRUE(absl::c_includes(s, v, std::greater<int>()));
461 }
462 
463 class NumericMutatingTest : public testing::Test {
464  protected:
465   std::list<int> list_ = {1, 2, 3};
466   std::vector<int> output_;
467 };
468 
TEST_F(NumericMutatingTest,Iota)469 TEST_F(NumericMutatingTest, Iota) {
470   absl::c_iota(list_, 5);
471   std::list<int> expected{5, 6, 7};
472   EXPECT_EQ(list_, expected);
473 }
474 
TEST_F(NonMutatingTest,Accumulate)475 TEST_F(NonMutatingTest, Accumulate) {
476   EXPECT_EQ(absl::c_accumulate(sequence_, 4), 1 + 2 + 3 + 4);
477 }
478 
TEST_F(NonMutatingTest,AccumulateWithBinaryOp)479 TEST_F(NonMutatingTest, AccumulateWithBinaryOp) {
480   EXPECT_EQ(absl::c_accumulate(sequence_, 4, std::multiplies<int>()),
481             1 * 2 * 3 * 4);
482 }
483 
TEST_F(NonMutatingTest,AccumulateLvalueInit)484 TEST_F(NonMutatingTest, AccumulateLvalueInit) {
485   int lvalue = 4;
486   EXPECT_EQ(absl::c_accumulate(sequence_, lvalue), 1 + 2 + 3 + 4);
487 }
488 
TEST_F(NonMutatingTest,AccumulateWithBinaryOpLvalueInit)489 TEST_F(NonMutatingTest, AccumulateWithBinaryOpLvalueInit) {
490   int lvalue = 4;
491   EXPECT_EQ(absl::c_accumulate(sequence_, lvalue, std::multiplies<int>()),
492             1 * 2 * 3 * 4);
493 }
494 
TEST_F(NonMutatingTest,InnerProduct)495 TEST_F(NonMutatingTest, InnerProduct) {
496   EXPECT_EQ(absl::c_inner_product(sequence_, vector_, 1000),
497             1000 + 1 * 1 + 2 * 2 + 3 * 3);
498 }
499 
TEST_F(NonMutatingTest,InnerProductWithBinaryOps)500 TEST_F(NonMutatingTest, InnerProductWithBinaryOps) {
501   EXPECT_EQ(absl::c_inner_product(sequence_, vector_, 10,
502                                   std::multiplies<int>(), std::plus<int>()),
503             10 * (1 + 1) * (2 + 2) * (3 + 3));
504 }
505 
TEST_F(NonMutatingTest,InnerProductLvalueInit)506 TEST_F(NonMutatingTest, InnerProductLvalueInit) {
507   int lvalue = 1000;
508   EXPECT_EQ(absl::c_inner_product(sequence_, vector_, lvalue),
509             1000 + 1 * 1 + 2 * 2 + 3 * 3);
510 }
511 
TEST_F(NonMutatingTest,InnerProductWithBinaryOpsLvalueInit)512 TEST_F(NonMutatingTest, InnerProductWithBinaryOpsLvalueInit) {
513   int lvalue = 10;
514   EXPECT_EQ(absl::c_inner_product(sequence_, vector_, lvalue,
515                                   std::multiplies<int>(), std::plus<int>()),
516             10 * (1 + 1) * (2 + 2) * (3 + 3));
517 }
518 
TEST_F(NumericMutatingTest,AdjacentDifference)519 TEST_F(NumericMutatingTest, AdjacentDifference) {
520   auto last = absl::c_adjacent_difference(list_, std::back_inserter(output_));
521   *last = 1000;
522   std::vector<int> expected{1, 2 - 1, 3 - 2, 1000};
523   EXPECT_EQ(output_, expected);
524 }
525 
TEST_F(NumericMutatingTest,AdjacentDifferenceWithBinaryOp)526 TEST_F(NumericMutatingTest, AdjacentDifferenceWithBinaryOp) {
527   auto last = absl::c_adjacent_difference(list_, std::back_inserter(output_),
528                                           std::multiplies<int>());
529   *last = 1000;
530   std::vector<int> expected{1, 2 * 1, 3 * 2, 1000};
531   EXPECT_EQ(output_, expected);
532 }
533 
TEST_F(NumericMutatingTest,PartialSum)534 TEST_F(NumericMutatingTest, PartialSum) {
535   auto last = absl::c_partial_sum(list_, std::back_inserter(output_));
536   *last = 1000;
537   std::vector<int> expected{1, 1 + 2, 1 + 2 + 3, 1000};
538   EXPECT_EQ(output_, expected);
539 }
540 
TEST_F(NumericMutatingTest,PartialSumWithBinaryOp)541 TEST_F(NumericMutatingTest, PartialSumWithBinaryOp) {
542   auto last = absl::c_partial_sum(list_, std::back_inserter(output_),
543                                   std::multiplies<int>());
544   *last = 1000;
545   std::vector<int> expected{1, 1 * 2, 1 * 2 * 3, 1000};
546   EXPECT_EQ(output_, expected);
547 }
548 
TEST_F(NonMutatingTest,LinearSearch)549 TEST_F(NonMutatingTest, LinearSearch) {
550   EXPECT_TRUE(absl::c_linear_search(container_, 3));
551   EXPECT_FALSE(absl::c_linear_search(container_, 4));
552 }
553 
TEST_F(NonMutatingTest,AllOf)554 TEST_F(NonMutatingTest, AllOf) {
555   const std::vector<int>& v = vector_;
556   EXPECT_FALSE(absl::c_all_of(v, [](int x) { return x > 1; }));
557   EXPECT_TRUE(absl::c_all_of(v, [](int x) { return x > 0; }));
558 }
559 
TEST_F(NonMutatingTest,AnyOf)560 TEST_F(NonMutatingTest, AnyOf) {
561   const std::vector<int>& v = vector_;
562   EXPECT_TRUE(absl::c_any_of(v, [](int x) { return x > 2; }));
563   EXPECT_FALSE(absl::c_any_of(v, [](int x) { return x > 5; }));
564 }
565 
TEST_F(NonMutatingTest,NoneOf)566 TEST_F(NonMutatingTest, NoneOf) {
567   const std::vector<int>& v = vector_;
568   EXPECT_FALSE(absl::c_none_of(v, [](int x) { return x > 2; }));
569   EXPECT_TRUE(absl::c_none_of(v, [](int x) { return x > 5; }));
570 }
571 
TEST_F(NonMutatingTest,MinMaxElementLess)572 TEST_F(NonMutatingTest, MinMaxElementLess) {
573   std::pair<std::vector<int>::const_iterator, std::vector<int>::const_iterator>
574       p = absl::c_minmax_element(vector_, std::less<int>());
575   EXPECT_TRUE(p.first == vector_.begin());
576   EXPECT_TRUE(p.second == vector_.begin() + 2);
577 }
578 
TEST_F(NonMutatingTest,MinMaxElementGreater)579 TEST_F(NonMutatingTest, MinMaxElementGreater) {
580   std::pair<std::vector<int>::const_iterator, std::vector<int>::const_iterator>
581       p = absl::c_minmax_element(vector_, std::greater<int>());
582   EXPECT_TRUE(p.first == vector_.begin() + 2);
583   EXPECT_TRUE(p.second == vector_.begin());
584 }
585 
TEST_F(NonMutatingTest,MinMaxElementNoPredicate)586 TEST_F(NonMutatingTest, MinMaxElementNoPredicate) {
587   std::pair<std::vector<int>::const_iterator, std::vector<int>::const_iterator>
588       p = absl::c_minmax_element(vector_);
589   EXPECT_TRUE(p.first == vector_.begin());
590   EXPECT_TRUE(p.second == vector_.begin() + 2);
591 }
592 
593 class SortingTest : public testing::Test {
594  protected:
595   std::list<int> sorted_ = {1, 2, 3, 4};
596   std::list<int> unsorted_ = {2, 4, 1, 3};
597   std::list<int> reversed_ = {4, 3, 2, 1};
598 };
599 
TEST_F(SortingTest,IsSorted)600 TEST_F(SortingTest, IsSorted) {
601   EXPECT_TRUE(absl::c_is_sorted(sorted_));
602   EXPECT_FALSE(absl::c_is_sorted(unsorted_));
603   EXPECT_FALSE(absl::c_is_sorted(reversed_));
604 }
605 
TEST_F(SortingTest,IsSortedWithPredicate)606 TEST_F(SortingTest, IsSortedWithPredicate) {
607   EXPECT_FALSE(absl::c_is_sorted(sorted_, std::greater<int>()));
608   EXPECT_FALSE(absl::c_is_sorted(unsorted_, std::greater<int>()));
609   EXPECT_TRUE(absl::c_is_sorted(reversed_, std::greater<int>()));
610 }
611 
TEST_F(SortingTest,IsSortedUntil)612 TEST_F(SortingTest, IsSortedUntil) {
613   EXPECT_EQ(1, *absl::c_is_sorted_until(unsorted_));
614   EXPECT_EQ(4, *absl::c_is_sorted_until(unsorted_, std::greater<int>()));
615 }
616 
TEST_F(SortingTest,NthElement)617 TEST_F(SortingTest, NthElement) {
618   std::vector<int> unsorted = {2, 4, 1, 3};
619   absl::c_nth_element(unsorted, unsorted.begin() + 2);
620   EXPECT_THAT(unsorted, ElementsAre(Lt(3), Lt(3), 3, Gt(3)));
621   absl::c_nth_element(unsorted, unsorted.begin() + 2, std::greater<int>());
622   EXPECT_THAT(unsorted, ElementsAre(Gt(2), Gt(2), 2, Lt(2)));
623 }
624 
TEST(MutatingTest,IsPartitioned)625 TEST(MutatingTest, IsPartitioned) {
626   EXPECT_TRUE(
627       absl::c_is_partitioned(std::vector<int>{1, 3, 5, 2, 4, 6}, IsOdd));
628   EXPECT_FALSE(
629       absl::c_is_partitioned(std::vector<int>{1, 2, 3, 4, 5, 6}, IsOdd));
630   EXPECT_FALSE(
631       absl::c_is_partitioned(std::vector<int>{2, 4, 6, 1, 3, 5}, IsOdd));
632 }
633 
TEST(MutatingTest,Partition)634 TEST(MutatingTest, Partition) {
635   std::vector<int> actual = {1, 2, 3, 4, 5};
636   absl::c_partition(actual, IsOdd);
637   EXPECT_THAT(actual, Truly([](const std::vector<int>& c) {
638                 return absl::c_is_partitioned(c, IsOdd);
639               }));
640 }
641 
TEST(MutatingTest,StablePartition)642 TEST(MutatingTest, StablePartition) {
643   std::vector<int> actual = {1, 2, 3, 4, 5};
644   absl::c_stable_partition(actual, IsOdd);
645   EXPECT_THAT(actual, ElementsAre(1, 3, 5, 2, 4));
646 }
647 
TEST(MutatingTest,PartitionCopy)648 TEST(MutatingTest, PartitionCopy) {
649   const std::vector<int> initial = {1, 2, 3, 4, 5};
650   std::vector<int> odds, evens;
651   auto ends = absl::c_partition_copy(initial, back_inserter(odds),
652                                      back_inserter(evens), IsOdd);
653   *ends.first = 7;
654   *ends.second = 6;
655   EXPECT_THAT(odds, ElementsAre(1, 3, 5, 7));
656   EXPECT_THAT(evens, ElementsAre(2, 4, 6));
657 }
658 
TEST(MutatingTest,PartitionPoint)659 TEST(MutatingTest, PartitionPoint) {
660   const std::vector<int> initial = {1, 3, 5, 2, 4};
661   auto middle = absl::c_partition_point(initial, IsOdd);
662   EXPECT_EQ(2, *middle);
663 }
664 
TEST(MutatingTest,CopyMiddle)665 TEST(MutatingTest, CopyMiddle) {
666   const std::vector<int> initial = {4, -1, -2, -3, 5};
667   const std::list<int> input = {1, 2, 3};
668   const std::vector<int> expected = {4, 1, 2, 3, 5};
669 
670   std::list<int> test_list(initial.begin(), initial.end());
671   absl::c_copy(input, ++test_list.begin());
672   EXPECT_EQ(std::list<int>(expected.begin(), expected.end()), test_list);
673 
674   std::vector<int> test_vector = initial;
675   absl::c_copy(input, test_vector.begin() + 1);
676   EXPECT_EQ(expected, test_vector);
677 }
678 
TEST(MutatingTest,CopyFrontInserter)679 TEST(MutatingTest, CopyFrontInserter) {
680   const std::list<int> initial = {4, 5};
681   const std::list<int> input = {1, 2, 3};
682   const std::list<int> expected = {3, 2, 1, 4, 5};
683 
684   std::list<int> test_list = initial;
685   absl::c_copy(input, std::front_inserter(test_list));
686   EXPECT_EQ(expected, test_list);
687 }
688 
TEST(MutatingTest,CopyBackInserter)689 TEST(MutatingTest, CopyBackInserter) {
690   const std::vector<int> initial = {4, 5};
691   const std::list<int> input = {1, 2, 3};
692   const std::vector<int> expected = {4, 5, 1, 2, 3};
693 
694   std::list<int> test_list(initial.begin(), initial.end());
695   absl::c_copy(input, std::back_inserter(test_list));
696   EXPECT_EQ(std::list<int>(expected.begin(), expected.end()), test_list);
697 
698   std::vector<int> test_vector = initial;
699   absl::c_copy(input, std::back_inserter(test_vector));
700   EXPECT_EQ(expected, test_vector);
701 }
702 
TEST(MutatingTest,CopyN)703 TEST(MutatingTest, CopyN) {
704   const std::vector<int> initial = {1, 2, 3, 4, 5};
705   const std::vector<int> expected = {1, 2};
706   std::vector<int> actual;
707   absl::c_copy_n(initial, 2, back_inserter(actual));
708   EXPECT_EQ(expected, actual);
709 }
710 
TEST(MutatingTest,CopyIf)711 TEST(MutatingTest, CopyIf) {
712   const std::list<int> input = {1, 2, 3};
713   std::vector<int> output;
714   absl::c_copy_if(input, std::back_inserter(output),
715                   [](int i) { return i != 2; });
716   EXPECT_THAT(output, ElementsAre(1, 3));
717 }
718 
TEST(MutatingTest,CopyBackward)719 TEST(MutatingTest, CopyBackward) {
720   std::vector<int> actual = {1, 2, 3, 4, 5};
721   std::vector<int> expected = {1, 2, 1, 2, 3};
722   absl::c_copy_backward(absl::MakeSpan(actual.data(), 3), actual.end());
723   EXPECT_EQ(expected, actual);
724 }
725 
TEST(MutatingTest,Move)726 TEST(MutatingTest, Move) {
727   std::vector<std::unique_ptr<int>> src;
728   src.emplace_back(absl::make_unique<int>(1));
729   src.emplace_back(absl::make_unique<int>(2));
730   src.emplace_back(absl::make_unique<int>(3));
731   src.emplace_back(absl::make_unique<int>(4));
732   src.emplace_back(absl::make_unique<int>(5));
733 
734   std::vector<std::unique_ptr<int>> dest = {};
735   absl::c_move(src, std::back_inserter(dest));
736   EXPECT_THAT(src, Each(IsNull()));
737   EXPECT_THAT(dest, ElementsAre(Pointee(1), Pointee(2), Pointee(3), Pointee(4),
738                                 Pointee(5)));
739 }
740 
TEST(MutatingTest,MoveBackward)741 TEST(MutatingTest, MoveBackward) {
742   std::vector<std::unique_ptr<int>> actual;
743   actual.emplace_back(absl::make_unique<int>(1));
744   actual.emplace_back(absl::make_unique<int>(2));
745   actual.emplace_back(absl::make_unique<int>(3));
746   actual.emplace_back(absl::make_unique<int>(4));
747   actual.emplace_back(absl::make_unique<int>(5));
748   auto subrange = absl::MakeSpan(actual.data(), 3);
749   absl::c_move_backward(subrange, actual.end());
750   EXPECT_THAT(actual, ElementsAre(IsNull(), IsNull(), Pointee(1), Pointee(2),
751                                   Pointee(3)));
752 }
753 
TEST(MutatingTest,MoveWithRvalue)754 TEST(MutatingTest, MoveWithRvalue) {
755   auto MakeRValueSrc = [] {
756     std::vector<std::unique_ptr<int>> src;
757     src.emplace_back(absl::make_unique<int>(1));
758     src.emplace_back(absl::make_unique<int>(2));
759     src.emplace_back(absl::make_unique<int>(3));
760     return src;
761   };
762 
763   std::vector<std::unique_ptr<int>> dest = MakeRValueSrc();
764   absl::c_move(MakeRValueSrc(), std::back_inserter(dest));
765   EXPECT_THAT(dest, ElementsAre(Pointee(1), Pointee(2), Pointee(3), Pointee(1),
766                                 Pointee(2), Pointee(3)));
767 }
768 
TEST(MutatingTest,SwapRanges)769 TEST(MutatingTest, SwapRanges) {
770   std::vector<int> odds = {2, 4, 6};
771   std::vector<int> evens = {1, 3, 5};
772   absl::c_swap_ranges(odds, evens);
773   EXPECT_THAT(odds, ElementsAre(1, 3, 5));
774   EXPECT_THAT(evens, ElementsAre(2, 4, 6));
775 
776   odds.pop_back();
777   absl::c_swap_ranges(odds, evens);
778   EXPECT_THAT(odds, ElementsAre(2, 4));
779   EXPECT_THAT(evens, ElementsAre(1, 3, 6));
780 
781   absl::c_swap_ranges(evens, odds);
782   EXPECT_THAT(odds, ElementsAre(1, 3));
783   EXPECT_THAT(evens, ElementsAre(2, 4, 6));
784 }
785 
TEST_F(NonMutatingTest,Transform)786 TEST_F(NonMutatingTest, Transform) {
787   std::vector<int> x{0, 2, 4}, y, z;
788   auto end = absl::c_transform(x, back_inserter(y), std::negate<int>());
789   EXPECT_EQ(std::vector<int>({0, -2, -4}), y);
790   *end = 7;
791   EXPECT_EQ(std::vector<int>({0, -2, -4, 7}), y);
792 
793   y = {1, 3, 0};
794   end = absl::c_transform(x, y, back_inserter(z), std::plus<int>());
795   EXPECT_EQ(std::vector<int>({1, 5, 4}), z);
796   *end = 7;
797   EXPECT_EQ(std::vector<int>({1, 5, 4, 7}), z);
798 
799   z.clear();
800   y.pop_back();
801   end = absl::c_transform(x, y, std::back_inserter(z), std::plus<int>());
802   EXPECT_EQ(std::vector<int>({1, 5}), z);
803   *end = 7;
804   EXPECT_EQ(std::vector<int>({1, 5, 7}), z);
805 
806   z.clear();
807   std::swap(x, y);
808   end = absl::c_transform(x, y, std::back_inserter(z), std::plus<int>());
809   EXPECT_EQ(std::vector<int>({1, 5}), z);
810   *end = 7;
811   EXPECT_EQ(std::vector<int>({1, 5, 7}), z);
812 }
813 
TEST(MutatingTest,Replace)814 TEST(MutatingTest, Replace) {
815   const std::vector<int> initial = {1, 2, 3, 1, 4, 5};
816   const std::vector<int> expected = {4, 2, 3, 4, 4, 5};
817 
818   std::vector<int> test_vector = initial;
819   absl::c_replace(test_vector, 1, 4);
820   EXPECT_EQ(expected, test_vector);
821 
822   std::list<int> test_list(initial.begin(), initial.end());
823   absl::c_replace(test_list, 1, 4);
824   EXPECT_EQ(std::list<int>(expected.begin(), expected.end()), test_list);
825 }
826 
TEST(MutatingTest,ReplaceIf)827 TEST(MutatingTest, ReplaceIf) {
828   std::vector<int> actual = {1, 2, 3, 4, 5};
829   const std::vector<int> expected = {0, 2, 0, 4, 0};
830 
831   absl::c_replace_if(actual, IsOdd, 0);
832   EXPECT_EQ(expected, actual);
833 }
834 
TEST(MutatingTest,ReplaceCopy)835 TEST(MutatingTest, ReplaceCopy) {
836   const std::vector<int> initial = {1, 2, 3, 1, 4, 5};
837   const std::vector<int> expected = {4, 2, 3, 4, 4, 5};
838 
839   std::vector<int> actual;
840   absl::c_replace_copy(initial, back_inserter(actual), 1, 4);
841   EXPECT_EQ(expected, actual);
842 }
843 
TEST(MutatingTest,Sort)844 TEST(MutatingTest, Sort) {
845   std::vector<int> test_vector = {2, 3, 1, 4};
846   absl::c_sort(test_vector);
847   EXPECT_THAT(test_vector, ElementsAre(1, 2, 3, 4));
848 }
849 
TEST(MutatingTest,SortWithPredicate)850 TEST(MutatingTest, SortWithPredicate) {
851   std::vector<int> test_vector = {2, 3, 1, 4};
852   absl::c_sort(test_vector, std::greater<int>());
853   EXPECT_THAT(test_vector, ElementsAre(4, 3, 2, 1));
854 }
855 
856 // For absl::c_stable_sort tests. Needs an operator< that does not cover all
857 // fields so that the test can check the sort preserves order of equal elements.
858 struct Element {
859   int key;
860   int value;
operator <(const Element & e1,const Element & e2)861   friend bool operator<(const Element& e1, const Element& e2) {
862     return e1.key < e2.key;
863   }
864   // Make gmock print useful diagnostics.
operator <<(std::ostream & o,const Element & e)865   friend std::ostream& operator<<(std::ostream& o, const Element& e) {
866     return o << "{" << e.key << ", " << e.value << "}";
867   }
868 };
869 
870 MATCHER_P2(IsElement, key, value, "") {
871   return arg.key == key && arg.value == value;
872 }
873 
TEST(MutatingTest,StableSort)874 TEST(MutatingTest, StableSort) {
875   std::vector<Element> test_vector = {{1, 1}, {2, 1}, {2, 0}, {1, 0}, {2, 2}};
876   absl::c_stable_sort(test_vector);
877   EXPECT_THAT(test_vector,
878               ElementsAre(IsElement(1, 1), IsElement(1, 0), IsElement(2, 1),
879                           IsElement(2, 0), IsElement(2, 2)));
880 }
881 
TEST(MutatingTest,StableSortWithPredicate)882 TEST(MutatingTest, StableSortWithPredicate) {
883   std::vector<Element> test_vector = {{1, 1}, {2, 1}, {2, 0}, {1, 0}, {2, 2}};
884   absl::c_stable_sort(test_vector, [](const Element& e1, const Element& e2) {
885     return e2 < e1;
886   });
887   EXPECT_THAT(test_vector,
888               ElementsAre(IsElement(2, 1), IsElement(2, 0), IsElement(2, 2),
889                           IsElement(1, 1), IsElement(1, 0)));
890 }
891 
TEST(MutatingTest,ReplaceCopyIf)892 TEST(MutatingTest, ReplaceCopyIf) {
893   const std::vector<int> initial = {1, 2, 3, 4, 5};
894   const std::vector<int> expected = {0, 2, 0, 4, 0};
895 
896   std::vector<int> actual;
897   absl::c_replace_copy_if(initial, back_inserter(actual), IsOdd, 0);
898   EXPECT_EQ(expected, actual);
899 }
900 
TEST(MutatingTest,Fill)901 TEST(MutatingTest, Fill) {
902   std::vector<int> actual(5);
903   absl::c_fill(actual, 1);
904   EXPECT_THAT(actual, ElementsAre(1, 1, 1, 1, 1));
905 }
906 
TEST(MutatingTest,FillN)907 TEST(MutatingTest, FillN) {
908   std::vector<int> actual(5, 0);
909   absl::c_fill_n(actual, 2, 1);
910   EXPECT_THAT(actual, ElementsAre(1, 1, 0, 0, 0));
911 }
912 
TEST(MutatingTest,Generate)913 TEST(MutatingTest, Generate) {
914   std::vector<int> actual(5);
915   int x = 0;
916   absl::c_generate(actual, [&x]() { return ++x; });
917   EXPECT_THAT(actual, ElementsAre(1, 2, 3, 4, 5));
918 }
919 
TEST(MutatingTest,GenerateN)920 TEST(MutatingTest, GenerateN) {
921   std::vector<int> actual(5, 0);
922   int x = 0;
923   absl::c_generate_n(actual, 3, [&x]() { return ++x; });
924   EXPECT_THAT(actual, ElementsAre(1, 2, 3, 0, 0));
925 }
926 
TEST(MutatingTest,RemoveCopy)927 TEST(MutatingTest, RemoveCopy) {
928   std::vector<int> actual;
929   absl::c_remove_copy(std::vector<int>{1, 2, 3}, back_inserter(actual), 2);
930   EXPECT_THAT(actual, ElementsAre(1, 3));
931 }
932 
TEST(MutatingTest,RemoveCopyIf)933 TEST(MutatingTest, RemoveCopyIf) {
934   std::vector<int> actual;
935   absl::c_remove_copy_if(std::vector<int>{1, 2, 3}, back_inserter(actual),
936                          IsOdd);
937   EXPECT_THAT(actual, ElementsAre(2));
938 }
939 
TEST(MutatingTest,UniqueCopy)940 TEST(MutatingTest, UniqueCopy) {
941   std::vector<int> actual;
942   absl::c_unique_copy(std::vector<int>{1, 2, 2, 2, 3, 3, 2},
943                       back_inserter(actual));
944   EXPECT_THAT(actual, ElementsAre(1, 2, 3, 2));
945 }
946 
TEST(MutatingTest,UniqueCopyWithPredicate)947 TEST(MutatingTest, UniqueCopyWithPredicate) {
948   std::vector<int> actual;
949   absl::c_unique_copy(std::vector<int>{1, 2, 3, -1, -2, -3, 1},
950                       back_inserter(actual),
951                       [](int x, int y) { return (x < 0) == (y < 0); });
952   EXPECT_THAT(actual, ElementsAre(1, -1, 1));
953 }
954 
TEST(MutatingTest,Reverse)955 TEST(MutatingTest, Reverse) {
956   std::vector<int> test_vector = {1, 2, 3, 4};
957   absl::c_reverse(test_vector);
958   EXPECT_THAT(test_vector, ElementsAre(4, 3, 2, 1));
959 
960   std::list<int> test_list = {1, 2, 3, 4};
961   absl::c_reverse(test_list);
962   EXPECT_THAT(test_list, ElementsAre(4, 3, 2, 1));
963 }
964 
TEST(MutatingTest,ReverseCopy)965 TEST(MutatingTest, ReverseCopy) {
966   std::vector<int> actual;
967   absl::c_reverse_copy(std::vector<int>{1, 2, 3, 4}, back_inserter(actual));
968   EXPECT_THAT(actual, ElementsAre(4, 3, 2, 1));
969 }
970 
TEST(MutatingTest,Rotate)971 TEST(MutatingTest, Rotate) {
972   std::vector<int> actual = {1, 2, 3, 4};
973   auto it = absl::c_rotate(actual, actual.begin() + 2);
974   EXPECT_THAT(actual, testing::ElementsAreArray({3, 4, 1, 2}));
975   EXPECT_EQ(*it, 1);
976 }
977 
TEST(MutatingTest,RotateCopy)978 TEST(MutatingTest, RotateCopy) {
979   std::vector<int> initial = {1, 2, 3, 4};
980   std::vector<int> actual;
981   auto end =
982       absl::c_rotate_copy(initial, initial.begin() + 2, back_inserter(actual));
983   *end = 5;
984   EXPECT_THAT(actual, ElementsAre(3, 4, 1, 2, 5));
985 }
986 
987 template <typename T>
RandomlySeededPrng()988 T RandomlySeededPrng() {
989   std::random_device rdev;
990   std::seed_seq::result_type data[T::state_size];
991   std::generate_n(data, T::state_size, std::ref(rdev));
992   std::seed_seq prng_seed(data, data + T::state_size);
993   return T(prng_seed);
994 }
995 
TEST(MutatingTest,Shuffle)996 TEST(MutatingTest, Shuffle) {
997   std::vector<int> actual = {1, 2, 3, 4, 5};
998   absl::c_shuffle(actual, RandomlySeededPrng<std::mt19937_64>());
999   EXPECT_THAT(actual, UnorderedElementsAre(1, 2, 3, 4, 5));
1000 }
1001 
TEST(MutatingTest,Sample)1002 TEST(MutatingTest, Sample) {
1003   std::vector<int> actual;
1004   absl::c_sample(std::vector<int>{1, 2, 3, 4, 5}, std::back_inserter(actual), 3,
1005                  RandomlySeededPrng<std::mt19937_64>());
1006   EXPECT_THAT(actual, IsSubsetOf({1, 2, 3, 4, 5}));
1007   EXPECT_THAT(actual, SizeIs(3));
1008 }
1009 
TEST(MutatingTest,PartialSort)1010 TEST(MutatingTest, PartialSort) {
1011   std::vector<int> sequence{5, 3, 42, 0};
1012   absl::c_partial_sort(sequence, sequence.begin() + 2);
1013   EXPECT_THAT(absl::MakeSpan(sequence.data(), 2), ElementsAre(0, 3));
1014   absl::c_partial_sort(sequence, sequence.begin() + 2, std::greater<int>());
1015   EXPECT_THAT(absl::MakeSpan(sequence.data(), 2), ElementsAre(42, 5));
1016 }
1017 
TEST(MutatingTest,PartialSortCopy)1018 TEST(MutatingTest, PartialSortCopy) {
1019   const std::vector<int> initial = {5, 3, 42, 0};
1020   std::vector<int> actual(2);
1021   absl::c_partial_sort_copy(initial, actual);
1022   EXPECT_THAT(actual, ElementsAre(0, 3));
1023   absl::c_partial_sort_copy(initial, actual, std::greater<int>());
1024   EXPECT_THAT(actual, ElementsAre(42, 5));
1025 }
1026 
TEST(MutatingTest,Merge)1027 TEST(MutatingTest, Merge) {
1028   std::vector<int> actual;
1029   absl::c_merge(std::vector<int>{1, 3, 5}, std::vector<int>{2, 4},
1030                 back_inserter(actual));
1031   EXPECT_THAT(actual, ElementsAre(1, 2, 3, 4, 5));
1032 }
1033 
TEST(MutatingTest,MergeWithComparator)1034 TEST(MutatingTest, MergeWithComparator) {
1035   std::vector<int> actual;
1036   absl::c_merge(std::vector<int>{5, 3, 1}, std::vector<int>{4, 2},
1037                 back_inserter(actual), std::greater<int>());
1038   EXPECT_THAT(actual, ElementsAre(5, 4, 3, 2, 1));
1039 }
1040 
TEST(MutatingTest,InplaceMerge)1041 TEST(MutatingTest, InplaceMerge) {
1042   std::vector<int> actual = {1, 3, 5, 2, 4};
1043   absl::c_inplace_merge(actual, actual.begin() + 3);
1044   EXPECT_THAT(actual, ElementsAre(1, 2, 3, 4, 5));
1045 }
1046 
TEST(MutatingTest,InplaceMergeWithComparator)1047 TEST(MutatingTest, InplaceMergeWithComparator) {
1048   std::vector<int> actual = {5, 3, 1, 4, 2};
1049   absl::c_inplace_merge(actual, actual.begin() + 3, std::greater<int>());
1050   EXPECT_THAT(actual, ElementsAre(5, 4, 3, 2, 1));
1051 }
1052 
1053 class SetOperationsTest : public testing::Test {
1054  protected:
1055   std::vector<int> a_ = {1, 2, 3};
1056   std::vector<int> b_ = {1, 3, 5};
1057 
1058   std::vector<int> a_reversed_ = {3, 2, 1};
1059   std::vector<int> b_reversed_ = {5, 3, 1};
1060 };
1061 
TEST_F(SetOperationsTest,SetUnion)1062 TEST_F(SetOperationsTest, SetUnion) {
1063   std::vector<int> actual;
1064   absl::c_set_union(a_, b_, back_inserter(actual));
1065   EXPECT_THAT(actual, ElementsAre(1, 2, 3, 5));
1066 }
1067 
TEST_F(SetOperationsTest,SetUnionWithComparator)1068 TEST_F(SetOperationsTest, SetUnionWithComparator) {
1069   std::vector<int> actual;
1070   absl::c_set_union(a_reversed_, b_reversed_, back_inserter(actual),
1071                     std::greater<int>());
1072   EXPECT_THAT(actual, ElementsAre(5, 3, 2, 1));
1073 }
1074 
TEST_F(SetOperationsTest,SetIntersection)1075 TEST_F(SetOperationsTest, SetIntersection) {
1076   std::vector<int> actual;
1077   absl::c_set_intersection(a_, b_, back_inserter(actual));
1078   EXPECT_THAT(actual, ElementsAre(1, 3));
1079 }
1080 
TEST_F(SetOperationsTest,SetIntersectionWithComparator)1081 TEST_F(SetOperationsTest, SetIntersectionWithComparator) {
1082   std::vector<int> actual;
1083   absl::c_set_intersection(a_reversed_, b_reversed_, back_inserter(actual),
1084                            std::greater<int>());
1085   EXPECT_THAT(actual, ElementsAre(3, 1));
1086 }
1087 
TEST_F(SetOperationsTest,SetDifference)1088 TEST_F(SetOperationsTest, SetDifference) {
1089   std::vector<int> actual;
1090   absl::c_set_difference(a_, b_, back_inserter(actual));
1091   EXPECT_THAT(actual, ElementsAre(2));
1092 }
1093 
TEST_F(SetOperationsTest,SetDifferenceWithComparator)1094 TEST_F(SetOperationsTest, SetDifferenceWithComparator) {
1095   std::vector<int> actual;
1096   absl::c_set_difference(a_reversed_, b_reversed_, back_inserter(actual),
1097                          std::greater<int>());
1098   EXPECT_THAT(actual, ElementsAre(2));
1099 }
1100 
TEST_F(SetOperationsTest,SetSymmetricDifference)1101 TEST_F(SetOperationsTest, SetSymmetricDifference) {
1102   std::vector<int> actual;
1103   absl::c_set_symmetric_difference(a_, b_, back_inserter(actual));
1104   EXPECT_THAT(actual, ElementsAre(2, 5));
1105 }
1106 
TEST_F(SetOperationsTest,SetSymmetricDifferenceWithComparator)1107 TEST_F(SetOperationsTest, SetSymmetricDifferenceWithComparator) {
1108   std::vector<int> actual;
1109   absl::c_set_symmetric_difference(a_reversed_, b_reversed_,
1110                                    back_inserter(actual), std::greater<int>());
1111   EXPECT_THAT(actual, ElementsAre(5, 2));
1112 }
1113 
TEST(HeapOperationsTest,WithoutComparator)1114 TEST(HeapOperationsTest, WithoutComparator) {
1115   std::vector<int> heap = {1, 2, 3};
1116   EXPECT_FALSE(absl::c_is_heap(heap));
1117   absl::c_make_heap(heap);
1118   EXPECT_TRUE(absl::c_is_heap(heap));
1119   heap.push_back(4);
1120   EXPECT_EQ(3, absl::c_is_heap_until(heap) - heap.begin());
1121   absl::c_push_heap(heap);
1122   EXPECT_EQ(4, heap[0]);
1123   absl::c_pop_heap(heap);
1124   EXPECT_EQ(4, heap[3]);
1125   absl::c_make_heap(heap);
1126   absl::c_sort_heap(heap);
1127   EXPECT_THAT(heap, ElementsAre(1, 2, 3, 4));
1128   EXPECT_FALSE(absl::c_is_heap(heap));
1129 }
1130 
TEST(HeapOperationsTest,WithComparator)1131 TEST(HeapOperationsTest, WithComparator) {
1132   using greater = std::greater<int>;
1133   std::vector<int> heap = {3, 2, 1};
1134   EXPECT_FALSE(absl::c_is_heap(heap, greater()));
1135   absl::c_make_heap(heap, greater());
1136   EXPECT_TRUE(absl::c_is_heap(heap, greater()));
1137   heap.push_back(0);
1138   EXPECT_EQ(3, absl::c_is_heap_until(heap, greater()) - heap.begin());
1139   absl::c_push_heap(heap, greater());
1140   EXPECT_EQ(0, heap[0]);
1141   absl::c_pop_heap(heap, greater());
1142   EXPECT_EQ(0, heap[3]);
1143   absl::c_make_heap(heap, greater());
1144   absl::c_sort_heap(heap, greater());
1145   EXPECT_THAT(heap, ElementsAre(3, 2, 1, 0));
1146   EXPECT_FALSE(absl::c_is_heap(heap, greater()));
1147 }
1148 
TEST(MutatingTest,PermutationOperations)1149 TEST(MutatingTest, PermutationOperations) {
1150   std::vector<int> initial = {1, 2, 3, 4};
1151   std::vector<int> permuted = initial;
1152 
1153   absl::c_next_permutation(permuted);
1154   EXPECT_TRUE(absl::c_is_permutation(initial, permuted));
1155   EXPECT_TRUE(absl::c_is_permutation(initial, permuted, std::equal_to<int>()));
1156 
1157   std::vector<int> permuted2 = initial;
1158   absl::c_prev_permutation(permuted2, std::greater<int>());
1159   EXPECT_EQ(permuted, permuted2);
1160 
1161   absl::c_prev_permutation(permuted);
1162   EXPECT_EQ(initial, permuted);
1163 }
1164 
1165 #if defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \
1166     ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
TEST(ConstexprTest,Distance)1167 TEST(ConstexprTest, Distance) {
1168   // Works at compile time with constexpr containers.
1169   static_assert(absl::c_distance(std::array<int, 3>()) == 3);
1170 }
1171 
TEST(ConstexprTest,MinElement)1172 TEST(ConstexprTest, MinElement) {
1173   constexpr std::array<int, 3> kArray = {1, 2, 3};
1174   static_assert(*absl::c_min_element(kArray) == 1);
1175 }
1176 
TEST(ConstexprTest,MinElementWithPredicate)1177 TEST(ConstexprTest, MinElementWithPredicate) {
1178   constexpr std::array<int, 3> kArray = {1, 2, 3};
1179   static_assert(*absl::c_min_element(kArray, std::greater<int>()) == 3);
1180 }
1181 
TEST(ConstexprTest,MaxElement)1182 TEST(ConstexprTest, MaxElement) {
1183   constexpr std::array<int, 3> kArray = {1, 2, 3};
1184   static_assert(*absl::c_max_element(kArray) == 3);
1185 }
1186 
TEST(ConstexprTest,MaxElementWithPredicate)1187 TEST(ConstexprTest, MaxElementWithPredicate) {
1188   constexpr std::array<int, 3> kArray = {1, 2, 3};
1189   static_assert(*absl::c_max_element(kArray, std::greater<int>()) == 1);
1190 }
1191 
TEST(ConstexprTest,MinMaxElement)1192 TEST(ConstexprTest, MinMaxElement) {
1193   static constexpr std::array<int, 3> kArray = {1, 2, 3};
1194   constexpr auto kMinMaxPair = absl::c_minmax_element(kArray);
1195   static_assert(*kMinMaxPair.first == 1);
1196   static_assert(*kMinMaxPair.second == 3);
1197 }
1198 
TEST(ConstexprTest,MinMaxElementWithPredicate)1199 TEST(ConstexprTest, MinMaxElementWithPredicate) {
1200   static constexpr std::array<int, 3> kArray = {1, 2, 3};
1201   constexpr auto kMinMaxPair =
1202       absl::c_minmax_element(kArray, std::greater<int>());
1203   static_assert(*kMinMaxPair.first == 3);
1204   static_assert(*kMinMaxPair.second == 1);
1205 }
1206 
1207 #endif  // defined(ABSL_INTERNAL_CPLUSPLUS_LANG) &&
1208         //  ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
1209 
1210 }  // namespace
1211