From ec49433a687241ee93ce29cef4bbf1baddd23f0c Mon Sep 17 00:00:00 2001 From: Chris Pearce Date: Thu, 19 Jan 2012 09:15:56 +1300 Subject: [PATCH] Bug 713381 - Add nsDeque::RemoveObjectAt(index). r=bsmedberg --- xpcom/glue/nsDeque.cpp | 16 +++ xpcom/glue/nsDeque.h | 8 ++ xpcom/tests/Makefile.in | 2 +- xpcom/tests/TestDeque.cpp | 214 +++++++++++++++++++++++++++++--------- 4 files changed, 191 insertions(+), 49 deletions(-) diff --git a/xpcom/glue/nsDeque.cpp b/xpcom/glue/nsDeque.cpp index e451ca20774..70f0f6c9979 100644 --- a/xpcom/glue/nsDeque.cpp +++ b/xpcom/glue/nsDeque.cpp @@ -350,6 +350,22 @@ void* nsDeque::ObjectAt(PRInt32 aIndex) const { return result; } +void* nsDeque::RemoveObjectAt(PRInt32 aIndex) { + if ((aIndex<0) || (aIndex>=mSize)) { + return 0; + } + void* result=mData[modulus(mOrigin + aIndex, mCapacity)]; + + // "Shuffle down" all elements in the array by 1, overwritting the element + // being removed. + for (PRInt32 i=aIndex; i @@ -44,13 +45,12 @@ **************************************************************/ class _TestDeque { public: - _TestDeque() { - SelfTest(); - } - int SelfTest(); - nsresult OriginalTest(); - nsresult OriginalFlaw(); - nsresult AssignFlaw(); + int Test(); +private: + int OriginalTest(); + int OriginalFlaw(); + int AssignFlaw(); + int TestRemove(); }; static _TestDeque sTestDeque; @@ -60,96 +60,214 @@ class _Dealloc: public nsDequeFunctor { } }; +#define TEST(aCondition, aMsg) \ + if (!(aCondition)) { fail("TestDeque: "#aMsg); return 1; } + + /** * conduct automated self test for this class * * @param * @return */ -int _TestDeque::SelfTest() { +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(); return results; } -nsresult _TestDeque::OriginalTest() { - int ints[200]; - int count=sizeof(ints)/sizeof(int); +int _TestDeque::OriginalTest() { + const int size = 200; + int ints[size]; int i=0; - int* temp; + int temp; nsDeque theDeque(new _Dealloc); //construct a simple one... - for (i=0;icapacity\n"); + TEST(d.GetSize() == 6, "OriginalFlaw size check #1"); - /* Oh, I see ... it's a circular buffer */ - printf("but the old code wasn't behaving accordingly.\n"); + 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"); - /*right*/ - printf("we shouldn't crash or anything interesting, "); + for (i=0; i<4; i++) { + d.Push(&ints[6 + i]); + } + // d = [4...9] - temp=(int*)secondDeque.Peek(); - printf("peek: %d\n",*temp); - return NS_OK; + for (i=4; i<=9; i++) { + temp=*(int*)d.PopFront(); + TEST(temp == i, "OriginalFlaw empty check"); + } + + return 0; } -nsresult _TestDeque::AssignFlaw() { +int _TestDeque::AssignFlaw() { nsDeque src(new _Dealloc),dest(new _Dealloc); - return NS_OK; + return 0; +} + +static bool VerifyContents(const nsDeque& aDeque, const int* aContents, int aLength) { + for (int i=0; i