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