1 /* 2 * Copyright (C) 2016 The Android Open Source Project 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 17 package libcore.java.util; 18 19 import junit.framework.TestCase; 20 21 import java.util.ArrayList; 22 import java.util.Arrays; 23 import java.util.List; 24 import java.util.PrimitiveIterator; 25 import java.util.function.Consumer; 26 import java.util.function.DoubleConsumer; 27 import java.util.function.IntConsumer; 28 import java.util.function.LongConsumer; 29 30 public class PrimitiveIteratorTest extends TestCase { 31 public static class CannedIntIterator implements PrimitiveIterator.OfInt { 32 private final int[] ints; 33 private int idx; 34 35 public CannedIntIterator(int[] ints) { 36 this.ints = ints; 37 this.idx = 0; 38 } 39 40 @Override 41 public int nextInt() { 42 return ints[idx++]; 43 } 44 45 @Override 46 public boolean hasNext() { 47 return idx < ints.length; 48 } 49 } 50 51 public static class CannedLongIterator implements PrimitiveIterator.OfLong { 52 private final long[] longs; 53 private int idx; 54 55 public CannedLongIterator(long[] longs) { 56 this.longs = longs; 57 this.idx = 0; 58 } 59 60 @Override 61 public long nextLong() { 62 return longs[idx++]; 63 } 64 65 @Override 66 public boolean hasNext() { 67 return idx < longs.length; 68 } 69 } 70 71 public static class CannedDoubleIterator implements PrimitiveIterator.OfDouble { 72 private final double[] doubles; 73 private int idx; 74 75 public CannedDoubleIterator(double[] doubles) { 76 this.doubles = doubles; 77 this.idx = 0; 78 } 79 80 @Override 81 public double nextDouble() { 82 return doubles[idx++]; 83 } 84 85 @Override 86 public boolean hasNext() { 87 return idx < doubles.length; 88 } 89 } 90 91 public void testIntIterator_forEachRemaining_Consumer() { 92 final int[] data = new int[] { 1, 2, 4, 5 }; 93 94 final ArrayList<Integer> recorder = new ArrayList<>(); 95 CannedIntIterator cit = new CannedIntIterator(data); 96 cit.forEachRemaining((int i) -> recorder.add(i)); 97 98 assertEquals(Arrays.asList(1, 2, 4, 5), recorder); 99 100 cit = new CannedIntIterator(data); 101 try { 102 cit.forEachRemaining((IntConsumer) null); 103 fail(); 104 } catch (NullPointerException expected) { 105 } 106 } 107 108 public void testIntIterator_forEachRemaining_boxedConsumer() { 109 final int[] data = new int[] { 1, 2, 4, 5 }; 110 111 final ArrayList<Integer> recorder = new ArrayList<>(); 112 CannedIntIterator cit = new CannedIntIterator(data); 113 cit.forEachRemaining((Integer i) -> recorder.add(i)); 114 115 assertEquals(Arrays.asList(1, 2, 4, 5), recorder); 116 117 // Test that the boxed and unboxed iterators produce the same 118 // set of events. 119 final ArrayList<Integer> recorder2 = new ArrayList<>(); 120 cit = new CannedIntIterator(data); 121 cit.forEachRemaining((int i) -> recorder2.add(i)); 122 assertEquals(recorder, recorder2); 123 124 cit = new CannedIntIterator(data); 125 try { 126 cit.forEachRemaining((Consumer<Integer>) null); 127 fail(); 128 } catch (NullPointerException expected) { 129 } 130 } 131 132 public void testIntIterator_forEachRemaining_boxedNext() { 133 final int[] data = new int[] { 1 }; 134 CannedIntIterator cit = new CannedIntIterator(data); 135 assertEquals(1, (int) cit.next()); 136 } 137 138 public void testLongIterator_forEachRemaining_Consumer() { 139 final long[] data = new long[] { 1, 2, 4, 5 }; 140 141 final ArrayList<Long> recorder = new ArrayList<>(); 142 CannedLongIterator cit = new CannedLongIterator(data); 143 cit.forEachRemaining((long i) -> recorder.add(i)); 144 145 assertEquals(Arrays.asList(1L, 2L, 4L, 5L), recorder); 146 147 cit = new CannedLongIterator(data); 148 try { 149 cit.forEachRemaining((LongConsumer) null); 150 fail(); 151 } catch (NullPointerException expected) { 152 } 153 } 154 155 public void testLongIterator_forEachRemaining_boxedConsumer() { 156 final long[] data = new long[] { 1, 2, 4, 5 }; 157 158 final ArrayList<Long> recorder = new ArrayList<>(); 159 CannedLongIterator cit = new CannedLongIterator(data); 160 cit.forEachRemaining((Long i) -> recorder.add(i)); 161 162 assertEquals(Arrays.asList(1L, 2L, 4L, 5L), recorder); 163 164 // Test that the boxed and unboxed iterators produce the same 165 // set of events. 166 final ArrayList<Long> recorder2 = new ArrayList<>(); 167 cit = new CannedLongIterator(data); 168 cit.forEachRemaining((long i) -> recorder2.add(i)); 169 assertEquals(recorder, recorder2); 170 171 cit = new CannedLongIterator(data); 172 try { 173 cit.forEachRemaining((Consumer<Long>) null); 174 fail(); 175 } catch (NullPointerException expected) { 176 } 177 } 178 179 public void testLongIterator_forEachRemaining_boxedNext() { 180 final long[] data = new long[] { 1L }; 181 CannedLongIterator clt = new CannedLongIterator(data); 182 assertEquals(1, (long) clt.next()); 183 } 184 185 public void testDoubleIterator_forEachRemaining_Consumer() { 186 final double[] data = new double[] { 1, 2, 4, 5 }; 187 188 final ArrayList<Double> recorder = new ArrayList<>(); 189 CannedDoubleIterator cit = new CannedDoubleIterator(data); 190 cit.forEachRemaining((double i) -> recorder.add(i)); 191 192 assertEquals(Arrays.asList(1.0d, 2.0d, 4.0d, 5.0d), recorder); 193 194 cit = new CannedDoubleIterator(data); 195 try { 196 cit.forEachRemaining((DoubleConsumer) null); 197 fail(); 198 } catch (NullPointerException expected) { 199 } 200 } 201 202 public void testDoubleIterator_forEachRemaining_boxedConsumer() { 203 final double[] data = new double[] { 1, 2, 4, 5 }; 204 205 final ArrayList<Double> recorder = new ArrayList<>(); 206 CannedDoubleIterator cit = new CannedDoubleIterator(data); 207 cit.forEachRemaining((Double i) -> recorder.add(i)); 208 209 assertEquals(Arrays.asList(1.0d, 2.0d, 4.0d, 5.0d), recorder); 210 211 // Test that the boxed and unboxed iterators produce the same 212 // set of events. 213 final ArrayList<Double> recorder2 = new ArrayList<>(); 214 cit = new CannedDoubleIterator(data); 215 cit.forEachRemaining((double i) -> recorder2.add(i)); 216 assertEquals(recorder, recorder2); 217 218 cit = new CannedDoubleIterator(data); 219 try { 220 cit.forEachRemaining((Consumer<Double>) null); 221 fail(); 222 } catch (NullPointerException expected) { 223 } 224 } 225 226 public void testDoubleIterator_forEachRemaining_boxedNext() { 227 final double[] data = new double[] { 1.0 }; 228 CannedDoubleIterator clt = new CannedDoubleIterator(data); 229 assertEquals(1.0, (double) clt.next()); 230 } 231 } 232