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