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.Iterator4;
025: import com.db4o.internal.btree.*;
026:
027: import db4ounit.Assert;
028:
029: /**
030: * @exclude
031: */
032: public class BTreePointerTestCase extends BTreeTestCaseBase {
033:
034: public static void main(String[] args) {
035: new BTreePointerTestCase().runSolo();
036: }
037:
038: private final int[] keys = new int[] { -5, -4, -3, -2, -1, 0, 1, 2,
039: 3, 4, 7, 9 };
040:
041: protected void db4oSetupAfterStore() throws Exception {
042: super .db4oSetupAfterStore();
043: add(keys);
044: commit();
045: }
046:
047: public void testLastPointer() {
048: BTreePointer pointer = _btree.lastPointer(trans());
049: assertPointerKey(9, pointer);
050: }
051:
052: public void testPrevious() {
053: BTreePointer pointer = getPointerForKey(3);
054: BTreePointer previousPointer = pointer.previous();
055: assertPointerKey(2, previousPointer);
056: }
057:
058: public void testNextOperatesInReadMode() {
059: BTreePointer pointer = _btree.firstPointer(trans());
060: assertReadModePointerIteration(keys, pointer);
061: }
062:
063: public void testSearchOperatesInReadMode() {
064: final BTreePointer pointer = getPointerForKey(3);
065: assertReadModePointerIteration(new int[] { 3, 4, 7, 9 },
066: pointer);
067: }
068:
069: private BTreePointer getPointerForKey(final int key) {
070: final BTreeRange range = search(key);
071: final Iterator4 pointers = range.pointers();
072: Assert.isTrue(pointers.moveNext());
073: final BTreePointer pointer = (BTreePointer) pointers.current();
074: return pointer;
075: }
076:
077: private void assertReadModePointerIteration(
078: final int[] expectedKeys, BTreePointer pointer) {
079: Object[] expected = IntArrays4.toObjectArray(expectedKeys);
080: for (int i = 0; i < expected.length; i++) {
081: Assert.isNotNull(pointer, "Expected '" + expected[i] + "'");
082: Assert.areNotSame(_btree.root(), pointer.node());
083: assertInReadMode(pointer.node());
084: Assert.areEqual(expected[i], pointer.key());
085: assertInReadMode(pointer.node());
086: pointer = pointer.next();
087: }
088: }
089:
090: private void assertInReadMode(BTreeNode node) {
091: Assert.isFalse(node.canWrite());
092: }
093:
094: protected BTree newBTree() {
095: return newBTreeWithNoNodeCaching();
096: }
097:
098: private BTree newBTreeWithNoNodeCaching() {
099: return BTreeAssert.createIntKeyBTree(stream(), 0, 0,
100: BTREE_NODE_SIZE);
101: }
102:
103: }
|