container_test.cc (48160B)
1 // 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 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 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 <algorithm> 18 #include <array> 19 #include <functional> 20 #include <initializer_list> 21 #include <iterator> 22 #include <list> 23 #include <memory> 24 #include <ostream> 25 #include <random> 26 #include <set> 27 #include <unordered_set> 28 #include <utility> 29 #include <valarray> 30 #include <vector> 31 32 #include "gmock/gmock.h" 33 #include "gtest/gtest.h" 34 #include "absl/base/casts.h" 35 #include "absl/base/config.h" 36 #include "absl/base/macros.h" 37 #include "absl/memory/memory.h" 38 #include "absl/types/span.h" 39 40 namespace { 41 42 using ::testing::Each; 43 using ::testing::ElementsAre; 44 using ::testing::Gt; 45 using ::testing::IsNull; 46 using ::testing::IsSubsetOf; 47 using ::testing::Lt; 48 using ::testing::Pointee; 49 using ::testing::SizeIs; 50 using ::testing::Truly; 51 using ::testing::UnorderedElementsAre; 52 53 // Most of these tests just check that the code compiles, not that it 54 // does the right thing. That's fine since the functions just forward 55 // to the STL implementation. 56 class NonMutatingTest : public testing::Test { 57 protected: 58 std::unordered_set<int> container_ = {1, 2, 3}; 59 std::list<int> sequence_ = {1, 2, 3}; 60 std::vector<int> vector_ = {1, 2, 3}; 61 int array_[3] = {1, 2, 3}; 62 }; 63 64 struct AccumulateCalls { 65 void operator()(int value) { calls.push_back(value); } 66 std::vector<int> calls; 67 }; 68 69 bool Predicate(int value) { return value < 3; } 70 bool BinPredicate(int v1, int v2) { return v1 < v2; } 71 bool Equals(int v1, int v2) { return v1 == v2; } 72 bool IsOdd(int x) { return x % 2 != 0; } 73 74 TEST_F(NonMutatingTest, Distance) { 75 EXPECT_EQ(container_.size(), 76 static_cast<size_t>(absl::c_distance(container_))); 77 EXPECT_EQ(sequence_.size(), static_cast<size_t>(absl::c_distance(sequence_))); 78 EXPECT_EQ(vector_.size(), static_cast<size_t>(absl::c_distance(vector_))); 79 EXPECT_EQ(ABSL_ARRAYSIZE(array_), 80 static_cast<size_t>(absl::c_distance(array_))); 81 82 // Works with a temporary argument. 83 EXPECT_EQ(vector_.size(), 84 static_cast<size_t>(absl::c_distance(std::vector<int>(vector_)))); 85 } 86 87 TEST_F(NonMutatingTest, Distance_OverloadedBeginEnd) { 88 // Works with classes which have custom ADL-selected overloads of std::begin 89 // and std::end. 90 std::initializer_list<int> a = {1, 2, 3}; 91 std::valarray<int> b = {1, 2, 3}; 92 EXPECT_EQ(3, absl::c_distance(a)); 93 EXPECT_EQ(3, absl::c_distance(b)); 94 95 // It is assumed that other c_* functions use the same mechanism for 96 // ADL-selecting begin/end overloads. 97 } 98 99 TEST_F(NonMutatingTest, ForEach) { 100 AccumulateCalls c = absl::c_for_each(container_, AccumulateCalls()); 101 // Don't rely on the unordered_set's order. 102 std::sort(c.calls.begin(), c.calls.end()); 103 EXPECT_EQ(vector_, c.calls); 104 105 // Works with temporary container, too. 106 AccumulateCalls c2 = 107 absl::c_for_each(std::unordered_set<int>(container_), AccumulateCalls()); 108 std::sort(c2.calls.begin(), c2.calls.end()); 109 EXPECT_EQ(vector_, c2.calls); 110 } 111 112 TEST_F(NonMutatingTest, FindReturnsCorrectType) { 113 auto it = absl::c_find(container_, 3); 114 EXPECT_EQ(3, *it); 115 absl::c_find(absl::implicit_cast<const std::list<int>&>(sequence_), 3); 116 } 117 118 TEST_F(NonMutatingTest, Contains) { 119 EXPECT_TRUE(absl::c_contains(container_, 3)); 120 EXPECT_FALSE(absl::c_contains(container_, 4)); 121 } 122 123 TEST_F(NonMutatingTest, FindIf) { absl::c_find_if(container_, Predicate); } 124 125 TEST_F(NonMutatingTest, FindIfNot) { 126 absl::c_find_if_not(container_, Predicate); 127 } 128 129 TEST_F(NonMutatingTest, FindEnd) { 130 absl::c_find_end(sequence_, vector_); 131 absl::c_find_end(vector_, sequence_); 132 } 133 134 TEST_F(NonMutatingTest, FindEndWithPredicate) { 135 absl::c_find_end(sequence_, vector_, BinPredicate); 136 absl::c_find_end(vector_, sequence_, BinPredicate); 137 } 138 139 TEST_F(NonMutatingTest, FindFirstOf) { 140 absl::c_find_first_of(container_, sequence_); 141 absl::c_find_first_of(sequence_, container_); 142 absl::c_find_first_of(sequence_, std::array<int, 2>{1, 2}); 143 } 144 145 TEST_F(NonMutatingTest, FindFirstOfWithPredicate) { 146 absl::c_find_first_of(container_, sequence_, BinPredicate); 147 absl::c_find_first_of(sequence_, container_, BinPredicate); 148 absl::c_find_first_of(sequence_, std::array<int, 2>{1, 2}, BinPredicate); 149 } 150 151 TEST_F(NonMutatingTest, AdjacentFind) { absl::c_adjacent_find(sequence_); } 152 153 TEST_F(NonMutatingTest, AdjacentFindWithPredicate) { 154 absl::c_adjacent_find(sequence_, BinPredicate); 155 } 156 157 TEST_F(NonMutatingTest, Count) { EXPECT_EQ(1, absl::c_count(container_, 3)); } 158 159 TEST_F(NonMutatingTest, CountIf) { 160 EXPECT_EQ(2, absl::c_count_if(container_, Predicate)); 161 const std::unordered_set<int>& const_container = container_; 162 EXPECT_EQ(2, absl::c_count_if(const_container, Predicate)); 163 } 164 165 TEST_F(NonMutatingTest, Mismatch) { 166 // Testing necessary as absl::c_mismatch executes logic. 167 { 168 auto result = absl::c_mismatch(vector_, sequence_); 169 EXPECT_EQ(result.first, vector_.end()); 170 EXPECT_EQ(result.second, sequence_.end()); 171 } 172 { 173 auto result = absl::c_mismatch(sequence_, vector_); 174 EXPECT_EQ(result.first, sequence_.end()); 175 EXPECT_EQ(result.second, vector_.end()); 176 } 177 178 sequence_.back() = 5; 179 { 180 auto result = absl::c_mismatch(vector_, sequence_); 181 EXPECT_EQ(result.first, std::prev(vector_.end())); 182 EXPECT_EQ(result.second, std::prev(sequence_.end())); 183 } 184 { 185 auto result = absl::c_mismatch(sequence_, vector_); 186 EXPECT_EQ(result.first, std::prev(sequence_.end())); 187 EXPECT_EQ(result.second, std::prev(vector_.end())); 188 } 189 190 sequence_.pop_back(); 191 { 192 auto result = absl::c_mismatch(vector_, sequence_); 193 EXPECT_EQ(result.first, std::prev(vector_.end())); 194 EXPECT_EQ(result.second, sequence_.end()); 195 } 196 { 197 auto result = absl::c_mismatch(sequence_, vector_); 198 EXPECT_EQ(result.first, sequence_.end()); 199 EXPECT_EQ(result.second, std::prev(vector_.end())); 200 } 201 { 202 struct NoNotEquals { 203 constexpr bool operator==(NoNotEquals) const { return true; } 204 constexpr bool operator!=(NoNotEquals) const = delete; 205 }; 206 std::vector<NoNotEquals> first; 207 std::list<NoNotEquals> second; 208 209 // Check this still compiles. 210 absl::c_mismatch(first, second); 211 } 212 } 213 214 TEST_F(NonMutatingTest, MismatchWithPredicate) { 215 // Testing necessary as absl::c_mismatch executes logic. 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, sequence_.begin()); 224 EXPECT_EQ(result.second, vector_.begin()); 225 } 226 227 sequence_.front() = 0; 228 { 229 auto result = absl::c_mismatch(vector_, sequence_, BinPredicate); 230 EXPECT_EQ(result.first, vector_.begin()); 231 EXPECT_EQ(result.second, sequence_.begin()); 232 } 233 { 234 auto result = absl::c_mismatch(sequence_, vector_, BinPredicate); 235 EXPECT_EQ(result.first, std::next(sequence_.begin())); 236 EXPECT_EQ(result.second, std::next(vector_.begin())); 237 } 238 239 sequence_.clear(); 240 { 241 auto result = absl::c_mismatch(vector_, sequence_, BinPredicate); 242 EXPECT_EQ(result.first, vector_.begin()); 243 EXPECT_EQ(result.second, sequence_.end()); 244 } 245 { 246 auto result = absl::c_mismatch(sequence_, vector_, BinPredicate); 247 EXPECT_EQ(result.first, sequence_.end()); 248 EXPECT_EQ(result.second, vector_.begin()); 249 } 250 } 251 252 TEST_F(NonMutatingTest, Equal) { 253 EXPECT_TRUE(absl::c_equal(vector_, sequence_)); 254 EXPECT_TRUE(absl::c_equal(sequence_, vector_)); 255 EXPECT_TRUE(absl::c_equal(sequence_, array_)); 256 EXPECT_TRUE(absl::c_equal(array_, vector_)); 257 258 // Test that behavior appropriately differs from that of equal(). 259 std::vector<int> vector_plus = {1, 2, 3}; 260 vector_plus.push_back(4); 261 EXPECT_FALSE(absl::c_equal(vector_plus, sequence_)); 262 EXPECT_FALSE(absl::c_equal(sequence_, vector_plus)); 263 EXPECT_FALSE(absl::c_equal(array_, vector_plus)); 264 } 265 266 TEST_F(NonMutatingTest, EqualWithPredicate) { 267 EXPECT_TRUE(absl::c_equal(vector_, sequence_, Equals)); 268 EXPECT_TRUE(absl::c_equal(sequence_, vector_, Equals)); 269 EXPECT_TRUE(absl::c_equal(array_, sequence_, Equals)); 270 EXPECT_TRUE(absl::c_equal(vector_, array_, Equals)); 271 272 // Test that behavior appropriately differs from that of equal(). 273 std::vector<int> vector_plus = {1, 2, 3}; 274 vector_plus.push_back(4); 275 EXPECT_FALSE(absl::c_equal(vector_plus, sequence_, Equals)); 276 EXPECT_FALSE(absl::c_equal(sequence_, vector_plus, Equals)); 277 EXPECT_FALSE(absl::c_equal(vector_plus, array_, Equals)); 278 } 279 280 TEST_F(NonMutatingTest, IsPermutation) { 281 auto vector_permut_ = vector_; 282 std::next_permutation(vector_permut_.begin(), vector_permut_.end()); 283 EXPECT_TRUE(absl::c_is_permutation(vector_permut_, sequence_)); 284 EXPECT_TRUE(absl::c_is_permutation(sequence_, vector_permut_)); 285 286 // Test that behavior appropriately differs from that of is_permutation(). 287 std::vector<int> vector_plus = {1, 2, 3}; 288 vector_plus.push_back(4); 289 EXPECT_FALSE(absl::c_is_permutation(vector_plus, sequence_)); 290 EXPECT_FALSE(absl::c_is_permutation(sequence_, vector_plus)); 291 } 292 293 TEST_F(NonMutatingTest, IsPermutationWithPredicate) { 294 auto vector_permut_ = vector_; 295 std::next_permutation(vector_permut_.begin(), vector_permut_.end()); 296 EXPECT_TRUE(absl::c_is_permutation(vector_permut_, sequence_, Equals)); 297 EXPECT_TRUE(absl::c_is_permutation(sequence_, vector_permut_, Equals)); 298 299 // Test that behavior appropriately differs from that of is_permutation(). 300 std::vector<int> vector_plus = {1, 2, 3}; 301 vector_plus.push_back(4); 302 EXPECT_FALSE(absl::c_is_permutation(vector_plus, sequence_, Equals)); 303 EXPECT_FALSE(absl::c_is_permutation(sequence_, vector_plus, Equals)); 304 } 305 306 TEST_F(NonMutatingTest, Search) { 307 absl::c_search(sequence_, vector_); 308 absl::c_search(vector_, sequence_); 309 absl::c_search(array_, sequence_); 310 } 311 312 TEST_F(NonMutatingTest, SearchWithPredicate) { 313 absl::c_search(sequence_, vector_, BinPredicate); 314 absl::c_search(vector_, sequence_, BinPredicate); 315 } 316 317 TEST_F(NonMutatingTest, ContainsSubrange) { 318 EXPECT_TRUE(absl::c_contains_subrange(sequence_, vector_)); 319 EXPECT_TRUE(absl::c_contains_subrange(vector_, sequence_)); 320 EXPECT_TRUE(absl::c_contains_subrange(array_, sequence_)); 321 } 322 323 TEST_F(NonMutatingTest, ContainsSubrangeWithPredicate) { 324 EXPECT_TRUE(absl::c_contains_subrange(sequence_, vector_, Equals)); 325 EXPECT_TRUE(absl::c_contains_subrange(vector_, sequence_, Equals)); 326 } 327 328 TEST_F(NonMutatingTest, SearchN) { absl::c_search_n(sequence_, 3, 1); } 329 330 TEST_F(NonMutatingTest, SearchNWithPredicate) { 331 absl::c_search_n(sequence_, 3, 1, BinPredicate); 332 } 333 334 TEST_F(NonMutatingTest, LowerBound) { 335 std::list<int>::iterator i = absl::c_lower_bound(sequence_, 3); 336 ASSERT_TRUE(i != sequence_.end()); 337 EXPECT_EQ(2, std::distance(sequence_.begin(), i)); 338 EXPECT_EQ(3, *i); 339 } 340 341 TEST_F(NonMutatingTest, LowerBoundWithPredicate) { 342 std::vector<int> v(vector_); 343 std::sort(v.begin(), v.end(), std::greater<int>()); 344 std::vector<int>::iterator i = absl::c_lower_bound(v, 3, std::greater<int>()); 345 EXPECT_TRUE(i == v.begin()); 346 EXPECT_EQ(3, *i); 347 } 348 349 TEST_F(NonMutatingTest, UpperBound) { 350 std::list<int>::iterator i = absl::c_upper_bound(sequence_, 1); 351 ASSERT_TRUE(i != sequence_.end()); 352 EXPECT_EQ(1, std::distance(sequence_.begin(), i)); 353 EXPECT_EQ(2, *i); 354 } 355 356 TEST_F(NonMutatingTest, UpperBoundWithPredicate) { 357 std::vector<int> v(vector_); 358 std::sort(v.begin(), v.end(), std::greater<int>()); 359 std::vector<int>::iterator i = absl::c_upper_bound(v, 1, std::greater<int>()); 360 EXPECT_EQ(3, i - v.begin()); 361 EXPECT_TRUE(i == v.end()); 362 } 363 364 TEST_F(NonMutatingTest, EqualRange) { 365 std::pair<std::list<int>::iterator, std::list<int>::iterator> p = 366 absl::c_equal_range(sequence_, 2); 367 EXPECT_EQ(1, std::distance(sequence_.begin(), p.first)); 368 EXPECT_EQ(2, std::distance(sequence_.begin(), p.second)); 369 } 370 371 TEST_F(NonMutatingTest, EqualRangeArray) { 372 auto p = absl::c_equal_range(array_, 2); 373 EXPECT_EQ(1, std::distance(std::begin(array_), p.first)); 374 EXPECT_EQ(2, std::distance(std::begin(array_), p.second)); 375 } 376 377 TEST_F(NonMutatingTest, EqualRangeWithPredicate) { 378 std::vector<int> v(vector_); 379 std::sort(v.begin(), v.end(), std::greater<int>()); 380 std::pair<std::vector<int>::iterator, std::vector<int>::iterator> p = 381 absl::c_equal_range(v, 2, std::greater<int>()); 382 EXPECT_EQ(1, std::distance(v.begin(), p.first)); 383 EXPECT_EQ(2, std::distance(v.begin(), p.second)); 384 } 385 386 TEST_F(NonMutatingTest, BinarySearch) { 387 EXPECT_TRUE(absl::c_binary_search(vector_, 2)); 388 EXPECT_TRUE(absl::c_binary_search(std::vector<int>(vector_), 2)); 389 } 390 391 TEST_F(NonMutatingTest, BinarySearchWithPredicate) { 392 std::vector<int> v(vector_); 393 std::sort(v.begin(), v.end(), std::greater<int>()); 394 EXPECT_TRUE(absl::c_binary_search(v, 2, std::greater<int>())); 395 EXPECT_TRUE( 396 absl::c_binary_search(std::vector<int>(v), 2, std::greater<int>())); 397 } 398 399 TEST_F(NonMutatingTest, MinElement) { 400 std::list<int>::iterator i = absl::c_min_element(sequence_); 401 ASSERT_TRUE(i != sequence_.end()); 402 EXPECT_EQ(*i, 1); 403 } 404 405 TEST_F(NonMutatingTest, MinElementWithPredicate) { 406 std::list<int>::iterator i = 407 absl::c_min_element(sequence_, std::greater<int>()); 408 ASSERT_TRUE(i != sequence_.end()); 409 EXPECT_EQ(*i, 3); 410 } 411 412 TEST_F(NonMutatingTest, MaxElement) { 413 std::list<int>::iterator i = absl::c_max_element(sequence_); 414 ASSERT_TRUE(i != sequence_.end()); 415 EXPECT_EQ(*i, 3); 416 } 417 418 TEST_F(NonMutatingTest, MaxElementWithPredicate) { 419 std::list<int>::iterator i = 420 absl::c_max_element(sequence_, std::greater<int>()); 421 ASSERT_TRUE(i != sequence_.end()); 422 EXPECT_EQ(*i, 1); 423 } 424 425 TEST_F(NonMutatingTest, LexicographicalCompare) { 426 EXPECT_FALSE(absl::c_lexicographical_compare(sequence_, sequence_)); 427 428 std::vector<int> v; 429 v.push_back(1); 430 v.push_back(2); 431 v.push_back(4); 432 433 EXPECT_TRUE(absl::c_lexicographical_compare(sequence_, v)); 434 EXPECT_TRUE(absl::c_lexicographical_compare(std::list<int>(sequence_), v)); 435 } 436 437 TEST_F(NonMutatingTest, LexicographicalCopmareWithPredicate) { 438 EXPECT_FALSE(absl::c_lexicographical_compare(sequence_, sequence_, 439 std::greater<int>())); 440 441 std::vector<int> v; 442 v.push_back(1); 443 v.push_back(2); 444 v.push_back(4); 445 446 EXPECT_TRUE( 447 absl::c_lexicographical_compare(v, sequence_, std::greater<int>())); 448 EXPECT_TRUE(absl::c_lexicographical_compare( 449 std::vector<int>(v), std::list<int>(sequence_), std::greater<int>())); 450 } 451 452 TEST_F(NonMutatingTest, Includes) { 453 std::set<int> s(vector_.begin(), vector_.end()); 454 s.insert(4); 455 EXPECT_TRUE(absl::c_includes(s, vector_)); 456 } 457 458 TEST_F(NonMutatingTest, IncludesWithPredicate) { 459 std::vector<int> v = {3, 2, 1}; 460 std::set<int, std::greater<int>> s(v.begin(), v.end()); 461 s.insert(4); 462 EXPECT_TRUE(absl::c_includes(s, v, std::greater<int>())); 463 } 464 465 class NumericMutatingTest : public testing::Test { 466 protected: 467 std::list<int> list_ = {1, 2, 3}; 468 std::vector<int> output_; 469 }; 470 471 TEST_F(NumericMutatingTest, Iota) { 472 absl::c_iota(list_, 5); 473 std::list<int> expected{5, 6, 7}; 474 EXPECT_EQ(list_, expected); 475 } 476 477 TEST_F(NonMutatingTest, Accumulate) { 478 EXPECT_EQ(absl::c_accumulate(sequence_, 4), 1 + 2 + 3 + 4); 479 } 480 481 TEST_F(NonMutatingTest, AccumulateWithBinaryOp) { 482 EXPECT_EQ(absl::c_accumulate(sequence_, 4, std::multiplies<int>()), 483 1 * 2 * 3 * 4); 484 } 485 486 TEST_F(NonMutatingTest, AccumulateLvalueInit) { 487 int lvalue = 4; 488 EXPECT_EQ(absl::c_accumulate(sequence_, lvalue), 1 + 2 + 3 + 4); 489 } 490 491 TEST_F(NonMutatingTest, AccumulateWithBinaryOpLvalueInit) { 492 int lvalue = 4; 493 EXPECT_EQ(absl::c_accumulate(sequence_, lvalue, std::multiplies<int>()), 494 1 * 2 * 3 * 4); 495 } 496 497 TEST_F(NonMutatingTest, InnerProduct) { 498 EXPECT_EQ(absl::c_inner_product(sequence_, vector_, 1000), 499 1000 + 1 * 1 + 2 * 2 + 3 * 3); 500 } 501 502 TEST_F(NonMutatingTest, InnerProductWithBinaryOps) { 503 EXPECT_EQ(absl::c_inner_product(sequence_, vector_, 10, 504 std::multiplies<int>(), std::plus<int>()), 505 10 * (1 + 1) * (2 + 2) * (3 + 3)); 506 } 507 508 TEST_F(NonMutatingTest, InnerProductLvalueInit) { 509 int lvalue = 1000; 510 EXPECT_EQ(absl::c_inner_product(sequence_, vector_, lvalue), 511 1000 + 1 * 1 + 2 * 2 + 3 * 3); 512 } 513 514 TEST_F(NonMutatingTest, InnerProductWithBinaryOpsLvalueInit) { 515 int lvalue = 10; 516 EXPECT_EQ(absl::c_inner_product(sequence_, vector_, lvalue, 517 std::multiplies<int>(), std::plus<int>()), 518 10 * (1 + 1) * (2 + 2) * (3 + 3)); 519 } 520 521 TEST_F(NumericMutatingTest, AdjacentDifference) { 522 auto last = absl::c_adjacent_difference(list_, std::back_inserter(output_)); 523 *last = 1000; 524 std::vector<int> expected{1, 2 - 1, 3 - 2, 1000}; 525 EXPECT_EQ(output_, expected); 526 } 527 528 TEST_F(NumericMutatingTest, AdjacentDifferenceWithBinaryOp) { 529 auto last = absl::c_adjacent_difference(list_, std::back_inserter(output_), 530 std::multiplies<int>()); 531 *last = 1000; 532 std::vector<int> expected{1, 2 * 1, 3 * 2, 1000}; 533 EXPECT_EQ(output_, expected); 534 } 535 536 TEST_F(NumericMutatingTest, PartialSum) { 537 auto last = absl::c_partial_sum(list_, std::back_inserter(output_)); 538 *last = 1000; 539 std::vector<int> expected{1, 1 + 2, 1 + 2 + 3, 1000}; 540 EXPECT_EQ(output_, expected); 541 } 542 543 TEST_F(NumericMutatingTest, PartialSumWithBinaryOp) { 544 auto last = absl::c_partial_sum(list_, std::back_inserter(output_), 545 std::multiplies<int>()); 546 *last = 1000; 547 std::vector<int> expected{1, 1 * 2, 1 * 2 * 3, 1000}; 548 EXPECT_EQ(output_, expected); 549 } 550 551 TEST_F(NonMutatingTest, LinearSearch) { 552 EXPECT_TRUE(absl::c_linear_search(container_, 3)); 553 EXPECT_FALSE(absl::c_linear_search(container_, 4)); 554 } 555 556 TEST_F(NonMutatingTest, AllOf) { 557 const std::vector<int>& v = vector_; 558 EXPECT_FALSE(absl::c_all_of(v, [](int x) { return x > 1; })); 559 EXPECT_TRUE(absl::c_all_of(v, [](int x) { return x > 0; })); 560 } 561 562 TEST_F(NonMutatingTest, AnyOf) { 563 const std::vector<int>& v = vector_; 564 EXPECT_TRUE(absl::c_any_of(v, [](int x) { return x > 2; })); 565 EXPECT_FALSE(absl::c_any_of(v, [](int x) { return x > 5; })); 566 } 567 568 TEST_F(NonMutatingTest, NoneOf) { 569 const std::vector<int>& v = vector_; 570 EXPECT_FALSE(absl::c_none_of(v, [](int x) { return x > 2; })); 571 EXPECT_TRUE(absl::c_none_of(v, [](int x) { return x > 5; })); 572 } 573 574 TEST_F(NonMutatingTest, MinMaxElementLess) { 575 std::pair<std::vector<int>::const_iterator, std::vector<int>::const_iterator> 576 p = absl::c_minmax_element(vector_, std::less<int>()); 577 EXPECT_TRUE(p.first == vector_.begin()); 578 EXPECT_TRUE(p.second == vector_.begin() + 2); 579 } 580 581 TEST_F(NonMutatingTest, MinMaxElementGreater) { 582 std::pair<std::vector<int>::const_iterator, std::vector<int>::const_iterator> 583 p = absl::c_minmax_element(vector_, std::greater<int>()); 584 EXPECT_TRUE(p.first == vector_.begin() + 2); 585 EXPECT_TRUE(p.second == vector_.begin()); 586 } 587 588 TEST_F(NonMutatingTest, MinMaxElementNoPredicate) { 589 std::pair<std::vector<int>::const_iterator, std::vector<int>::const_iterator> 590 p = absl::c_minmax_element(vector_); 591 EXPECT_TRUE(p.first == vector_.begin()); 592 EXPECT_TRUE(p.second == vector_.begin() + 2); 593 } 594 595 class SortingTest : public testing::Test { 596 protected: 597 std::list<int> sorted_ = {1, 2, 3, 4}; 598 std::list<int> unsorted_ = {2, 4, 1, 3}; 599 std::list<int> reversed_ = {4, 3, 2, 1}; 600 }; 601 602 TEST_F(SortingTest, IsSorted) { 603 EXPECT_TRUE(absl::c_is_sorted(sorted_)); 604 EXPECT_FALSE(absl::c_is_sorted(unsorted_)); 605 EXPECT_FALSE(absl::c_is_sorted(reversed_)); 606 } 607 608 TEST_F(SortingTest, IsSortedWithPredicate) { 609 EXPECT_FALSE(absl::c_is_sorted(sorted_, std::greater<int>())); 610 EXPECT_FALSE(absl::c_is_sorted(unsorted_, std::greater<int>())); 611 EXPECT_TRUE(absl::c_is_sorted(reversed_, std::greater<int>())); 612 } 613 614 TEST_F(SortingTest, IsSortedUntil) { 615 EXPECT_EQ(1, *absl::c_is_sorted_until(unsorted_)); 616 EXPECT_EQ(4, *absl::c_is_sorted_until(unsorted_, std::greater<int>())); 617 } 618 619 TEST_F(SortingTest, NthElement) { 620 std::vector<int> unsorted = {2, 4, 1, 3}; 621 absl::c_nth_element(unsorted, unsorted.begin() + 2); 622 EXPECT_THAT(unsorted, ElementsAre(Lt(3), Lt(3), 3, Gt(3))); 623 absl::c_nth_element(unsorted, unsorted.begin() + 2, std::greater<int>()); 624 EXPECT_THAT(unsorted, ElementsAre(Gt(2), Gt(2), 2, Lt(2))); 625 } 626 627 TEST(MutatingTest, IsPartitioned) { 628 EXPECT_TRUE( 629 absl::c_is_partitioned(std::vector<int>{1, 3, 5, 2, 4, 6}, IsOdd)); 630 EXPECT_FALSE( 631 absl::c_is_partitioned(std::vector<int>{1, 2, 3, 4, 5, 6}, IsOdd)); 632 EXPECT_FALSE( 633 absl::c_is_partitioned(std::vector<int>{2, 4, 6, 1, 3, 5}, IsOdd)); 634 } 635 636 TEST(MutatingTest, Partition) { 637 std::vector<int> actual = {1, 2, 3, 4, 5}; 638 absl::c_partition(actual, IsOdd); 639 EXPECT_THAT(actual, Truly([](const std::vector<int>& c) { 640 return absl::c_is_partitioned(c, IsOdd); 641 })); 642 } 643 644 TEST(MutatingTest, StablePartition) { 645 std::vector<int> actual = {1, 2, 3, 4, 5}; 646 absl::c_stable_partition(actual, IsOdd); 647 EXPECT_THAT(actual, ElementsAre(1, 3, 5, 2, 4)); 648 } 649 650 TEST(MutatingTest, PartitionCopy) { 651 const std::vector<int> initial = {1, 2, 3, 4, 5}; 652 std::vector<int> odds, evens; 653 auto ends = absl::c_partition_copy(initial, back_inserter(odds), 654 back_inserter(evens), IsOdd); 655 *ends.first = 7; 656 *ends.second = 6; 657 EXPECT_THAT(odds, ElementsAre(1, 3, 5, 7)); 658 EXPECT_THAT(evens, ElementsAre(2, 4, 6)); 659 } 660 661 TEST(MutatingTest, PartitionPoint) { 662 const std::vector<int> initial = {1, 3, 5, 2, 4}; 663 auto middle = absl::c_partition_point(initial, IsOdd); 664 EXPECT_EQ(2, *middle); 665 } 666 667 TEST(MutatingTest, CopyMiddle) { 668 const std::vector<int> initial = {4, -1, -2, -3, 5}; 669 const std::list<int> input = {1, 2, 3}; 670 const std::vector<int> expected = {4, 1, 2, 3, 5}; 671 672 std::list<int> test_list(initial.begin(), initial.end()); 673 absl::c_copy(input, ++test_list.begin()); 674 EXPECT_EQ(std::list<int>(expected.begin(), expected.end()), test_list); 675 676 std::vector<int> test_vector = initial; 677 absl::c_copy(input, test_vector.begin() + 1); 678 EXPECT_EQ(expected, test_vector); 679 } 680 681 TEST(MutatingTest, CopyFrontInserter) { 682 const std::list<int> initial = {4, 5}; 683 const std::list<int> input = {1, 2, 3}; 684 const std::list<int> expected = {3, 2, 1, 4, 5}; 685 686 std::list<int> test_list = initial; 687 absl::c_copy(input, std::front_inserter(test_list)); 688 EXPECT_EQ(expected, test_list); 689 } 690 691 TEST(MutatingTest, CopyBackInserter) { 692 const std::vector<int> initial = {4, 5}; 693 const std::list<int> input = {1, 2, 3}; 694 const std::vector<int> expected = {4, 5, 1, 2, 3}; 695 696 std::list<int> test_list(initial.begin(), initial.end()); 697 absl::c_copy(input, std::back_inserter(test_list)); 698 EXPECT_EQ(std::list<int>(expected.begin(), expected.end()), test_list); 699 700 std::vector<int> test_vector = initial; 701 absl::c_copy(input, std::back_inserter(test_vector)); 702 EXPECT_EQ(expected, test_vector); 703 } 704 705 TEST(MutatingTest, CopyN) { 706 const std::vector<int> initial = {1, 2, 3, 4, 5}; 707 const std::vector<int> expected = {1, 2}; 708 std::vector<int> actual; 709 absl::c_copy_n(initial, 2, back_inserter(actual)); 710 EXPECT_EQ(expected, actual); 711 } 712 713 TEST(MutatingTest, CopyIf) { 714 const std::list<int> input = {1, 2, 3}; 715 std::vector<int> output; 716 absl::c_copy_if(input, std::back_inserter(output), 717 [](int i) { return i != 2; }); 718 EXPECT_THAT(output, ElementsAre(1, 3)); 719 } 720 721 TEST(MutatingTest, CopyBackward) { 722 std::vector<int> actual = {1, 2, 3, 4, 5}; 723 std::vector<int> expected = {1, 2, 1, 2, 3}; 724 absl::c_copy_backward(absl::MakeSpan(actual.data(), 3), actual.end()); 725 EXPECT_EQ(expected, actual); 726 } 727 728 TEST(MutatingTest, Move) { 729 std::vector<std::unique_ptr<int>> src; 730 src.emplace_back(absl::make_unique<int>(1)); 731 src.emplace_back(absl::make_unique<int>(2)); 732 src.emplace_back(absl::make_unique<int>(3)); 733 src.emplace_back(absl::make_unique<int>(4)); 734 src.emplace_back(absl::make_unique<int>(5)); 735 736 std::vector<std::unique_ptr<int>> dest = {}; 737 absl::c_move(src, std::back_inserter(dest)); 738 EXPECT_THAT(src, Each(IsNull())); 739 EXPECT_THAT(dest, ElementsAre(Pointee(1), Pointee(2), Pointee(3), Pointee(4), 740 Pointee(5))); 741 } 742 743 TEST(MutatingTest, MoveBackward) { 744 std::vector<std::unique_ptr<int>> actual; 745 actual.emplace_back(absl::make_unique<int>(1)); 746 actual.emplace_back(absl::make_unique<int>(2)); 747 actual.emplace_back(absl::make_unique<int>(3)); 748 actual.emplace_back(absl::make_unique<int>(4)); 749 actual.emplace_back(absl::make_unique<int>(5)); 750 auto subrange = absl::MakeSpan(actual.data(), 3); 751 absl::c_move_backward(subrange, actual.end()); 752 EXPECT_THAT(actual, ElementsAre(IsNull(), IsNull(), Pointee(1), Pointee(2), 753 Pointee(3))); 754 } 755 756 TEST(MutatingTest, MoveWithRvalue) { 757 auto MakeRValueSrc = [] { 758 std::vector<std::unique_ptr<int>> src; 759 src.emplace_back(absl::make_unique<int>(1)); 760 src.emplace_back(absl::make_unique<int>(2)); 761 src.emplace_back(absl::make_unique<int>(3)); 762 return src; 763 }; 764 765 std::vector<std::unique_ptr<int>> dest = MakeRValueSrc(); 766 absl::c_move(MakeRValueSrc(), std::back_inserter(dest)); 767 EXPECT_THAT(dest, ElementsAre(Pointee(1), Pointee(2), Pointee(3), Pointee(1), 768 Pointee(2), Pointee(3))); 769 } 770 771 TEST(MutatingTest, SwapRanges) { 772 std::vector<int> odds = {2, 4, 6}; 773 std::vector<int> evens = {1, 3, 5}; 774 absl::c_swap_ranges(odds, evens); 775 EXPECT_THAT(odds, ElementsAre(1, 3, 5)); 776 EXPECT_THAT(evens, ElementsAre(2, 4, 6)); 777 778 odds.pop_back(); 779 absl::c_swap_ranges(odds, evens); 780 EXPECT_THAT(odds, ElementsAre(2, 4)); 781 EXPECT_THAT(evens, ElementsAre(1, 3, 6)); 782 783 absl::c_swap_ranges(evens, odds); 784 EXPECT_THAT(odds, ElementsAre(1, 3)); 785 EXPECT_THAT(evens, ElementsAre(2, 4, 6)); 786 } 787 788 TEST_F(NonMutatingTest, Transform) { 789 std::vector<int> x{0, 2, 4}, y, z; 790 auto end = absl::c_transform(x, back_inserter(y), std::negate<int>()); 791 EXPECT_EQ(std::vector<int>({0, -2, -4}), y); 792 *end = 7; 793 EXPECT_EQ(std::vector<int>({0, -2, -4, 7}), y); 794 795 y = {1, 3, 0}; 796 end = absl::c_transform(x, y, back_inserter(z), std::plus<int>()); 797 EXPECT_EQ(std::vector<int>({1, 5, 4}), z); 798 *end = 7; 799 EXPECT_EQ(std::vector<int>({1, 5, 4, 7}), z); 800 801 z.clear(); 802 y.pop_back(); 803 end = absl::c_transform(x, y, std::back_inserter(z), std::plus<int>()); 804 EXPECT_EQ(std::vector<int>({1, 5}), z); 805 *end = 7; 806 EXPECT_EQ(std::vector<int>({1, 5, 7}), z); 807 808 z.clear(); 809 std::swap(x, y); 810 end = absl::c_transform(x, y, std::back_inserter(z), std::plus<int>()); 811 EXPECT_EQ(std::vector<int>({1, 5}), z); 812 *end = 7; 813 EXPECT_EQ(std::vector<int>({1, 5, 7}), z); 814 } 815 816 TEST(MutatingTest, Replace) { 817 const std::vector<int> initial = {1, 2, 3, 1, 4, 5}; 818 const std::vector<int> expected = {4, 2, 3, 4, 4, 5}; 819 820 std::vector<int> test_vector = initial; 821 absl::c_replace(test_vector, 1, 4); 822 EXPECT_EQ(expected, test_vector); 823 824 std::list<int> test_list(initial.begin(), initial.end()); 825 absl::c_replace(test_list, 1, 4); 826 EXPECT_EQ(std::list<int>(expected.begin(), expected.end()), test_list); 827 } 828 829 TEST(MutatingTest, ReplaceIf) { 830 std::vector<int> actual = {1, 2, 3, 4, 5}; 831 const std::vector<int> expected = {0, 2, 0, 4, 0}; 832 833 absl::c_replace_if(actual, IsOdd, 0); 834 EXPECT_EQ(expected, actual); 835 } 836 837 TEST(MutatingTest, ReplaceCopy) { 838 const std::vector<int> initial = {1, 2, 3, 1, 4, 5}; 839 const std::vector<int> expected = {4, 2, 3, 4, 4, 5}; 840 841 std::vector<int> actual; 842 absl::c_replace_copy(initial, back_inserter(actual), 1, 4); 843 EXPECT_EQ(expected, actual); 844 } 845 846 TEST(MutatingTest, Sort) { 847 std::vector<int> test_vector = {2, 3, 1, 4}; 848 absl::c_sort(test_vector); 849 EXPECT_THAT(test_vector, ElementsAre(1, 2, 3, 4)); 850 } 851 852 TEST(MutatingTest, SortWithPredicate) { 853 std::vector<int> test_vector = {2, 3, 1, 4}; 854 absl::c_sort(test_vector, std::greater<int>()); 855 EXPECT_THAT(test_vector, ElementsAre(4, 3, 2, 1)); 856 } 857 858 // For absl::c_stable_sort tests. Needs an operator< that does not cover all 859 // fields so that the test can check the sort preserves order of equal elements. 860 struct Element { 861 int key; 862 int value; 863 friend bool operator<(const Element& e1, const Element& e2) { 864 return e1.key < e2.key; 865 } 866 // Make gmock print useful diagnostics. 867 friend std::ostream& operator<<(std::ostream& o, const Element& e) { 868 return o << "{" << e.key << ", " << e.value << "}"; 869 } 870 }; 871 872 MATCHER_P2(IsElement, key, value, "") { 873 return arg.key == key && arg.value == value; 874 } 875 876 TEST(MutatingTest, StableSort) { 877 std::vector<Element> test_vector = {{1, 1}, {2, 1}, {2, 0}, {1, 0}, {2, 2}}; 878 absl::c_stable_sort(test_vector); 879 EXPECT_THAT(test_vector, 880 ElementsAre(IsElement(1, 1), IsElement(1, 0), IsElement(2, 1), 881 IsElement(2, 0), IsElement(2, 2))); 882 } 883 884 TEST(MutatingTest, StableSortWithPredicate) { 885 std::vector<Element> test_vector = {{1, 1}, {2, 1}, {2, 0}, {1, 0}, {2, 2}}; 886 absl::c_stable_sort(test_vector, [](const Element& e1, const Element& e2) { 887 return e2 < e1; 888 }); 889 EXPECT_THAT(test_vector, 890 ElementsAre(IsElement(2, 1), IsElement(2, 0), IsElement(2, 2), 891 IsElement(1, 1), IsElement(1, 0))); 892 } 893 894 TEST(MutatingTest, ReplaceCopyIf) { 895 const std::vector<int> initial = {1, 2, 3, 4, 5}; 896 const std::vector<int> expected = {0, 2, 0, 4, 0}; 897 898 std::vector<int> actual; 899 absl::c_replace_copy_if(initial, back_inserter(actual), IsOdd, 0); 900 EXPECT_EQ(expected, actual); 901 } 902 903 TEST(MutatingTest, Fill) { 904 std::vector<int> actual(5); 905 absl::c_fill(actual, 1); 906 EXPECT_THAT(actual, ElementsAre(1, 1, 1, 1, 1)); 907 } 908 909 TEST(MutatingTest, FillN) { 910 std::vector<int> actual(5, 0); 911 absl::c_fill_n(actual, 2, 1); 912 EXPECT_THAT(actual, ElementsAre(1, 1, 0, 0, 0)); 913 } 914 915 TEST(MutatingTest, Generate) { 916 std::vector<int> actual(5); 917 int x = 0; 918 absl::c_generate(actual, [&x]() { return ++x; }); 919 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 4, 5)); 920 } 921 922 TEST(MutatingTest, GenerateN) { 923 std::vector<int> actual(5, 0); 924 int x = 0; 925 absl::c_generate_n(actual, 3, [&x]() { return ++x; }); 926 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 0, 0)); 927 } 928 929 TEST(MutatingTest, RemoveCopy) { 930 std::vector<int> actual; 931 absl::c_remove_copy(std::vector<int>{1, 2, 3}, back_inserter(actual), 2); 932 EXPECT_THAT(actual, ElementsAre(1, 3)); 933 } 934 935 TEST(MutatingTest, RemoveCopyIf) { 936 std::vector<int> actual; 937 absl::c_remove_copy_if(std::vector<int>{1, 2, 3}, back_inserter(actual), 938 IsOdd); 939 EXPECT_THAT(actual, ElementsAre(2)); 940 } 941 942 TEST(MutatingTest, UniqueCopy) { 943 std::vector<int> actual; 944 absl::c_unique_copy(std::vector<int>{1, 2, 2, 2, 3, 3, 2}, 945 back_inserter(actual)); 946 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 2)); 947 } 948 949 TEST(MutatingTest, UniqueCopyWithPredicate) { 950 std::vector<int> actual; 951 absl::c_unique_copy(std::vector<int>{1, 2, 3, -1, -2, -3, 1}, 952 back_inserter(actual), 953 [](int x, int y) { return (x < 0) == (y < 0); }); 954 EXPECT_THAT(actual, ElementsAre(1, -1, 1)); 955 } 956 957 TEST(MutatingTest, Reverse) { 958 std::vector<int> test_vector = {1, 2, 3, 4}; 959 absl::c_reverse(test_vector); 960 EXPECT_THAT(test_vector, ElementsAre(4, 3, 2, 1)); 961 962 std::list<int> test_list = {1, 2, 3, 4}; 963 absl::c_reverse(test_list); 964 EXPECT_THAT(test_list, ElementsAre(4, 3, 2, 1)); 965 } 966 967 TEST(MutatingTest, ReverseCopy) { 968 std::vector<int> actual; 969 absl::c_reverse_copy(std::vector<int>{1, 2, 3, 4}, back_inserter(actual)); 970 EXPECT_THAT(actual, ElementsAre(4, 3, 2, 1)); 971 } 972 973 TEST(MutatingTest, Rotate) { 974 std::vector<int> actual = {1, 2, 3, 4}; 975 auto it = absl::c_rotate(actual, actual.begin() + 2); 976 EXPECT_THAT(actual, testing::ElementsAreArray({3, 4, 1, 2})); 977 EXPECT_EQ(*it, 1); 978 } 979 980 TEST(MutatingTest, RotateCopy) { 981 std::vector<int> initial = {1, 2, 3, 4}; 982 std::vector<int> actual; 983 auto end = 984 absl::c_rotate_copy(initial, initial.begin() + 2, back_inserter(actual)); 985 *end = 5; 986 EXPECT_THAT(actual, ElementsAre(3, 4, 1, 2, 5)); 987 } 988 989 template <typename T> 990 T RandomlySeededPrng() { 991 std::random_device rdev; 992 std::seed_seq::result_type data[T::state_size]; 993 std::generate_n(data, T::state_size, std::ref(rdev)); 994 std::seed_seq prng_seed(data, data + T::state_size); 995 return T(prng_seed); 996 } 997 998 TEST(MutatingTest, Shuffle) { 999 std::vector<int> actual = {1, 2, 3, 4, 5}; 1000 absl::c_shuffle(actual, RandomlySeededPrng<std::mt19937_64>()); 1001 EXPECT_THAT(actual, UnorderedElementsAre(1, 2, 3, 4, 5)); 1002 } 1003 1004 TEST(MutatingTest, Sample) { 1005 std::vector<int> actual; 1006 absl::c_sample(std::vector<int>{1, 2, 3, 4, 5}, std::back_inserter(actual), 3, 1007 RandomlySeededPrng<std::mt19937_64>()); 1008 EXPECT_THAT(actual, IsSubsetOf({1, 2, 3, 4, 5})); 1009 EXPECT_THAT(actual, SizeIs(3)); 1010 } 1011 1012 TEST(MutatingTest, PartialSort) { 1013 std::vector<int> sequence{5, 3, 42, 0}; 1014 absl::c_partial_sort(sequence, sequence.begin() + 2); 1015 EXPECT_THAT(absl::MakeSpan(sequence.data(), 2), ElementsAre(0, 3)); 1016 absl::c_partial_sort(sequence, sequence.begin() + 2, std::greater<int>()); 1017 EXPECT_THAT(absl::MakeSpan(sequence.data(), 2), ElementsAre(42, 5)); 1018 } 1019 1020 TEST(MutatingTest, PartialSortCopy) { 1021 const std::vector<int> initial = {5, 3, 42, 0}; 1022 std::vector<int> actual(2); 1023 absl::c_partial_sort_copy(initial, actual); 1024 EXPECT_THAT(actual, ElementsAre(0, 3)); 1025 absl::c_partial_sort_copy(initial, actual, std::greater<int>()); 1026 EXPECT_THAT(actual, ElementsAre(42, 5)); 1027 } 1028 1029 TEST(MutatingTest, Merge) { 1030 std::vector<int> actual; 1031 absl::c_merge(std::vector<int>{1, 3, 5}, std::vector<int>{2, 4}, 1032 back_inserter(actual)); 1033 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 4, 5)); 1034 } 1035 1036 TEST(MutatingTest, MergeWithComparator) { 1037 std::vector<int> actual; 1038 absl::c_merge(std::vector<int>{5, 3, 1}, std::vector<int>{4, 2}, 1039 back_inserter(actual), std::greater<int>()); 1040 EXPECT_THAT(actual, ElementsAre(5, 4, 3, 2, 1)); 1041 } 1042 1043 TEST(MutatingTest, InplaceMerge) { 1044 std::vector<int> actual = {1, 3, 5, 2, 4}; 1045 absl::c_inplace_merge(actual, actual.begin() + 3); 1046 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 4, 5)); 1047 } 1048 1049 TEST(MutatingTest, InplaceMergeWithComparator) { 1050 std::vector<int> actual = {5, 3, 1, 4, 2}; 1051 absl::c_inplace_merge(actual, actual.begin() + 3, std::greater<int>()); 1052 EXPECT_THAT(actual, ElementsAre(5, 4, 3, 2, 1)); 1053 } 1054 1055 class SetOperationsTest : public testing::Test { 1056 protected: 1057 std::vector<int> a_ = {1, 2, 3}; 1058 std::vector<int> b_ = {1, 3, 5}; 1059 1060 std::vector<int> a_reversed_ = {3, 2, 1}; 1061 std::vector<int> b_reversed_ = {5, 3, 1}; 1062 }; 1063 1064 TEST_F(SetOperationsTest, SetUnion) { 1065 std::vector<int> actual; 1066 absl::c_set_union(a_, b_, back_inserter(actual)); 1067 EXPECT_THAT(actual, ElementsAre(1, 2, 3, 5)); 1068 } 1069 1070 TEST_F(SetOperationsTest, SetUnionWithComparator) { 1071 std::vector<int> actual; 1072 absl::c_set_union(a_reversed_, b_reversed_, back_inserter(actual), 1073 std::greater<int>()); 1074 EXPECT_THAT(actual, ElementsAre(5, 3, 2, 1)); 1075 } 1076 1077 TEST_F(SetOperationsTest, SetIntersection) { 1078 std::vector<int> actual; 1079 absl::c_set_intersection(a_, b_, back_inserter(actual)); 1080 EXPECT_THAT(actual, ElementsAre(1, 3)); 1081 } 1082 1083 TEST_F(SetOperationsTest, SetIntersectionWithComparator) { 1084 std::vector<int> actual; 1085 absl::c_set_intersection(a_reversed_, b_reversed_, back_inserter(actual), 1086 std::greater<int>()); 1087 EXPECT_THAT(actual, ElementsAre(3, 1)); 1088 } 1089 1090 TEST_F(SetOperationsTest, SetDifference) { 1091 std::vector<int> actual; 1092 absl::c_set_difference(a_, b_, back_inserter(actual)); 1093 EXPECT_THAT(actual, ElementsAre(2)); 1094 } 1095 1096 TEST_F(SetOperationsTest, SetDifferenceWithComparator) { 1097 std::vector<int> actual; 1098 absl::c_set_difference(a_reversed_, b_reversed_, back_inserter(actual), 1099 std::greater<int>()); 1100 EXPECT_THAT(actual, ElementsAre(2)); 1101 } 1102 1103 TEST_F(SetOperationsTest, SetSymmetricDifference) { 1104 std::vector<int> actual; 1105 absl::c_set_symmetric_difference(a_, b_, back_inserter(actual)); 1106 EXPECT_THAT(actual, ElementsAre(2, 5)); 1107 } 1108 1109 TEST_F(SetOperationsTest, SetSymmetricDifferenceWithComparator) { 1110 std::vector<int> actual; 1111 absl::c_set_symmetric_difference(a_reversed_, b_reversed_, 1112 back_inserter(actual), std::greater<int>()); 1113 EXPECT_THAT(actual, ElementsAre(5, 2)); 1114 } 1115 1116 TEST(HeapOperationsTest, WithoutComparator) { 1117 std::vector<int> heap = {1, 2, 3}; 1118 EXPECT_FALSE(absl::c_is_heap(heap)); 1119 absl::c_make_heap(heap); 1120 EXPECT_TRUE(absl::c_is_heap(heap)); 1121 heap.push_back(4); 1122 EXPECT_EQ(3, absl::c_is_heap_until(heap) - heap.begin()); 1123 absl::c_push_heap(heap); 1124 EXPECT_EQ(4, heap[0]); 1125 absl::c_pop_heap(heap); 1126 EXPECT_EQ(4, heap[3]); 1127 absl::c_make_heap(heap); 1128 absl::c_sort_heap(heap); 1129 EXPECT_THAT(heap, ElementsAre(1, 2, 3, 4)); 1130 EXPECT_FALSE(absl::c_is_heap(heap)); 1131 } 1132 1133 TEST(HeapOperationsTest, WithComparator) { 1134 using greater = std::greater<int>; 1135 std::vector<int> heap = {3, 2, 1}; 1136 EXPECT_FALSE(absl::c_is_heap(heap, greater())); 1137 absl::c_make_heap(heap, greater()); 1138 EXPECT_TRUE(absl::c_is_heap(heap, greater())); 1139 heap.push_back(0); 1140 EXPECT_EQ(3, absl::c_is_heap_until(heap, greater()) - heap.begin()); 1141 absl::c_push_heap(heap, greater()); 1142 EXPECT_EQ(0, heap[0]); 1143 absl::c_pop_heap(heap, greater()); 1144 EXPECT_EQ(0, heap[3]); 1145 absl::c_make_heap(heap, greater()); 1146 absl::c_sort_heap(heap, greater()); 1147 EXPECT_THAT(heap, ElementsAre(3, 2, 1, 0)); 1148 EXPECT_FALSE(absl::c_is_heap(heap, greater())); 1149 } 1150 1151 TEST(MutatingTest, PermutationOperations) { 1152 std::vector<int> initial = {1, 2, 3, 4}; 1153 std::vector<int> permuted = initial; 1154 1155 absl::c_next_permutation(permuted); 1156 EXPECT_TRUE(absl::c_is_permutation(initial, permuted)); 1157 EXPECT_TRUE(absl::c_is_permutation(initial, permuted, std::equal_to<int>())); 1158 1159 std::vector<int> permuted2 = initial; 1160 absl::c_prev_permutation(permuted2, std::greater<int>()); 1161 EXPECT_EQ(permuted, permuted2); 1162 1163 absl::c_prev_permutation(permuted); 1164 EXPECT_EQ(initial, permuted); 1165 } 1166 1167 #if defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \ 1168 ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L 1169 1170 TEST(ConstexprTest, Distance) { 1171 // Works at compile time with constexpr containers. 1172 static_assert(absl::c_distance(std::array<int, 3>()) == 3); 1173 } 1174 1175 TEST(ConstexprTest, MinElement) { 1176 constexpr std::array<int, 3> kArray = {1, 2, 3}; 1177 static_assert(*absl::c_min_element(kArray) == 1); 1178 } 1179 1180 TEST(ConstexprTest, MinElementWithPredicate) { 1181 constexpr std::array<int, 3> kArray = {1, 2, 3}; 1182 static_assert(*absl::c_min_element(kArray, std::greater<int>()) == 3); 1183 } 1184 1185 TEST(ConstexprTest, MaxElement) { 1186 constexpr std::array<int, 3> kArray = {1, 2, 3}; 1187 static_assert(*absl::c_max_element(kArray) == 3); 1188 } 1189 1190 TEST(ConstexprTest, MaxElementWithPredicate) { 1191 constexpr std::array<int, 3> kArray = {1, 2, 3}; 1192 static_assert(*absl::c_max_element(kArray, std::greater<int>()) == 1); 1193 } 1194 1195 TEST(ConstexprTest, MinMaxElement) { 1196 static constexpr std::array<int, 3> kArray = {1, 2, 3}; 1197 constexpr auto kMinMaxPair = absl::c_minmax_element(kArray); 1198 static_assert(*kMinMaxPair.first == 1); 1199 static_assert(*kMinMaxPair.second == 3); 1200 } 1201 1202 TEST(ConstexprTest, MinMaxElementWithPredicate) { 1203 static constexpr std::array<int, 3> kArray = {1, 2, 3}; 1204 constexpr auto kMinMaxPair = 1205 absl::c_minmax_element(kArray, std::greater<int>()); 1206 static_assert(*kMinMaxPair.first == 3); 1207 static_assert(*kMinMaxPair.second == 1); 1208 } 1209 #endif // defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && 1210 // ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L 1211 1212 #if defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \ 1213 ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L 1214 1215 TEST(ConstexprTest, LinearSearch) { 1216 static constexpr std::array<int, 3> kArray = {1, 2, 3}; 1217 static_assert(absl::c_linear_search(kArray, 3)); 1218 static_assert(!absl::c_linear_search(kArray, 4)); 1219 } 1220 1221 TEST(ConstexprTest, AllOf) { 1222 static constexpr std::array<int, 3> kArray = {1, 2, 3}; 1223 static_assert(!absl::c_all_of(kArray, [](int x) { return x > 1; })); 1224 static_assert(absl::c_all_of(kArray, [](int x) { return x > 0; })); 1225 } 1226 1227 TEST(ConstexprTest, AnyOf) { 1228 static constexpr std::array<int, 3> kArray = {1, 2, 3}; 1229 static_assert(absl::c_any_of(kArray, [](int x) { return x > 2; })); 1230 static_assert(!absl::c_any_of(kArray, [](int x) { return x > 5; })); 1231 } 1232 1233 TEST(ConstexprTest, NoneOf) { 1234 static constexpr std::array<int, 3> kArray = {1, 2, 3}; 1235 static_assert(!absl::c_none_of(kArray, [](int x) { return x > 2; })); 1236 static_assert(absl::c_none_of(kArray, [](int x) { return x > 5; })); 1237 } 1238 1239 TEST(ConstexprTest, ForEach) { 1240 static constexpr std::array<int, 3> kArray = [] { 1241 std::array<int, 3> array = {1, 2, 3}; 1242 absl::c_for_each(array, [](int& x) { x += 1; }); 1243 return array; 1244 }(); 1245 static_assert(kArray == std::array{2, 3, 4}); 1246 } 1247 1248 TEST(ConstexprTest, Find) { 1249 static constexpr std::array<int, 3> kArray = {1, 2, 3}; 1250 static_assert(absl::c_find(kArray, 1) == kArray.begin()); 1251 static_assert(absl::c_find(kArray, 4) == kArray.end()); 1252 } 1253 1254 TEST(ConstexprTest, Contains) { 1255 static constexpr std::array<int, 3> kArray = {1, 2, 3}; 1256 static_assert(absl::c_contains(kArray, 1)); 1257 static_assert(!absl::c_contains(kArray, 4)); 1258 } 1259 1260 TEST(ConstexprTest, FindIf) { 1261 static constexpr std::array<int, 3> kArray = {1, 2, 3}; 1262 static_assert(absl::c_find_if(kArray, [](int x) { return x > 2; }) == 1263 kArray.begin() + 2); 1264 static_assert(absl::c_find_if(kArray, [](int x) { return x > 5; }) == 1265 kArray.end()); 1266 } 1267 1268 TEST(ConstexprTest, FindIfNot) { 1269 static constexpr std::array<int, 3> kArray = {1, 2, 3}; 1270 static_assert(absl::c_find_if_not(kArray, [](int x) { return x > 1; }) == 1271 kArray.begin()); 1272 static_assert(absl::c_find_if_not(kArray, [](int x) { return x > 0; }) == 1273 kArray.end()); 1274 } 1275 1276 TEST(ConstexprTest, FindEnd) { 1277 static constexpr std::array<int, 5> kHaystack = {1, 2, 3, 2, 3}; 1278 static constexpr std::array<int, 2> kNeedle = {2, 3}; 1279 static_assert(absl::c_find_end(kHaystack, kNeedle) == kHaystack.begin() + 3); 1280 } 1281 1282 TEST(ConstexprTest, FindFirstOf) { 1283 static constexpr std::array<int, 3> kArray = {1, 2, 3}; 1284 static_assert(absl::c_find_first_of(kArray, kArray) == kArray.begin()); 1285 } 1286 1287 TEST(ConstexprTest, AdjacentFind) { 1288 static constexpr std::array<int, 4> kArray = {1, 2, 2, 3}; 1289 static_assert(absl::c_adjacent_find(kArray) == kArray.begin() + 1); 1290 } 1291 1292 TEST(ConstexprTest, AdjacentFindWithPredicate) { 1293 static constexpr std::array<int, 3> kArray = {1, 2, 3}; 1294 static_assert(absl::c_adjacent_find(kArray, std::less<int>()) == 1295 kArray.begin()); 1296 } 1297 1298 TEST(ConstexprTest, Count) { 1299 static constexpr std::array<int, 3> kArray = {1, 2, 3}; 1300 static_assert(absl::c_count(kArray, 1) == 1); 1301 static_assert(absl::c_count(kArray, 2) == 1); 1302 static_assert(absl::c_count(kArray, 3) == 1); 1303 static_assert(absl::c_count(kArray, 4) == 0); 1304 } 1305 1306 TEST(ConstexprTest, CountIf) { 1307 static constexpr std::array<int, 3> kArray = {1, 2, 3}; 1308 static_assert(absl::c_count_if(kArray, [](int x) { return x > 0; }) == 3); 1309 static_assert(absl::c_count_if(kArray, [](int x) { return x > 1; }) == 2); 1310 } 1311 1312 TEST(ConstexprTest, Mismatch) { 1313 static constexpr std::array<int, 3> kArray1 = {1, 2, 3}; 1314 static constexpr std::array<int, 3> kArray2 = {1, 2, 3}; 1315 static constexpr std::array<int, 3> kArray3 = {2, 3, 4}; 1316 static_assert(absl::c_mismatch(kArray1, kArray2) == 1317 std::pair{kArray1.end(), kArray2.end()}); 1318 static_assert(absl::c_mismatch(kArray1, kArray3) == 1319 std::pair{kArray1.begin(), kArray3.begin()}); 1320 } 1321 1322 TEST(ConstexprTest, MismatchWithPredicate) { 1323 static constexpr std::array<int, 3> kArray1 = {1, 2, 3}; 1324 static constexpr std::array<int, 3> kArray2 = {1, 2, 3}; 1325 static constexpr std::array<int, 3> kArray3 = {2, 3, 4}; 1326 static_assert(absl::c_mismatch(kArray1, kArray2, std::not_equal_to<int>()) == 1327 std::pair{kArray1.begin(), kArray2.begin()}); 1328 static_assert(absl::c_mismatch(kArray1, kArray3, std::not_equal_to<int>()) == 1329 std::pair{kArray1.end(), kArray3.end()}); 1330 } 1331 1332 TEST(ConstexprTest, Equal) { 1333 static constexpr std::array<int, 3> kArray1 = {1, 2, 3}; 1334 static constexpr std::array<int, 3> kArray2 = {1, 2, 3}; 1335 static constexpr std::array<int, 3> kArray3 = {2, 3, 4}; 1336 static_assert(absl::c_equal(kArray1, kArray2)); 1337 static_assert(!absl::c_equal(kArray1, kArray3)); 1338 } 1339 1340 TEST(ConstexprTest, EqualWithPredicate) { 1341 static constexpr std::array<int, 3> kArray1 = {1, 2, 3}; 1342 static constexpr std::array<int, 3> kArray2 = {1, 2, 3}; 1343 static constexpr std::array<int, 3> kArray3 = {2, 3, 4}; 1344 static_assert(!absl::c_equal(kArray1, kArray2, std::not_equal_to<int>())); 1345 static_assert(absl::c_equal(kArray1, kArray3, std::not_equal_to<int>())); 1346 } 1347 1348 TEST(ConstexprTest, IsPermutation) { 1349 static constexpr std::array<int, 3> kArray1 = {1, 2, 3}; 1350 static constexpr std::array<int, 3> kArray2 = {3, 2, 1}; 1351 static constexpr std::array<int, 3> kArray3 = {2, 3, 4}; 1352 static_assert(absl::c_is_permutation(kArray1, kArray2)); 1353 static_assert(!absl::c_is_permutation(kArray1, kArray3)); 1354 } 1355 1356 TEST(ConstexprTest, IsPermutationWithPredicate) { 1357 static constexpr std::array<int, 3> kArray1 = {1, 2, 3}; 1358 static constexpr std::array<int, 3> kArray2 = {3, 2, 1}; 1359 static constexpr std::array<int, 3> kArray3 = {2, 3, 4}; 1360 static_assert(absl::c_is_permutation(kArray1, kArray2, std::equal_to<int>())); 1361 static_assert( 1362 !absl::c_is_permutation(kArray1, kArray3, std::equal_to<int>())); 1363 } 1364 1365 TEST(ConstexprTest, Search) { 1366 static constexpr std::array<int, 3> kArray1 = {1, 2, 3}; 1367 static constexpr std::array<int, 3> kArray2 = {1, 2, 3}; 1368 static constexpr std::array<int, 3> kArray3 = {2, 3, 4}; 1369 static_assert(absl::c_search(kArray1, kArray2) == kArray1.begin()); 1370 static_assert(absl::c_search(kArray1, kArray3) == kArray1.end()); 1371 } 1372 1373 TEST(ConstexprTest, SearchWithPredicate) { 1374 static constexpr std::array<int, 3> kArray1 = {1, 2, 3}; 1375 static constexpr std::array<int, 3> kArray2 = {1, 2, 3}; 1376 static constexpr std::array<int, 3> kArray3 = {2, 3, 4}; 1377 static_assert(absl::c_search(kArray1, kArray2, std::not_equal_to<int>()) == 1378 kArray1.end()); 1379 static_assert(absl::c_search(kArray1, kArray3, std::not_equal_to<int>()) == 1380 kArray1.begin()); 1381 } 1382 1383 TEST(ConstexprTest, ContainsSubrange) { 1384 static constexpr std::array<int, 3> kArray1 = {1, 2, 3}; 1385 static constexpr std::array<int, 3> kArray2 = {1, 2, 3}; 1386 static constexpr std::array<int, 3> kArray3 = {2, 3, 4}; 1387 static_assert(absl::c_contains_subrange(kArray1, kArray2)); 1388 static_assert(!absl::c_contains_subrange(kArray1, kArray3)); 1389 } 1390 1391 TEST(ConstexprTest, ContainsSubrangeWithPredicate) { 1392 static constexpr std::array<int, 3> kArray1 = {1, 2, 3}; 1393 static constexpr std::array<int, 3> kArray2 = {1, 2, 3}; 1394 static constexpr std::array<int, 3> kArray3 = {2, 3, 4}; 1395 static_assert( 1396 !absl::c_contains_subrange(kArray1, kArray2, std::not_equal_to<>())); 1397 static_assert( 1398 absl::c_contains_subrange(kArray1, kArray3, std::not_equal_to<>())); 1399 } 1400 1401 TEST(ConstexprTest, SearchN) { 1402 static constexpr std::array<int, 4> kArray = {1, 2, 2, 3}; 1403 static_assert(absl::c_search_n(kArray, 1, 1) == kArray.begin()); 1404 static_assert(absl::c_search_n(kArray, 2, 2) == kArray.begin() + 1); 1405 static_assert(absl::c_search_n(kArray, 1, 4) == kArray.end()); 1406 } 1407 1408 TEST(ConstexprTest, SearchNWithPredicate) { 1409 static constexpr std::array<int, 4> kArray = {1, 2, 2, 3}; 1410 static_assert(absl::c_search_n(kArray, 1, 1, std::not_equal_to<int>()) == 1411 kArray.begin() + 1); 1412 static_assert(absl::c_search_n(kArray, 2, 2, std::not_equal_to<int>()) == 1413 kArray.end()); 1414 static_assert(absl::c_search_n(kArray, 1, 4, std::not_equal_to<int>()) == 1415 kArray.begin()); 1416 } 1417 1418 #endif // defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && 1419 // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L 1420 1421 } // namespace