View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.iterators;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.util.Iterator;
24  import java.util.NoSuchElementException;
25  
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Tests the ArrayIterator with primitive type arrays.
30   *
31   * @param <E> the type of elements tested by this iterator.
32   */
33  public class ArrayIterator2Test<E> extends AbstractIteratorTest<E> {
34  
35      protected int[] testArray = { 2, 4, 6, 8 };
36  
37      public ArrayIterator<E> makeArrayIterator(final Object array) {
38          return new ArrayIterator<>(array);
39      }
40  
41      public ArrayIterator<E> makeArrayIterator(final Object array, final int index) {
42          return new ArrayIterator<>(array, index);
43      }
44  
45      public ArrayIterator<E> makeArrayIterator(final Object array, final int start, final int end) {
46          return new ArrayIterator<>(array, start, end);
47      }
48  
49      @Override
50      public ArrayIterator<E> makeEmptyIterator() {
51          return new ArrayIterator<>(new int[0]);
52      }
53  
54      @Override
55      public ArrayIterator<E> makeObject() {
56          return new ArrayIterator<>(testArray);
57      }
58  
59      @Override
60      public boolean supportsRemove() {
61          return false;
62      }
63  
64      @Test
65      public void testIndexedArray() {
66          Iterator<E> iter = makeArrayIterator(testArray, 2);
67          int count = 0;
68          while (iter.hasNext()) {
69              ++count;
70              iter.next();
71          }
72  
73          assertEquals(count, testArray.length - 2, "the count should be right using ArrayIterator(Object,2) ");
74  
75          iter = makeArrayIterator(testArray, 1, testArray.length - 1);
76          count = 0;
77          while (iter.hasNext()) {
78              ++count;
79              iter.next();
80          }
81  
82          assertEquals(count, testArray.length - 2, "the count should be right using ArrayIterator(Object,1," + (testArray.length - 1) + ") ");
83          assertThrows(ArrayIndexOutOfBoundsException.class, () -> makeArrayIterator(testArray, -1),
84                  "new ArrayIterator(Object,-1) should throw an ArrayIndexOutOfBoundsException");
85          assertThrows(ArrayIndexOutOfBoundsException.class, () -> makeArrayIterator(testArray, testArray.length + 1),
86                  "new ArrayIterator(Object,length+1) should throw an ArrayIndexOutOfBoundsException");
87          assertThrows(ArrayIndexOutOfBoundsException.class, () -> makeArrayIterator(testArray, 0, -1),
88                  "new ArrayIterator(Object,0,-1) should throw an ArrayIndexOutOfBoundsException");
89          assertThrows(ArrayIndexOutOfBoundsException.class, () -> makeArrayIterator(testArray, 0, testArray.length + 1),
90                  "new ArrayIterator(Object,0,length+1) should throw an ArrayIndexOutOfBoundsException");
91          assertThrows(IllegalArgumentException.class, () -> makeArrayIterator(testArray, testArray.length - 1, testArray.length - 2),
92                  "new ArrayIterator(Object,length-2,length-1) should throw an IllegalArgumentException");
93          iter = makeArrayIterator(testArray, 1, 1);
94          // MODIFIED: an iterator over a zero-length section of array
95          // should be perfectly legal behavior
96      }
97  
98      @Test
99      public void testIterator() {
100         final Iterator<E> iter = makeObject();
101         for (final int element : testArray) {
102             final Integer testValue = Integer.valueOf(element);
103             final Number iterValue = (Number) iter.next();
104             assertEquals(testValue, iterValue, "Iteration value is correct");
105         }
106         assertFalse(iter.hasNext(), "Iterator should now be empty");
107         assertThrows(NoSuchElementException.class, iter::next);
108     }
109 
110 }