1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
41 #include "nsISupportsArray.h"
43 // {9e70a320-be02-11d1-8031-006008159b5a}
45 {0x9e70a320, 0xbe02, 0x11d1, \
46 {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
50 static const PRBool kExitOnError
= PR_TRUE
;
52 class IFoo
: public nsISupports
{
55 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID
)
57 NS_IMETHOD_(nsrefcnt
) RefCnt() = 0;
58 NS_IMETHOD_(PRInt32
) ID() = 0;
61 NS_DEFINE_STATIC_IID_ACCESSOR(IFoo
, NS_IFOO_IID
)
63 class Foo
: public IFoo
{
68 // nsISupports implementation
71 // IFoo implementation
72 NS_IMETHOD_(nsrefcnt
) RefCnt() { return mRefCnt
; }
73 NS_IMETHOD_(PRInt32
) ID() { return mID
; }
75 static PRInt32 gCount
;
89 fprintf(stdout
, "init: %d (%p), %d total)\n", mID
, this, gCount
);
95 fprintf(stdout
, "destruct: %d (%p), %d remain)\n", mID
, this, gCount
);
98 NS_IMPL_ISUPPORTS1(Foo
, IFoo
)
100 const char* AssertEqual(PRInt32 aValue1
, PRInt32 aValue2
)
102 if (aValue1
== aValue2
) {
111 void DumpArray(nsISupportsArray
* aArray
, PRInt32 aExpectedCount
, PRInt32 aElementIDs
[], PRInt32 aExpectedTotal
)
114 nsresult rv
= aArray
->Count(&cnt
);
115 NS_ASSERTION(NS_SUCCEEDED(rv
), "Count failed");
119 fprintf(stdout
, "object count %d = %d %s\n", Foo::gCount
, aExpectedTotal
,
120 AssertEqual(Foo::gCount
, aExpectedTotal
));
121 fprintf(stdout
, "array count %d = %d %s\n", count
, aExpectedCount
,
122 AssertEqual(count
, aExpectedCount
));
124 for (index
= 0; (index
< count
) && (index
< aExpectedCount
); index
++) {
125 IFoo
* foo
= (IFoo
*)(aArray
->ElementAt(index
));
126 fprintf(stdout
, "%2d: %d=%d (%p) c: %d %s\n",
127 index
, aElementIDs
[index
], foo
->ID(), foo
, foo
->RefCnt() - 1,
128 AssertEqual(foo
->ID(), aElementIDs
[index
]));
133 void FillArray(nsISupportsArray
* aArray
, PRInt32 aCount
)
136 for (index
= 0; index
< aCount
; index
++) {
137 nsCOMPtr
<IFoo
> foo
= new Foo(index
);
138 aArray
->AppendElement(foo
);
144 using namespace TestArray
;
146 int main(int argc
, char *argv
[])
148 nsISupportsArray
* array
;
151 if (NS_OK
== (rv
= NS_NewISupportsArray(&array
))) {
152 FillArray(array
, 10);
153 fprintf(stdout
, "Array created:\n");
154 PRInt32 fillResult
[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
155 DumpArray(array
, 10, fillResult
, 10);
158 IFoo
* foo
= (IFoo
*)array
->ElementAt(3);
159 foo
->Release(); // pre-release to fix ref count for dumps
160 array
->InsertElementAt(foo
, 5);
161 fprintf(stdout
, "insert 3 at 5:\n");
162 PRInt32 insertResult
[11] = {0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9};
163 DumpArray(array
, 11, insertResult
, 10);
164 fprintf(stdout
, "insert 3 at 0:\n");
165 array
->InsertElementAt(foo
, 0);
166 PRInt32 insertResult2
[12] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9};
167 DumpArray(array
, 12, insertResult2
, 10);
168 fprintf(stdout
, "append 3:\n");
169 array
->AppendElement(foo
);
170 PRInt32 appendResult
[13] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9, 3};
171 DumpArray(array
, 13, appendResult
, 10);
174 // test IndexOf && LastIndexOf
175 PRInt32 expectedIndex
[5] = {0, 4, 6, 12, -1};
177 PRInt32 index
= array
->IndexOf(foo
);
178 fprintf(stdout
, "IndexOf(foo): %d=%d %s\n", index
, expectedIndex
[count
],
179 AssertEqual(index
, expectedIndex
[count
]));
180 while (-1 != index
) {
182 index
= array
->IndexOfStartingAt(foo
, index
+ 1);
184 fprintf(stdout
, "IndexOf(foo): %d=%d %s\n", index
, expectedIndex
[count
],
185 AssertEqual(index
, expectedIndex
[count
]));
187 index
= array
->LastIndexOf(foo
);
189 fprintf(stdout
, "LastIndexOf(foo): %d=%d %s\n", index
, expectedIndex
[count
],
190 AssertEqual(index
, expectedIndex
[count
]));
192 // test ReplaceElementAt
193 fprintf(stdout
, "ReplaceElementAt(8):\n");
194 array
->ReplaceElementAt(foo
, 8);
195 PRInt32 replaceResult
[13] = {3, 0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3};
196 DumpArray(array
, 13, replaceResult
, 9);
198 // test RemoveElementAt, RemoveElement RemoveLastElement
199 fprintf(stdout
, "RemoveElementAt(0):\n");
200 array
->RemoveElementAt(0);
201 PRInt32 removeResult
[12] = {0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3};
202 DumpArray(array
, 12, removeResult
, 9);
203 fprintf(stdout
, "RemoveElementAt(7):\n");
204 array
->RemoveElementAt(7);
205 PRInt32 removeResult2
[11] = {0, 1, 2, 3, 4, 3, 5, 7, 8, 9, 3};
206 DumpArray(array
, 11, removeResult2
, 9);
207 fprintf(stdout
, "RemoveElement(foo):\n");
208 array
->RemoveElement(foo
);
209 PRInt32 removeResult3
[10] = {0, 1, 2, 4, 3, 5, 7, 8, 9, 3};
210 DumpArray(array
, 10, removeResult3
, 9);
211 fprintf(stdout
, "RemoveLastElement(foo):\n");
212 array
->RemoveLastElement(foo
);
213 PRInt32 removeResult4
[9] = {0, 1, 2, 4, 3, 5, 7, 8, 9};
214 DumpArray(array
, 9, removeResult4
, 9);
217 fprintf(stdout
, "clear array:\n");
219 DumpArray(array
, 0, 0, 0);
220 fprintf(stdout
, "add 4 new:\n");
222 DumpArray(array
, 4, fillResult
, 4);
225 fprintf(stdout
, "compact array:\n");
227 DumpArray(array
, 4, fillResult
, 4);
230 fprintf(stdout
, "release array:\n");
234 fprintf(stdout
, "error can't create array: %x\n", rv
);