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