2010-03-15 14:38:50 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
// vim:cindent:ts=4:et:sw=4:
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2010-03-15 14:38:50 +03:00
|
|
|
|
|
|
|
#include "TestHarness.h"
|
|
|
|
#include "nsCOMArray.h"
|
2012-06-20 07:41:56 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2010-03-15 14:38:50 +03:00
|
|
|
|
|
|
|
// {9e70a320-be02-11d1-8031-006008159b5a}
|
|
|
|
#define NS_IFOO_IID \
|
|
|
|
{0x9e70a320, 0xbe02, 0x11d1, \
|
|
|
|
{0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
|
|
|
|
|
|
|
|
class IFoo : public nsISupports {
|
|
|
|
public:
|
|
|
|
|
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID)
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHOD_(MozExternalRefCountType) RefCnt() = 0;
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHOD_(int32_t) ID() = 0;
|
2010-03-15 14:38:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(IFoo, NS_IFOO_IID)
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class Foo final : public IFoo {
|
2014-07-01 02:11:53 +04:00
|
|
|
~Foo();
|
|
|
|
|
2010-03-15 14:38:50 +03:00
|
|
|
public:
|
|
|
|
|
2014-08-05 17:36:59 +04:00
|
|
|
explicit Foo(int32_t aID);
|
2010-03-15 14:38:50 +03:00
|
|
|
|
|
|
|
// nsISupports implementation
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// IFoo implementation
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD_(MozExternalRefCountType) RefCnt() override { return mRefCnt; }
|
|
|
|
NS_IMETHOD_(int32_t) ID() override { return mID; }
|
2010-03-15 14:38:50 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t gCount;
|
2010-03-15 14:38:50 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mID;
|
2010-03-15 14:38:50 +03:00
|
|
|
};
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t Foo::gCount = 0;
|
2010-03-15 14:38:50 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
Foo::Foo(int32_t aID)
|
2010-03-15 14:38:50 +03:00
|
|
|
{
|
|
|
|
mID = aID;
|
|
|
|
++gCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
Foo::~Foo()
|
|
|
|
{
|
|
|
|
--gCount;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(Foo, IFoo)
|
2010-03-15 14:38:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
typedef nsCOMArray<IFoo> Array;
|
|
|
|
|
|
|
|
|
2010-06-09 22:13:16 +04:00
|
|
|
// {0e70a320-be02-11d1-8031-006008159b5a}
|
|
|
|
#define NS_IBAR_IID \
|
|
|
|
{0x0e70a320, 0xbe02, 0x11d1, \
|
|
|
|
{0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
|
|
|
|
|
|
|
|
class IBar : public nsISupports {
|
|
|
|
public:
|
|
|
|
|
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_IBAR_IID)
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(IBar, NS_IBAR_IID)
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class Bar final : public IBar {
|
2010-06-09 22:13:16 +04:00
|
|
|
public:
|
|
|
|
|
2011-04-14 18:18:02 +04:00
|
|
|
explicit Bar(nsCOMArray<IBar>& aArray);
|
2010-06-09 22:13:16 +04:00
|
|
|
|
|
|
|
// nsISupports implementation
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t sReleaseCalled;
|
2010-06-09 22:13:16 +04:00
|
|
|
|
|
|
|
private:
|
2014-07-01 02:11:53 +04:00
|
|
|
~Bar();
|
|
|
|
|
2010-06-09 22:13:16 +04:00
|
|
|
nsCOMArray<IBar>& mArray;
|
|
|
|
};
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t Bar::sReleaseCalled = 0;
|
2010-06-09 22:13:16 +04:00
|
|
|
|
|
|
|
typedef nsCOMArray<IBar> Array2;
|
|
|
|
|
2011-04-14 18:18:02 +04:00
|
|
|
Bar::Bar(Array2& aArray)
|
2010-06-09 22:13:16 +04:00
|
|
|
: mArray(aArray)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Bar::~Bar()
|
|
|
|
{
|
2011-04-14 18:18:02 +04:00
|
|
|
if (mArray.RemoveObject(this)) {
|
2010-06-09 22:13:16 +04:00
|
|
|
fail("We should never manage to remove the object here");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(Bar)
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE(Bar, IBar)
|
2010-06-09 22:13:16 +04:00
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2010-06-09 22:13:16 +04:00
|
|
|
Bar::Release(void)
|
|
|
|
{
|
|
|
|
++Bar::sReleaseCalled;
|
2013-02-08 08:50:05 +04:00
|
|
|
MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release");
|
2010-06-09 22:13:16 +04:00
|
|
|
NS_ASSERT_OWNINGTHREAD(_class);
|
|
|
|
--mRefCnt;
|
|
|
|
NS_LOG_RELEASE(this, mRefCnt, "Bar");
|
|
|
|
if (mRefCnt == 0) {
|
|
|
|
mRefCnt = 1; /* stabilize */
|
2010-07-05 13:42:18 +04:00
|
|
|
delete this;
|
2010-06-09 22:13:16 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return mRefCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-15 14:38:50 +03:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
ScopedXPCOM xpcom("nsCOMArrayTests");
|
|
|
|
if (xpcom.failed()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
Array arr;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < 20; ++i) {
|
2010-03-15 14:38:50 +03:00
|
|
|
nsCOMPtr<IFoo> foo = new Foo(i);
|
|
|
|
arr.AppendObject(foo);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arr.Count() != 20 || Foo::gCount != 20) {
|
|
|
|
fail("nsCOMArray::AppendObject failed");
|
|
|
|
rv = 1;
|
|
|
|
}
|
|
|
|
|
2013-02-05 02:05:40 +04:00
|
|
|
arr.TruncateLength(10);
|
2010-03-15 14:38:50 +03:00
|
|
|
|
|
|
|
if (arr.Count() != 10 || Foo::gCount != 10) {
|
2013-02-05 02:05:40 +04:00
|
|
|
fail("nsCOMArray::TruncateLength shortening of array failed");
|
2010-03-15 14:38:50 +03:00
|
|
|
rv = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
arr.SetCount(30);
|
|
|
|
|
|
|
|
if (arr.Count() != 30 || Foo::gCount != 10) {
|
|
|
|
fail("nsCOMArray::SetCount lengthening of array failed");
|
|
|
|
rv = 1;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < 10; ++i) {
|
2012-07-30 18:20:58 +04:00
|
|
|
if (arr[i] == nullptr) {
|
2010-03-15 14:38:50 +03:00
|
|
|
fail("nsCOMArray elements should be non-null");
|
|
|
|
rv = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 10; i < 30; ++i) {
|
2012-07-30 18:20:58 +04:00
|
|
|
if (arr[i] != nullptr) {
|
2010-03-15 14:38:50 +03:00
|
|
|
fail("nsCOMArray elements should be null");
|
|
|
|
rv = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t base;
|
2010-06-09 22:13:16 +04:00
|
|
|
{
|
|
|
|
Array2 arr2;
|
|
|
|
|
2014-01-16 17:26:00 +04:00
|
|
|
IBar *thirdObject = nullptr,
|
|
|
|
*fourthObject = nullptr,
|
|
|
|
*fifthObject = nullptr,
|
|
|
|
*ninthObject = nullptr;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < 20; ++i) {
|
2011-04-14 18:18:02 +04:00
|
|
|
nsCOMPtr<IBar> bar = new Bar(arr2);
|
|
|
|
switch (i) {
|
|
|
|
case 2:
|
|
|
|
thirdObject = bar; break;
|
|
|
|
case 3:
|
|
|
|
fourthObject = bar; break;
|
|
|
|
case 4:
|
|
|
|
fifthObject = bar; break;
|
|
|
|
case 8:
|
|
|
|
ninthObject = bar; break;
|
2010-06-09 22:13:16 +04:00
|
|
|
}
|
|
|
|
arr2.AppendObject(bar);
|
|
|
|
}
|
|
|
|
|
|
|
|
base = Bar::sReleaseCalled;
|
|
|
|
|
|
|
|
arr2.SetCount(10);
|
|
|
|
if (Bar::sReleaseCalled != base + 10) {
|
|
|
|
fail("Release called multiple times for SetCount");
|
|
|
|
}
|
2011-04-14 18:18:02 +04:00
|
|
|
if (arr2.Count() != 10) {
|
|
|
|
fail("SetCount(10) should remove exactly ten objects");
|
|
|
|
}
|
2010-06-09 22:13:16 +04:00
|
|
|
|
|
|
|
arr2.RemoveObjectAt(9);
|
|
|
|
if (Bar::sReleaseCalled != base + 11) {
|
|
|
|
fail("Release called multiple times for RemoveObjectAt");
|
|
|
|
}
|
2011-04-14 18:18:02 +04:00
|
|
|
if (arr2.Count() != 9) {
|
|
|
|
fail("RemoveObjectAt should remove exactly one object");
|
|
|
|
}
|
2010-06-09 22:13:16 +04:00
|
|
|
|
|
|
|
arr2.RemoveObject(ninthObject);
|
|
|
|
if (Bar::sReleaseCalled != base + 12) {
|
|
|
|
fail("Release called multiple times for RemoveObject");
|
|
|
|
}
|
2011-04-14 18:18:02 +04:00
|
|
|
if (arr2.Count() != 8) {
|
|
|
|
fail("RemoveObject should remove exactly one object");
|
|
|
|
}
|
|
|
|
|
|
|
|
arr2.RemoveObjectsAt(2, 3);
|
|
|
|
if (Bar::sReleaseCalled != base + 15) {
|
|
|
|
fail("Release called more or less than three times for RemoveObjectsAt");
|
|
|
|
}
|
|
|
|
if (arr2.Count() != 5) {
|
|
|
|
fail("RemoveObjectsAt should remove exactly three objects");
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t j = 0; j < arr2.Count(); ++j) {
|
2011-04-14 18:18:02 +04:00
|
|
|
if (arr2.ObjectAt(j) == thirdObject) {
|
|
|
|
fail("RemoveObjectsAt should have removed thirdObject");
|
|
|
|
}
|
|
|
|
if (arr2.ObjectAt(j) == fourthObject) {
|
|
|
|
fail("RemoveObjectsAt should have removed fourthObject");
|
|
|
|
}
|
|
|
|
if (arr2.ObjectAt(j) == fifthObject) {
|
|
|
|
fail("RemoveObjectsAt should have removed fifthObject");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
arr2.RemoveObjectsAt(4, 1);
|
|
|
|
if (Bar::sReleaseCalled != base + 16) {
|
|
|
|
fail("Release called more or less than one time for RemoveObjectsAt");
|
|
|
|
}
|
|
|
|
if (arr2.Count() != 4) {
|
|
|
|
fail("RemoveObjectsAt should work for removing the last element");
|
|
|
|
}
|
2010-06-09 22:13:16 +04:00
|
|
|
|
|
|
|
arr2.Clear();
|
|
|
|
if (Bar::sReleaseCalled != base + 20) {
|
|
|
|
fail("Release called multiple times for Clear");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-05 02:05:40 +04:00
|
|
|
{
|
|
|
|
Array2 arr2;
|
|
|
|
|
2014-01-16 17:26:00 +04:00
|
|
|
IBar *thirdElement = nullptr,
|
|
|
|
*fourthElement = nullptr,
|
|
|
|
*fifthElement = nullptr,
|
|
|
|
*ninthElement = nullptr;
|
2013-02-05 02:05:40 +04:00
|
|
|
for (int32_t i = 0; i < 20; ++i) {
|
|
|
|
nsCOMPtr<IBar> bar = new Bar(arr2);
|
|
|
|
switch (i) {
|
|
|
|
case 2:
|
|
|
|
thirdElement = bar; break;
|
|
|
|
case 3:
|
|
|
|
fourthElement = bar; break;
|
|
|
|
case 4:
|
|
|
|
fifthElement = bar; break;
|
|
|
|
case 8:
|
|
|
|
ninthElement = bar; break;
|
|
|
|
}
|
|
|
|
arr2.AppendElement(bar);
|
|
|
|
}
|
|
|
|
|
|
|
|
base = Bar::sReleaseCalled;
|
|
|
|
|
|
|
|
arr2.TruncateLength(10);
|
|
|
|
if (Bar::sReleaseCalled != base + 10) {
|
|
|
|
fail("Release called multiple times for TruncateLength");
|
|
|
|
}
|
|
|
|
if (arr2.Length() != 10) {
|
|
|
|
fail("TruncateLength(10) should remove exactly ten objects");
|
|
|
|
}
|
|
|
|
|
|
|
|
arr2.RemoveElementAt(9);
|
|
|
|
if (Bar::sReleaseCalled != base + 11) {
|
|
|
|
fail("Release called multiple times for RemoveElementAt");
|
|
|
|
}
|
|
|
|
if (arr2.Length() != 9) {
|
|
|
|
fail("RemoveElementAt should remove exactly one object");
|
|
|
|
}
|
|
|
|
|
|
|
|
arr2.RemoveElement(ninthElement);
|
|
|
|
if (Bar::sReleaseCalled != base + 12) {
|
|
|
|
fail("Release called multiple times for RemoveElement");
|
|
|
|
}
|
|
|
|
if (arr2.Length() != 8) {
|
|
|
|
fail("RemoveElement should remove exactly one object");
|
|
|
|
}
|
|
|
|
|
|
|
|
arr2.RemoveElementsAt(2, 3);
|
|
|
|
if (Bar::sReleaseCalled != base + 15) {
|
|
|
|
fail("Release called more or less than three times for RemoveElementsAt");
|
|
|
|
}
|
|
|
|
if (arr2.Length() != 5) {
|
|
|
|
fail("RemoveElementsAt should remove exactly three objects");
|
|
|
|
}
|
|
|
|
for (uint32_t j = 0; j < arr2.Length(); ++j) {
|
|
|
|
if (arr2.ElementAt(j) == thirdElement) {
|
|
|
|
fail("RemoveElementsAt should have removed thirdElement");
|
|
|
|
}
|
|
|
|
if (arr2.ElementAt(j) == fourthElement) {
|
|
|
|
fail("RemoveElementsAt should have removed fourthElement");
|
|
|
|
}
|
|
|
|
if (arr2.ElementAt(j) == fifthElement) {
|
|
|
|
fail("RemoveElementsAt should have removed fifthElement");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
arr2.RemoveElementsAt(4, 1);
|
|
|
|
if (Bar::sReleaseCalled != base + 16) {
|
|
|
|
fail("Release called more or less than one time for RemoveElementsAt");
|
|
|
|
}
|
|
|
|
if (arr2.Length() != 4) {
|
|
|
|
fail("RemoveElementsAt should work for removing the last element");
|
|
|
|
}
|
|
|
|
|
|
|
|
arr2.Clear();
|
|
|
|
if (Bar::sReleaseCalled != base + 20) {
|
|
|
|
fail("Release called multiple times for Clear");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-09 22:13:16 +04:00
|
|
|
Bar::sReleaseCalled = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
Array2 arr2;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < 20; ++i) {
|
2011-04-14 18:18:02 +04:00
|
|
|
nsCOMPtr<IBar> bar = new Bar(arr2);
|
2010-06-09 22:13:16 +04:00
|
|
|
arr2.AppendObject(bar);
|
|
|
|
}
|
|
|
|
|
|
|
|
base = Bar::sReleaseCalled;
|
|
|
|
|
|
|
|
// Let arr2 be destroyed
|
|
|
|
}
|
|
|
|
if (Bar::sReleaseCalled != base + 20) {
|
|
|
|
fail("Release called multiple times for nsCOMArray::~nsCOMArray");
|
|
|
|
}
|
|
|
|
|
2010-03-15 14:38:50 +03:00
|
|
|
return rv;
|
|
|
|
}
|