KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > btree > BTreeSearchTestCase


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.db4ounit.common.btree;
22
23 import com.db4o.*;
24 import com.db4o.db4ounit.common.foundation.IntArrays4;
25 import com.db4o.inside.btree.*;
26
27 import db4ounit.extensions.AbstractDb4oTestCase;
28 import db4ounit.extensions.fixtures.*;
29
30 public class BTreeSearchTestCase extends AbstractDb4oTestCase implements
31         OptOutDefragSolo, OptOutCS {
32
33     protected static final int BTREE_NODE_SIZE = 4;
34
35     public static void main(String JavaDoc[] arguments) {
36         new BTreeSearchTestCase().runSolo();
37     }
38
39     public void test() throws Exception JavaDoc {
40         cycleIntKeys(new int[] { 3, 5, 5, 5, 7, 10, 11, 12, 12, 14 });
41         cycleIntKeys(new int[] { 3, 5, 5, 5, 5, 7, 10, 11, 12, 12, 14 });
42         cycleIntKeys(new int[] { 3, 5, 5, 5, 5, 5, 7, 10, 11, 12, 12, 14 });
43         cycleIntKeys(new int[] { 3, 3, 5, 5, 5, 7, 10, 11, 12, 12, 14, 14 });
44         cycleIntKeys(new int[] { 3, 3, 3, 5, 5, 5, 7, 10, 11, 12, 12, 14, 14,
45                 14 });
46     }
47
48     private void cycleIntKeys(int[] values) throws Exception JavaDoc {
49         BTree btree = BTreeAssert.createIntKeyBTree(stream(), 0,
50                 BTREE_NODE_SIZE);
51         for (int i = 0; i < 5; i++) {
52             btree = cycleIntKeys(btree, values);
53         }
54     }
55
56     private BTree cycleIntKeys(BTree btree, int[] values) throws Exception JavaDoc {
57         for (int i = 0; i < values.length; i++) {
58             btree.add(trans(), new Integer JavaDoc(values[i]));
59         }
60         expectKeysSearch(trans(), btree, values);
61
62         btree.commit(trans());
63
64         int id = btree.getID();
65
66         stream().commit();
67
68         reopen();
69
70         btree = BTreeAssert.createIntKeyBTree(stream(), id, BTREE_NODE_SIZE);
71
72         expectKeysSearch(trans(), btree, values);
73
74         for (int i = 0; i < values.length; i++) {
75             btree.remove(trans(), new Integer JavaDoc(values[i]));
76         }
77
78         BTreeAssert.assertEmpty(trans(), btree);
79
80         btree.commit(trans());
81
82         BTreeAssert.assertEmpty(trans(), btree);
83
84         return btree;
85     }
86
87     private void expectKeysSearch(Transaction trans, BTree btree, int[] keys) {
88         int lastValue = Integer.MIN_VALUE;
89         for (int i = 0; i < keys.length; i++) {
90             if (keys[i] != lastValue) {
91                 ExpectingVisitor expectingVisitor = BTreeAssert
92                         .createExpectingVisitor(keys[i], IntArrays4.occurences(
93                                 keys, keys[i]));
94                 BTreeRange range = btree.search(trans, new Integer JavaDoc(keys[i]));
95                 BTreeAssert.traverseKeys(range, expectingVisitor);
96                 expectingVisitor.assertExpectations();
97                 lastValue = keys[i];
98             }
99         }
100     }
101 }
102
Popular Tags