1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3
* The contents of this file are subject to the Mozilla Public License
4
* Version 1.1 (the "MPL"); you may not use this file except in
5
* compliance with the MPL. You may obtain a copy of the MPL at
6
* http://www.mozilla.org/MPL/
8
* Software distributed under the MPL is distributed on an "AS IS" basis,
9
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the MPL
10
* for the specific language governing rights and limitations under the
13
* The Initial Developer of this code under the MPL is Netscape
14
* Communications Corporation. Portions created by Netscape are
15
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
19
* Chris Waterson <waterson@netscape.com>
24
#include "nsVoidBTree.h"
25
#include "nsVoidArray.h"
28
#define POINTER(i) NS_REINTERPRET_CAST(void*, 4 + 4 * (i))
31
Equal(const nsVoidArray& aControl, const nsVoidBTree& aTest)
33
if (aControl.Count() != aTest.Count()) {
34
printf("counts not equal; ");
38
for (PRInt32 i = aControl.Count() - 1; i >= 0; --i) {
39
if (aControl[i] != aTest[i]) {
40
printf("element %d differs; ", i);
50
CheckForwardEnumeration(const nsVoidArray& aControl, const nsVoidBTree& aTest)
54
nsVoidBTree::ConstIterator last = aTest.Last();
55
for (nsVoidBTree::ConstIterator element = aTest.First(); element != last; ++element, ++index) {
56
if (*element != aControl[index]) {
57
printf("failed forward enumeration\n");
62
if (index != aControl.Count()) {
63
printf("erratic termination during forward enumeration\n");
69
CheckBackwardEnumeration(const nsVoidArray& aControl, const nsVoidBTree& aTest)
71
PRInt32 index = aControl.Count();
72
nsVoidBTree::ConstIterator first = aTest.First();
73
nsVoidBTree::ConstIterator element = aTest.Last();
75
if (first != element) {
77
if (*--element != aControl[--index]) {
78
printf("failed backward enumeration\n");
81
} while (element != first);
85
printf("erratic termination during backward enumeration\n");
90
int main(int argc, char* argv[])
96
//----------------------------------------
98
for (i = 0; i < COUNT; ++i)
99
btree.InsertElementAt(NS_REINTERPRET_CAST(void*, POINTER(i)), i);
101
for (i = 0; i < COUNT; ++i) {
102
if (btree[i] != POINTER(i)) {
103
printf("tail fill failed\n");
108
printf("tail fill succeeded\n");
110
//----------------------------------------
112
for (i = COUNT - 1; i >= 0; --i)
113
btree.RemoveElementAt(i);
115
if (btree.Count() != 0) {
116
printf("tail empty failed\n");
120
printf("tail empty succeeded\n");
122
// N.B. no intervening Clear() to verify that we handle the re-use
125
//----------------------------------------
127
for (i = 0; i < COUNT; ++i)
128
btree.InsertElementAt(POINTER(i), 0);
130
for (i = 0; i < COUNT; ++i) {
131
if (btree[COUNT - (i + 1)] != POINTER(i)) {
132
printf("simple front fill failed\n");
137
printf("front fill succeeded\n");
139
//----------------------------------------
141
for (i = COUNT - 1; i >= 0; --i)
142
btree.RemoveElementAt(0);
144
if (btree.Count() != 0) {
145
printf("front empty failed\n");
149
printf("front empty succeeded\n");
152
//----------------------------------------
153
// Test boundary conditions with small btree
156
printf("small btree boundary conditions ");
162
CheckBackwardEnumeration(control, btree);
163
CheckForwardEnumeration(control, btree);
165
btree.AppendElement(POINTER(0));
166
control.AppendElement(POINTER(0));
168
CheckBackwardEnumeration(control, btree);
169
CheckForwardEnumeration(control, btree);
171
btree.AppendElement(POINTER(1));
172
control.AppendElement(POINTER(1));
174
CheckBackwardEnumeration(control, btree);
175
CheckForwardEnumeration(control, btree);
177
btree.RemoveElementAt(0);
178
btree.RemoveElementAt(0);
179
control.RemoveElementAt(0);
180
control.RemoveElementAt(0);
182
CheckBackwardEnumeration(control, btree);
183
CheckForwardEnumeration(control, btree);
185
printf("succeeded\n");
188
//----------------------------------------
195
for (i = 0; i < COUNT; ++i) {
196
PRInt32 slot = i ? rand() % i : 0;
198
btree.InsertElementAt(POINTER(i), slot);
199
control.InsertElementAt(POINTER(i), slot);
201
if (! Equal(control, btree)) {
207
for (nsVoidBTree::Iterator m = btree.First(); m != btree.Last(); ++m) {
208
nsVoidBTree::Iterator n;
209
for (n = m, ++n; n != btree.Last(); ++n) {
218
nsVoidBTree::Iterator el;
219
for (el = btree.First(), i = 0; el != btree.Last(); ++el, ++i) {
220
if (*el != POINTER(i)) {
221
printf("bubble sort failed\n");
226
printf("iteration succeeded\n");
229
//----------------------------------------
232
printf("random fill/empty: ");
234
for (PRInt32 iter = 10; iter >= 1; --iter) {
242
for (i = 0; i < COUNT; ++i) {
243
PRInt32 slot = i ? rand() % i : 0;
245
btree.InsertElementAt(POINTER(i), slot);
246
control.InsertElementAt(POINTER(i), slot);
248
if (! Equal(control, btree)) {
255
for (i = 0; i < COUNT; ++i) {
256
void* element = control[i];
257
if (btree.IndexOf(element) != i) {
258
printf("failed IndexOf at %d\n", i);
263
// Backward enumeration
264
CheckBackwardEnumeration(control, btree);
266
// Forward enumeration
267
CheckForwardEnumeration(control, btree);
270
for (i = COUNT - 1; i >= 0; --i) {
271
PRInt32 slot = i ? rand() % i : 0;
273
btree.RemoveElementAt(slot);
274
control.RemoveElementAt(slot);
276
if (! Equal(control, btree)) {
283
printf("succeeded\n");