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.internal.*;
025: import com.db4o.internal.btree.*;
026:
027: import db4ounit.extensions.AbstractDb4oTestCase;
028: import db4ounit.extensions.fixtures.*;
029:
030: public class BTreeSearchTestCase extends AbstractDb4oTestCase implements
031: OptOutDefragSolo, OptOutCS {
032:
033: protected static final int BTREE_NODE_SIZE = 4;
034:
035: public static void main(String[] arguments) {
036: new BTreeSearchTestCase().runSolo();
037: }
038:
039: public void test() throws Exception {
040: cycleIntKeys(new int[] { 3, 5, 7, 10, 11, 12, 14, 15, 17, 20,
041: 21, 25 });
042: }
043:
044: private void cycleIntKeys(int[] values) throws Exception {
045: BTree btree = BTreeAssert.createIntKeyBTree(stream(), 0,
046: BTREE_NODE_SIZE);
047: for (int i = 0; i < 5; i++) {
048: btree = cycleIntKeys(btree, values);
049: }
050: }
051:
052: private BTree cycleIntKeys(BTree btree, int[] values)
053: throws Exception {
054: for (int i = 0; i < values.length; i++) {
055: btree.add(trans(), new Integer(values[i]));
056: }
057: expectKeysSearch(trans(), btree, values);
058:
059: btree.commit(trans());
060:
061: int id = btree.getID();
062:
063: stream().commit(trans());
064:
065: reopen();
066:
067: btree = BTreeAssert.createIntKeyBTree(stream(), id,
068: BTREE_NODE_SIZE);
069:
070: expectKeysSearch(trans(), btree, values);
071:
072: for (int i = 0; i < values.length; i++) {
073: btree.remove(trans(), new Integer(values[i]));
074: }
075:
076: BTreeAssert.assertEmpty(trans(), btree);
077:
078: btree.commit(trans());
079:
080: BTreeAssert.assertEmpty(trans(), btree);
081:
082: return btree;
083: }
084:
085: private void expectKeysSearch(Transaction trans, BTree btree,
086: int[] keys) {
087: int lastValue = Integer.MIN_VALUE;
088: for (int i = 0; i < keys.length; i++) {
089: if (keys[i] != lastValue) {
090: ExpectingVisitor expectingVisitor = BTreeAssert
091: .createExpectingVisitor(keys[i], IntArrays4
092: .occurences(keys, keys[i]));
093: BTreeRange range = btree.search(trans, new Integer(
094: keys[i]));
095: BTreeAssert.traverseKeys(range, expectingVisitor);
096: expectingVisitor.assertExpectations();
097: lastValue = keys[i];
098: }
099: }
100: }
101: }
|