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