001: /*
002: * $Id: TestIntIteratorIntListIterator.java,v 1.1 2003/05/15 22:51:39 rwald 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.ArrayIntList;
050: import org.apache.commons.collections.primitives.IntList;
051: import org.apache.commons.collections.primitives.IntListIterator;
052:
053: /**
054: * @version $Revision: 1.1 $ $Date: 2003/05/15 22:51:39 $
055: * @author Rodney Waldhoff
056: */
057: public class TestIntIteratorIntListIterator extends TestCase {
058:
059: //------------------------------------------------------------ Conventional
060:
061: public TestIntIteratorIntListIterator(String testName) {
062: super (testName);
063: }
064:
065: public static Test suite() {
066: return new TestSuite(TestIntIteratorIntListIterator.class);
067: }
068:
069: //------------------------------------------------------------------- Tests
070:
071: public void testEmpty() throws Exception {
072: IntListIterator iter = new IntIteratorIntListIterator(
073: (new ArrayIntList()).iterator());
074: for (int i = 0; i < 3; i++) {
075: assertTrue(!iter.hasNext());
076: assertEquals(0, iter.nextIndex());
077: assertTrue(!iter.hasPrevious());
078: assertEquals(-1, iter.previousIndex());
079: try {
080: iter.next();
081: fail("Expected NoSuchElementException");
082: } catch (NoSuchElementException e) {
083: // expected
084: }
085: try {
086: iter.previous();
087: fail("Expected NoSuchElementException");
088: } catch (NoSuchElementException e) {
089: // expected
090: }
091: }
092: }
093:
094: public void testEndToEndWalk() throws Exception {
095: IntList list = new ArrayIntList();
096: for (int i = 0; i < 10; i++) {
097: list.add(i);
098: }
099: IntListIterator iter = new IntIteratorIntListIterator(list
100: .iterator());
101:
102: for (int j = 0; j < 3; j++) {
103: assertTrue(!iter.hasPrevious());
104:
105: for (int i = 0; i < 10; i++) {
106: assertEquals(i, iter.nextIndex());
107: assertEquals(i - 1, iter.previousIndex());
108: assertTrue(iter.hasNext());
109: assertEquals(i, iter.next());
110: assertEquals(i + 1, iter.nextIndex());
111: assertEquals(i, iter.previousIndex());
112: assertTrue(iter.hasPrevious());
113: }
114:
115: assertTrue(!iter.hasNext());
116:
117: for (int i = 9; i >= 0; i--) {
118: assertEquals(i + 1, iter.nextIndex());
119: assertEquals(i, iter.previousIndex());
120: assertTrue(iter.hasPrevious());
121: assertEquals(i, iter.previous());
122: assertEquals(i, iter.nextIndex());
123: assertEquals(i - 1, iter.previousIndex());
124: assertTrue(iter.hasNext());
125: }
126:
127: assertTrue(!iter.hasPrevious());
128: }
129: }
130:
131: public void testSillyWalk() throws Exception {
132: IntList list = new ArrayIntList();
133: for (int i = 0; i < 10; i++) {
134: list.add(i);
135: }
136: IntListIterator iter = new IntIteratorIntListIterator(list
137: .iterator());
138:
139: for (int k = 0; k < 3; k++) {
140: assertTrue(!iter.hasPrevious());
141:
142: for (int j = 0; j < 10; j++) {
143: for (int i = 0; i < j; i++) {
144: assertEquals(i, iter.nextIndex());
145: assertEquals(i - 1, iter.previousIndex());
146: assertTrue(iter.hasNext());
147: assertEquals(i, iter.next());
148: assertEquals(i + 1, iter.nextIndex());
149: assertEquals(i, iter.previousIndex());
150: assertTrue(iter.hasPrevious());
151: }
152: for (int i = (j - 1); i >= 0; i--) {
153: assertEquals(i + 1, iter.nextIndex());
154: assertEquals(i, iter.previousIndex());
155: assertTrue(iter.hasPrevious());
156: assertEquals(i, iter.previous());
157: assertEquals(i, iter.nextIndex());
158: assertEquals(i - 1, iter.previousIndex());
159: assertTrue(iter.hasNext());
160: }
161: }
162:
163: assertTrue(!iter.hasPrevious());
164:
165: for (int i = 0; i < 10; i++) {
166: assertEquals(i, iter.nextIndex());
167: assertEquals(i - 1, iter.previousIndex());
168: assertTrue(iter.hasNext());
169: assertEquals(i, iter.next());
170: assertEquals(i + 1, iter.nextIndex());
171: assertEquals(i, iter.previousIndex());
172: assertTrue(iter.hasPrevious());
173: }
174:
175: assertTrue(!iter.hasNext());
176:
177: for (int j = 9; j >= 0; j--) {
178: for (int i = j; i >= 0; i--) {
179: assertEquals(i + 1, iter.nextIndex());
180: assertEquals(i, iter.previousIndex());
181: assertTrue(iter.hasPrevious());
182: assertEquals(i, iter.previous());
183: assertEquals(i, iter.nextIndex());
184: assertEquals(i - 1, iter.previousIndex());
185: assertTrue(iter.hasNext());
186: }
187: for (int i = 0; i < j; i++) {
188: assertEquals(k + "," + j + "," + i, i, iter
189: .nextIndex());
190: assertEquals(i - 1, iter.previousIndex());
191: assertTrue(iter.hasNext());
192: assertEquals(i, iter.next());
193: assertEquals(i + 1, iter.nextIndex());
194: assertEquals(i, iter.previousIndex());
195: assertTrue(iter.hasPrevious());
196: }
197: }
198:
199: assertTrue(!iter.hasPrevious());
200: }
201: }
202:
203: public void testNotModifiable() throws Exception {
204: IntList list = new ArrayIntList();
205: list.add(1);
206: list.add(2);
207: list.add(3);
208: IntListIterator iter = new IntIteratorIntListIterator(list
209: .iterator());
210:
211: assertTrue(iter.hasNext());
212: assertEquals(1, iter.next());
213:
214: try {
215: iter.add(4);
216: fail("Expected UnsupportedOperationException");
217: } catch (UnsupportedOperationException e) {
218: // expected
219: }
220:
221: try {
222: iter.remove();
223: fail("Expected UnsupportedOperationException");
224: } catch (UnsupportedOperationException e) {
225: // expected
226: }
227:
228: try {
229: iter.set(4);
230: fail("Expected UnsupportedOperationException");
231: } catch (UnsupportedOperationException e) {
232: // expected
233: }
234: }
235: }
|