1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31
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
95
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 }