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