001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.common.btree;
022:
023: import com.db4o.db4ounit.common.foundation.IntArrays4;
024: import com.db4o.foundation.*;
025: import com.db4o.internal.*;
026: import com.db4o.internal.btree.*;
027: import com.db4o.internal.handlers.IntHandler;
028:
029: import db4ounit.Assert;
030:
031: public class BTreeAssert {
032:
033: public static ExpectingVisitor createExpectingVisitor(int value,
034: int count) {
035: int[] values = new int[count];
036: for (int i = 0; i < values.length; i++) {
037: values[i] = value;
038: }
039: return new ExpectingVisitor(IntArrays4.toObjectArray(values));
040: }
041:
042: public static ExpectingVisitor createExpectingVisitor(int[] keys) {
043: return new ExpectingVisitor(IntArrays4.toObjectArray(keys));
044: }
045:
046: private static ExpectingVisitor createSortedExpectingVisitor(
047: int[] keys) {
048: return new ExpectingVisitor(IntArrays4.toObjectArray(keys),
049: true, false);
050: }
051:
052: public static void traverseKeys(BTreeRange result, Visitor4 visitor) {
053: final Iterator4 i = result.keys();
054: while (i.moveNext()) {
055: visitor.visit(i.current());
056: }
057: }
058:
059: public static void assertKeys(final Transaction transaction,
060: BTree btree, final int[] keys) {
061: final ExpectingVisitor visitor = createExpectingVisitor(keys);
062: btree.traverseKeys(transaction, visitor);
063: visitor.assertExpectations();
064: }
065:
066: public static void assertEmpty(Transaction transaction, BTree tree) {
067: final ExpectingVisitor visitor = new ExpectingVisitor(
068: new Object[0]);
069: tree.traverseKeys(transaction, visitor);
070: visitor.assertExpectations();
071: Assert.areEqual(0, tree.size(transaction));
072: }
073:
074: public static void dumpKeys(Transaction trans, BTree tree) {
075: tree.traverseKeys(trans, new Visitor4() {
076: public void visit(Object obj) {
077: System.out.println(obj);
078: }
079: });
080: }
081:
082: public static ExpectingVisitor createExpectingVisitor(
083: final int expectedID) {
084: return createExpectingVisitor(expectedID, 1);
085: }
086:
087: public static int fillSize(BTree btree) {
088: return btree.nodeSize() + 1;
089: }
090:
091: public static int[] newBTreeNodeSizedArray(final BTree btree,
092: int value) {
093: return IntArrays4.fill(new int[fillSize(btree)], value);
094: }
095:
096: public static void assertRange(int[] expectedKeys, BTreeRange range) {
097: Assert.isNotNull(range);
098: final ExpectingVisitor visitor = createSortedExpectingVisitor(expectedKeys);
099:
100: traverseKeys(range, visitor);
101: visitor.assertExpectations();
102: }
103:
104: public static BTree createIntKeyBTree(
105: final ObjectContainerBase stream, int id, int nodeSize) {
106: return new BTree(stream.systemTransaction(), id,
107: new IntHandler(stream), nodeSize, stream.configImpl()
108: .bTreeCacheHeight());
109: }
110:
111: public static BTree createIntKeyBTree(
112: final ObjectContainerBase stream, int id,
113: int treeCacheHeight, int nodeSize) {
114: return new BTree(stream.systemTransaction(), id,
115: new IntHandler(stream), nodeSize, treeCacheHeight);
116: }
117:
118: public static void assertSingleElement(Transaction trans,
119: BTree btree, Object element) {
120: Assert.areEqual(1, btree.size(trans));
121:
122: final BTreeRange result = btree.search(trans, element);
123: ExpectingVisitor expectingVisitor = new ExpectingVisitor(
124: new Object[] { element });
125: BTreeAssert.traverseKeys(result, expectingVisitor);
126: expectingVisitor.assertExpectations();
127:
128: expectingVisitor = new ExpectingVisitor(
129: new Object[] { element });
130: btree.traverseKeys(trans, expectingVisitor);
131: expectingVisitor.assertExpectations();
132: }
133:
134: }
|