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