1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.ArrayList ; 19 import java.util.Comparator ; 20 import java.util.Iterator ; 21 22 import junit.framework.Test; 23 import junit.framework.TestSuite; 24 25 import org.apache.commons.collections.comparators.ComparableComparator; 26 27 33 public class TestCollatingIterator extends AbstractTestIterator { 34 35 37 public TestCollatingIterator(String testName) { 38 super(testName); 39 } 40 41 public static Test suite() { 42 return new TestSuite(TestCollatingIterator.class); 43 } 44 45 47 private Comparator comparator = null; 48 private ArrayList evens = null; 49 private ArrayList odds = null; 50 private ArrayList fib = null; 51 52 public void setUp() throws Exception { 53 super.setUp(); 54 comparator = new ComparableComparator(); 55 evens = new ArrayList (); 56 odds = new ArrayList (); 57 for(int i=0;i<20;i++) { 58 if(0 == i%2) { 59 evens.add(new Integer (i)); 60 } else { 61 odds.add(new Integer (i)); 62 } 63 } 64 fib = new ArrayList (); 65 fib.add(new Integer (1)); 66 fib.add(new Integer (1)); 67 fib.add(new Integer (2)); 68 fib.add(new Integer (3)); 69 fib.add(new Integer (5)); 70 fib.add(new Integer (8)); 71 fib.add(new Integer (13)); 72 fib.add(new Integer (21)); 73 } 74 75 77 public Iterator makeEmptyIterator() { 78 return new CollatingIterator(comparator); 79 } 80 81 public Iterator 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 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 (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 (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 (0),iter.next()); assertEquals(new Integer (1),iter.next()); assertEquals(new Integer (1),iter.next()); assertEquals(new Integer (1),iter.next()); assertEquals(new Integer (2),iter.next()); assertEquals(new Integer (2),iter.next()); assertEquals(new Integer (3),iter.next()); assertEquals(new Integer (3),iter.next()); assertEquals(new Integer (4),iter.next()); assertEquals(new Integer (5),iter.next()); assertEquals(new Integer (5),iter.next()); assertEquals(new Integer (6),iter.next()); assertEquals(new Integer (7),iter.next()); assertEquals(new Integer (8),iter.next()); assertEquals(new Integer (8),iter.next()); assertEquals(new Integer (9),iter.next()); assertEquals(new Integer (10),iter.next()); assertEquals(new Integer (11),iter.next()); assertEquals(new Integer (12),iter.next()); assertEquals(new Integer (13),iter.next()); assertEquals(new Integer (13),iter.next()); assertEquals(new Integer (14),iter.next()); assertEquals(new Integer (15),iter.next()); assertEquals(new Integer (16),iter.next()); assertEquals(new Integer (17),iter.next()); assertEquals(new Integer (18),iter.next()); assertEquals(new Integer (19),iter.next()); assertEquals(new Integer (21),iter.next()); 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 val = (Integer )(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 val = (Integer )(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 |