VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/tests/TestArray.cpp@ 6542

Last change on this file since 6542 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
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
4 *
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/
9 *
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
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
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.
21 *
22 * Contributor(s):
23 * Pierre Phaneuf <[email protected]>
24 *
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.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39#include <stdio.h>
40#include <stdlib.h>
41#include "nsISupportsArray.h"
42
43// {9e70a320-be02-11d1-8031-006008159b5a}
44#define NS_IFOO_IID \
45 {0x9e70a320, 0xbe02, 0x11d1, \
46 {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
47
48static const PRBool kExitOnError = PR_TRUE;
49
50class IFoo : public nsISupports {
51public:
52
53 NS_DEFINE_STATIC_IID_ACCESSOR(NS_IFOO_IID)
54
55 NS_IMETHOD_(nsrefcnt) RefCnt() = 0;
56 NS_IMETHOD_(PRInt32) ID() = 0;
57};
58
59class Foo : public IFoo {
60public:
61
62 Foo(PRInt32 aID);
63
64 // nsISupports implementation
65 NS_DECL_ISUPPORTS
66
67 // IFoo implementation
68 NS_IMETHOD_(nsrefcnt) RefCnt() { return mRefCnt; }
69 NS_IMETHOD_(PRInt32) ID() { return mID; }
70
71 static PRInt32 gCount;
72
73 PRInt32 mID;
74
75private:
76 ~Foo();
77};
78
79PRInt32 Foo::gCount;
80
81Foo::Foo(PRInt32 aID)
82{
83 mID = aID;
84 ++gCount;
85 fprintf(stdout, "init: %d (%p), %d total)\n", mID, this, gCount);
86}
87
88Foo::~Foo()
89{
90 --gCount;
91 fprintf(stdout, "destruct: %d (%p), %d remain)\n", mID, this, gCount);
92}
93
94NS_IMPL_ISUPPORTS1(Foo, IFoo)
95
96const char* AssertEqual(PRInt32 aValue1, PRInt32 aValue2)
97{
98 if (aValue1 == aValue2) {
99 return "OK";
100 }
101 if (kExitOnError) {
102 exit(1);
103 }
104 return "ERROR";
105}
106
107void DumpArray(nsISupportsArray* aArray, PRInt32 aExpectedCount, PRInt32 aElementIDs[], PRInt32 aExpectedTotal)
108{
109 PRUint32 cnt = 0;
110 nsresult rv = aArray->Count(&cnt);
111 NS_ASSERTION(NS_SUCCEEDED(rv), "Count failed");
112 PRInt32 count = cnt;
113 PRInt32 index;
114
115 fprintf(stdout, "object count %d = %d %s\n", Foo::gCount, aExpectedTotal,
116 AssertEqual(Foo::gCount, aExpectedTotal));
117 fprintf(stdout, "array count %d = %d %s\n", count, aExpectedCount,
118 AssertEqual(count, aExpectedCount));
119
120 for (index = 0; (index < count) && (index < aExpectedCount); index++) {
121 IFoo* foo = (IFoo*)(aArray->ElementAt(index));
122 fprintf(stdout, "%2d: %d=%d (%p) c: %d %s\n",
123 index, aElementIDs[index], foo->ID(), foo, foo->RefCnt() - 1,
124 AssertEqual(foo->ID(), aElementIDs[index]));
125 foo->Release();
126 }
127}
128
129void FillArray(nsISupportsArray* aArray, PRInt32 aCount)
130{
131 PRInt32 index;
132 for (index = 0; index < aCount; index++) {
133 nsCOMPtr<IFoo> foo = new Foo(index);
134 aArray->AppendElement(foo);
135 }
136}
137
138int main(int argc, char *argv[])
139{
140 nsISupportsArray* array;
141 nsresult rv;
142
143 if (NS_OK == (rv = NS_NewISupportsArray(&array))) {
144 FillArray(array, 10);
145 fprintf(stdout, "Array created:\n");
146 PRInt32 fillResult[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
147 DumpArray(array, 10, fillResult, 10);
148
149 // test insert
150 IFoo* foo = (IFoo*)array->ElementAt(3);
151 foo->Release(); // pre-release to fix ref count for dumps
152 array->InsertElementAt(foo, 5);
153 fprintf(stdout, "insert 3 at 5:\n");
154 PRInt32 insertResult[11] = {0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9};
155 DumpArray(array, 11, insertResult, 10);
156 fprintf(stdout, "insert 3 at 0:\n");
157 array->InsertElementAt(foo, 0);
158 PRInt32 insertResult2[12] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9};
159 DumpArray(array, 12, insertResult2, 10);
160 fprintf(stdout, "append 3:\n");
161 array->AppendElement(foo);
162 PRInt32 appendResult[13] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9, 3};
163 DumpArray(array, 13, appendResult, 10);
164
165
166 // test IndexOf && LastIndexOf
167 PRInt32 expectedIndex[5] = {0, 4, 6, 12, -1};
168 PRInt32 count = 0;
169 PRInt32 index = array->IndexOf(foo);
170 fprintf(stdout, "IndexOf(foo): %d=%d %s\n", index, expectedIndex[count],
171 AssertEqual(index, expectedIndex[count]));
172 while (-1 != index) {
173 count++;
174 index = array->IndexOfStartingAt(foo, index + 1);
175 if (-1 != index)
176 fprintf(stdout, "IndexOf(foo): %d=%d %s\n", index, expectedIndex[count],
177 AssertEqual(index, expectedIndex[count]));
178 }
179 index = array->LastIndexOf(foo);
180 count--;
181 fprintf(stdout, "LastIndexOf(foo): %d=%d %s\n", index, expectedIndex[count],
182 AssertEqual(index, expectedIndex[count]));
183
184 // test ReplaceElementAt
185 fprintf(stdout, "ReplaceElementAt(8):\n");
186 array->ReplaceElementAt(foo, 8);
187 PRInt32 replaceResult[13] = {3, 0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3};
188 DumpArray(array, 13, replaceResult, 9);
189
190 // test RemoveElementAt, RemoveElement RemoveLastElement
191 fprintf(stdout, "RemoveElementAt(0):\n");
192 array->RemoveElementAt(0);
193 PRInt32 removeResult[12] = {0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3};
194 DumpArray(array, 12, removeResult, 9);
195 fprintf(stdout, "RemoveElementAt(7):\n");
196 array->RemoveElementAt(7);
197 PRInt32 removeResult2[11] = {0, 1, 2, 3, 4, 3, 5, 7, 8, 9, 3};
198 DumpArray(array, 11, removeResult2, 9);
199 fprintf(stdout, "RemoveElement(foo):\n");
200 array->RemoveElement(foo);
201 PRInt32 removeResult3[10] = {0, 1, 2, 4, 3, 5, 7, 8, 9, 3};
202 DumpArray(array, 10, removeResult3, 9);
203 fprintf(stdout, "RemoveLastElement(foo):\n");
204 array->RemoveLastElement(foo);
205 PRInt32 removeResult4[9] = {0, 1, 2, 4, 3, 5, 7, 8, 9};
206 DumpArray(array, 9, removeResult4, 9);
207
208 // test clear
209 fprintf(stdout, "clear array:\n");
210 array->Clear();
211 DumpArray(array, 0, 0, 0);
212 fprintf(stdout, "add 4 new:\n");
213 FillArray(array, 4);
214 DumpArray(array, 4, fillResult, 4);
215
216 // test compact
217 fprintf(stdout, "compact array:\n");
218 array->Compact();
219 DumpArray(array, 4, fillResult, 4);
220
221 // test delete
222 fprintf(stdout, "release array:\n");
223 NS_RELEASE(array);
224 }
225 else {
226 fprintf(stdout, "error can't create array: %x\n", rv);
227 }
228
229 return 0;
230}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette