@@ -91,6 +91,54 @@ public void setUp() {
9191 list3 .add ("Six" );
9292 }
9393
94+ @ Test
95+ public void testChaining () {
96+ IteratorChain <String > chain = new IteratorChain <>();
97+ chain .addIterator (list1 .iterator ());
98+ chain = new IteratorChain <>(chain );
99+ chain .addIterator (list2 .iterator ());
100+ chain = new IteratorChain <>(chain );
101+ chain .addIterator (list3 .iterator ());
102+
103+ for (final String testValue : testArray ) {
104+ assertTrue (chain .hasNext (), "chain contains values" );
105+ assertTrue (chain .hasNext (), "hasNext doesn't change on 2nd invocation" );
106+ final String iterValue = chain .next ();
107+ assertEquals (testValue , iterValue , "Iteration value is correct" );
108+ if (!iterValue .equals ("Four" )) {
109+ chain .remove ();
110+ }
111+ }
112+ assertFalse (chain .hasNext (), "all values got iterated" );
113+ assertTrue (list1 .isEmpty (), "List is empty" );
114+ assertEquals (1 , list2 .size (), "List is empty" );
115+ assertTrue (list3 .isEmpty (), "List is empty" );
116+ }
117+
118+ @ Test
119+ public void testChainingPerformsWell () {
120+ Iterator <String > iter = makeObject ();
121+ for (int i = 0 ; i < 150 ; i ++) {
122+ final IteratorChain <String > chain = new IteratorChain <>();
123+ chain .addIterator (iter );
124+ iter = chain ;
125+ }
126+ final Iterator <String > iterFinal = iter ;
127+ assertTimeoutPreemptively (Duration .ofSeconds (1 ), () -> {
128+ for (final String testValue : testArray ) {
129+ final String iterValue = iterFinal .next ();
130+ assertEquals (testValue , iterValue , "Iteration value is correct" );
131+ if (!iterValue .equals ("Four" )) {
132+ iterFinal .remove ();
133+ }
134+ }
135+ assertFalse (iterFinal .hasNext (), "all values got iterated" );
136+ assertTrue (list1 .isEmpty (), "List is empty" );
137+ assertEquals (1 , list2 .size (), "List is empty" );
138+ assertTrue (list3 .isEmpty (), "List is empty" );
139+ });
140+ }
141+
94142 @ Test
95143 void testConstructList () {
96144 final List <Iterator <String >> list = new ArrayList <>();
@@ -135,6 +183,22 @@ void testFirstIteratorIsEmptyBug() {
135183 assertFalse (chain .hasNext (), "should not change" );
136184 }
137185
186+ @ Test
187+ public void testHasNextIsInvokedOnEdgeBeforeRemove () {
188+ final Iterator <String > iter = makeObject ();
189+ assertEquals (iter .next (), "One" );
190+ assertEquals (iter .next (), "Two" );
191+ assertEquals (iter .next (), "Three" );
192+ assertTrue (iter .hasNext (), "next elements exists" );
193+ iter .remove (); // though hasNext() on next iterator has been invoked, removing an element on old iterator must still work
194+ assertTrue (iter .hasNext (), "next elements exists" );
195+ assertEquals (iter .next (), "Four" );
196+
197+ assertEquals (list1 , Arrays .asList ("One" , "Two" )); // Three must be gone
198+ assertEquals (list2 , Arrays .asList ("Four" )); // Four still be there
199+ assertEquals (list3 , Arrays .asList ("Five" , "Six" )); // Five+Six anyway
200+ }
201+
138202 @ Test
139203 void testIterator () {
140204 final Iterator <String > iter = makeObject ();
@@ -174,70 +238,6 @@ public void testRemoveDoubleCallShouldFail() {
174238 assertThrows (IllegalStateException .class , () -> iter .remove ());
175239 }
176240
177- @ Test
178- public void testHasNextIsInvokedOnEdgeBeforeRemove () {
179- final Iterator <String > iter = makeObject ();
180- assertEquals (iter .next (), "One" );
181- assertEquals (iter .next (), "Two" );
182- assertEquals (iter .next (), "Three" );
183- assertTrue (iter .hasNext (), "next elements exists" );
184- iter .remove (); // though hasNext() on next iterator has been invoked, removing an element on old iterator must still work
185- assertTrue (iter .hasNext (), "next elements exists" );
186- assertEquals (iter .next (), "Four" );
187-
188- assertEquals (list1 , Arrays .asList ("One" , "Two" )); // Three must be gone
189- assertEquals (list2 , Arrays .asList ("Four" )); // Four still be there
190- assertEquals (list3 , Arrays .asList ("Five" , "Six" )); // Five+Six anyway
191- }
192-
193- @ Test
194- public void testChainingPerformsWell () {
195- Iterator <String > iter = makeObject ();
196- for (int i = 0 ; i < 150 ; i ++) {
197- final IteratorChain <String > chain = new IteratorChain <>();
198- chain .addIterator (iter );
199- iter = chain ;
200- }
201- final Iterator <String > iterFinal = iter ;
202- assertTimeoutPreemptively (Duration .ofSeconds (1 ), () -> {
203- for (final String testValue : testArray ) {
204- final String iterValue = iterFinal .next ();
205- assertEquals (testValue , iterValue , "Iteration value is correct" );
206- if (!iterValue .equals ("Four" )) {
207- iterFinal .remove ();
208- }
209- }
210- assertFalse (iterFinal .hasNext (), "all values got iterated" );
211- assertTrue (list1 .isEmpty (), "List is empty" );
212- assertEquals (1 , list2 .size (), "List is empty" );
213- assertTrue (list3 .isEmpty (), "List is empty" );
214- });
215- }
216-
217- @ Test
218- public void testChaining () {
219- IteratorChain <String > chain = new IteratorChain <>();
220- chain .addIterator (list1 .iterator ());
221- chain = new IteratorChain <>(chain );
222- chain .addIterator (list2 .iterator ());
223- chain = new IteratorChain <>(chain );
224- chain .addIterator (list3 .iterator ());
225-
226- for (final String testValue : testArray ) {
227- assertTrue (chain .hasNext (), "chain contains values" );
228- assertTrue (chain .hasNext (), "hasNext doesn't change on 2nd invocation" );
229- final String iterValue = chain .next ();
230- assertEquals (testValue , iterValue , "Iteration value is correct" );
231- if (!iterValue .equals ("Four" )) {
232- chain .remove ();
233- }
234- }
235- assertFalse (chain .hasNext (), "all values got iterated" );
236- assertTrue (list1 .isEmpty (), "List is empty" );
237- assertEquals (1 , list2 .size (), "List is empty" );
238- assertTrue (list3 .isEmpty (), "List is empty" );
239- }
240-
241241 @ Test
242242 void testRemoveFromFilteredIterator () {
243243
0 commit comments