KickJava   Java API By Example, From Geeks To Geeks.

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


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.inside.btree.*;
24
25 import db4ounit.*;
26
27
28 public class SearcherTestCase implements TestCase, TestLifeCycle{
29     
30     private Searcher _searcher;
31     
32     private final int FIRST = 4;
33     
34     private final int LAST = 11;
35     
36     private final int[] EVEN_VALUES = new int[] {4, 7, 9, 11};
37     
38     private final int[] ODD_VALUES = new int[] {4, 7, 8, 9, 11};
39
40     private final int[] NON_MATCHES = new int[] {3, 5, 6, 10, 12};
41
42     private final int[] MATCHES = new int[] {4, 7, 9, 11};
43     
44     private final int BEFORE = FIRST - 1;
45     
46     private final int BEYOND = LAST + 1;
47     
48     public void ttestPrintResults(){
49         // not a test, but nice to visualize
50
int[] evenValues = new int[] {4, 7, 9, 11};
51         int[] searches = new int[]{3, 4, 5, 7, 10, 11, 12};
52         for (int i = 0; i < searches.length; i++) {
53             int res = search(evenValues, searches[i]);
54             System.out.println(res);
55         }
56     }
57     
58     public void testCursorEndsOnSmaller(){
59         Assert.areEqual(0, search(EVEN_VALUES, 6));
60         Assert.areEqual(0, search(ODD_VALUES, 6));
61         Assert.areEqual(2, search(EVEN_VALUES, 10));
62         Assert.areEqual(3, search(ODD_VALUES, 10));
63     }
64     
65     public void testMatchEven(){
66         assertMatch(EVEN_VALUES);
67     }
68     
69     public void testMatchOdd(){
70         assertMatch(ODD_VALUES);
71     }
72     
73     public void testNoMatchEven(){
74         assertNoMatch(EVEN_VALUES);
75     }
76     
77     public void testNoMatchOdd(){
78         assertNoMatch(ODD_VALUES);
79     }
80     
81     public void testBeyondEven(){
82         assertBeyond(EVEN_VALUES);
83     }
84     
85     public void testBeyondOdd(){
86         assertBeyond(ODD_VALUES);
87     }
88     
89     public void testNotBeyondEven(){
90         assertNotBeyond(EVEN_VALUES);
91     }
92     
93     public void testNotBeyondOdd(){
94         assertNotBeyond(ODD_VALUES);
95     }
96
97     public void testBeforeEven(){
98         assertBefore(EVEN_VALUES);
99     }
100     
101     public void testBeforeOdd(){
102         assertBefore(ODD_VALUES);
103     }
104     
105     public void testNotBeforeEven(){
106         assertNotBefore(EVEN_VALUES);
107     }
108     
109     public void testNotBeforeOdd(){
110         assertNotBefore(ODD_VALUES);
111     }
112     
113     public void testEmptySet(){
114         _searcher = new Searcher(SearchTarget.ANY, 0);
115         if(_searcher.incomplete()){
116             Assert.fail();
117         }
118         Assert.areEqual(0, _searcher.cursor());
119     }
120
121
122     private void assertMatch(int[] values) {
123         for (int i = 0; i < MATCHES.length; i++) {
124             search(values, MATCHES[i]);
125             Assert.isTrue(_searcher.foundMatch());
126         }
127     }
128
129     private void assertNoMatch(int[] values) {
130         for (int i = 0; i < NON_MATCHES.length; i++) {
131             search(values, NON_MATCHES[i]);
132             Assert.isFalse(_searcher.foundMatch());
133         }
134     }
135     
136     private void assertBeyond(int[] values) {
137         int res = search(values, BEYOND);
138         Assert.areEqual(values.length - 1, res);
139         Assert.isTrue(_searcher.afterLast());
140     }
141
142     private void assertNotBeyond(int[] values) {
143         int res = search(values, LAST);
144         Assert.areEqual(values.length - 1, res);
145         Assert.isFalse(_searcher.afterLast());
146     }
147     
148     private void assertBefore(int[] values) {
149         int res = search(values, BEFORE);
150         Assert.areEqual(0, res);
151         Assert.isTrue(_searcher.beforeFirst());
152     }
153
154     private void assertNotBefore(int[] values) {
155         int res = search(values, FIRST);
156         Assert.areEqual(0, res);
157         Assert.isFalse(_searcher.beforeFirst());
158     }
159
160     
161     
162     
163     private int search(int[] values, int value){
164         
165         _searcher = new Searcher(SearchTarget.ANY, values.length);
166         
167         while(_searcher.incomplete()){
168             _searcher.resultIs( values[_searcher.cursor()] - value );
169         }
170         
171         return _searcher.cursor();
172     }
173
174     public void setUp() throws Exception JavaDoc {
175         _searcher = null;
176     }
177
178     public void tearDown() throws Exception JavaDoc {
179         
180     }
181
182 }
183
Popular Tags