1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/containers/circular_deque.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/test/copy_only_int.h"
#include "base/test/gtest_util.h"
#include "base/test/move_only_int.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::internal::VectorBuffer;
namespace base {
namespace {
circular_deque<int> MakeSequence(size_t max) {
circular_deque<int> ret;
for (size_t i = 0; i < max; i++)
ret.push_back(i);
return ret;
}
// Cycles through the queue, popping items from the back and pushing items
// at the front to validate behavior across different configurations of the
// queue in relation to the underlying buffer. The tester closure is run for
// each cycle.
template <class QueueT, class Tester>
void CycleTest(circular_deque<QueueT>& queue, const Tester& tester) {
size_t steps = queue.size() * 2;
for (size_t i = 0; i < steps; i++) {
tester(queue, i);
queue.pop_back();
queue.push_front(QueueT());
}
}
class DestructorCounter {
public:
DestructorCounter(int* counter) : counter_(counter) {}
~DestructorCounter() { ++(*counter_); }
private:
raw_ptr<int> counter_;
};
// This class implements the interface that scoped_refptr expects, but actually
// just counts the number of reference count changes that are attempted.
class RefCountChangeCounter {
public:
void AddRef() { ++ref_count_changes_; }
void Release() { ++ref_count_changes_; }
int ref_count_changes() const { return ref_count_changes_; }
private:
int ref_count_changes_ = 0;
};
} // namespace
TEST(CircularDeque, FillConstructor) {
constexpr size_t num_elts = 9;
std::vector<int> foo(15);
EXPECT_EQ(15u, foo.size());
// Fill with default constructor.
{
circular_deque<int> buf(num_elts);
EXPECT_EQ(num_elts, buf.size());
EXPECT_EQ(num_elts, static_cast<size_t>(buf.end() - buf.begin()));
for (size_t i = 0; i < num_elts; i++)
EXPECT_EQ(0, buf[i]);
}
// Fill with explicit value.
{
int value = 199;
circular_deque<int> buf(num_elts, value);
EXPECT_EQ(num_elts, buf.size());
EXPECT_EQ(num_elts, static_cast<size_t>(buf.end() - buf.begin()));
for (size_t i = 0; i < num_elts; i++)
EXPECT_EQ(value, buf[i]);
}
}
TEST(CircularDeque, CopyAndRangeConstructor) {
int values[] = {1, 2, 3, 4, 5, 6};
circular_deque<CopyOnlyInt> first(std::begin(values), std::end(values));
circular_deque<CopyOnlyInt> second(first);
EXPECT_EQ(6u, second.size());
for (int i = 0; i < 6; i++)
EXPECT_EQ(i + 1, second[i].data());
}
TEST(CircularDeque, MoveConstructor) {
int values[] = {1, 2, 3, 4, 5, 6};
circular_deque<MoveOnlyInt> first(std::begin(values), std::end(values));
circular_deque<MoveOnlyInt> second(std::move(first));
EXPECT_TRUE(first.empty());
EXPECT_EQ(6u, second.size());
for (int i = 0; i < 6; i++)
EXPECT_EQ(i + 1, second[i].data());
}
TEST(CircularDeque, InitializerListConstructor) {
circular_deque<int> empty({});
ASSERT_TRUE(empty.empty());
circular_deque<int> first({1, 2, 3, 4, 5, 6});
EXPECT_EQ(6u, first.size());
for (int i = 0; i < 6; i++)
EXPECT_EQ(i + 1, first[i]);
}
TEST(CircularDeque, Destructor) {
int destruct_count = 0;
// Contiguous buffer.
{
circular_deque<DestructorCounter> q;
q.resize(5, DestructorCounter(&destruct_count));
EXPECT_EQ(1, destruct_count); // The temporary in the call to resize().
destruct_count = 0;
}
EXPECT_EQ(5, destruct_count); // One call for each.
// Force a wraparound buffer.
{
circular_deque<DestructorCounter> q;
q.reserve(7);
q.resize(5, DestructorCounter(&destruct_count));
// Cycle throught some elements in our buffer to force a wraparound.
destruct_count = 0;
for (int i = 0; i < 4; i++) {
q.emplace_back(&destruct_count);
q.pop_front();
}
EXPECT_EQ(4, destruct_count); // One for each cycle.
destruct_count = 0;
}
EXPECT_EQ(5, destruct_count); // One call for each.
}
TEST(CircularDeque, EqualsCopy) {
circular_deque<int> first = {1, 2, 3, 4, 5, 6};
circular_deque<int> copy;
EXPECT_TRUE(copy.empty());
copy = first;
EXPECT_EQ(6u, copy.size());
for (int i = 0; i < 6; i++) {
EXPECT_EQ(i + 1, first[i]);
EXPECT_EQ(i + 1, copy[i]);
EXPECT_NE(&first[i], ©[i]);
}
}
TEST(CircularDeque, EqualsMove) {
circular_deque<int> first = {1, 2, 3, 4, 5, 6};
circular_deque<int> move;
EXPECT_TRUE(move.empty());
move = std::move(first);
EXPECT_TRUE(first.empty());
EXPECT_EQ(6u, move.size());
for (int i = 0; i < 6; i++)
EXPECT_EQ(i + 1, move[i]);
}
// Tests that self-assignment is a no-op.
TEST(CircularDeque, EqualsSelf) {
circular_deque<int> q = {1, 2, 3, 4, 5, 6};
q = *&q; // The *& defeats Clang's -Wself-assign warning.
EXPECT_EQ(6u, q.size());
for (int i = 0; i < 6; i++)
EXPECT_EQ(i + 1, q[i]);
}
TEST(CircularDeque, EqualsInitializerList) {
circular_deque<int> q;
EXPECT_TRUE(q.empty());
q = {1, 2, 3, 4, 5, 6};
EXPECT_EQ(6u, q.size());
for (int i = 0; i < 6; i++)
EXPECT_EQ(i + 1, q[i]);
}
TEST(CircularDeque, AssignCountValue) {
circular_deque<int> empty;
empty.assign(0, 52);
EXPECT_EQ(0u, empty.size());
circular_deque<int> full;
size_t count = 13;
int value = 12345;
full.assign(count, value);
EXPECT_EQ(count, full.size());
for (size_t i = 0; i < count; i++)
EXPECT_EQ(value, full[i]);
}
TEST(CircularDeque, AssignIterator) {
int range[8] = {11, 12, 13, 14, 15, 16, 17, 18};
circular_deque<int> empty;
empty.assign(std::begin(range), std::begin(range));
EXPECT_TRUE(empty.empty());
circular_deque<int> full;
full.assign(std::begin(range), std::end(range));
EXPECT_EQ(8u, full.size());
for (size_t i = 0; i < 8; i++)
EXPECT_EQ(range[i], full[i]);
}
TEST(CircularDeque, AssignInitializerList) {
circular_deque<int> empty;
empty.assign({});
EXPECT_TRUE(empty.empty());
circular_deque<int> full;
full.assign({11, 12, 13, 14, 15, 16, 17, 18});
EXPECT_EQ(8u, full.size());
for (int i = 0; i < 8; i++)
EXPECT_EQ(11 + i, full[i]);
}
// Tests [] and .at().
TEST(CircularDeque, At) {
circular_deque<int> q = MakeSequence(10);
CycleTest(q, [](const circular_deque<int>& q, size_t cycle) {
size_t expected_size = 10;
EXPECT_EQ(expected_size, q.size());
// A sequence of 0's.
size_t index = 0;
size_t num_zeros = std::min(expected_size, cycle);
for (size_t i = 0; i < num_zeros; i++, index++) {
EXPECT_EQ(0, q[index]);
EXPECT_EQ(0, q.at(index));
}
// Followed by a sequence of increasing ints.
size_t num_ints = expected_size - num_zeros;
for (int i = 0; i < static_cast<int>(num_ints); i++, index++) {
EXPECT_EQ(i, q[index]);
EXPECT_EQ(i, q.at(index));
}
});
}
// This also tests the copy constructor with lots of different types of
// input configurations.
TEST(CircularDeque, FrontBackPushPop) {
circular_deque<int> q = MakeSequence(10);
int expected_front = 0;
int expected_back = 9;
// Go in one direction.
for (int i = 0; i < 100; i++) {
const circular_deque<int> const_q(q);
EXPECT_EQ(expected_front, q.front());
EXPECT_EQ(expected_back, q.back());
EXPECT_EQ(expected_front, const_q.front());
EXPECT_EQ(expected_back, const_q.back());
expected_front++;
expected_back++;
q.pop_front();
q.push_back(expected_back);
}
// Go back in reverse.
for (int i = 0; i < 100; i++) {
const circular_deque<int> const_q(q);
EXPECT_EQ(expected_front, q.front());
EXPECT_EQ(expected_back, q.back());
EXPECT_EQ(expected_front, const_q.front());
EXPECT_EQ(expected_back, const_q.back());
expected_front--;
expected_back--;
q.pop_back();
q.push_front(expected_front);
}
}
TEST(CircularDeque, ReallocateWithSplitBuffer) {
// Tests reallocating a deque with an internal buffer that looks like this:
// 4 5 x x 0 1 2 3
// end-^ ^-begin
circular_deque<int> q;
q.reserve(7); // Internal buffer is always 1 larger than requested.
q.push_back(-1);
q.push_back(-1);
q.push_back(-1);
q.push_back(-1);
q.push_back(0);
q.pop_front();
q.pop_front();
q.pop_front();
q.pop_front();
q.push_back(1);
q.push_back(2);
q.push_back(3);
q.push_back(4);
q.push_back(5);
q.shrink_to_fit();
EXPECT_EQ(6u, q.size());
EXPECT_EQ(0, q[0]);
EXPECT_EQ(1, q[1]);
EXPECT_EQ(2, q[2]);
EXPECT_EQ(3, q[3]);
EXPECT_EQ(4, q[4]);
EXPECT_EQ(5, q[5]);
}
TEST(CircularDeque, Swap) {
circular_deque<int> a = MakeSequence(10);
circular_deque<int> b = MakeSequence(100);
a.swap(b);
EXPECT_EQ(100u, a.size());
for (int i = 0; i < 100; i++)
EXPECT_EQ(i, a[i]);
EXPECT_EQ(10u, b.size());
for (int i = 0; i < 10; i++)
EXPECT_EQ(i, b[i]);
}
TEST(CircularDeque, Iteration) {
circular_deque<int> q = MakeSequence(10);
int expected_front = 0;
int expected_back = 9;
// This loop causes various combinations of begin and end to be tested.
for (int i = 0; i < 30; i++) {
// Range-based for loop going forward.
int current_expected = expected_front;
for (int cur : q) {
EXPECT_EQ(current_expected, cur);
current_expected++;
}
// Manually test reverse iterators.
current_expected = expected_back;
for (auto cur = q.crbegin(); cur < q.crend(); cur++) {
EXPECT_EQ(current_expected, *cur);
current_expected--;
}
expected_front++;
expected_back++;
q.pop_front();
q.push_back(expected_back);
}
// Go back in reverse.
for (int i = 0; i < 100; i++) {
const circular_deque<int> const_q(q);
EXPECT_EQ(expected_front, q.front());
EXPECT_EQ(expected_back, q.back());
EXPECT_EQ(expected_front, const_q.front());
EXPECT_EQ(expected_back, const_q.back());
expected_front--;
expected_back--;
q.pop_back();
q.push_front(expected_front);
}
}
TEST(CircularDeque, IteratorComparisons) {
circular_deque<int> q = MakeSequence(10);
// This loop causes various combinations of begin and end to be tested.
for (int i = 0; i < 30; i++) {
EXPECT_LT(q.begin(), q.end());
EXPECT_LE(q.begin(), q.end());
EXPECT_LE(q.begin(), q.begin());
EXPECT_GT(q.end(), q.begin());
EXPECT_GE(q.end(), q.begin());
EXPECT_GE(q.end(), q.end());
EXPECT_EQ(q.begin(), q.begin());
EXPECT_NE(q.begin(), q.end());
q.push_front(10);
q.pop_back();
}
}
TEST(CircularDeque, IteratorIncDec) {
circular_deque<int> q;
// No-op offset computations with no capacity.
EXPECT_EQ(q.end(), q.end() + 0);
EXPECT_EQ(q.end(), q.end() - 0);
q = MakeSequence(10);
// Mutable preincrement, predecrement.
{
circular_deque<int>::iterator it = q.begin();
circular_deque<int>::iterator op_result = ++it;
EXPECT_EQ(1, *op_result);
EXPECT_EQ(1, *it);
op_result = --it;
EXPECT_EQ(0, *op_result);
EXPECT_EQ(0, *it);
}
// Const preincrement, predecrement.
{
circular_deque<int>::const_iterator it = q.begin();
circular_deque<int>::const_iterator op_result = ++it;
EXPECT_EQ(1, *op_result);
EXPECT_EQ(1, *it);
op_result = --it;
EXPECT_EQ(0, *op_result);
EXPECT_EQ(0, *it);
}
// Mutable postincrement, postdecrement.
{
circular_deque<int>::iterator it = q.begin();
circular_deque<int>::iterator op_result = it++;
EXPECT_EQ(0, *op_result);
EXPECT_EQ(1, *it);
op_result = it--;
EXPECT_EQ(1, *op_result);
EXPECT_EQ(0, *it);
}
// Const postincrement, postdecrement.
{
circular_deque<int>::const_iterator it = q.begin();
circular_deque<int>::const_iterator op_result = it++;
EXPECT_EQ(0, *op_result);
EXPECT_EQ(1, *it);
op_result = it--;
EXPECT_EQ(1, *op_result);
EXPECT_EQ(0, *it);
}
}
TEST(CircularDeque, IteratorIntegerOps) {
circular_deque<int> q = MakeSequence(10);
int expected_front = 0;
int expected_back = 9;
for (int i = 0; i < 30; i++) {
EXPECT_EQ(0, q.begin() - q.begin());
EXPECT_EQ(0, q.end() - q.end());
EXPECT_EQ(q.size(), static_cast<size_t>(q.end() - q.begin()));
// +=
circular_deque<int>::iterator eight = q.begin();
eight += 8;
EXPECT_EQ(8, eight - q.begin());
EXPECT_EQ(expected_front + 8, *eight);
// -=
eight -= 8;
EXPECT_EQ(q.begin(), eight);
// +
eight = eight + 8;
EXPECT_EQ(8, eight - q.begin());
// -
eight = eight - 8;
EXPECT_EQ(q.begin(), eight);
expected_front++;
expected_back++;
q.pop_front();
q.push_back(expected_back);
}
}
TEST(CircularDeque, IteratorArrayAccess) {
circular_deque<int> q = MakeSequence(10);
circular_deque<int>::iterator begin = q.begin();
EXPECT_EQ(0, begin[0]);
EXPECT_EQ(9, begin[9]);
circular_deque<int>::iterator end = q.end();
EXPECT_EQ(0, end[-10]);
EXPECT_EQ(9, end[-1]);
begin[0] = 100;
EXPECT_EQ(100, end[-10]);
}
TEST(CircularDeque, ReverseIterator) {
circular_deque<int> q;
q.push_back(4);
q.push_back(3);
q.push_back(2);
q.push_back(1);
circular_deque<int>::reverse_iterator iter = q.rbegin();
EXPECT_EQ(1, *iter);
iter++;
EXPECT_EQ(2, *iter);
++iter;
EXPECT_EQ(3, *iter);
iter++;
EXPECT_EQ(4, *iter);
++iter;
EXPECT_EQ(q.rend(), iter);
}
TEST(CircularDeque, CapacityReserveShrink) {
circular_deque<int> q;
// A default constructed queue should have no capacity since it should waste
// no space.
EXPECT_TRUE(q.empty());
EXPECT_EQ(0u, q.size());
EXPECT_EQ(0u, q.capacity());
size_t new_capacity = 100;
q.reserve(new_capacity);
EXPECT_EQ(new_capacity, q.capacity());
// Adding that many items should not cause a resize.
for (size_t i = 0; i < new_capacity; i++)
q.push_back(i);
EXPECT_EQ(new_capacity, q.size());
EXPECT_EQ(new_capacity, q.capacity());
// Shrink to fit to a smaller size.
size_t capacity_2 = new_capacity / 2;
q.resize(capacity_2);
q.shrink_to_fit();
EXPECT_EQ(capacity_2, q.size());
EXPECT_EQ(capacity_2, q.capacity());
}
TEST(CircularDeque, CapacityAutoShrink) {
size_t big_size = 1000u;
circular_deque<int> q;
q.resize(big_size);
size_t big_capacity = q.capacity();
// Delete 3/4 of the items.
for (size_t i = 0; i < big_size / 4 * 3; i++)
q.pop_back();
// The capacity should have shrunk by deleting that many items.
size_t medium_capacity = q.capacity();
EXPECT_GT(big_capacity, medium_capacity);
// Using resize to shrink should keep some extra capacity.
q.resize(1);
EXPECT_LT(1u, q.capacity());
q.resize(0);
EXPECT_LT(0u, q.capacity());
// Using clear() should delete everything.
q.clear();
EXPECT_EQ(0u, q.capacity());
}
TEST(CircularDeque, ClearAndEmpty) {
circular_deque<int> q;
EXPECT_TRUE(q.empty());
q.resize(10);
EXPECT_EQ(10u, q.size());
EXPECT_FALSE(q.empty());
q.clear();
EXPECT_EQ(0u, q.size());
EXPECT_TRUE(q.empty());
// clear() also should reset the capacity.
EXPECT_EQ(0u, q.capacity());
}
TEST(CircularDeque, Resize) {
circular_deque<int> q;
// Resize with default constructor.
size_t first_size = 10;
q.resize(first_size);
EXPECT_EQ(first_size, q.size());
for (size_t i = 0; i < first_size; i++)
EXPECT_EQ(0, q[i]);
// Resize with different value.
size_t second_expand = 10;
q.resize(first_size + second_expand, 3);
EXPECT_EQ(first_size + second_expand, q.size());
for (size_t i = 0; i < first_size; i++)
EXPECT_EQ(0, q[i]);
for (size_t i = 0; i < second_expand; i++)
EXPECT_EQ(3, q[i + first_size]);
// Erase from the end and add to the beginning so resize is forced to cross
// a circular buffer wrap boundary.
q.shrink_to_fit();
for (int i = 0; i < 5; i++) {
q.pop_back();
q.push_front(6);
}
q.resize(10);
EXPECT_EQ(6, q[0]);
EXPECT_EQ(6, q[1]);
EXPECT_EQ(6, q[2]);
EXPECT_EQ(6, q[3]);
EXPECT_EQ(6, q[4]);
EXPECT_EQ(0, q[5]);
EXPECT_EQ(0, q[6]);
EXPECT_EQ(0, q[7]);
EXPECT_EQ(0, q[8]);
EXPECT_EQ(0, q[9]);
}
// Tests destructor behavior of resize.
TEST(CircularDeque, ResizeDelete) {
int counter = 0;
circular_deque<DestructorCounter> q;
q.resize(10, DestructorCounter(&counter));
// The one temporary when calling resize() should be deleted, that's it.
EXPECT_EQ(1, counter);
// The loops below assume the capacity will be set by resize().
EXPECT_EQ(10u, q.capacity());
// Delete some via resize(). This is done so that the wasted items are
// still greater than the size() so that auto-shrinking is not triggered
// (which will mess up our destructor counting).
counter = 0;
q.resize(8, DestructorCounter(&counter));
// 2 deleted ones + the one temporary in the resize() call.
EXPECT_EQ(3, counter);
// Cycle through some items so two items will cross the boundary in the
// 11-item buffer (one more than the capacity).
// Before: x x x x x x x x . . .
// After: x . . . x x x x x x x
counter = 0;
for (int i = 0; i < 4; i++) {
q.emplace_back(&counter);
q.pop_front();
}
EXPECT_EQ(4, counter); // Loop should have deleted 7 items.
// Delete two items with resize, these should be on either side of the
// buffer wrap point.
counter = 0;
q.resize(6, DestructorCounter(&counter));
// 2 deleted ones + the one temporary in the resize() call.
EXPECT_EQ(3, counter);
}
TEST(CircularDeque, InsertEraseSingle) {
circular_deque<int> q;
q.push_back(1);
q.push_back(2);
// Insert at the beginning.
auto result = q.insert(q.begin(), 0);
EXPECT_EQ(q.begin(), result);
EXPECT_EQ(3u, q.size());
EXPECT_EQ(0, q[0]);
EXPECT_EQ(1, q[1]);
EXPECT_EQ(2, q[2]);
// Erase at the beginning.
result = q.erase(q.begin());
EXPECT_EQ(q.begin(), result);
EXPECT_EQ(2u, q.size());
EXPECT_EQ(1, q[0]);
EXPECT_EQ(2, q[1]);
// Insert at the end.
result = q.insert(q.end(), 3);
EXPECT_EQ(q.end() - 1, result);
EXPECT_EQ(1, q[0]);
EXPECT_EQ(2, q[1]);
EXPECT_EQ(3, q[2]);
// Erase at the end.
result = q.erase(q.end() - 1);
EXPECT_EQ(q.end(), result);
EXPECT_EQ(1, q[0]);
EXPECT_EQ(2, q[1]);
// Insert in the middle.
result = q.insert(q.begin() + 1, 10);
EXPECT_EQ(q.begin() + 1, result);
EXPECT_EQ(1, q[0]);
EXPECT_EQ(10, q[1]);
EXPECT_EQ(2, q[2]);
// Erase in the middle.
result = q.erase(q.begin() + 1);
EXPECT_EQ(q.begin() + 1, result);
EXPECT_EQ(1, q[0]);
EXPECT_EQ(2, q[1]);
}
TEST(CircularDeque, InsertFill) {
circular_deque<int> q;
// Fill when empty.
q.insert(q.begin(), 2, 1);
// 0's at the beginning.
q.insert(q.begin(), 2, 0);
// 50's in the middle (now at offset 3).
q.insert(q.begin() + 3, 2, 50);
// 200's at the end.
q.insert(q.end(), 2, 200);
ASSERT_EQ(8u, q.size());
EXPECT_EQ(0, q[0]);
EXPECT_EQ(0, q[1]);
EXPECT_EQ(1, q[2]);
EXPECT_EQ(50, q[3]);
EXPECT_EQ(50, q[4]);
EXPECT_EQ(1, q[5]);
EXPECT_EQ(200, q[6]);
EXPECT_EQ(200, q[7]);
}
TEST(CircularDeque, InsertEraseRange) {
circular_deque<int> q;
// Erase nothing from an empty deque should work.
q.erase(q.begin(), q.end());
// Loop index used below to shift the used items in the buffer.
for (int i = 0; i < 10; i++) {
circular_deque<int> source;
// Fill empty range.
q.insert(q.begin(), source.begin(), source.end());
// Have some stuff to insert.
source.push_back(1);
source.push_back(2);
q.insert(q.begin(), source.begin(), source.end());
// Shift the used items in the buffer by i which will place the two used
// elements in different places in the buffer each time through this loop.
for (int shift_i = 0; shift_i < i; shift_i++) {
q.push_back(0);
q.pop_front();
}
// Set the two items to notable values so we can check for them later.
ASSERT_EQ(2u, q.size());
q[0] = 100;
q[1] = 101;
// Insert at the beginning, middle (now at offset 3), and end.
q.insert(q.begin(), source.begin(), source.end());
q.insert(q.begin() + 3, source.begin(), source.end());
q.insert(q.end(), source.begin(), source.end());
ASSERT_EQ(8u, q.size());
EXPECT_EQ(1, q[0]);
EXPECT_EQ(2, q[1]);
EXPECT_EQ(100, q[2]); // First inserted one.
EXPECT_EQ(1, q[3]);
EXPECT_EQ(2, q[4]);
EXPECT_EQ(101, q[5]); // First inserted second one.
EXPECT_EQ(1, q[6]);
EXPECT_EQ(2, q[7]);
// Now erase the inserted ranges. Try each subsection also with no items
// being erased, which should be a no-op.
auto result = q.erase(q.begin(), q.begin()); // No-op.
EXPECT_EQ(q.begin(), result);
result = q.erase(q.begin(), q.begin() + 2);
EXPECT_EQ(q.begin(), result);
result = q.erase(q.begin() + 1, q.begin() + 1); // No-op.
EXPECT_EQ(q.begin() + 1, result);
result = q.erase(q.begin() + 1, q.begin() + 3);
EXPECT_EQ(q.begin() + 1, result);
result = q.erase(q.end() - 2, q.end() - 2); // No-op.
EXPECT_EQ(q.end() - 2, result);
result = q.erase(q.end() - 2, q.end());
EXPECT_EQ(q.end(), result);
ASSERT_EQ(2u, q.size());
EXPECT_EQ(100, q[0]);
EXPECT_EQ(101, q[1]);
// Erase everything.
result = q.erase(q.begin(), q.end());
EXPECT_EQ(q.end(), result);
EXPECT_TRUE(q.empty());
}
}
TEST(CircularDeque, EmplaceMoveOnly) {
int values[] = {1, 3};
circular_deque<MoveOnlyInt> q(std::begin(values), std::end(values));
q.emplace(q.begin(), MoveOnlyInt(0));
q.emplace(q.begin() + 2, MoveOnlyInt(2));
q.emplace(q.end(), MoveOnlyInt(4));
ASSERT_EQ(5u, q.size());
EXPECT_EQ(0, q[0].data());
EXPECT_EQ(1, q[1].data());
EXPECT_EQ(2, q[2].data());
EXPECT_EQ(3, q[3].data());
EXPECT_EQ(4, q[4].data());
}
TEST(CircularDeque, EmplaceFrontBackReturnsReference) {
circular_deque<int> q;
q.reserve(2);
int& front = q.emplace_front(1);
int& back = q.emplace_back(2);
ASSERT_EQ(2u, q.size());
EXPECT_EQ(1, q[0]);
EXPECT_EQ(2, q[1]);
EXPECT_EQ(&front, &q.front());
EXPECT_EQ(&back, &q.back());
front = 3;
back = 4;
ASSERT_EQ(2u, q.size());
EXPECT_EQ(3, q[0]);
EXPECT_EQ(4, q[1]);
EXPECT_EQ(&front, &q.front());
EXPECT_EQ(&back, &q.back());
}
// This test tries to dereference an iterator after mutating the container.
TEST(CircularDeque, UseIteratorAfterMutate) {
circular_deque<int> q;
q.push_back(0);
auto old_begin = q.begin();
EXPECT_EQ(0, *old_begin);
q.push_back(1);
// This statement is not executed when DCHECKs are disabled.
EXPECT_DCHECK_DEATH(*old_begin);
}
// This test verifies that a scoped_refptr specifically is moved rather than
// copied when a circular_deque is resized. It would be extremely inefficient if
// it was copied in this case.
TEST(CircularDeque, DoesntChurnRefCount) {
static constexpr size_t kCount = 10;
RefCountChangeCounter counters[kCount];
circular_deque<scoped_refptr<RefCountChangeCounter>> deque;
bool checked_capacity = false;
for (auto& counter : counters) {
deque.push_back(scoped_refptr<RefCountChangeCounter>(&counter));
if (!checked_capacity) {
// Verify that the deque will have to reallocate.
EXPECT_LT(deque.capacity(), kCount);
checked_capacity = true;
}
}
// Verify that reallocation has happened.
EXPECT_GE(deque.capacity(), kCount);
for (const auto& counter : counters) {
EXPECT_EQ(1, counter.ref_count_changes());
}
}
} // namespace base
|