001: /*
002: * $Id: TestIntArrayIndex.java,v 1.1 2004/08/13 02:01:11 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine.indexes;
042:
043: import junit.framework.Test;
044: import junit.framework.TestSuite;
045:
046: import org.axiondb.Column;
047: import org.axiondb.Index;
048: import org.axiondb.engine.indexes.IntArrayIndex;
049: import org.axiondb.functions.EqualFunction;
050: import org.axiondb.functions.GreaterThanFunction;
051: import org.axiondb.functions.GreaterThanOrEqualFunction;
052: import org.axiondb.functions.IsNotNullFunction;
053: import org.axiondb.functions.IsNullFunction;
054: import org.axiondb.functions.LessThanFunction;
055: import org.axiondb.functions.LessThanOrEqualFunction;
056: import org.axiondb.functions.NotEqualFunction;
057: import org.axiondb.types.IntegerType;
058:
059: /**
060: * @version $Revision: 1.1 $ $Date: 2004/08/13 02:01:11 $
061: * @author Rodney Waldhoff
062: * @author Chuck Burdick
063: */
064: public class TestIntArrayIndex extends AbstractIntIndexTest {
065:
066: //------------------------------------------------------------ Conventional
067:
068: public TestIntArrayIndex(String testName) {
069: super (testName);
070: }
071:
072: public static void main(String args[]) {
073: String[] testCaseName = { TestIntArrayIndex.class.getName() };
074: junit.textui.TestRunner.main(testCaseName);
075: }
076:
077: public static Test suite() {
078: return new TestSuite(TestIntArrayIndex.class);
079: }
080:
081: //--------------------------------------------------------------- Lifecycle
082:
083: private Column _column = null;
084:
085: public void setUp() throws Exception {
086: super .setUp();
087: _column = new Column("val", new IntegerType());
088: }
089:
090: public void tearDown() throws Exception {
091: super .tearDown();
092: _column = null;
093: }
094:
095: //------------------------------------------------------------------- Tests
096:
097: public void testFindRequired() throws Exception {
098: IntArrayIndex index = new IntArrayIndex("MYINDEX", _column,
099: true);
100: findRequired(index);
101: }
102:
103: public void testFindRequiredNonUnique() throws Exception {
104: IntArrayIndex index = new IntArrayIndex("MYINDEX", _column,
105: false);
106: findRequired(index);
107: }
108:
109: public void testFind() throws Exception {
110: IntArrayIndex index = new IntArrayIndex("MYINDEX", _column,
111: true);
112: find(index);
113: }
114:
115: public void testFindNonUnique() throws Exception {
116: IntArrayIndex index = new IntArrayIndex("MYINDEX", _column,
117: false);
118: find(index);
119: }
120:
121: public void testSupportsFunction() throws Exception {
122: IntArrayIndex index = new IntArrayIndex("testindex", _column,
123: true);
124: assertTrue(index.supportsFunction(new EqualFunction()));
125: assertTrue(index.supportsFunction(new GreaterThanFunction()));
126: assertTrue(index
127: .supportsFunction(new GreaterThanOrEqualFunction()));
128: assertTrue(!index.supportsFunction(new IsNotNullFunction()));
129: assertTrue(!index.supportsFunction(new IsNullFunction()));
130: assertTrue(index.supportsFunction(new LessThanFunction()));
131: assertTrue(index
132: .supportsFunction(new LessThanOrEqualFunction()));
133: assertTrue(!index.supportsFunction(new NotEqualFunction()));
134: }
135:
136: public void testRemoveKey() throws Exception {
137: IntArrayIndex index = new IntArrayIndex("testindex", _column,
138: true);
139: index.insertKey(10);
140: assertEquals(0, index.find(10, true));
141: index.removeKey(10);
142: assertEquals(-1, index.find(10, true));
143: index.removeKey(10);
144: assertEquals(-1, index.find(10, true));
145: }
146:
147: public void testTruncate() throws Exception {
148: IntArrayIndex index = new IntArrayIndex("MYINDEX", _column,
149: true);
150:
151: index.insertKey(10);
152: index.truncate();
153: assertEquals(-1, index.find(10, true));
154: }
155:
156: private void findRequired(IntArrayIndex index) throws Exception {
157: assertEquals(-1, index.find(0, true));
158: assertEquals(-1, index.find(1, true));
159: assertEquals(-1, index.find(2, true));
160: assertEquals(-1, index.find(3, true));
161: assertEquals(-1, index.find(4, true));
162: assertEquals(-1, index.find(5, true));
163: assertEquals(-1, index.find(6, true));
164: index.insertKey(5);
165: assertEquals(-1, index.find(0, true));
166: assertEquals(-1, index.find(1, true));
167: assertEquals(-1, index.find(2, true));
168: assertEquals(-1, index.find(3, true));
169: assertEquals(-1, index.find(4, true));
170: assertEquals(0, index.find(5, true));
171: assertEquals(-1, index.find(6, true));
172: index.insertKey(1);
173: assertEquals(-1, index.find(0, true));
174: assertEquals(0, index.find(1, true));
175: assertEquals(-1, index.find(2, true));
176: assertEquals(-1, index.find(3, true));
177: assertEquals(-1, index.find(4, true));
178: assertEquals(1, index.find(5, true));
179: assertEquals(-1, index.find(6, true));
180: index.insertKey(3);
181: assertEquals(-1, index.find(0, true));
182: assertEquals(0, index.find(1, true));
183: assertEquals(-1, index.find(2, true));
184: assertEquals(1, index.find(3, true));
185: assertEquals(-1, index.find(4, true));
186: assertEquals(2, index.find(5, true));
187: assertEquals(-1, index.find(6, true));
188: index.insertKey(6);
189: assertEquals(-1, index.find(0, true));
190: assertEquals(0, index.find(1, true));
191: assertEquals(-1, index.find(2, true));
192: assertEquals(1, index.find(3, true));
193: assertEquals(-1, index.find(4, true));
194: assertEquals(2, index.find(5, true));
195: assertEquals(3, index.find(6, true));
196: index.insertKey(0);
197: assertEquals(0, index.find(0, true));
198: assertEquals(1, index.find(1, true));
199: assertEquals(-1, index.find(2, true));
200: assertEquals(2, index.find(3, true));
201: assertEquals(-1, index.find(4, true));
202: assertEquals(3, index.find(5, true));
203: assertEquals(4, index.find(6, true));
204: }
205:
206: private void find(IntArrayIndex index) throws Exception {
207: assertEquals(0, index.find(-1, false));
208: assertEquals(0, index.find(0, false));
209: assertEquals(0, index.find(1, false));
210: assertEquals(0, index.find(2, false));
211: assertEquals(0, index.find(3, false));
212: assertEquals(0, index.find(4, false));
213: assertEquals(0, index.find(5, false));
214: assertEquals(0, index.find(6, false));
215: assertEquals(0, index.find(7, false));
216: index.insertKey(5);
217: assertEquals(0, index.find(-1, false));
218: assertEquals(0, index.find(0, false));
219: assertEquals(0, index.find(1, false));
220: assertEquals(0, index.find(2, false));
221: assertEquals(0, index.find(3, false));
222: assertEquals(0, index.find(4, false));
223: assertEquals(0, index.find(5, false));
224: assertEquals(1, index.find(6, false));
225: assertEquals(1, index.find(7, false));
226: index.insertKey(1);
227: assertEquals(0, index.find(-1, false));
228: assertEquals(0, index.find(0, false));
229: assertEquals(0, index.find(1, false));
230: assertEquals(1, index.find(2, false));
231: assertEquals(1, index.find(3, false));
232: assertEquals(1, index.find(4, false));
233: assertEquals(1, index.find(5, false));
234: assertEquals(2, index.find(6, false));
235: assertEquals(2, index.find(7, false));
236: index.insertKey(3);
237: assertEquals(0, index.find(-1, false));
238: assertEquals(0, index.find(0, false));
239: assertEquals(0, index.find(1, false));
240: assertEquals(1, index.find(2, false));
241: assertEquals(1, index.find(3, false));
242: assertEquals(2, index.find(4, false));
243: assertEquals(2, index.find(5, false));
244: assertEquals(3, index.find(6, false));
245: assertEquals(3, index.find(7, false));
246: index.insertKey(6);
247: assertEquals(0, index.find(-1, false));
248: assertEquals(0, index.find(0, false));
249: assertEquals(0, index.find(1, false));
250: assertEquals(1, index.find(2, false));
251: assertEquals(1, index.find(3, false));
252: assertEquals(2, index.find(4, false));
253: assertEquals(2, index.find(5, false));
254: assertEquals(3, index.find(6, false));
255: assertEquals(4, index.find(7, false));
256: index.insertKey(0);
257: assertEquals(0, index.find(-1, false));
258: assertEquals(0, index.find(0, false));
259: assertEquals(1, index.find(1, false));
260: assertEquals(2, index.find(2, false));
261: assertEquals(2, index.find(3, false));
262: assertEquals(3, index.find(4, false));
263: assertEquals(3, index.find(5, false));
264: assertEquals(4, index.find(6, false));
265: assertEquals(5, index.find(7, false));
266: }
267:
268: //========================================================= TEST FRAMEWORK
269:
270: protected Index createIndex(String name, Column col, boolean unique) {
271: return new IntArrayIndex(name, col, unique);
272: }
273: }
|