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