001: /*
002: * $Id: TestIntRowMap.java,v 1.2 2005/12/22 09:02:29 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2003 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.util;
042:
043: import java.util.NoSuchElementException;
044:
045: import junit.framework.Test;
046: import junit.framework.TestCase;
047: import junit.framework.TestSuite;
048:
049: import org.apache.commons.collections.primitives.IntIterator;
050: import org.axiondb.RowCollection;
051: import org.axiondb.RowIterator;
052: import org.axiondb.engine.rowcollection.IntRowMap;
053: import org.axiondb.engine.rows.SimpleRow;
054:
055: /**
056: * @version $Revision: 1.2 $ $Date: 2005/12/22 09:02:29 $
057: * @author Ahimanikya Satapathy
058: */
059: public class TestIntRowMap extends TestCase {
060:
061: //------------------------------------------------------------ Conventional
062:
063: public TestIntRowMap(String testName) {
064: super (testName);
065: }
066:
067: public static Test suite() {
068: return new TestSuite(TestIntRowMap.class);
069: }
070:
071: //------------------------------------------------------------------- Tests
072:
073: public void testEmpty() throws Exception {
074: IntRowMap map = new IntRowMap();
075: RowIterator iter = map.rowValues().rowIterator();
076: for (int i = 0; i < 3; i++) {
077: assertTrue(!iter.hasNext());
078: assertEquals(0, iter.nextIndex());
079: assertTrue(!iter.hasPrevious());
080: assertEquals(-1, iter.previousIndex());
081: try {
082: iter.next();
083: fail("Expected NoSuchElementException");
084: } catch (NoSuchElementException e) {
085: // expected
086: }
087: try {
088: iter.previous();
089: fail("Expected NoSuchElementException");
090: } catch (NoSuchElementException e) {
091: // expected
092: }
093: }
094: }
095:
096: public void testCopy() throws Exception {
097: IntRowMap map = new IntRowMap();
098: IntRowMap map2 = new IntRowMap();
099: for (int i = 0; i < 13; i++) {
100: map.put(i, new SimpleRow(new Object[] { new Integer(i) }));
101: }
102: map2.putAll(map);
103: map.clear();
104: map2.putAll(map);
105:
106: // Test int key iterator
107: {
108: IntIterator iter = map2.keyIterator();
109: for (int i = 0; i < 13; i++) {
110: assertTrue(iter.hasNext());
111: assertEquals(i, iter.next());
112: }
113: assertTrue(!iter.hasNext());
114: }
115: }
116:
117: public void testValues() throws Exception {
118: IntRowMap map = new IntRowMap();
119: for (int i = 0; i < 10; i++) {
120: map.put(i, new SimpleRow(new Object[] { new Integer(i) }));
121: }
122: RowCollection values = map.rowValues();
123: assertNotNull(values.toString());
124: assertEquals(10, values.size());
125:
126: // Test value iterator
127: {
128: RowIterator iter = values.rowIterator();
129: for (int i = 0; i < 10; i++) {
130: assertTrue(iter.hasNext());
131: assertEquals(new SimpleRow(
132: new Object[] { new Integer(i) }), iter.next());
133: }
134: assertTrue(!iter.hasNext());
135: }
136:
137: // Test value list iterator
138: {
139: RowIterator iter = map.rowValues().rowIterator();
140: assertNotNull(iter.toString());
141: assertTrue(!iter.hasPrevious());
142: for (int i = 0; i < 10; i++) {
143: assertEquals(i, iter.nextIndex());
144: assertTrue(iter.hasNext());
145: assertEquals(new SimpleRow(
146: new Object[] { new Integer(i) }), iter.next());
147: assertNotNull(iter.toString());
148: assertEquals(i + 1, iter.nextIndex());
149: assertEquals(i, iter.previousIndex());
150: assertTrue(iter.hasPrevious());
151: }
152: assertTrue(!iter.hasNext());
153:
154: for (int i = 9; i > 0; i--) {
155: assertEquals(i + 1, iter.nextIndex());
156: assertEquals(i, iter.previousIndex());
157: assertTrue(iter.hasPrevious());
158: assertEquals(new SimpleRow(
159: new Object[] { new Integer(i) }), iter
160: .previous());
161: assertEquals(i, iter.nextIndex());
162: assertEquals(i - 1, iter.previousIndex());
163: assertTrue(iter.hasNext());
164: }
165:
166: }
167:
168: for (int i = 0; i < 10; i++) {
169: assertTrue(values.contains(new SimpleRow(
170: new Object[] { new Integer(i) })));
171: assertTrue(values.remove(new SimpleRow(
172: new Object[] { new Integer(i) })));
173: assertFalse(values.contains(new SimpleRow(
174: new Object[] { new Integer(i) })));
175: }
176: assertEquals(0, values.size());
177:
178: for (int i = 0; i < 10; i++) {
179: map.put(i, new SimpleRow(new Object[] { new Integer(i) }));
180: }
181: values = map.rowValues();
182: assertEquals(10, values.size());
183: values.clear();
184: assertEquals(0, values.size());
185: }
186:
187: public void testIntKeySet() throws Exception {
188: IntRowMap map = new IntRowMap();
189: for (int i = 0; i < 10; i++) {
190: map.put(i, new SimpleRow(new Object[] { new Integer(i) }));
191: }
192:
193: // Test int key iterator
194: {
195: IntIterator iter = map.keyIterator();
196: for (int i = 0; i < 10; i++) {
197: assertTrue(iter.hasNext());
198: assertEquals(i, iter.next());
199: }
200: assertTrue(!iter.hasNext());
201: }
202: }
203:
204: public void testIntEntrySet() throws Exception {
205: IntRowMap map = new IntRowMap();
206: IntRowMap.Entry[] entries = new IntRowMap.Entry[10];
207: for (int i = 0; i < 10; i++) {
208: map.put(i, new SimpleRow(new Object[] { new Integer(i) }));
209: }
210:
211: // Test entry iterator
212: {
213: IntRowMap.EntryIterator iter = map.entryIterator();
214: for (int i = 0; i < 10; i++) {
215: assertTrue(iter.hasNext());
216: entries[i] = iter.nextEntry();
217: assertEquals(i, entries[i].getKey());
218: assertEquals(new SimpleRow(
219: new Object[] { new Integer(i) }), entries[i]
220: .getValue());
221:
222: assertTrue(entries[i].equals(entries[i]));
223: assertFalse(entries[i].equals(entries[(i + 1) % 9]));
224:
225: assertTrue(entries[i].hashCode() == entries[i]
226: .hashCode());
227: }
228: assertTrue(!iter.hasNext());
229: }
230:
231: IntRowMap.EntryIterator iter = map.entryIterator();
232: while (iter.hasNext()) {
233: iter.nextEntry();
234: iter.remove();
235: }
236: assertEquals(0, iter.size());
237:
238: for (int i = 0; i < 10; i++) {
239: map.put(i, new SimpleRow(new Object[] { new Integer(i) }));
240: }
241: iter = map.entryIterator();
242: assertEquals(10, iter.size());
243: map.clear();
244: assertEquals(0, iter.size());
245: }
246:
247: public void testIntHashMap() throws Exception {
248: IntRowMap map = new IntRowMap();
249: for (int i = 0; i < 10; i++) {
250: map.put(i, new SimpleRow(new Object[] { new Integer(i) }));
251: }
252:
253: assertEquals(10, map.size());
254:
255: for (int i = 0; i < 10; i++) {
256: assertTrue(map.containsKey(i));
257: assertEquals(
258: new SimpleRow(new Object[] { new Integer(i) }), map
259: .remove(i));
260: assertFalse(map.containsKey(i));
261: }
262: assertEquals(0, map.size());
263:
264: for (int i = 0; i < 10; i++) {
265: map.put(i, new SimpleRow(new Object[] { new Integer(i) }));
266: }
267:
268: assertEquals(10, map.size());
269: map.clear();
270: assertEquals(0, map.size());
271:
272: for (int i = 0; i < 10; i++) {
273: map.put(i, new SimpleRow(new Object[] { new Integer(i) }));
274: }
275:
276: for (int i = 0; i < 10; i++) {
277: assertTrue(map.containsValue(new SimpleRow(
278: new Object[] { new Integer(i) })));
279: assertEquals(
280: new SimpleRow(new Object[] { new Integer(i) }), map
281: .remove(i));
282: assertFalse(map.containsValue(new SimpleRow(
283: new Object[] { new Integer(i) })));
284: }
285:
286: assertEquals(0, map.size());
287:
288: }
289: }
|