michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include "nsISupportsArray.h" michael@0: michael@0: // {9e70a320-be02-11d1-8031-006008159b5a} michael@0: #define NS_IFOO_IID \ michael@0: {0x9e70a320, 0xbe02, 0x11d1, \ michael@0: {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}} michael@0: michael@0: namespace TestArray { michael@0: michael@0: static const bool kExitOnError = true; michael@0: michael@0: class IFoo : public nsISupports { michael@0: public: michael@0: michael@0: NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID) michael@0: michael@0: NS_IMETHOD_(nsrefcnt) RefCnt() = 0; michael@0: NS_IMETHOD_(int32_t) ID() = 0; michael@0: }; michael@0: michael@0: NS_DEFINE_STATIC_IID_ACCESSOR(IFoo, NS_IFOO_IID) michael@0: michael@0: class Foo : public IFoo { michael@0: public: michael@0: michael@0: Foo(int32_t aID); michael@0: michael@0: // nsISupports implementation michael@0: NS_DECL_ISUPPORTS michael@0: michael@0: // IFoo implementation michael@0: NS_IMETHOD_(nsrefcnt) RefCnt() { return mRefCnt; } michael@0: NS_IMETHOD_(int32_t) ID() { return mID; } michael@0: michael@0: static int32_t gCount; michael@0: michael@0: int32_t mID; michael@0: michael@0: private: michael@0: ~Foo(); michael@0: }; michael@0: michael@0: int32_t Foo::gCount; michael@0: michael@0: Foo::Foo(int32_t aID) michael@0: { michael@0: mID = aID; michael@0: ++gCount; michael@0: fprintf(stdout, "init: %d (%p), %d total)\n", michael@0: mID, static_cast(this), gCount); michael@0: } michael@0: michael@0: Foo::~Foo() michael@0: { michael@0: --gCount; michael@0: fprintf(stdout, "destruct: %d (%p), %d remain)\n", michael@0: mID, static_cast(this), gCount); michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(Foo, IFoo) michael@0: michael@0: const char* AssertEqual(int32_t aValue1, int32_t aValue2) michael@0: { michael@0: if (aValue1 == aValue2) { michael@0: return "OK"; michael@0: } michael@0: if (kExitOnError) { michael@0: exit(1); michael@0: } michael@0: return "ERROR"; michael@0: } michael@0: michael@0: void DumpArray(nsISupportsArray* aArray, int32_t aExpectedCount, int32_t aElementIDs[], int32_t aExpectedTotal) michael@0: { michael@0: uint32_t cnt = 0; michael@0: #ifdef DEBUG michael@0: nsresult rv = michael@0: #endif michael@0: aArray->Count(&cnt); michael@0: NS_ASSERTION(NS_SUCCEEDED(rv), "Count failed"); michael@0: int32_t count = cnt; michael@0: int32_t index; michael@0: michael@0: fprintf(stdout, "object count %d = %d %s\n", Foo::gCount, aExpectedTotal, michael@0: AssertEqual(Foo::gCount, aExpectedTotal)); michael@0: fprintf(stdout, "array count %d = %d %s\n", count, aExpectedCount, michael@0: AssertEqual(count, aExpectedCount)); michael@0: michael@0: for (index = 0; (index < count) && (index < aExpectedCount); index++) { michael@0: IFoo* foo = (IFoo*)(aArray->ElementAt(index)); michael@0: fprintf(stdout, "%2d: %d=%d (%p) c: %d %s\n", michael@0: index, aElementIDs[index], foo->ID(), michael@0: static_cast(foo), foo->RefCnt() - 1, michael@0: AssertEqual(foo->ID(), aElementIDs[index])); michael@0: foo->Release(); michael@0: } michael@0: } michael@0: michael@0: void FillArray(nsISupportsArray* aArray, int32_t aCount) michael@0: { michael@0: int32_t index; michael@0: for (index = 0; index < aCount; index++) { michael@0: nsCOMPtr foo = new Foo(index); michael@0: aArray->AppendElement(foo); michael@0: } michael@0: } michael@0: michael@0: } michael@0: michael@0: using namespace TestArray; michael@0: michael@0: int main(int argc, char *argv[]) michael@0: { michael@0: nsISupportsArray* array; michael@0: nsresult rv; michael@0: michael@0: if (NS_OK == (rv = NS_NewISupportsArray(&array))) { michael@0: FillArray(array, 10); michael@0: fprintf(stdout, "Array created:\n"); michael@0: int32_t fillResult[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; michael@0: DumpArray(array, 10, fillResult, 10); michael@0: michael@0: // test insert michael@0: IFoo* foo = (IFoo*)array->ElementAt(3); michael@0: foo->Release(); // pre-release to fix ref count for dumps michael@0: array->InsertElementAt(foo, 5); michael@0: fprintf(stdout, "insert 3 at 5:\n"); michael@0: int32_t insertResult[11] = {0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9}; michael@0: DumpArray(array, 11, insertResult, 10); michael@0: fprintf(stdout, "insert 3 at 0:\n"); michael@0: array->InsertElementAt(foo, 0); michael@0: int32_t insertResult2[12] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9}; michael@0: DumpArray(array, 12, insertResult2, 10); michael@0: fprintf(stdout, "append 3:\n"); michael@0: array->AppendElement(foo); michael@0: int32_t appendResult[13] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9, 3}; michael@0: DumpArray(array, 13, appendResult, 10); michael@0: michael@0: michael@0: // test IndexOf && LastIndexOf michael@0: int32_t expectedIndex[5] = {0, 4, 6, 12, -1}; michael@0: int32_t count = 0; michael@0: int32_t index = array->IndexOf(foo); michael@0: fprintf(stdout, "IndexOf(foo): %d=%d %s\n", index, expectedIndex[count], michael@0: AssertEqual(index, expectedIndex[count])); michael@0: while (-1 != index) { michael@0: count++; michael@0: index = array->IndexOfStartingAt(foo, index + 1); michael@0: if (-1 != index) michael@0: fprintf(stdout, "IndexOf(foo): %d=%d %s\n", index, expectedIndex[count], michael@0: AssertEqual(index, expectedIndex[count])); michael@0: } michael@0: index = array->LastIndexOf(foo); michael@0: count--; michael@0: fprintf(stdout, "LastIndexOf(foo): %d=%d %s\n", index, expectedIndex[count], michael@0: AssertEqual(index, expectedIndex[count])); michael@0: michael@0: // test ReplaceElementAt michael@0: fprintf(stdout, "ReplaceElementAt(8):\n"); michael@0: array->ReplaceElementAt(foo, 8); michael@0: int32_t replaceResult[13] = {3, 0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3}; michael@0: DumpArray(array, 13, replaceResult, 9); michael@0: michael@0: // test RemoveElementAt, RemoveElement RemoveLastElement michael@0: fprintf(stdout, "RemoveElementAt(0):\n"); michael@0: array->RemoveElementAt(0); michael@0: int32_t removeResult[12] = {0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3}; michael@0: DumpArray(array, 12, removeResult, 9); michael@0: fprintf(stdout, "RemoveElementAt(7):\n"); michael@0: array->RemoveElementAt(7); michael@0: int32_t removeResult2[11] = {0, 1, 2, 3, 4, 3, 5, 7, 8, 9, 3}; michael@0: DumpArray(array, 11, removeResult2, 9); michael@0: fprintf(stdout, "RemoveElement(foo):\n"); michael@0: array->RemoveElement(foo); michael@0: int32_t removeResult3[10] = {0, 1, 2, 4, 3, 5, 7, 8, 9, 3}; michael@0: DumpArray(array, 10, removeResult3, 9); michael@0: fprintf(stdout, "RemoveLastElement(foo):\n"); michael@0: array->RemoveLastElement(foo); michael@0: int32_t removeResult4[9] = {0, 1, 2, 4, 3, 5, 7, 8, 9}; michael@0: DumpArray(array, 9, removeResult4, 9); michael@0: michael@0: // test clear michael@0: fprintf(stdout, "clear array:\n"); michael@0: array->Clear(); michael@0: DumpArray(array, 0, 0, 0); michael@0: fprintf(stdout, "add 4 new:\n"); michael@0: FillArray(array, 4); michael@0: DumpArray(array, 4, fillResult, 4); michael@0: michael@0: // test compact michael@0: fprintf(stdout, "compact array:\n"); michael@0: array->Compact(); michael@0: DumpArray(array, 4, fillResult, 4); michael@0: michael@0: // test delete michael@0: fprintf(stdout, "release array:\n"); michael@0: NS_RELEASE(array); michael@0: } michael@0: else { michael@0: fprintf(stdout, "error can't create array: %x\n", rv); michael@0: } michael@0: michael@0: return 0; michael@0: }