Bug 1199400 - Part 2: Add tests for possible nsDeque corner cases. r=froydnj
authorEric Rahm <erahm@mozilla.com>
Fri, 04 Sep 2015 15:05:01 -0700
changeset 293650 2b1e7c1466c562c917d02f2b8df9c6fa79dbe15a
parent 293649 7fd9619376872691295a9ed787c281e77be3481a
child 293651 187273e992bfbb9463ba08366a61df20c7c148ed
push id5245
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:30:51 +0000
treeherdermozilla-beta@dac831dc1bd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1199400
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1199400 - Part 2: Add tests for possible nsDeque corner cases. r=froydnj
xpcom/tests/TestDeque.cpp
--- a/xpcom/tests/TestDeque.cpp
+++ b/xpcom/tests/TestDeque.cpp
@@ -14,16 +14,18 @@
 class _TestDeque {
 public:
   int Test();
 private:
   int OriginalTest();
   int OriginalFlaw();
   int AssignFlaw();
   int TestRemove();
+  int TestPushFront();
+  int TestEmpty();
 };
 
 class _Dealloc: public nsDequeFunctor {
   virtual void* operator()(void* aObject) {
     return 0;
   }
 };
 
@@ -39,16 +41,18 @@ class _Dealloc: public nsDequeFunctor {
  */
 int _TestDeque::Test() {
   /* the old deque should have failed a bunch of these tests */
   int results=0;
   results+=OriginalTest();
   results+=OriginalFlaw();
   results+=AssignFlaw();
   results+=TestRemove();
+  results+=TestPushFront();
+  results+=TestEmpty();
   return results;
 }
 
 int _TestDeque::OriginalTest() {
   const int size = 200;
   int ints[size];
   int i=0;
   int temp;
@@ -224,17 +228,91 @@ int _TestDeque::TestRemove() {
   //  d == [0,1,2,4] // (2==mOrigin)
   static const int t5[] = {0,1,2,4};
   TEST(VerifyContents(d, t5, 4), "verify contents t5");
 
 
   return 0;
 }
 
+int _TestDeque::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 int kMaxSizeBeforeGrowth = 8;
+
+  int pool[kPoolSize];
+  for (int i = 0; i < kPoolSize; i++) {
+    pool[i] = i;
+  }
+
+  for (int i = 0; i < kMaxSizeBeforeGrowth; i++) {
+    d.PushFront(pool + i);
+  }
+
+  TEST(d.GetSize() == kMaxSizeBeforeGrowth, "verify size");
+
+  static const int t1[] = {7,6,5,4,3,2,1,0};
+  TEST(VerifyContents(d, t1, kMaxSizeBeforeGrowth), "verify pushfront 1");
+
+  // Now push one more so it grows
+  d.PushFront(pool + kMaxSizeBeforeGrowth);
+  TEST(d.GetSize() == kMaxSizeBeforeGrowth + 1, "verify size");
+
+  static const int t2[] = {8,7,6,5,4,3,2,1,0};
+  TEST(VerifyContents(d, t2, kMaxSizeBeforeGrowth + 1), "verify pushfront 2");
+
+  // And one more so that it wraps again
+  d.PushFront(pool + kMaxSizeBeforeGrowth + 1);
+  TEST(d.GetSize() == kMaxSizeBeforeGrowth + 2, "verify size");
+
+  static const int t3[] = {9,8,7,6,5,4,3,2,1,0};
+  TEST(VerifyContents(d, t3, kMaxSizeBeforeGrowth + 2), "verify pushfront 3");
+
+  return 0;
+}
+
+int _TestDeque::TestEmpty() {
+  // Make sure nsDeque gives sane results if it's empty.
+  nsDeque d;
+
+  TEST(d.GetSize() == 0, "Size should be 0");
+  TEST(d.Pop() == nullptr, "Invalid operation should return nullptr");
+  TEST(d.PopFront() == nullptr, "Invalid operation should return nullptr");
+  TEST(d.Peek() == nullptr, "Invalid operation should return nullptr");
+  TEST(d.PeekFront() == nullptr, "Invalid operation should return nullptr");
+  TEST(d.ObjectAt(0) == nullptr, "Invalid operation should return nullptr");
+  TEST(d.Last() == nullptr, "Invalid operation should return nullptr");
+
+  // Fill it up and drain it.
+  for (size_t i = 0; i < 8; i++) {
+    d.Push((void*)0xAA);
+  }
+
+  for (size_t i = 0; i < 8; i++) {
+    (void)d.Pop();
+  }
+
+  // Now check it again.
+  TEST(d.GetSize() == 0, "Size should be 0");
+  TEST(d.Pop() == nullptr, "Invalid operation should return nullptr");
+  TEST(d.PopFront() == nullptr, "Invalid operation should return nullptr");
+  TEST(d.Peek() == nullptr, "Invalid operation should return nullptr");
+  TEST(d.PeekFront() == nullptr, "Invalid operation should return nullptr");
+  TEST(d.ObjectAt(0) == nullptr, "Invalid operation should return nullptr");
+  TEST(d.Last() == nullptr, "Invalid operation should return nullptr");
+
+  return 0;
+}
+
 int main (void) {
-  ScopedXPCOM xpcom("TestTimers");
+  ScopedXPCOM xpcom("TestDeque");
   NS_ENSURE_FALSE(xpcom.failed(), 1);
 
   _TestDeque test;
   int result = test.Test();
   TEST(result == 0, "All tests pass");
   return 0;
 }