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