|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include <stdio.h> |
|
7 #include <stdlib.h> |
|
8 #include "nsISupportsArray.h" |
|
9 |
|
10 // {9e70a320-be02-11d1-8031-006008159b5a} |
|
11 #define NS_IFOO_IID \ |
|
12 {0x9e70a320, 0xbe02, 0x11d1, \ |
|
13 {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}} |
|
14 |
|
15 namespace TestArray { |
|
16 |
|
17 static const bool kExitOnError = true; |
|
18 |
|
19 class IFoo : public nsISupports { |
|
20 public: |
|
21 |
|
22 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID) |
|
23 |
|
24 NS_IMETHOD_(nsrefcnt) RefCnt() = 0; |
|
25 NS_IMETHOD_(int32_t) ID() = 0; |
|
26 }; |
|
27 |
|
28 NS_DEFINE_STATIC_IID_ACCESSOR(IFoo, NS_IFOO_IID) |
|
29 |
|
30 class Foo : public IFoo { |
|
31 public: |
|
32 |
|
33 Foo(int32_t aID); |
|
34 |
|
35 // nsISupports implementation |
|
36 NS_DECL_ISUPPORTS |
|
37 |
|
38 // IFoo implementation |
|
39 NS_IMETHOD_(nsrefcnt) RefCnt() { return mRefCnt; } |
|
40 NS_IMETHOD_(int32_t) ID() { return mID; } |
|
41 |
|
42 static int32_t gCount; |
|
43 |
|
44 int32_t mID; |
|
45 |
|
46 private: |
|
47 ~Foo(); |
|
48 }; |
|
49 |
|
50 int32_t Foo::gCount; |
|
51 |
|
52 Foo::Foo(int32_t aID) |
|
53 { |
|
54 mID = aID; |
|
55 ++gCount; |
|
56 fprintf(stdout, "init: %d (%p), %d total)\n", |
|
57 mID, static_cast<void*>(this), gCount); |
|
58 } |
|
59 |
|
60 Foo::~Foo() |
|
61 { |
|
62 --gCount; |
|
63 fprintf(stdout, "destruct: %d (%p), %d remain)\n", |
|
64 mID, static_cast<void*>(this), gCount); |
|
65 } |
|
66 |
|
67 NS_IMPL_ISUPPORTS(Foo, IFoo) |
|
68 |
|
69 const char* AssertEqual(int32_t aValue1, int32_t aValue2) |
|
70 { |
|
71 if (aValue1 == aValue2) { |
|
72 return "OK"; |
|
73 } |
|
74 if (kExitOnError) { |
|
75 exit(1); |
|
76 } |
|
77 return "ERROR"; |
|
78 } |
|
79 |
|
80 void DumpArray(nsISupportsArray* aArray, int32_t aExpectedCount, int32_t aElementIDs[], int32_t aExpectedTotal) |
|
81 { |
|
82 uint32_t cnt = 0; |
|
83 #ifdef DEBUG |
|
84 nsresult rv = |
|
85 #endif |
|
86 aArray->Count(&cnt); |
|
87 NS_ASSERTION(NS_SUCCEEDED(rv), "Count failed"); |
|
88 int32_t count = cnt; |
|
89 int32_t index; |
|
90 |
|
91 fprintf(stdout, "object count %d = %d %s\n", Foo::gCount, aExpectedTotal, |
|
92 AssertEqual(Foo::gCount, aExpectedTotal)); |
|
93 fprintf(stdout, "array count %d = %d %s\n", count, aExpectedCount, |
|
94 AssertEqual(count, aExpectedCount)); |
|
95 |
|
96 for (index = 0; (index < count) && (index < aExpectedCount); index++) { |
|
97 IFoo* foo = (IFoo*)(aArray->ElementAt(index)); |
|
98 fprintf(stdout, "%2d: %d=%d (%p) c: %d %s\n", |
|
99 index, aElementIDs[index], foo->ID(), |
|
100 static_cast<void*>(foo), foo->RefCnt() - 1, |
|
101 AssertEqual(foo->ID(), aElementIDs[index])); |
|
102 foo->Release(); |
|
103 } |
|
104 } |
|
105 |
|
106 void FillArray(nsISupportsArray* aArray, int32_t aCount) |
|
107 { |
|
108 int32_t index; |
|
109 for (index = 0; index < aCount; index++) { |
|
110 nsCOMPtr<IFoo> foo = new Foo(index); |
|
111 aArray->AppendElement(foo); |
|
112 } |
|
113 } |
|
114 |
|
115 } |
|
116 |
|
117 using namespace TestArray; |
|
118 |
|
119 int main(int argc, char *argv[]) |
|
120 { |
|
121 nsISupportsArray* array; |
|
122 nsresult rv; |
|
123 |
|
124 if (NS_OK == (rv = NS_NewISupportsArray(&array))) { |
|
125 FillArray(array, 10); |
|
126 fprintf(stdout, "Array created:\n"); |
|
127 int32_t fillResult[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
|
128 DumpArray(array, 10, fillResult, 10); |
|
129 |
|
130 // test insert |
|
131 IFoo* foo = (IFoo*)array->ElementAt(3); |
|
132 foo->Release(); // pre-release to fix ref count for dumps |
|
133 array->InsertElementAt(foo, 5); |
|
134 fprintf(stdout, "insert 3 at 5:\n"); |
|
135 int32_t insertResult[11] = {0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9}; |
|
136 DumpArray(array, 11, insertResult, 10); |
|
137 fprintf(stdout, "insert 3 at 0:\n"); |
|
138 array->InsertElementAt(foo, 0); |
|
139 int32_t insertResult2[12] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9}; |
|
140 DumpArray(array, 12, insertResult2, 10); |
|
141 fprintf(stdout, "append 3:\n"); |
|
142 array->AppendElement(foo); |
|
143 int32_t appendResult[13] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9, 3}; |
|
144 DumpArray(array, 13, appendResult, 10); |
|
145 |
|
146 |
|
147 // test IndexOf && LastIndexOf |
|
148 int32_t expectedIndex[5] = {0, 4, 6, 12, -1}; |
|
149 int32_t count = 0; |
|
150 int32_t index = array->IndexOf(foo); |
|
151 fprintf(stdout, "IndexOf(foo): %d=%d %s\n", index, expectedIndex[count], |
|
152 AssertEqual(index, expectedIndex[count])); |
|
153 while (-1 != index) { |
|
154 count++; |
|
155 index = array->IndexOfStartingAt(foo, index + 1); |
|
156 if (-1 != index) |
|
157 fprintf(stdout, "IndexOf(foo): %d=%d %s\n", index, expectedIndex[count], |
|
158 AssertEqual(index, expectedIndex[count])); |
|
159 } |
|
160 index = array->LastIndexOf(foo); |
|
161 count--; |
|
162 fprintf(stdout, "LastIndexOf(foo): %d=%d %s\n", index, expectedIndex[count], |
|
163 AssertEqual(index, expectedIndex[count])); |
|
164 |
|
165 // test ReplaceElementAt |
|
166 fprintf(stdout, "ReplaceElementAt(8):\n"); |
|
167 array->ReplaceElementAt(foo, 8); |
|
168 int32_t replaceResult[13] = {3, 0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3}; |
|
169 DumpArray(array, 13, replaceResult, 9); |
|
170 |
|
171 // test RemoveElementAt, RemoveElement RemoveLastElement |
|
172 fprintf(stdout, "RemoveElementAt(0):\n"); |
|
173 array->RemoveElementAt(0); |
|
174 int32_t removeResult[12] = {0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3}; |
|
175 DumpArray(array, 12, removeResult, 9); |
|
176 fprintf(stdout, "RemoveElementAt(7):\n"); |
|
177 array->RemoveElementAt(7); |
|
178 int32_t removeResult2[11] = {0, 1, 2, 3, 4, 3, 5, 7, 8, 9, 3}; |
|
179 DumpArray(array, 11, removeResult2, 9); |
|
180 fprintf(stdout, "RemoveElement(foo):\n"); |
|
181 array->RemoveElement(foo); |
|
182 int32_t removeResult3[10] = {0, 1, 2, 4, 3, 5, 7, 8, 9, 3}; |
|
183 DumpArray(array, 10, removeResult3, 9); |
|
184 fprintf(stdout, "RemoveLastElement(foo):\n"); |
|
185 array->RemoveLastElement(foo); |
|
186 int32_t removeResult4[9] = {0, 1, 2, 4, 3, 5, 7, 8, 9}; |
|
187 DumpArray(array, 9, removeResult4, 9); |
|
188 |
|
189 // test clear |
|
190 fprintf(stdout, "clear array:\n"); |
|
191 array->Clear(); |
|
192 DumpArray(array, 0, 0, 0); |
|
193 fprintf(stdout, "add 4 new:\n"); |
|
194 FillArray(array, 4); |
|
195 DumpArray(array, 4, fillResult, 4); |
|
196 |
|
197 // test compact |
|
198 fprintf(stdout, "compact array:\n"); |
|
199 array->Compact(); |
|
200 DumpArray(array, 4, fillResult, 4); |
|
201 |
|
202 // test delete |
|
203 fprintf(stdout, "release array:\n"); |
|
204 NS_RELEASE(array); |
|
205 } |
|
206 else { |
|
207 fprintf(stdout, "error can't create array: %x\n", rv); |
|
208 } |
|
209 |
|
210 return 0; |
|
211 } |