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.btree.*;
024:
025: import db4ounit.*;
026:
027: public class SearcherTestCase implements TestCase, TestLifeCycle {
028:
029: private Searcher _searcher;
030:
031: private final int FIRST = 4;
032:
033: private final int LAST = 11;
034:
035: private final int[] EVEN_VALUES = new int[] { 4, 7, 9, 11 };
036:
037: private final int[] ODD_VALUES = new int[] { 4, 7, 8, 9, 11 };
038:
039: private final int[] NON_MATCHES = new int[] { 3, 5, 6, 10, 12 };
040:
041: private final int[] MATCHES = new int[] { 4, 7, 9, 11 };
042:
043: private final int BEFORE = FIRST - 1;
044:
045: private final int BEYOND = LAST + 1;
046:
047: public void ttestPrintResults() {
048: // not a test, but nice to visualize
049: int[] evenValues = new int[] { 4, 7, 9, 11 };
050: int[] searches = new int[] { 3, 4, 5, 7, 10, 11, 12 };
051: for (int i = 0; i < searches.length; i++) {
052: int res = search(evenValues, searches[i]);
053: System.out.println(res);
054: }
055: }
056:
057: public void testCursorEndsOnSmaller() {
058: Assert.areEqual(0, search(EVEN_VALUES, 6));
059: Assert.areEqual(0, search(ODD_VALUES, 6));
060: Assert.areEqual(2, search(EVEN_VALUES, 10));
061: Assert.areEqual(3, search(ODD_VALUES, 10));
062: }
063:
064: public void testMatchEven() {
065: assertMatch(EVEN_VALUES);
066: }
067:
068: public void testMatchOdd() {
069: assertMatch(ODD_VALUES);
070: }
071:
072: public void testNoMatchEven() {
073: assertNoMatch(EVEN_VALUES);
074: }
075:
076: public void testNoMatchOdd() {
077: assertNoMatch(ODD_VALUES);
078: }
079:
080: public void testBeyondEven() {
081: assertBeyond(EVEN_VALUES);
082: }
083:
084: public void testBeyondOdd() {
085: assertBeyond(ODD_VALUES);
086: }
087:
088: public void testNotBeyondEven() {
089: assertNotBeyond(EVEN_VALUES);
090: }
091:
092: public void testNotBeyondOdd() {
093: assertNotBeyond(ODD_VALUES);
094: }
095:
096: public void testBeforeEven() {
097: assertBefore(EVEN_VALUES);
098: }
099:
100: public void testBeforeOdd() {
101: assertBefore(ODD_VALUES);
102: }
103:
104: public void testNotBeforeEven() {
105: assertNotBefore(EVEN_VALUES);
106: }
107:
108: public void testNotBeforeOdd() {
109: assertNotBefore(ODD_VALUES);
110: }
111:
112: public void testEmptySet() {
113: _searcher = new Searcher(SearchTarget.ANY, 0);
114: if (_searcher.incomplete()) {
115: Assert.fail();
116: }
117: Assert.areEqual(0, _searcher.cursor());
118: }
119:
120: private void assertMatch(int[] values) {
121: for (int i = 0; i < MATCHES.length; i++) {
122: search(values, MATCHES[i]);
123: Assert.isTrue(_searcher.foundMatch());
124: }
125: }
126:
127: private void assertNoMatch(int[] values) {
128: for (int i = 0; i < NON_MATCHES.length; i++) {
129: search(values, NON_MATCHES[i]);
130: Assert.isFalse(_searcher.foundMatch());
131: }
132: }
133:
134: private void assertBeyond(int[] values) {
135: int res = search(values, BEYOND);
136: Assert.areEqual(values.length - 1, res);
137: Assert.isTrue(_searcher.afterLast());
138: }
139:
140: private void assertNotBeyond(int[] values) {
141: int res = search(values, LAST);
142: Assert.areEqual(values.length - 1, res);
143: Assert.isFalse(_searcher.afterLast());
144: }
145:
146: private void assertBefore(int[] values) {
147: int res = search(values, BEFORE);
148: Assert.areEqual(0, res);
149: Assert.isTrue(_searcher.beforeFirst());
150: }
151:
152: private void assertNotBefore(int[] values) {
153: int res = search(values, FIRST);
154: Assert.areEqual(0, res);
155: Assert.isFalse(_searcher.beforeFirst());
156: }
157:
158: private int search(int[] values, int value) {
159:
160: _searcher = new Searcher(SearchTarget.ANY, values.length);
161:
162: while (_searcher.incomplete()) {
163: _searcher.resultIs(values[_searcher.cursor()] - value);
164: }
165:
166: return _searcher.cursor();
167: }
168:
169: public void setUp() throws Exception {
170: _searcher = null;
171: }
172:
173: public void tearDown() throws Exception {
174:
175: }
176:
177: }
|