1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 // vim:cindent:ts=4:et:sw=4:
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is the Mozilla Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "TestHarness.h"
39 #include "nsCOMArray.h"
41 // {9e70a320-be02-11d1-8031-006008159b5a}
43 {0x9e70a320, 0xbe02, 0x11d1, \
44 {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
46 class IFoo
: public nsISupports
{
49 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID
)
51 NS_IMETHOD_(nsrefcnt
) RefCnt() = 0;
52 NS_IMETHOD_(PRInt32
) ID() = 0;
55 NS_DEFINE_STATIC_IID_ACCESSOR(IFoo
, NS_IFOO_IID
)
57 class Foo
: public IFoo
{
63 // nsISupports implementation
66 // IFoo implementation
67 NS_IMETHOD_(nsrefcnt
) RefCnt() { return mRefCnt
; }
68 NS_IMETHOD_(PRInt32
) ID() { return mID
; }
70 static PRInt32 gCount
;
75 PRInt32
Foo::gCount
= 0;
88 NS_IMPL_ISUPPORTS1(Foo
, IFoo
)
91 typedef nsCOMArray
<IFoo
> Array
;
94 // {0e70a320-be02-11d1-8031-006008159b5a}
96 {0x0e70a320, 0xbe02, 0x11d1, \
97 {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
99 class IBar
: public nsISupports
{
102 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IBAR_IID
)
105 NS_DEFINE_STATIC_IID_ACCESSOR(IBar
, NS_IBAR_IID
)
107 class Bar
: public IBar
{
110 Bar(nsCOMArray
<IBar
>& aArray
, PRInt32 aIndex
);
113 // nsISupports implementation
116 static PRInt32 sReleaseCalled
;
119 nsCOMArray
<IBar
>& mArray
;
123 PRInt32
Bar::sReleaseCalled
= 0;
125 typedef nsCOMArray
<IBar
> Array2
;
127 Bar::Bar(Array2
& aArray
, PRInt32 aIndex
)
135 if (mArray
.RemoveObjectAt(mIndex
)) {
136 fail("We should never manage to remove the object here");
141 NS_IMPL_QUERY_INTERFACE1(Bar
, IBar
)
143 NS_IMETHODIMP_(nsrefcnt
)
146 ++Bar::sReleaseCalled
;
147 NS_PRECONDITION(0 != mRefCnt
, "dup release");
148 NS_ASSERT_OWNINGTHREAD(_class
);
150 NS_LOG_RELEASE(this, mRefCnt
, "Bar");
152 mRefCnt
= 1; /* stabilize */
160 int main(int argc
, char **argv
)
162 ScopedXPCOM
xpcom("nsCOMArrayTests");
163 if (xpcom
.failed()) {
171 for (PRInt32 i
= 0; i
< 20; ++i
) {
172 nsCOMPtr
<IFoo
> foo
= new Foo(i
);
173 arr
.AppendObject(foo
);
176 if (arr
.Count() != 20 || Foo::gCount
!= 20) {
177 fail("nsCOMArray::AppendObject failed");
183 if (arr
.Count() != 10 || Foo::gCount
!= 10) {
184 fail("nsCOMArray::SetCount shortening of array failed");
190 if (arr
.Count() != 30 || Foo::gCount
!= 10) {
191 fail("nsCOMArray::SetCount lengthening of array failed");
195 for (PRInt32 i
= 0; i
< 10; ++i
) {
196 if (arr
[i
] == nsnull
) {
197 fail("nsCOMArray elements should be non-null");
203 for (PRInt32 i
= 10; i
< 30; ++i
) {
204 if (arr
[i
] != nsnull
) {
205 fail("nsCOMArray elements should be null");
216 for (PRInt32 i
= 0; i
< 20; ++i
) {
217 nsCOMPtr
<IBar
> bar
= new Bar(arr2
, i
);
221 arr2
.AppendObject(bar
);
224 base
= Bar::sReleaseCalled
;
227 if (Bar::sReleaseCalled
!= base
+ 10) {
228 fail("Release called multiple times for SetCount");
231 arr2
.RemoveObjectAt(9);
232 if (Bar::sReleaseCalled
!= base
+ 11) {
233 fail("Release called multiple times for RemoveObjectAt");
236 arr2
.RemoveObject(ninthObject
);
237 if (Bar::sReleaseCalled
!= base
+ 12) {
238 fail("Release called multiple times for RemoveObject");
242 if (Bar::sReleaseCalled
!= base
+ 20) {
243 fail("Release called multiple times for Clear");
247 Bar::sReleaseCalled
= 0;
252 for (PRInt32 i
= 0; i
< 20; ++i
) {
253 nsCOMPtr
<IBar
> bar
= new Bar(arr2
, i
);
254 arr2
.AppendObject(bar
);
257 base
= Bar::sReleaseCalled
;
259 // Let arr2 be destroyed
261 if (Bar::sReleaseCalled
!= base
+ 20) {
262 fail("Release called multiple times for nsCOMArray::~nsCOMArray");