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.Transaction;
024: import com.db4o.internal.btree.BTree;
025:
026: import db4ounit.extensions.AbstractDb4oTestCase;
027: import db4ounit.extensions.fixtures.*;
028:
029: public class BTreeSimpleTestCase extends AbstractDb4oTestCase implements
030: OptOutDefragSolo, OptOutCS {
031:
032: protected static final int BTREE_NODE_SIZE = 4;
033:
034: int[] _keys = { 3, 234, 55, 87, 2, 1, 101, 59, 70, 300, 288 };
035:
036: int[] _values;
037:
038: int[] _sortedKeys = { 1, 2, 3, 55, 59, 70, 87, 101, 234, 288, 300 };
039:
040: int[] _sortedValues;
041:
042: int[] _keysOnRemoval = { 1, 2, 55, 59, 70, 87, 234, 288, 300 };
043:
044: int[] _valuesOnRemoval;
045:
046: int[] _one = { 1 };
047:
048: int[] _none = {};
049:
050: public BTreeSimpleTestCase() {
051: super ();
052:
053: _values = new int[_keys.length];
054: for (int i = 0; i < _keys.length; i++) {
055: _values[i] = _keys[i];
056: }
057:
058: _sortedValues = new int[_sortedKeys.length];
059: for (int i = 0; i < _sortedKeys.length; i++) {
060: _sortedValues[i] = _sortedKeys[i];
061: }
062:
063: _valuesOnRemoval = new int[_keysOnRemoval.length];
064: for (int i = 0; i < _keysOnRemoval.length; i++) {
065: _valuesOnRemoval[i] = _keysOnRemoval[i];
066: }
067: }
068:
069: public void testIntKeys() throws Exception {
070: BTree btree = BTreeAssert.createIntKeyBTree(stream(), 0,
071: BTREE_NODE_SIZE);
072: for (int i = 0; i < 5; i++) {
073: btree = cycleIntKeys(btree);
074: }
075: }
076:
077: private BTree cycleIntKeys(BTree btree) throws Exception {
078: addKeys(btree);
079: expectKeys(btree, _sortedKeys);
080:
081: btree.commit(trans());
082:
083: expectKeys(btree, _sortedKeys);
084:
085: removeKeys(btree);
086:
087: expectKeys(btree, _keysOnRemoval);
088:
089: btree.rollback(trans());
090:
091: expectKeys(btree, _sortedKeys);
092:
093: int id = btree.getID();
094:
095: reopen();
096:
097: btree = BTreeAssert.createIntKeyBTree(stream(), id,
098: BTREE_NODE_SIZE);
099:
100: expectKeys(btree, _sortedKeys);
101:
102: removeKeys(btree);
103:
104: expectKeys(btree, _keysOnRemoval);
105:
106: btree.commit(trans());
107:
108: expectKeys(btree, _keysOnRemoval);
109:
110: // remove all but 1
111: for (int i = 1; i < _keysOnRemoval.length; i++) {
112: btree.remove(trans(), new Integer(_keysOnRemoval[i]));
113: }
114:
115: expectKeys(btree, _one);
116:
117: btree.commit(trans());
118:
119: expectKeys(btree, _one);
120:
121: btree.remove(trans(), new Integer(1));
122:
123: btree.rollback(trans());
124:
125: expectKeys(btree, _one);
126:
127: btree.remove(trans(), new Integer(1));
128:
129: btree.commit(trans());
130:
131: expectKeys(btree, _none);
132:
133: return btree;
134:
135: }
136:
137: private void removeKeys(BTree btree) {
138: btree.remove(trans(), new Integer(3));
139: btree.remove(trans(), new Integer(101));
140: }
141:
142: private void addKeys(BTree btree) {
143: Transaction trans = trans();
144: for (int i = 0; i < _keys.length; i++) {
145: btree.add(trans, new Integer(_keys[i]));
146: }
147: }
148:
149: private void expectKeys(BTree btree, final int[] keys) {
150: BTreeAssert.assertKeys(trans(), btree, keys);
151: }
152: }
|