VirtualBox

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

最後變更 在這個檔案是 103505,由 vboxsync 提交於 13 月 前

libs/xpcom: Fix some unused variable warnings, bugref:3409

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

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