blob: 093b281477c8a7a04d0ae654368662cb80756099 [file] [log] [blame]
mistergc2e75482017-09-19 16:54:40 -04001// 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// http://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, SwapRanges) {
653 std::vector<int> odds = {2, 4, 6};
654 std::vector<int> evens = {1, 3, 5};
655 absl::c_swap_ranges(odds, evens);
656 EXPECT_THAT(odds, ElementsAre(1, 3, 5));
657 EXPECT_THAT(evens, ElementsAre(2, 4, 6));
658}
659
660TEST_F(NonMutatingTest, Transform) {
661 std::vector<int> x{0, 2, 4}, y, z;
662 auto end = absl::c_transform(x, back_inserter(y), std::negate<int>());
663 EXPECT_EQ(std::vector<int>({0, -2, -4}), y);
664 *end = 7;
665 EXPECT_EQ(std::vector<int>({0, -2, -4, 7}), y);
666
667 y = {1, 3, 0};
668 end = absl::c_transform(x, y, back_inserter(z), std::plus<int>());
669 EXPECT_EQ(std::vector<int>({1, 5, 4}), z);
670 *end = 7;
671 EXPECT_EQ(std::vector<int>({1, 5, 4, 7}), z);
672}
673
674TEST(MutatingTest, Replace) {
675 const std::vector<int> initial = {1, 2, 3, 1, 4, 5};
676 const std::vector<int> expected = {4, 2, 3, 4, 4, 5};
677
678 std::vector<int> test_vector = initial;
679 absl::c_replace(test_vector, 1, 4);
680 EXPECT_EQ(expected, test_vector);
681
682 std::list<int> test_list(initial.begin(), initial.end());
683 absl::c_replace(test_list, 1, 4);
684 EXPECT_EQ(std::list<int>(expected.begin(), expected.end()), test_list);
685}
686
687TEST(MutatingTest, ReplaceIf) {
688 std::vector<int> actual = {1, 2, 3, 4, 5};
689 const std::vector<int> expected = {0, 2, 0, 4, 0};
690
691 absl::c_replace_if(actual, IsOdd, 0);
692 EXPECT_EQ(expected, actual);
693}
694
695TEST(MutatingTest, ReplaceCopy) {
696 const std::vector<int> initial = {1, 2, 3, 1, 4, 5};
697 const std::vector<int> expected = {4, 2, 3, 4, 4, 5};
698
699 std::vector<int> actual;
700 absl::c_replace_copy(initial, back_inserter(actual), 1, 4);
701 EXPECT_EQ(expected, actual);
702}
703
704TEST(MutatingTest, Sort) {
705 std::vector<int> test_vector = {2, 3, 1, 4};
706 absl::c_sort(test_vector);
707 EXPECT_THAT(test_vector, ElementsAre(1, 2, 3, 4));
708}
709
710TEST(MutatingTest, SortWithPredicate) {
711 std::vector<int> test_vector = {2, 3, 1, 4};
712 absl::c_sort(test_vector, std::greater<int>());
713 EXPECT_THAT(test_vector, ElementsAre(4, 3, 2, 1));
714}
715
716// For absl::c_stable_sort tests. Needs an operator< that does not cover all
717// fields so that the test can check the sort preserves order of equal elements.
718struct Element {
719 int key;
720 int value;
721 friend bool operator<(const Element& e1, const Element& e2) {
722 return e1.key < e2.key;
723 }
724 // Make gmock print useful diagnostics.
725 friend std::ostream& operator<<(std::ostream& o, const Element& e) {
726 return o << "{" << e.key << ", " << e.value << "}";
727 }
728};
729
730MATCHER_P2(IsElement, key, value, "") {
731 return arg.key == key && arg.value == value;
732}
733
734TEST(MutatingTest, StableSort) {
735 std::vector<Element> test_vector = {{1, 1}, {2, 1}, {2, 0}, {1, 0}, {2, 2}};
736 absl::c_stable_sort(test_vector);
737 EXPECT_THAT(
738 test_vector,
739 ElementsAre(IsElement(1, 1), IsElement(1, 0), IsElement(2, 1),
740 IsElement(2, 0), IsElement(2, 2)));
741}
742
743TEST(MutatingTest, StableSortWithPredicate) {
744 std::vector<Element> test_vector = {{1, 1}, {2, 1}, {2, 0}, {1, 0}, {2, 2}};
745 absl::c_stable_sort(test_vector, [](const Element& e1, const Element& e2) {
746 return e2 < e1;
747 });
748 EXPECT_THAT(
749 test_vector,
750 ElementsAre(IsElement(2, 1), IsElement(2, 0), IsElement(2, 2),
751 IsElement(1, 1), IsElement(1, 0)));
752}
753
754TEST(MutatingTest, ReplaceCopyIf) {
755 const std::vector<int> initial = {1, 2, 3, 4, 5};
756 const std::vector<int> expected = {0, 2, 0, 4, 0};
757
758 std::vector<int> actual;
759 absl::c_replace_copy_if(initial, back_inserter(actual), IsOdd, 0);
760 EXPECT_EQ(expected, actual);
761}
762
763TEST(MutatingTest, Fill) {
764 std::vector<int> actual(5);
765 absl::c_fill(actual, 1);
766 EXPECT_THAT(actual, ElementsAre(1, 1, 1, 1, 1));
767}
768
769TEST(MutatingTest, FillN) {
770 std::vector<int> actual(5, 0);
771 absl::c_fill_n(actual, 2, 1);
772 EXPECT_THAT(actual, ElementsAre(1, 1, 0, 0, 0));
773}
774
775TEST(MutatingTest, Generate) {
776 std::vector<int> actual(5);
777 int x = 0;
778 absl::c_generate(actual, [&x]() { return ++x; });
779 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 4, 5));
780}
781
782TEST(MutatingTest, GenerateN) {
783 std::vector<int> actual(5, 0);
784 int x = 0;
785 absl::c_generate_n(actual, 3, [&x]() { return ++x; });
786 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 0, 0));
787}
788
789TEST(MutatingTest, RemoveCopy) {
790 std::vector<int> actual;
791 absl::c_remove_copy(std::vector<int>{1, 2, 3}, back_inserter(actual), 2);
792 EXPECT_THAT(actual, ElementsAre(1, 3));
793}
794
795TEST(MutatingTest, RemoveCopyIf) {
796 std::vector<int> actual;
797 absl::c_remove_copy_if(std::vector<int>{1, 2, 3}, back_inserter(actual),
798 IsOdd);
799 EXPECT_THAT(actual, ElementsAre(2));
800}
801
802TEST(MutatingTest, UniqueCopy) {
803 std::vector<int> actual;
804 absl::c_unique_copy(std::vector<int>{1, 2, 2, 2, 3, 3, 2},
805 back_inserter(actual));
806 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 2));
807}
808
809TEST(MutatingTest, UniqueCopyWithPredicate) {
810 std::vector<int> actual;
811 absl::c_unique_copy(std::vector<int>{1, 2, 3, -1, -2, -3, 1},
812 back_inserter(actual),
813 [](int x, int y) { return (x < 0) == (y < 0); });
814 EXPECT_THAT(actual, ElementsAre(1, -1, 1));
815}
816
817TEST(MutatingTest, Reverse) {
818 std::vector<int> test_vector = {1, 2, 3, 4};
819 absl::c_reverse(test_vector);
820 EXPECT_THAT(test_vector, ElementsAre(4, 3, 2, 1));
821
822 std::list<int> test_list = {1, 2, 3, 4};
823 absl::c_reverse(test_list);
824 EXPECT_THAT(test_list, ElementsAre(4, 3, 2, 1));
825}
826
827TEST(MutatingTest, ReverseCopy) {
828 std::vector<int> actual;
829 absl::c_reverse_copy(std::vector<int>{1, 2, 3, 4}, back_inserter(actual));
830 EXPECT_THAT(actual, ElementsAre(4, 3, 2, 1));
831}
832
833TEST(MutatingTest, Rotate) {
834 std::vector<int> actual = {1, 2, 3, 4};
835 auto it = absl::c_rotate(actual, actual.begin() + 2);
836 EXPECT_THAT(actual, testing::ElementsAreArray({3, 4, 1, 2}));
837 EXPECT_EQ(*it, 1);
838}
839
840TEST(MutatingTest, RotateCopy) {
841 std::vector<int> initial = {1, 2, 3, 4};
842 std::vector<int> actual;
843 auto end =
844 absl::c_rotate_copy(initial, initial.begin() + 2, back_inserter(actual));
845 *end = 5;
846 EXPECT_THAT(actual, ElementsAre(3, 4, 1, 2, 5));
847}
848
849TEST(MutatingTest, Shuffle) {
850 std::vector<int> actual = {1, 2, 3, 4, 5};
851 absl::c_shuffle(actual, std::random_device());
852 EXPECT_THAT(actual, UnorderedElementsAre(1, 2, 3, 4, 5));
853}
854
855TEST(MutatingTest, PartialSort) {
856 std::vector<int> sequence{5, 3, 42, 0};
857 absl::c_partial_sort(sequence, sequence.begin() + 2);
858 EXPECT_THAT(absl::MakeSpan(sequence.data(), 2), ElementsAre(0, 3));
859 absl::c_partial_sort(sequence, sequence.begin() + 2, std::greater<int>());
860 EXPECT_THAT(absl::MakeSpan(sequence.data(), 2), ElementsAre(42, 5));
861}
862
863TEST(MutatingTest, PartialSortCopy) {
864 const std::vector<int> initial = {5, 3, 42, 0};
865 std::vector<int> actual(2);
866 absl::c_partial_sort_copy(initial, actual);
867 EXPECT_THAT(actual, ElementsAre(0, 3));
868 absl::c_partial_sort_copy(initial, actual, std::greater<int>());
869 EXPECT_THAT(actual, ElementsAre(42, 5));
870}
871
872TEST(MutatingTest, Merge) {
873 std::vector<int> actual;
874 absl::c_merge(std::vector<int>{1, 3, 5}, std::vector<int>{2, 4},
875 back_inserter(actual));
876 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 4, 5));
877}
878
879TEST(MutatingTest, MergeWithComparator) {
880 std::vector<int> actual;
881 absl::c_merge(std::vector<int>{5, 3, 1}, std::vector<int>{4, 2},
882 back_inserter(actual), std::greater<int>());
883 EXPECT_THAT(actual, ElementsAre(5, 4, 3, 2, 1));
884}
885
886TEST(MutatingTest, InplaceMerge) {
887 std::vector<int> actual = {1, 3, 5, 2, 4};
888 absl::c_inplace_merge(actual, actual.begin() + 3);
889 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 4, 5));
890}
891
892TEST(MutatingTest, InplaceMergeWithComparator) {
893 std::vector<int> actual = {5, 3, 1, 4, 2};
894 absl::c_inplace_merge(actual, actual.begin() + 3, std::greater<int>());
895 EXPECT_THAT(actual, ElementsAre(5, 4, 3, 2, 1));
896}
897
898class SetOperationsTest : public testing::Test {
899 protected:
900 std::vector<int> a_ = {1, 2, 3};
901 std::vector<int> b_ = {1, 3, 5};
902
903 std::vector<int> a_reversed_ = {3, 2, 1};
904 std::vector<int> b_reversed_ = {5, 3, 1};
905};
906
907TEST_F(SetOperationsTest, SetUnion) {
908 std::vector<int> actual;
909 absl::c_set_union(a_, b_, back_inserter(actual));
910 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 5));
911}
912
913TEST_F(SetOperationsTest, SetUnionWithComparator) {
914 std::vector<int> actual;
915 absl::c_set_union(a_reversed_, b_reversed_, back_inserter(actual),
916 std::greater<int>());
917 EXPECT_THAT(actual, ElementsAre(5, 3, 2, 1));
918}
919
920TEST_F(SetOperationsTest, SetIntersection) {
921 std::vector<int> actual;
922 absl::c_set_intersection(a_, b_, back_inserter(actual));
923 EXPECT_THAT(actual, ElementsAre(1, 3));
924}
925
926TEST_F(SetOperationsTest, SetIntersectionWithComparator) {
927 std::vector<int> actual;
928 absl::c_set_intersection(a_reversed_, b_reversed_, back_inserter(actual),
929 std::greater<int>());
930 EXPECT_THAT(actual, ElementsAre(3, 1));
931}
932
933TEST_F(SetOperationsTest, SetDifference) {
934 std::vector<int> actual;
935 absl::c_set_difference(a_, b_, back_inserter(actual));
936 EXPECT_THAT(actual, ElementsAre(2));
937}
938
939TEST_F(SetOperationsTest, SetDifferenceWithComparator) {
940 std::vector<int> actual;
941 absl::c_set_difference(a_reversed_, b_reversed_, back_inserter(actual),
942 std::greater<int>());
943 EXPECT_THAT(actual, ElementsAre(2));
944}
945
946TEST_F(SetOperationsTest, SetSymmetricDifference) {
947 std::vector<int> actual;
948 absl::c_set_symmetric_difference(a_, b_, back_inserter(actual));
949 EXPECT_THAT(actual, ElementsAre(2, 5));
950}
951
952TEST_F(SetOperationsTest, SetSymmetricDifferenceWithComparator) {
953 std::vector<int> actual;
954 absl::c_set_symmetric_difference(a_reversed_, b_reversed_,
955 back_inserter(actual), std::greater<int>());
956 EXPECT_THAT(actual, ElementsAre(5, 2));
957}
958
959TEST(HeapOperationsTest, WithoutComparator) {
960 std::vector<int> heap = {1, 2, 3};
961 EXPECT_FALSE(absl::c_is_heap(heap));
962 absl::c_make_heap(heap);
963 EXPECT_TRUE(absl::c_is_heap(heap));
964 heap.push_back(4);
965 EXPECT_EQ(3, absl::c_is_heap_until(heap) - heap.begin());
966 absl::c_push_heap(heap);
967 EXPECT_EQ(4, heap[0]);
968 absl::c_pop_heap(heap);
969 EXPECT_EQ(4, heap[3]);
970 absl::c_make_heap(heap);
971 absl::c_sort_heap(heap);
972 EXPECT_THAT(heap, ElementsAre(1, 2, 3, 4));
973 EXPECT_FALSE(absl::c_is_heap(heap));
974}
975
976TEST(HeapOperationsTest, WithComparator) {
977 using greater = std::greater<int>;
978 std::vector<int> heap = {3, 2, 1};
979 EXPECT_FALSE(absl::c_is_heap(heap, greater()));
980 absl::c_make_heap(heap, greater());
981 EXPECT_TRUE(absl::c_is_heap(heap, greater()));
982 heap.push_back(0);
983 EXPECT_EQ(3, absl::c_is_heap_until(heap, greater()) - heap.begin());
984 absl::c_push_heap(heap, greater());
985 EXPECT_EQ(0, heap[0]);
986 absl::c_pop_heap(heap, greater());
987 EXPECT_EQ(0, heap[3]);
988 absl::c_make_heap(heap, greater());
989 absl::c_sort_heap(heap, greater());
990 EXPECT_THAT(heap, ElementsAre(3, 2, 1, 0));
991 EXPECT_FALSE(absl::c_is_heap(heap, greater()));
992}
993
994TEST(MutatingTest, PermutationOperations) {
995 std::vector<int> initial = {1, 2, 3, 4};
996 std::vector<int> permuted = initial;
997
998 absl::c_next_permutation(permuted);
999 EXPECT_TRUE(absl::c_is_permutation(initial, permuted));
1000 EXPECT_TRUE(absl::c_is_permutation(initial, permuted, std::equal_to<int>()));
1001
1002 std::vector<int> permuted2 = initial;
1003 absl::c_prev_permutation(permuted2, std::greater<int>());
1004 EXPECT_EQ(permuted, permuted2);
1005
1006 absl::c_prev_permutation(permuted);
1007 EXPECT_EQ(initial, permuted);
1008}
1009
1010} // namespace