KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > iterators > TestCollatingIterator


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.iterators;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Comparator JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24
25 import org.apache.commons.collections.comparators.ComparableComparator;
26
27 /**
28  * Unit test suite for {@link CollatingIterator}.
29  *
30  * @version $Revision: 1.6 $ $Date: 2004/02/18 01:20:33 $
31  * @author Rodney Waldhoff
32  */

33 public class TestCollatingIterator extends AbstractTestIterator {
34
35     //------------------------------------------------------------ Conventional
36

37     public TestCollatingIterator(String JavaDoc testName) {
38         super(testName);
39     }
40     
41     public static Test suite() {
42         return new TestSuite(TestCollatingIterator.class);
43     }
44
45     //--------------------------------------------------------------- Lifecycle
46

47     private Comparator JavaDoc comparator = null;
48     private ArrayList JavaDoc evens = null;
49     private ArrayList JavaDoc odds = null;
50     private ArrayList JavaDoc fib = null;
51
52     public void setUp() throws Exception JavaDoc {
53         super.setUp();
54         comparator = new ComparableComparator();
55         evens = new ArrayList JavaDoc();
56         odds = new ArrayList JavaDoc();
57         for(int i=0;i<20;i++) {
58             if(0 == i%2) {
59                 evens.add(new Integer JavaDoc(i));
60             } else {
61                 odds.add(new Integer JavaDoc(i));
62             }
63         }
64         fib = new ArrayList JavaDoc();
65         fib.add(new Integer JavaDoc(1));
66         fib.add(new Integer JavaDoc(1));
67         fib.add(new Integer JavaDoc(2));
68         fib.add(new Integer JavaDoc(3));
69         fib.add(new Integer JavaDoc(5));
70         fib.add(new Integer JavaDoc(8));
71         fib.add(new Integer JavaDoc(13));
72         fib.add(new Integer JavaDoc(21));
73     }
74
75     //---------------------------------------------------- TestIterator Methods
76

77     public Iterator JavaDoc makeEmptyIterator() {
78         return new CollatingIterator(comparator);
79     }
80
81     public Iterator JavaDoc makeFullIterator() {
82         CollatingIterator iter = new CollatingIterator(comparator);
83         iter.addIterator(evens.iterator());
84         iter.addIterator(odds.iterator());
85         iter.addIterator(fib.iterator());
86         return iter;
87     }
88
89     //------------------------------------------------------------------- Tests
90

91     public void testGetSetComparator() {
92         CollatingIterator iter = new CollatingIterator();
93         assertNull(iter.getComparator());
94         iter.setComparator(comparator);
95         assertSame(comparator,iter.getComparator());
96         iter.setComparator(null);
97         assertNull(iter.getComparator());
98     }
99
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,evens.iterator(),odds.iterator());
112         for(int i=0;i<20;i++) {
113             assertTrue(iter.hasNext());
114             assertEquals(new Integer JavaDoc(i),iter.next());
115         }
116         assertTrue(!iter.hasNext());
117     }
118
119     public void testIterateOddEven() {
120         CollatingIterator iter = new CollatingIterator(comparator,odds.iterator(),evens.iterator());
121         for(int i=0;i<20;i++) {
122             assertTrue(iter.hasNext());
123             assertEquals(new Integer JavaDoc(i),iter.next());
124         }
125         assertTrue(!iter.hasNext());
126     }
127
128     public void testIterateEvenEven() {
129         CollatingIterator iter = new CollatingIterator(comparator);
130         iter.addIterator(evens.iterator());
131         iter.addIterator(evens.iterator());
132         for(int i=0;i<evens.size();i++) {
133             assertTrue(iter.hasNext());
134             assertEquals(evens.get(i),iter.next());
135             assertTrue(iter.hasNext());
136             assertEquals(evens.get(i),iter.next());
137         }
138         assertTrue(!iter.hasNext());
139     }
140
141
142     public void testIterateFibEvenOdd() {
143         CollatingIterator iter = new CollatingIterator(comparator);
144         iter.addIterator(fib.iterator());
145         iter.addIterator(evens.iterator());
146         iter.addIterator(odds.iterator());
147         
148         assertEquals(new Integer JavaDoc(0),iter.next()); // even 0
149
assertEquals(new Integer JavaDoc(1),iter.next()); // fib 1
150
assertEquals(new Integer JavaDoc(1),iter.next()); // fib 1
151
assertEquals(new Integer JavaDoc(1),iter.next()); // odd 1
152
assertEquals(new Integer JavaDoc(2),iter.next()); // fib 2
153
assertEquals(new Integer JavaDoc(2),iter.next()); // even 2
154
assertEquals(new Integer JavaDoc(3),iter.next()); // fib 3
155
assertEquals(new Integer JavaDoc(3),iter.next()); // odd 3
156
assertEquals(new Integer JavaDoc(4),iter.next()); // even 4
157
assertEquals(new Integer JavaDoc(5),iter.next()); // fib 5
158
assertEquals(new Integer JavaDoc(5),iter.next()); // odd 5
159
assertEquals(new Integer JavaDoc(6),iter.next()); // even 6
160
assertEquals(new Integer JavaDoc(7),iter.next()); // odd 7
161
assertEquals(new Integer JavaDoc(8),iter.next()); // fib 8
162
assertEquals(new Integer JavaDoc(8),iter.next()); // even 8
163
assertEquals(new Integer JavaDoc(9),iter.next()); // odd 9
164
assertEquals(new Integer JavaDoc(10),iter.next()); // even 10
165
assertEquals(new Integer JavaDoc(11),iter.next()); // odd 11
166
assertEquals(new Integer JavaDoc(12),iter.next()); // even 12
167
assertEquals(new Integer JavaDoc(13),iter.next()); // fib 13
168
assertEquals(new Integer JavaDoc(13),iter.next()); // odd 13
169
assertEquals(new Integer JavaDoc(14),iter.next()); // even 14
170
assertEquals(new Integer JavaDoc(15),iter.next()); // odd 15
171
assertEquals(new Integer JavaDoc(16),iter.next()); // even 16
172
assertEquals(new Integer JavaDoc(17),iter.next()); // odd 17
173
assertEquals(new Integer JavaDoc(18),iter.next()); // even 18
174
assertEquals(new Integer JavaDoc(19),iter.next()); // odd 19
175
assertEquals(new Integer JavaDoc(21),iter.next()); // fib 21
176

177         assertTrue(!iter.hasNext());
178     }
179
180     public void testRemoveFromSingle() {
181         CollatingIterator iter = new CollatingIterator(comparator);
182         iter.addIterator(evens.iterator());
183         int expectedSize = evens.size();
184         while(iter.hasNext()) {
185             Integer JavaDoc val = (Integer JavaDoc)(iter.next());
186             if(val.intValue() % 4 == 0) {
187                 expectedSize--;
188                 iter.remove();
189             }
190         }
191         assertEquals(expectedSize,evens.size());
192     }
193
194     public void testRemoveFromDouble() {
195         CollatingIterator iter = new CollatingIterator(comparator);
196         iter.addIterator(evens.iterator());
197         iter.addIterator(odds.iterator());
198         int expectedSize = evens.size() + odds.size();
199         while(iter.hasNext()) {
200             Integer JavaDoc val = (Integer JavaDoc)(iter.next());
201             if(val.intValue() % 4 == 0 || val.intValue() % 3 == 0 ) {
202                 expectedSize--;
203                 iter.remove();
204             }
205         }
206         assertEquals(expectedSize,(evens.size() + odds.size()));
207     }
208
209 }
210
211
Popular Tags