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.internal.*;
024: import com.db4o.internal.btree.*;
025:
026: import db4ounit.Assert;
027: import db4ounit.extensions.AbstractDb4oTestCase;
028: import db4ounit.extensions.fixtures.OptOutCS;
029:
030: public abstract class BTreeTestCaseBase extends AbstractDb4oTestCase
031: implements OptOutCS {
032:
033: protected static final int BTREE_NODE_SIZE = 4;
034:
035: protected BTree _btree;
036:
037: protected void db4oSetupAfterStore() throws Exception {
038: _btree = newBTree();
039: }
040:
041: protected BTree newBTree() {
042: return BTreeAssert.createIntKeyBTree(stream(), 0,
043: BTREE_NODE_SIZE);
044: }
045:
046: protected BTreeRange range(int lower, int upper) {
047: final BTreeRange lowerRange = search(lower);
048: final BTreeRange upperRange = search(upper);
049: return lowerRange.extendToLastOf(upperRange);
050: }
051:
052: protected BTreeRange search(int key) {
053: return search(trans(), key);
054: }
055:
056: protected void add(int[] keys) {
057: for (int i = 0; i < keys.length; ++i) {
058: add(keys[i]);
059: }
060: }
061:
062: protected BTreeRange search(Transaction trans, int key) {
063: return _btree.search(trans, new Integer(key));
064: }
065:
066: protected void commit(Transaction trans) {
067: _btree.commit(trans);
068: }
069:
070: protected void commit() {
071: commit(trans());
072: }
073:
074: protected void remove(Transaction transaction, int[] keys) {
075: for (int i = 0; i < keys.length; i++) {
076: remove(transaction, keys[i]);
077: }
078: }
079:
080: protected void add(Transaction transaction, int[] keys) {
081: for (int i = 0; i < keys.length; i++) {
082: add(transaction, keys[i]);
083: }
084: }
085:
086: protected void assertEmpty(Transaction transaction) {
087: BTreeAssert.assertEmpty(transaction, _btree);
088: }
089:
090: protected void add(Transaction transaction, int element) {
091: _btree.add(transaction, new Integer(element));
092: }
093:
094: protected void remove(final int element) {
095: remove(trans(), element);
096: }
097:
098: protected void remove(final Transaction trans, final int element) {
099: _btree.remove(trans, new Integer(element));
100: }
101:
102: protected void add(int element) {
103: add(trans(), element);
104: }
105:
106: private int size() {
107: return _btree.size(trans());
108: }
109:
110: protected void assertSize(int expected) {
111: Assert.areEqual(expected, size());
112: }
113:
114: protected void assertSingleElement(final int element) {
115: assertSingleElement(trans(), element);
116: }
117:
118: protected void assertSingleElement(final Transaction trans,
119: final int element) {
120: BTreeAssert.assertSingleElement(trans, _btree, new Integer(
121: element));
122: }
123:
124: protected void assertPointerKey(int key, BTreePointer pointer) {
125: Assert.areEqual(new Integer(key), pointer.key());
126: }
127: }
|