001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.iterators;
017:
018: import java.util.ArrayList;
019: import java.util.Comparator;
020: import java.util.Iterator;
021:
022: import junit.framework.Test;
023: import junit.framework.TestSuite;
024:
025: import org.apache.commons.collections.comparators.ComparableComparator;
026:
027: /**
028: * Unit test suite for {@link CollatingIterator}.
029: *
030: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
031: * @author Rodney Waldhoff
032: */
033: public class TestCollatingIterator extends AbstractTestIterator {
034:
035: //------------------------------------------------------------ Conventional
036:
037: public TestCollatingIterator(String testName) {
038: super (testName);
039: }
040:
041: public static Test suite() {
042: return new TestSuite(TestCollatingIterator.class);
043: }
044:
045: //--------------------------------------------------------------- Lifecycle
046:
047: private Comparator comparator = null;
048: private ArrayList evens = null;
049: private ArrayList odds = null;
050: private ArrayList fib = null;
051:
052: public void setUp() throws Exception {
053: super .setUp();
054: comparator = new ComparableComparator();
055: evens = new ArrayList();
056: odds = new ArrayList();
057: for (int i = 0; i < 20; i++) {
058: if (0 == i % 2) {
059: evens.add(new Integer(i));
060: } else {
061: odds.add(new Integer(i));
062: }
063: }
064: fib = new ArrayList();
065: fib.add(new Integer(1));
066: fib.add(new Integer(1));
067: fib.add(new Integer(2));
068: fib.add(new Integer(3));
069: fib.add(new Integer(5));
070: fib.add(new Integer(8));
071: fib.add(new Integer(13));
072: fib.add(new Integer(21));
073: }
074:
075: //---------------------------------------------------- TestIterator Methods
076:
077: public Iterator makeEmptyIterator() {
078: return new CollatingIterator(comparator);
079: }
080:
081: public Iterator makeFullIterator() {
082: CollatingIterator iter = new CollatingIterator(comparator);
083: iter.addIterator(evens.iterator());
084: iter.addIterator(odds.iterator());
085: iter.addIterator(fib.iterator());
086: return iter;
087: }
088:
089: //------------------------------------------------------------------- Tests
090:
091: public void testGetSetComparator() {
092: CollatingIterator iter = new CollatingIterator();
093: assertNull(iter.getComparator());
094: iter.setComparator(comparator);
095: assertSame(comparator, iter.getComparator());
096: iter.setComparator(null);
097: assertNull(iter.getComparator());
098: }
099:
100: public void testIterateEven() {
101: CollatingIterator iter = new CollatingIterator(comparator);
102: iter.addIterator(evens.iterator());
103: for (int i = 0; i < evens.size(); i++) {
104: assertTrue(iter.hasNext());
105: assertEquals(evens.get(i), iter.next());
106: }
107: assertTrue(!iter.hasNext());
108: }
109:
110: public void testIterateEvenOdd() {
111: CollatingIterator iter = new CollatingIterator(comparator,
112: evens.iterator(), odds.iterator());
113: for (int i = 0; i < 20; i++) {
114: assertTrue(iter.hasNext());
115: assertEquals(new Integer(i), iter.next());
116: }
117: assertTrue(!iter.hasNext());
118: }
119:
120: public void testIterateOddEven() {
121: CollatingIterator iter = new CollatingIterator(comparator, odds
122: .iterator(), evens.iterator());
123: for (int i = 0; i < 20; i++) {
124: assertTrue(iter.hasNext());
125: assertEquals(new Integer(i), iter.next());
126: }
127: assertTrue(!iter.hasNext());
128: }
129:
130: public void testIterateEvenEven() {
131: CollatingIterator iter = new CollatingIterator(comparator);
132: iter.addIterator(evens.iterator());
133: iter.addIterator(evens.iterator());
134: for (int i = 0; i < evens.size(); i++) {
135: assertTrue(iter.hasNext());
136: assertEquals(evens.get(i), iter.next());
137: assertTrue(iter.hasNext());
138: assertEquals(evens.get(i), iter.next());
139: }
140: assertTrue(!iter.hasNext());
141: }
142:
143: public void testIterateFibEvenOdd() {
144: CollatingIterator iter = new CollatingIterator(comparator);
145: iter.addIterator(fib.iterator());
146: iter.addIterator(evens.iterator());
147: iter.addIterator(odds.iterator());
148:
149: assertEquals(new Integer(0), iter.next()); // even 0
150: assertEquals(new Integer(1), iter.next()); // fib 1
151: assertEquals(new Integer(1), iter.next()); // fib 1
152: assertEquals(new Integer(1), iter.next()); // odd 1
153: assertEquals(new Integer(2), iter.next()); // fib 2
154: assertEquals(new Integer(2), iter.next()); // even 2
155: assertEquals(new Integer(3), iter.next()); // fib 3
156: assertEquals(new Integer(3), iter.next()); // odd 3
157: assertEquals(new Integer(4), iter.next()); // even 4
158: assertEquals(new Integer(5), iter.next()); // fib 5
159: assertEquals(new Integer(5), iter.next()); // odd 5
160: assertEquals(new Integer(6), iter.next()); // even 6
161: assertEquals(new Integer(7), iter.next()); // odd 7
162: assertEquals(new Integer(8), iter.next()); // fib 8
163: assertEquals(new Integer(8), iter.next()); // even 8
164: assertEquals(new Integer(9), iter.next()); // odd 9
165: assertEquals(new Integer(10), iter.next()); // even 10
166: assertEquals(new Integer(11), iter.next()); // odd 11
167: assertEquals(new Integer(12), iter.next()); // even 12
168: assertEquals(new Integer(13), iter.next()); // fib 13
169: assertEquals(new Integer(13), iter.next()); // odd 13
170: assertEquals(new Integer(14), iter.next()); // even 14
171: assertEquals(new Integer(15), iter.next()); // odd 15
172: assertEquals(new Integer(16), iter.next()); // even 16
173: assertEquals(new Integer(17), iter.next()); // odd 17
174: assertEquals(new Integer(18), iter.next()); // even 18
175: assertEquals(new Integer(19), iter.next()); // odd 19
176: assertEquals(new Integer(21), iter.next()); // fib 21
177:
178: assertTrue(!iter.hasNext());
179: }
180:
181: public void testRemoveFromSingle() {
182: CollatingIterator iter = new CollatingIterator(comparator);
183: iter.addIterator(evens.iterator());
184: int expectedSize = evens.size();
185: while (iter.hasNext()) {
186: Integer val = (Integer) (iter.next());
187: if (val.intValue() % 4 == 0) {
188: expectedSize--;
189: iter.remove();
190: }
191: }
192: assertEquals(expectedSize, evens.size());
193: }
194:
195: public void testRemoveFromDouble() {
196: CollatingIterator iter = new CollatingIterator(comparator);
197: iter.addIterator(evens.iterator());
198: iter.addIterator(odds.iterator());
199: int expectedSize = evens.size() + odds.size();
200: while (iter.hasNext()) {
201: Integer val = (Integer) (iter.next());
202: if (val.intValue() % 4 == 0 || val.intValue() % 3 == 0) {
203: expectedSize--;
204: iter.remove();
205: }
206: }
207: assertEquals(expectedSize, (evens.size() + odds.size()));
208: }
209:
210: }
|