xpcom/tests/gtest/TestNsDeque.cpp
author Chris Peterson <cpeterson@mozilla.com>
Sun, 24 Feb 2019 17:35:59 -0800
changeset 464363 5ff84e853d700af894616574f581551a88a93dc7
parent 450554 09c71a7cf75aeaf2963050e315276fb9a866fd62
child 468049 389b6bbd76dbdf3357453f0989bbe9595751b7ae
permissions -rw-r--r--
Bug 1534878 - xpcom: Make some global functions static. r=erahm clang's -Wmissing-prototypes option identifies global functions that can be made static (because they're only called from one compilation unit) or removed (if they're never called). xpcom/base/Logging.cpp:85:13 [-Wmissing-prototypes] no previous prototype for function 'ToLogStr' xpcom/base/Logging.cpp:132:13 [-Wmissing-prototypes] no previous prototype for function 'ExpandPIDMarker' xpcom/base/LogModulePrefWatcher.cpp:37:6 [-Wmissing-prototypes] no previous prototype for function 'ResetExistingPrefs' xpcom/base/LogModulePrefWatcher.cpp:109:6 [-Wmissing-prototypes] no previous prototype for function 'LoadExistingPrefs' xpcom/base/nsCycleCollector.cpp:212:6 [-Wmissing-prototypes] no previous prototype for function 'SuspectUsingNurseryPurpleBuffer' xpcom/components/nsComponentManager.cpp:421:31 [-Wmissing-prototypes] no previous prototype for function 'begin' xpcom/components/nsComponentManager.cpp:427:31 [-Wmissing-prototypes] no previous prototype for function 'end' xpcom/ds/Dafsa.cpp:23:6 [-Wmissing-prototypes] no previous prototype for function 'GetNextOffset' xpcom/ds/Dafsa.cpp:55:6 [-Wmissing-prototypes] no previous prototype for function 'IsEOL' xpcom/ds/Dafsa.cpp:62:6 [-Wmissing-prototypes] no previous prototype for function 'IsMatch' xpcom/ds/Dafsa.cpp:70:6 [-Wmissing-prototypes] no previous prototype for function 'IsEndCharMatch' xpcom/ds/Dafsa.cpp:78:6 [-Wmissing-prototypes] no previous prototype for function 'GetReturnValue' xpcom/ds/Dafsa.cpp:91:5 [-Wmissing-prototypes] no previous prototype for function 'LookupString' xpcom/io/CocoaFileUtils.mm:195:13 [-Wmissing-prototypes] no previous prototype for function 'GetQuarantinePropKey' xpcom/io/CocoaFileUtils.mm:203:24 [-Wmissing-prototypes] no previous prototype for function 'CreateQuarantineDictionary' xpcom/rust/gtest/bench-collections/Bench.cpp:65:11 [-Wmissing-prototypes] no previous prototype for function 'MyRand' xpcom/rust/gtest/bench-collections/Bench.cpp:85:6 [-Wmissing-prototypes] no previous prototype for function 'Bench_Cpp_unordered_set' xpcom/rust/gtest/bench-collections/Bench.cpp:125:6 [-Wmissing-prototypes] no previous prototype for function 'Bench_Cpp_PLDHashTable' xpcom/rust/gtest/bench-collections/Bench.cpp:166:6 [-Wmissing-prototypes] no previous prototype for function 'Bench_Cpp_MozHashSet' xpcom/tests/gtest/TestAtoms.cpp:114:6 [-Wmissing-prototypes] no previous prototype for function 'isStaticAtom' xpcom/tests/gtest/TestCallTemplates.cpp:72:6 [-Wmissing-prototypes] no previous prototype for function 'JustTestingCompilation' xpcom/tests/gtest/TestCOMPtr.cpp:87:10 [-Wmissing-prototypes] no previous prototype for function 'CreateIFoo' xpcom/tests/gtest/TestCOMPtr.cpp:98:6 [-Wmissing-prototypes] no previous prototype for function 'set_a_IFoo' xpcom/tests/gtest/TestCOMPtr.cpp:105:16 [-Wmissing-prototypes] no previous prototype for function 'return_a_IFoo' xpcom/tests/gtest/TestCOMPtr.cpp:164:10 [-Wmissing-prototypes] no previous prototype for function 'CreateIBar' xpcom/tests/gtest/TestCOMPtr.cpp:175:6 [-Wmissing-prototypes] no previous prototype for function 'AnIFooPtrPtrContext' xpcom/tests/gtest/TestCOMPtr.cpp:177:6 [-Wmissing-prototypes] no previous prototype for function 'AVoidPtrPtrContext' xpcom/tests/gtest/TestCOMPtr.cpp:179:6 [-Wmissing-prototypes] no previous prototype for function 'AnISupportsPtrPtrContext' xpcom/tests/gtest/TestCOMPtr.cpp:263:6 [-Wmissing-prototypes] no previous prototype for function 'Comparison' xpcom/tests/gtest/TestCOMPtr.cpp:298:6 [-Wmissing-prototypes] no previous prototype for function 'DontAddRef' xpcom/tests/gtest/TestCRT.cpp:17:5 [-Wmissing-prototypes] no previous prototype for function 'sign' xpcom/tests/gtest/TestDeadlockDetector.cpp:62:6 [-Wmissing-prototypes] no previous prototype for function 'DisableCrashReporter' xpcom/tests/gtest/TestDeadlockDetector.cpp:74:5 [-Wmissing-prototypes] no previous prototype for function 'Sanity_Child' xpcom/tests/gtest/TestDeadlockDetector.cpp:95:5 [-Wmissing-prototypes] no previous prototype for function 'Sanity2_Child' xpcom/tests/gtest/TestDeadlockDetector.cpp:159:5 [-Wmissing-prototypes] no previous prototype for function 'Sanity4_Child' xpcom/tests/gtest/TestDeadlockDetector.cpp:182:5 [-Wmissing-prototypes] no previous prototype for function 'Sanity5_Child' xpcom/tests/gtest/TestDeadlockDetector.cpp:303:5 [-Wmissing-prototypes] no previous prototype for function 'ContentionNoDeadlock_Child' xpcom/tests/gtest/TestHashtables.cpp:88:6 [-Wmissing-prototypes] no previous prototype for function 'testTHashtable' xpcom/tests/gtest/TestHashtables.cpp:205:10 [-Wmissing-prototypes] no previous prototype for function 'CreateIFoo' xpcom/tests/gtest/TestMoveString.cpp:25:6 [-Wmissing-prototypes] no previous prototype for function 'SetAsOwned' xpcom/tests/gtest/TestMoveString.cpp:34:6 [-Wmissing-prototypes] no previous prototype for function 'ExpectTruncated' xpcom/tests/gtest/TestMoveString.cpp:40:6 [-Wmissing-prototypes] no previous prototype for function 'ExpectNew' xpcom/tests/gtest/TestMruCache.cpp:52:11 [-Wmissing-prototypes] no previous prototype for function 'MakeStringKey' xpcom/tests/gtest/TestMultiplexInputStream.cpp:106:34 [-Wmissing-prototypes] no previous prototype for function 'CreateStreamHelper' xpcom/tests/gtest/TestNonBlockingAsyncInputStream.cpp:62:10 [-Wmissing-prototypes] no previous prototype for function 'ReadSegmentsFunction' xpcom/tests/gtest/TestNsDeque.cpp:240:6 [-Wmissing-prototypes] no previous prototype for function 'CheckIfQueueEmpty' xpcom/tests/gtest/TestNsRefPtr.cpp:105:10 [-Wmissing-prototypes] no previous prototype for function 'CreateFoo' xpcom/tests/gtest/TestNsRefPtr.cpp:116:6 [-Wmissing-prototypes] no previous prototype for function 'set_a_Foo' xpcom/tests/gtest/TestNsRefPtr.cpp:123:13 [-Wmissing-prototypes] no previous prototype for function 'return_a_Foo' xpcom/tests/gtest/TestNsRefPtr.cpp:391:6 [-Wmissing-prototypes] no previous prototype for function 'AnFooPtrPtrContext' xpcom/tests/gtest/TestNsRefPtr.cpp:392:6 [-Wmissing-prototypes] no previous prototype for function 'AVoidPtrPtrContext' xpcom/tests/gtest/TestPLDHash.cpp:33:6 [-Wmissing-prototypes] no previous prototype for function 'TestCrashyOperation' xpcom/tests/gtest/TestPipes.cpp:98:10 [-Wmissing-prototypes] no previous prototype for function 'TestPipe' xpcom/tests/gtest/TestPipes.cpp:212:10 [-Wmissing-prototypes] no previous prototype for function 'TestShortWrites' xpcom/tests/gtest/TestPipes.cpp:354:6 [-Wmissing-prototypes] no previous prototype for function 'RunTests' xpcom/tests/gtest/TestPLDHash.cpp:90:6 [-Wmissing-prototypes] no previous prototype for function 'InitCapacityOk_InitialLengthTooBig' xpcom/tests/gtest/TestPLDHash.cpp:95:6 [-Wmissing-prototypes] no previous prototype for function 'InitCapacityOk_InitialEntryStoreTooBig' xpcom/tests/gtest/TestPLDHash.cpp:102:6 [-Wmissing-prototypes] no previous prototype for function 'InitCapacityOk_EntrySizeTooBig' xpcom/tests/gtest/TestSlicedInputStream.cpp:111:20 [-Wmissing-prototypes] no previous prototype for function 'CreateSeekableStreams' xpcom/tests/gtest/TestSlicedInputStream.cpp:125:20 [-Wmissing-prototypes] no previous prototype for function 'CreateNonSeekableStreams' xpcom/tests/gtest/TestStrings.cpp:471:6 [-Wmissing-prototypes] no previous prototype for function 'test_assign_helper' xpcom/tests/gtest/TestTArray.cpp:60:22 [-Wmissing-prototypes] no previous prototype for function 'DummyArray' xpcom/tests/gtest/TestTArray.cpp:72:22 [-Wmissing-prototypes] no previous prototype for function 'FakeHugeArray' xpcom/tests/gtest/TestThrottledEventQueue.cpp:96:6 [-Wmissing-prototypes] no previous prototype for function 'Enqueue' xpcom/threads/BlockingResourceBase.cpp:86:6 [-Wmissing-prototypes] no previous prototype for function 'PrintCycle' xpcom/threads/CPUUsageWatcher.cpp:41:10 [-Wmissing-prototypes] no previous prototype for function 'GetMicroseconds' xpcom/threads/CPUUsageWatcher.cpp:46:10 [-Wmissing-prototypes] no previous prototype for function 'GetMicroseconds' xpcom/threads/CPUUsageWatcher.cpp:51:40 [-Wmissing-prototypes] no previous prototype for function 'GetProcessCPUStats' xpcom/threads/CPUUsageWatcher.cpp:80:40 [-Wmissing-prototypes] no previous prototype for function 'GetGlobalCPUStats' xpcom/threads/nsTimerImpl.cpp:196:21 [-Wmissing-prototypes] no previous prototype for function 'GetTimerFiringsLog' Differential Revision: https://phabricator.services.mozilla.com/D23264

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"
#include "nsDeque.h"
#include "nsCRT.h"
#include "mozilla/TypeTraits.h"
#include <stdio.h>
#include <functional>

using mozilla::DeclVal;
using mozilla::IsSame;

/**************************************************************
  Now define the token deallocator class...
 **************************************************************/
namespace TestNsDeque {

class _Dealloc : public nsDequeFunctor {
  virtual void operator()(void* aObject) {}
};

static bool VerifyContents(const nsDeque& aDeque, const int* aContents,
                           size_t aLength) {
  for (size_t i = 0; i < aLength; ++i) {
    if (*(int*)aDeque.ObjectAt(i) != aContents[i]) {
      return false;
    }
  }
  return true;
}

class Deallocator : public nsDequeFunctor {
  virtual void operator()(void* aObject) {
    if (aObject) {
      // Set value to -1, to use in test function.
      *((int*)aObject) = -1;
    }
  }
};

class ForEachAdder : public nsDequeFunctor {
  virtual void operator()(void* aObject) {
    if (aObject) {
      sum += *(int*)aObject;
    }
  }

 private:
  int sum = 0;

 public:
  int GetSum() { return sum; }
};
}  // namespace TestNsDeque

using namespace TestNsDeque;

TEST(NsDeque, OriginalTest) {
  const size_t size = 200;
  int ints[size];
  size_t i = 0;
  int temp;
  nsDeque theDeque(new _Dealloc);  // construct a simple one...

  // ints = [0...199]
  for (i = 0; i < size; i++) {  // initialize'em
    ints[i] = static_cast<int>(i);
  }
  // queue = [0...69]
  for (i = 0; i < 70; i++) {
    theDeque.Push(&ints[i]);
    temp = *(int*)theDeque.Peek();
    EXPECT_EQ(static_cast<int>(i), temp) << "Verify end after push #1";
    EXPECT_EQ(i + 1, theDeque.GetSize()) << "Verify size after push #1";
  }

  EXPECT_EQ(70u, theDeque.GetSize()) << "Verify overall size after pushes #1";

  // queue = [0...14]
  for (i = 1; i <= 55; i++) {
    temp = *(int*)theDeque.Pop();
    EXPECT_EQ(70 - static_cast<int>(i), temp) << "Verify end after pop # 1";
    EXPECT_EQ(70u - i, theDeque.GetSize()) << "Verify size after pop # 1";
  }
  EXPECT_EQ(15u, theDeque.GetSize()) << "Verify overall size after pops";

  // queue = [0...14,0...54]
  for (i = 0; i < 55; i++) {
    theDeque.Push(&ints[i]);
    temp = *(int*)theDeque.Peek();
    EXPECT_EQ(static_cast<int>(i), temp) << "Verify end after push #2";
    EXPECT_EQ(i + 15u + 1, theDeque.GetSize()) << "Verify size after push # 2";
  }
  EXPECT_EQ(70u, theDeque.GetSize())
      << "Verify size after end of all pushes #2";

  // queue = [0...14,0...19]
  for (i = 1; i <= 35; i++) {
    temp = *(int*)theDeque.Pop();
    EXPECT_EQ(55 - static_cast<int>(i), temp) << "Verify end after pop # 2";
    EXPECT_EQ(70u - i, theDeque.GetSize()) << "Verify size after pop #2";
  }
  EXPECT_EQ(35u, theDeque.GetSize())
      << "Verify overall size after end of all pops #2";

  // queue = [0...14,0...19,0...34]
  for (i = 0; i < 35; i++) {
    theDeque.Push(&ints[i]);
    temp = *(int*)theDeque.Peek();
    EXPECT_EQ(static_cast<int>(i), temp) << "Verify end after push # 3";
    EXPECT_EQ(35u + 1u + i, theDeque.GetSize()) << "Verify size after push #3";
  }

  // queue = [0...14,0...19]
  for (i = 0; i < 35; i++) {
    temp = *(int*)theDeque.Pop();
    EXPECT_EQ(34 - static_cast<int>(i), temp) << "Verify end after pop # 3";
  }

  // queue = [0...14]
  for (i = 0; i < 20; i++) {
    temp = *(int*)theDeque.Pop();
    EXPECT_EQ(19 - static_cast<int>(i), temp) << "Verify end after pop # 4";
  }

  // queue = []
  for (i = 0; i < 15; i++) {
    temp = *(int*)theDeque.Pop();
    EXPECT_EQ(14 - static_cast<int>(i), temp) << "Verify end after pop # 5";
  }

  EXPECT_EQ(0u, theDeque.GetSize()) << "Deque should finish empty.";
}

TEST(NsDeque, OriginalFlaw) {
  int ints[200];
  int i = 0;
  int temp;
  nsDeque d(new _Dealloc);
  /**
   * Test 1. Origin near end, semi full, call Peek().
   * you start, mCapacity is 8
   */
  for (i = 0; i < 30; i++) ints[i] = i;

  for (i = 0; i < 6; i++) {
    d.Push(&ints[i]);
    temp = *(int*)d.Peek();
    EXPECT_EQ(i, temp) << "OriginalFlaw push #1";
  }
  EXPECT_EQ(6u, d.GetSize()) << "OriginalFlaw size check #1";

  for (i = 0; i < 4; i++) {
    temp = *(int*)d.PopFront();
    EXPECT_EQ(i, temp) << "PopFront test";
  }
  // d = [4,5]
  EXPECT_EQ(2u, d.GetSize()) << "OriginalFlaw size check #2";

  for (i = 0; i < 4; i++) {
    d.Push(&ints[6 + i]);
  }

  // d = [4...9]
  for (i = 4; i <= 9; i++) {
    temp = *(int*)d.PopFront();
    EXPECT_EQ(i, temp) << "OriginalFlaw empty check";
  }
}

TEST(NsDeque, TestObjectAt) {
  nsDeque d;
  const int count = 10;
  int ints[count];
  for (int i = 0; i < count; i++) {
    ints[i] = i;
  }

  for (int i = 0; i < 6; i++) {
    d.Push(&ints[i]);
  }
  // d = [0...5]
  d.PopFront();
  d.PopFront();

  // d = [2..5]
  for (size_t i = 2; i <= 5; i++) {
    int t = *(int*)d.ObjectAt(i - 2);
    EXPECT_EQ(static_cast<int>(i), t) << "Verify ObjectAt()";
  }
}

TEST(NsDeque, TestPushFront) {
  // PushFront has some interesting corner cases, primarily we're interested in
  // whether:
  // - wrapping around works properly
  // - growing works properly

  nsDeque d;

  const int kPoolSize = 10;
  const size_t kMaxSizeBeforeGrowth = 8;

  int pool[kPoolSize];
  for (int i = 0; i < kPoolSize; i++) {
    pool[i] = i;
  }

  for (size_t i = 0; i < kMaxSizeBeforeGrowth; i++) {
    d.PushFront(pool + i);
  }

  EXPECT_EQ(kMaxSizeBeforeGrowth, d.GetSize()) << "verify size";

  static const int t1[] = {7, 6, 5, 4, 3, 2, 1, 0};
  EXPECT_TRUE(VerifyContents(d, t1, kMaxSizeBeforeGrowth))
      << "verify pushfront 1";

  // Now push one more so it grows
  d.PushFront(pool + kMaxSizeBeforeGrowth);
  EXPECT_EQ(kMaxSizeBeforeGrowth + 1, d.GetSize()) << "verify size";

  static const int t2[] = {8, 7, 6, 5, 4, 3, 2, 1, 0};
  EXPECT_TRUE(VerifyContents(d, t2, kMaxSizeBeforeGrowth + 1))
      << "verify pushfront 2";

  // And one more so that it wraps again
  d.PushFront(pool + kMaxSizeBeforeGrowth + 1);
  EXPECT_EQ(kMaxSizeBeforeGrowth + 2, d.GetSize()) << "verify size";

  static const int t3[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
  EXPECT_TRUE(VerifyContents(d, t3, kMaxSizeBeforeGrowth + 2))
      << "verify pushfront 3";
}

static void CheckIfQueueEmpty(nsDeque& d) {
  EXPECT_EQ(0u, d.GetSize()) << "Size should be 0";
  EXPECT_EQ(nullptr, d.Pop()) << "Invalid operation should return nullptr";
  EXPECT_EQ(nullptr, d.PopFront()) << "Invalid operation should return nullptr";
  EXPECT_EQ(nullptr, d.Peek()) << "Invalid operation should return nullptr";
  EXPECT_EQ(nullptr, d.PeekFront())
      << "Invalid operation should return nullptr";
  EXPECT_EQ(nullptr, d.ObjectAt(0u))
      << "Invalid operation should return nullptr";
}

TEST(NsDeque, TestEmpty) {
  // Make sure nsDeque gives sane results if it's empty.
  nsDeque d;
  size_t numberOfEntries = 8;

  CheckIfQueueEmpty(d);

  // Fill it up and drain it.
  for (size_t i = 0; i < numberOfEntries; i++) {
    d.Push((void*)0xAA);
  }

  EXPECT_EQ(numberOfEntries, d.GetSize());

  for (size_t i = 0; i < numberOfEntries; i++) {
    (void)d.Pop();
  }

  // Now check it again.
  CheckIfQueueEmpty(d);
}

TEST(NsDeque, TestEraseMethod) {
  nsDeque d;
  const size_t numberOfEntries = 8;

  // Fill it up before calling Erase
  for (size_t i = 0; i < numberOfEntries; i++) {
    d.Push((void*)0xAA);
  }

  // Call Erase
  d.Erase();

  // Now check it again.
  CheckIfQueueEmpty(d);
}

TEST(NsDeque, TestEraseShouldCallDeallocator) {
  nsDeque d(new Deallocator());
  const size_t NumTestValues = 8;

  int* testArray[NumTestValues];
  for (size_t i = 0; i < NumTestValues; i++) {
    testArray[i] = new int();
    *(testArray[i]) = i;
    d.Push((void*)testArray[i]);
  }

  d.Erase();

  // Now check it again.
  CheckIfQueueEmpty(d);

  for (size_t i = 0; i < NumTestValues; i++) {
    EXPECT_EQ(-1, *(testArray[i]))
        << "Erase should call deallocator: " << *(testArray[i]);
  }
}

TEST(NsDeque, TestForEach) {
  nsDeque d(new Deallocator());
  const size_t NumTestValues = 8;
  int sum = 0;

  int* testArray[NumTestValues];
  for (size_t i = 0; i < NumTestValues; i++) {
    testArray[i] = new int();
    *(testArray[i]) = i;
    sum += i;
    d.Push((void*)testArray[i]);
  }

  ForEachAdder adder;
  d.ForEach(adder);
  EXPECT_EQ(sum, adder.GetSum()) << "For each should iterate over values";

  d.Erase();
}

TEST(NsDeque, TestConstRangeFor) {
  nsDeque d(new Deallocator());

  const size_t NumTestValues = 3;
  for (size_t i = 0; i < NumTestValues; ++i) {
    d.Push(new int(i + 1));
  }

  static_assert(IsSame<nsDeque::ConstDequeIterator,
                       decltype(DeclVal<const nsDeque&>().begin())>::value,
                "(const nsDeque).begin() should return ConstDequeIterator");
  static_assert(IsSame<nsDeque::ConstDequeIterator,
                       decltype(DeclVal<const nsDeque&>().end())>::value,
                "(const nsDeque).end() should return ConstDequeIterator");

  int sum = 0;
  for (void* ob : const_cast<const nsDeque&>(d)) {
    sum += *static_cast<int*>(ob);
  }
  EXPECT_EQ(1 + 2 + 3, sum) << "Const-range-for should iterate over values";
}

TEST(NsDeque, TestRangeFor) {
  const size_t NumTestValues = 3;
  struct Test {
    size_t runAfterLoopCount;
    std::function<void(nsDeque&)> function;
    int expectedSum;
    const char* description;
  };
  // Note: All tests start with a deque containing 3 pointers to ints 1, 2, 3.
  Test tests[] = {
      {0, [](nsDeque& d) {}, 1 + 2 + 3, "no changes"},

      {1, [](nsDeque& d) { d.Pop(); }, 1 + 2, "Pop after 1st loop"},
      {2, [](nsDeque& d) { d.Pop(); }, 1 + 2, "Pop after 2nd loop"},
      {3, [](nsDeque& d) { d.Pop(); }, 1 + 2 + 3, "Pop after 3rd loop"},

      {1, [](nsDeque& d) { d.PopFront(); }, 1 + 3, "PopFront after 1st loop"},
      {2, [](nsDeque& d) { d.PopFront(); }, 1 + 2, "PopFront after 2nd loop"},
      {3, [](nsDeque& d) { d.PopFront(); }, 1 + 2 + 3,
       "PopFront after 3rd loop"},

      {1, [](nsDeque& d) { d.Push(new int(4)); }, 1 + 2 + 3 + 4,
       "Push after 1st loop"},
      {2, [](nsDeque& d) { d.Push(new int(4)); }, 1 + 2 + 3 + 4,
       "Push after 2nd loop"},
      {3, [](nsDeque& d) { d.Push(new int(4)); }, 1 + 2 + 3 + 4,
       "Push after 3rd loop"},
      {4, [](nsDeque& d) { d.Push(new int(4)); }, 1 + 2 + 3,
       "Push after would-be-4th loop"},

      {1, [](nsDeque& d) { d.PushFront(new int(4)); }, 1 + 1 + 2 + 3,
       "PushFront after 1st loop"},
      {2, [](nsDeque& d) { d.PushFront(new int(4)); }, 1 + 2 + 2 + 3,
       "PushFront after 2nd loop"},
      {3, [](nsDeque& d) { d.PushFront(new int(4)); }, 1 + 2 + 3 + 3,
       "PushFront after 3rd loop"},
      {4, [](nsDeque& d) { d.PushFront(new int(4)); }, 1 + 2 + 3,
       "PushFront after would-be-4th loop"},

      {1, [](nsDeque& d) { d.Erase(); }, 1, "Erase after 1st loop"},
      {2, [](nsDeque& d) { d.Erase(); }, 1 + 2, "Erase after 2nd loop"},
      {3, [](nsDeque& d) { d.Erase(); }, 1 + 2 + 3, "Erase after 3rd loop"},

      {1,
       [](nsDeque& d) {
         d.Erase();
         d.Push(new int(4));
       },
       1, "Erase after 1st loop, Push 4"},
      {1,
       [](nsDeque& d) {
         d.Erase();
         d.Push(new int(4));
         d.Push(new int(5));
       },
       1 + 5, "Erase after 1st loop, Push 4,5"},
      {2,
       [](nsDeque& d) {
         d.Erase();
         d.Push(new int(4));
       },
       1 + 2, "Erase after 2nd loop, Push 4"},
      {2,
       [](nsDeque& d) {
         d.Erase();
         d.Push(new int(4));
         d.Push(new int(5));
       },
       1 + 2, "Erase after 2nd loop, Push 4,5"},
      {2,
       [](nsDeque& d) {
         d.Erase();
         d.Push(new int(4));
         d.Push(new int(5));
         d.Push(new int(6));
       },
       1 + 2 + 6, "Erase after 2nd loop, Push 4,5,6"}};

  for (const Test& test : tests) {
    nsDeque d(new Deallocator());

    for (size_t i = 0; i < NumTestValues; ++i) {
      d.Push(new int(i + 1));
    }

    static_assert(IsSame<nsDeque::ConstIterator, decltype(d.begin())>::value,
                  "(non-const nsDeque).begin() should return ConstIterator");
    static_assert(IsSame<nsDeque::ConstIterator, decltype(d.end())>::value,
                  "(non-const nsDeque).end() should return ConstIterator");

    int sum = 0;
    size_t loopCount = 0;
    for (void* ob : d) {
      sum += *static_cast<int*>(ob);
      if (++loopCount == test.runAfterLoopCount) {
        test.function(d);
      }
    }
    EXPECT_EQ(test.expectedSum, sum)
        << "Range-for should iterate over values in test '" << test.description
        << "'";
  }
}