1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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) 1999
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 ***** */
40 #include "nsVoidBTree.h"
41 #include "nsVoidArray.h"
44 #define POINTER(i) reinterpret_cast<void*>(4 + 4 * (i))
47 Equal(const nsVoidArray
& aControl
, const nsVoidBTree
& aTest
)
49 if (aControl
.Count() != aTest
.Count()) {
50 printf("counts not equal; ");
54 for (PRInt32 i
= aControl
.Count() - 1; i
>= 0; --i
) {
55 if (aControl
[i
] != aTest
[i
]) {
56 printf("element %d differs; ", i
);
66 CheckForwardEnumeration(const nsVoidArray
& aControl
, const nsVoidBTree
& aTest
)
70 nsVoidBTree::ConstIterator last
= aTest
.Last();
71 for (nsVoidBTree::ConstIterator element
= aTest
.First(); element
!= last
; ++element
, ++index
) {
72 if (*element
!= aControl
[index
]) {
73 printf("failed forward enumeration\n");
78 if (index
!= aControl
.Count()) {
79 printf("erratic termination during forward enumeration\n");
85 CheckBackwardEnumeration(const nsVoidArray
& aControl
, const nsVoidBTree
& aTest
)
87 PRInt32 index
= aControl
.Count();
88 nsVoidBTree::ConstIterator first
= aTest
.First();
89 nsVoidBTree::ConstIterator element
= aTest
.Last();
91 if (first
!= element
) {
93 if (*--element
!= aControl
[--index
]) {
94 printf("failed backward enumeration\n");
97 } while (element
!= first
);
101 printf("erratic termination during backward enumeration\n");
106 int main(int argc
, char* argv
[])
112 //----------------------------------------
114 for (i
= 0; i
< COUNT
; ++i
)
115 btree
.InsertElementAt(reinterpret_cast<void*>(POINTER(i
)), i
);
117 for (i
= 0; i
< COUNT
; ++i
) {
118 if (btree
[i
] != POINTER(i
)) {
119 printf("tail fill failed\n");
124 printf("tail fill succeeded\n");
126 //----------------------------------------
128 for (i
= COUNT
- 1; i
>= 0; --i
)
129 btree
.RemoveElementAt(i
);
131 if (btree
.Count() != 0) {
132 printf("tail empty failed\n");
136 printf("tail empty succeeded\n");
138 // N.B. no intervening Clear() to verify that we handle the re-use
141 //----------------------------------------
143 for (i
= 0; i
< COUNT
; ++i
)
144 btree
.InsertElementAt(POINTER(i
), 0);
146 for (i
= 0; i
< COUNT
; ++i
) {
147 if (btree
[COUNT
- (i
+ 1)] != POINTER(i
)) {
148 printf("simple front fill failed\n");
153 printf("front fill succeeded\n");
155 //----------------------------------------
157 for (i
= COUNT
- 1; i
>= 0; --i
)
158 btree
.RemoveElementAt(0);
160 if (btree
.Count() != 0) {
161 printf("front empty failed\n");
165 printf("front empty succeeded\n");
168 //----------------------------------------
169 // Test boundary conditions with small btree
172 printf("small btree boundary conditions ");
178 CheckBackwardEnumeration(control
, btree
);
179 CheckForwardEnumeration(control
, btree
);
181 btree
.AppendElement(POINTER(0));
182 control
.AppendElement(POINTER(0));
184 CheckBackwardEnumeration(control
, btree
);
185 CheckForwardEnumeration(control
, btree
);
187 btree
.AppendElement(POINTER(1));
188 control
.AppendElement(POINTER(1));
190 CheckBackwardEnumeration(control
, btree
);
191 CheckForwardEnumeration(control
, btree
);
193 btree
.RemoveElementAt(0);
194 btree
.RemoveElementAt(0);
195 control
.RemoveElementAt(0);
196 control
.RemoveElementAt(0);
198 CheckBackwardEnumeration(control
, btree
);
199 CheckForwardEnumeration(control
, btree
);
201 printf("succeeded\n");
204 //----------------------------------------
211 for (i
= 0; i
< COUNT
; ++i
) {
212 PRInt32 slot
= i
? rand() % i
: 0;
214 btree
.InsertElementAt(POINTER(i
), slot
);
215 control
.InsertElementAt(POINTER(i
), slot
);
217 if (! Equal(control
, btree
)) {
223 for (nsVoidBTree::Iterator m
= btree
.First(); m
!= btree
.Last(); ++m
) {
224 nsVoidBTree::Iterator n
;
225 for (n
= m
, ++n
; n
!= btree
.Last(); ++n
) {
234 nsVoidBTree::Iterator el
;
235 for (el
= btree
.First(), i
= 0; el
!= btree
.Last(); ++el
, ++i
) {
236 if (*el
!= POINTER(i
)) {
237 printf("bubble sort failed\n");
242 printf("iteration succeeded\n");
245 //----------------------------------------
248 printf("random fill/empty: ");
250 for (PRInt32 iter
= 10; iter
>= 1; --iter
) {
258 for (i
= 0; i
< COUNT
; ++i
) {
259 PRInt32 slot
= i
? rand() % i
: 0;
261 btree
.InsertElementAt(POINTER(i
), slot
);
262 control
.InsertElementAt(POINTER(i
), slot
);
264 if (! Equal(control
, btree
)) {
271 for (i
= 0; i
< COUNT
; ++i
) {
272 void* element
= control
[i
];
273 if (btree
.IndexOf(element
) != i
) {
274 printf("failed IndexOf at %d\n", i
);
279 // Backward enumeration
280 CheckBackwardEnumeration(control
, btree
);
282 // Forward enumeration
283 CheckForwardEnumeration(control
, btree
);
286 for (i
= COUNT
- 1; i
>= 0; --i
) {
287 PRInt32 slot
= i
? rand() % i
: 0;
289 btree
.RemoveElementAt(slot
);
290 control
.RemoveElementAt(slot
);
292 if (! Equal(control
, btree
)) {
299 printf("succeeded\n");