mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-28 05:10:49 +00:00
Backed out changeset d84d5faa4852 (bug 1201997) for m2 perma failures
--HG-- rename : xpcom/glue/tests/gtest/TestNsDeque.cpp => xpcom/tests/TestDeque.cpp extra : rebase_source : 689453beeca1b317d9b2f57889ba1b133b628821
This commit is contained in:
parent
c6da644f91
commit
6d8a42769f
@ -29,6 +29,7 @@ skip-if = os == 'b2g' || (os == 'android' && debug) # Bug 1054246
|
||||
[TestDeadlockDetector]
|
||||
skip-if = os == 'b2g' || (os == 'android' && debug) # Bug 1054249
|
||||
[TestDeadlockDetectorScalability]
|
||||
[TestDeque]
|
||||
[TestDllInterceptor]
|
||||
skip-if = os != 'win'
|
||||
[TestEndian]
|
||||
|
@ -1,454 +0,0 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* 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 <stdio.h>
|
||||
|
||||
/**************************************************************
|
||||
Now define the token deallocator class...
|
||||
**************************************************************/
|
||||
namespace TestNsDeque {
|
||||
|
||||
class _Dealloc: public nsDequeFunctor
|
||||
{
|
||||
virtual void* operator()(void* aObject) {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
static bool VerifyContents(const nsDeque& aDeque, const int* aContents, int aLength)
|
||||
{
|
||||
for (int 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;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
class ForEachAdder: public nsDequeFunctor
|
||||
{
|
||||
virtual void* operator()(void* aObject)
|
||||
{
|
||||
if (aObject)
|
||||
{
|
||||
sum += *(int*)aObject;
|
||||
}
|
||||
|
||||
return aObject;
|
||||
}
|
||||
|
||||
private:
|
||||
int sum = 0;
|
||||
|
||||
public:
|
||||
int GetSum() { return sum; }
|
||||
|
||||
};
|
||||
|
||||
|
||||
class NumberSevenFinder: public nsDequeFunctor
|
||||
{
|
||||
virtual void* operator()(void* aObject)
|
||||
{
|
||||
if (aObject)
|
||||
{
|
||||
int value = *(int*)aObject;
|
||||
if (value == 7)
|
||||
{
|
||||
return (int*)1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
using namespace TestNsDeque;
|
||||
|
||||
TEST(NsDeque, OriginalTest)
|
||||
{
|
||||
const int size = 200;
|
||||
int ints[size];
|
||||
int 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]=i;
|
||||
}
|
||||
// queue = [0...69]
|
||||
for (i=0;i<70;i++) {
|
||||
theDeque.Push(&ints[i]);
|
||||
temp=*(int*)theDeque.Peek();
|
||||
EXPECT_EQ(i, temp) << "Verify end after push #1";
|
||||
EXPECT_EQ(i + 1, theDeque.GetSize()) << "Verify size after push #1";
|
||||
}
|
||||
|
||||
EXPECT_EQ(70,theDeque.GetSize()) << "Verify overall size after pushes #1";
|
||||
|
||||
// queue = [0...14]
|
||||
for (i=1;i<=55;i++) {
|
||||
temp=*(int*)theDeque.Pop();
|
||||
EXPECT_EQ(70-i,temp) << "Verify end after pop # 1";
|
||||
EXPECT_EQ(70 - i,theDeque.GetSize()) << "Verify size after pop # 1";
|
||||
}
|
||||
EXPECT_EQ(15,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(i,temp) << "Verify end after push #2";
|
||||
EXPECT_EQ(i + 15 + 1,theDeque.GetSize()) << "Verify size after push # 2";
|
||||
}
|
||||
EXPECT_EQ(70,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-i,temp ) << "Verify end after pop # 2";
|
||||
EXPECT_EQ(70 - i,theDeque.GetSize()) << "Verify size after pop #2";
|
||||
}
|
||||
EXPECT_EQ(35,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(i,temp) << "Verify end after push # 3";
|
||||
EXPECT_EQ(35 + 1 + 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 - i, temp) << "Verify end after pop # 3";
|
||||
}
|
||||
|
||||
// queue = [0...14]
|
||||
for (i=0;i<20;i++) {
|
||||
temp=*(int*)theDeque.Pop();
|
||||
EXPECT_EQ(19 - i,temp) << "Verify end after pop # 4";
|
||||
}
|
||||
|
||||
// queue = []
|
||||
for (i=0;i<15;i++) {
|
||||
temp=*(int*)theDeque.Pop();
|
||||
EXPECT_EQ(14 - i,temp) << "Verify end after pop # 5";
|
||||
}
|
||||
|
||||
EXPECT_EQ(0,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(6, 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(2, 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, TestRemove)
|
||||
{
|
||||
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 (int i=2; i<=5; i++) {
|
||||
int t = *(int*)d.ObjectAt(i-2);
|
||||
EXPECT_EQ(i,t) << "Verify ObjectAt()";
|
||||
}
|
||||
|
||||
d.RemoveObjectAt(1);
|
||||
// d == [2,4,5]
|
||||
static const int t1[] = {2,4,5};
|
||||
EXPECT_TRUE(VerifyContents(d, t1, 3)) << "verify contents t1";
|
||||
|
||||
d.PushFront(&ints[1]);
|
||||
d.PushFront(&ints[0]);
|
||||
d.PushFront(&ints[7]);
|
||||
d.PushFront(&ints[6]);
|
||||
// d == [6,7,0,1,2,4,5] // (0==mOrigin)
|
||||
static const int t2[] = {6,7,0,1,2,4,5};
|
||||
EXPECT_TRUE(VerifyContents(d, t2, 7)) << "verify contents t2";
|
||||
|
||||
d.RemoveObjectAt(1);
|
||||
// d == [6,0,1,2,4,5] // (1==mOrigin)
|
||||
static const int t3[] = {6,0,1,2,4,5};
|
||||
EXPECT_TRUE(VerifyContents(d, t3, 6)) << "verify contents t3";
|
||||
|
||||
d.RemoveObjectAt(5);
|
||||
// d == [6,0,1,2,4] // (1==mOrigin)
|
||||
static const int t4[] = {6,0,1,2,4};
|
||||
EXPECT_TRUE(VerifyContents(d, t4, 5)) << "verify contents t4";
|
||||
|
||||
d.RemoveObjectAt(0);
|
||||
// d == [0,1,2,4] // (2==mOrigin)
|
||||
static const int t5[] = {0,1,2,4};
|
||||
EXPECT_TRUE(VerifyContents(d, t5, 4)) << "verify contents t5";
|
||||
}
|
||||
|
||||
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 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);
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
TEST(NsDeque,TestEmpty)
|
||||
{
|
||||
// Make sure nsDeque gives sane results if it's empty.
|
||||
nsDeque d;
|
||||
size_t numberOfEntries = 8;
|
||||
|
||||
EXPECT_EQ(0, 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(0)) << "Invalid operation should return nullptr";
|
||||
EXPECT_EQ(nullptr, d.Last()) << "Invalid operation should return nullptr";
|
||||
|
||||
// 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.
|
||||
EXPECT_EQ(0, 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(0)) << "Invalid operation should return nullptr";
|
||||
EXPECT_EQ(nullptr, d.Last()) <<"Invalid operation should return nullptr";
|
||||
}
|
||||
|
||||
TEST(NsDeque,TestEmptyMethod)
|
||||
{
|
||||
nsDeque d;
|
||||
size_t numberOfEntries = 8;
|
||||
|
||||
// Fill it up before calling Empty
|
||||
for (size_t i = 0; i < numberOfEntries; i++) {
|
||||
d.Push((void*)0xAA);
|
||||
}
|
||||
|
||||
// Call empty
|
||||
d.Empty();
|
||||
|
||||
// Now check it again.
|
||||
EXPECT_EQ(0, 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(0)) << "Invalid operation should return nullptr";
|
||||
EXPECT_EQ(nullptr, d.Last()) <<"Invalid operation should return nullptr";
|
||||
|
||||
// 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.
|
||||
EXPECT_EQ(0, 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(0)) << "Invalid operation should return nullptr";
|
||||
EXPECT_EQ(nullptr, d.Last()) <<"Invalid operation should return nullptr";
|
||||
}
|
||||
|
||||
TEST(NsDeque,TestEraseShouldCallDeallocator)
|
||||
{
|
||||
nsDeque d(new Deallocator());
|
||||
const int 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.
|
||||
EXPECT_EQ(0, 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(0)) << "Invalid operation should return nullptr";
|
||||
EXPECT_EQ(nullptr, d.Last()) <<"Invalid operation should return nullptr";
|
||||
|
||||
for (size_t i=0; i < NumTestValues; i++)
|
||||
{
|
||||
EXPECT_EQ(-1, *(testArray[i])) << "Erase should call deallocator: " << *(testArray[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(NsDeque,TestEmptyShouldNotCallDeallocator)
|
||||
{
|
||||
nsDeque d(new Deallocator());
|
||||
const int 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.Empty();
|
||||
|
||||
// Now check it again.
|
||||
EXPECT_EQ(0, 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(0)) << "Invalid operation should return nullptr";
|
||||
EXPECT_EQ(nullptr, d.Last()) <<"Invalid operation should return nullptr";
|
||||
|
||||
for (size_t i=0; i < NumTestValues; i++)
|
||||
{
|
||||
EXPECT_EQ(i, *(testArray[i])) << "Empty should not call deallocator";
|
||||
}
|
||||
}
|
||||
|
||||
TEST(NsDeque, TestForEachAndFirstThat)
|
||||
{
|
||||
nsDeque d(new Deallocator());
|
||||
const int 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";
|
||||
|
||||
NumberSevenFinder finder;
|
||||
const int* value = (const int*)d.FirstThat(finder);
|
||||
EXPECT_EQ(7, *value) << "FirstThat should return value matching functor";
|
||||
|
||||
d.Erase();
|
||||
}
|
@ -8,7 +8,6 @@ UNIFIED_SOURCES += [
|
||||
'TestArray.cpp',
|
||||
'TestFileUtils.cpp',
|
||||
'TestGCPostBarriers.cpp',
|
||||
'TestNsDeque.cpp',
|
||||
'TestThreadUtils.cpp',
|
||||
]
|
||||
|
||||
|
318
xpcom/tests/TestDeque.cpp
Normal file
318
xpcom/tests/TestDeque.cpp
Normal file
@ -0,0 +1,318 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* 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 "TestHarness.h"
|
||||
#include "nsDeque.h"
|
||||
#include "nsCRT.h"
|
||||
#include <stdio.h>
|
||||
|
||||
/**************************************************************
|
||||
Now define the token deallocator class...
|
||||
**************************************************************/
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
#define TEST(aCondition, aMsg) \
|
||||
if (!(aCondition)) { fail("TestDeque: "#aMsg); return 1; }
|
||||
|
||||
|
||||
/**
|
||||
* conduct automated self test for this class
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
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;
|
||||
nsDeque theDeque(new _Dealloc); //construct a simple one...
|
||||
|
||||
// ints = [0...199]
|
||||
for (i=0;i<size;i++) { //initialize'em
|
||||
ints[i]=i;
|
||||
}
|
||||
// queue = [0...69]
|
||||
for (i=0;i<70;i++) {
|
||||
theDeque.Push(&ints[i]);
|
||||
temp=*(int*)theDeque.Peek();
|
||||
TEST(temp == i, "Verify end after push #1");
|
||||
TEST(theDeque.GetSize() == i + 1, "Verify size after push #1");
|
||||
}
|
||||
TEST(theDeque.GetSize() == 70, "Verify overall size after pushes #1");
|
||||
// queue = [0...14]
|
||||
for (i=1;i<=55;i++) {
|
||||
temp=*(int*)theDeque.Pop();
|
||||
TEST(temp == 70-i, "Verify end after pop # 1");
|
||||
TEST(theDeque.GetSize() == 70 - i, "Verify size after pop # 1");
|
||||
}
|
||||
TEST(theDeque.GetSize() == 15, "Verify overall size after pops");
|
||||
|
||||
// queue = [0...14,0...54]
|
||||
for (i=0;i<55;i++) {
|
||||
theDeque.Push(&ints[i]);
|
||||
temp=*(int*)theDeque.Peek();
|
||||
TEST(temp == i, "Verify end after push #2");
|
||||
TEST(theDeque.GetSize() == i + 15 + 1, "Verify size after push # 2");
|
||||
}
|
||||
TEST(theDeque.GetSize() == 70, "Verify size after end of all pushes #2");
|
||||
|
||||
// queue = [0...14,0...19]
|
||||
for (i=1;i<=35;i++) {
|
||||
temp=*(int*)theDeque.Pop();
|
||||
TEST(temp == 55-i, "Verify end after pop # 2");
|
||||
TEST(theDeque.GetSize() == 70 - i, "Verify size after pop #2");
|
||||
}
|
||||
TEST(theDeque.GetSize() == 35, "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();
|
||||
TEST(temp == i, "Verify end after push # 3");
|
||||
TEST(theDeque.GetSize() == 35 + 1 + i, "Verify size after push #3");
|
||||
}
|
||||
|
||||
// queue = [0...14,0...19]
|
||||
for (i=0;i<35;i++) {
|
||||
temp=*(int*)theDeque.Pop();
|
||||
TEST(temp == 34 - i, "Verify end after pop # 3");
|
||||
}
|
||||
|
||||
// queue = [0...14]
|
||||
for (i=0;i<20;i++) {
|
||||
temp=*(int*)theDeque.Pop();
|
||||
TEST(temp == 19 - i, "Verify end after pop # 4");
|
||||
}
|
||||
|
||||
// queue = []
|
||||
for (i=0;i<15;i++) {
|
||||
temp=*(int*)theDeque.Pop();
|
||||
TEST(temp == 14 - i, "Verify end after pop # 5");
|
||||
}
|
||||
|
||||
TEST(theDeque.GetSize() == 0, "Deque should finish empty.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _TestDeque::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
|
||||
*/
|
||||
printf("fill array\n");
|
||||
for (i=0; i<30; i++)
|
||||
ints[i]=i;
|
||||
|
||||
for (i=0; i<6; i++) {
|
||||
d.Push(&ints[i]);
|
||||
temp = *(int*)d.Peek();
|
||||
TEST(temp == i, "OriginalFlaw push #1");
|
||||
}
|
||||
TEST(d.GetSize() == 6, "OriginalFlaw size check #1");
|
||||
|
||||
for (i=0; i<4; i++) {
|
||||
temp=*(int*)d.PopFront();
|
||||
TEST(temp == i, "PopFront test");
|
||||
}
|
||||
// d = [4,5]
|
||||
TEST(d.GetSize() == 2, "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();
|
||||
TEST(temp == i, "OriginalFlaw empty check");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _TestDeque::AssignFlaw() {
|
||||
nsDeque src(new _Dealloc),dest(new _Dealloc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool VerifyContents(const nsDeque& aDeque, const int* aContents, int aLength) {
|
||||
for (int i=0; i<aLength; ++i) {
|
||||
if (*(int*)aDeque.ObjectAt(i) != aContents[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int _TestDeque::TestRemove() {
|
||||
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 (int i=2; i<=5; i++) {
|
||||
int t = *(int*)d.ObjectAt(i-2);
|
||||
TEST(t == i, "Verify ObjectAt()");
|
||||
}
|
||||
|
||||
d.RemoveObjectAt(1);
|
||||
// d == [2,4,5]
|
||||
static const int t1[] = {2,4,5};
|
||||
TEST(VerifyContents(d, t1, 3), "verify contents t1");
|
||||
|
||||
d.PushFront(&ints[1]);
|
||||
d.PushFront(&ints[0]);
|
||||
d.PushFront(&ints[7]);
|
||||
d.PushFront(&ints[6]);
|
||||
// d == [6,7,0,1,2,4,5] // (0==mOrigin)
|
||||
static const int t2[] = {6,7,0,1,2,4,5};
|
||||
TEST(VerifyContents(d, t2, 7), "verify contents t2");
|
||||
|
||||
d.RemoveObjectAt(1);
|
||||
// d == [6,0,1,2,4,5] // (1==mOrigin)
|
||||
static const int t3[] = {6,0,1,2,4,5};
|
||||
TEST(VerifyContents(d, t3, 6), "verify contents t3");
|
||||
|
||||
d.RemoveObjectAt(5);
|
||||
// d == [6,0,1,2,4] // (1==mOrigin)
|
||||
static const int t4[] = {6,0,1,2,4};
|
||||
TEST(VerifyContents(d, t4, 5), "verify contents t4");
|
||||
|
||||
d.RemoveObjectAt(0);
|
||||
// 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("TestDeque");
|
||||
NS_ENSURE_FALSE(xpcom.failed(), 1);
|
||||
|
||||
_TestDeque test;
|
||||
int result = test.Test();
|
||||
TEST(result == 0, "All tests pass");
|
||||
return 0;
|
||||
}
|
@ -56,6 +56,7 @@ GeckoCppUnitTests([
|
||||
'TestCOMArray',
|
||||
'TestCOMPtr',
|
||||
'TestCOMPtrEq',
|
||||
'TestDeque',
|
||||
'TestFile',
|
||||
'TestHashtables',
|
||||
'TestID',
|
||||
|
Loading…
x
Reference in New Issue
Block a user