KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > btree > BTreeAssert


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.db4ounit.common.btree;
22
23 import com.db4o.*;
24 import com.db4o.db4ounit.common.foundation.IntArrays4;
25 import com.db4o.foundation.*;
26 import com.db4o.inside.btree.*;
27
28 import db4ounit.Assert;
29
30 public class BTreeAssert {
31
32     public static ExpectingVisitor createExpectingVisitor(int value, int count) {
33         int[] values = new int[count];
34         for (int i = 0; i < values.length; i++) {
35             values[i] = value;
36         }
37         return new ExpectingVisitor(IntArrays4.toObjectArray(values));
38     }
39
40     public static ExpectingVisitor createExpectingVisitor(int[] keys) {
41         return new ExpectingVisitor(IntArrays4.toObjectArray(keys));
42     }
43     
44     private static ExpectingVisitor createSortedExpectingVisitor(int[] keys) {
45         return new ExpectingVisitor(IntArrays4.toObjectArray(keys), true, false);
46     }
47
48     public static void traverseKeys(BTreeRange result, Visitor4 visitor) {
49         final Iterator4 i = result.keys();
50         while (i.moveNext()) {
51             visitor.visit(i.current());
52         }
53     }
54
55     public static void assertKeys(final Transaction transaction, BTree btree, final int[] keys) {
56         final ExpectingVisitor visitor = createExpectingVisitor(keys);
57         btree.traverseKeys(transaction, visitor);
58         visitor.assertExpectations();
59     }
60
61     public static void assertEmpty(Transaction transaction, BTree tree) {
62         final ExpectingVisitor visitor = new ExpectingVisitor(new Object JavaDoc[0]);
63         tree.traverseKeys(transaction, visitor);
64         visitor.assertExpectations();
65         Assert.areEqual(0, tree.size(transaction));
66     }
67
68     public static void dumpKeys(Transaction trans, BTree tree) {
69         tree.traverseKeys(trans, new Visitor4() {
70             public void visit(Object JavaDoc obj) {
71                 System.out.println(obj);
72             }
73         });
74     }
75
76     public static ExpectingVisitor createExpectingVisitor(final int expectedID) {
77         return createExpectingVisitor(expectedID, 1);
78     }
79
80     public static int fillSize(BTree btree) {
81         return btree.nodeSize()+1;
82     }
83
84     public static int[] newBTreeNodeSizedArray(final BTree btree, int value) {
85         return IntArrays4.fill(new int[fillSize(btree)], value);
86     }
87
88     public static void assertRange(int[] expectedKeys, BTreeRange range) {
89         Assert.isNotNull(range);
90         final ExpectingVisitor visitor = createSortedExpectingVisitor(expectedKeys);
91         
92         traverseKeys(range, visitor);
93         visitor.assertExpectations();
94     }
95
96     public static BTree createIntKeyBTree(final YapStream stream, int id, int nodeSize) {
97         return new BTree(stream.getSystemTransaction(), id, new YInt(stream), null, nodeSize, stream.configImpl().bTreeCacheHeight());
98     }
99     
100     public static BTree createIntKeyBTree(final YapStream stream, int id, int treeCacheHeight, int nodeSize) {
101         return new BTree(stream.getSystemTransaction(), id, new YInt(stream), null, nodeSize, treeCacheHeight);
102     }
103
104     public static void assertSingleElement(Transaction trans, BTree btree, Object JavaDoc element) {
105         Assert.areEqual(1, btree.size(trans));
106         
107         final BTreeRange result = btree.search(trans, element);
108         ExpectingVisitor expectingVisitor = new ExpectingVisitor(new Object JavaDoc[] { element });
109         BTreeAssert.traverseKeys(result, expectingVisitor);
110         expectingVisitor.assertExpectations();
111         
112         expectingVisitor = new ExpectingVisitor(new Object JavaDoc[] { element });
113         btree.traverseKeys(trans, expectingVisitor);
114         expectingVisitor.assertExpectations();
115     }
116
117 }
118
Popular Tags