1 /* 2 * Copyright (C) 2010 The Guava Authors 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 com.google.common.collect; 18 19 import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE; 20 import static com.google.common.truth.Truth.assertThat; 21 22 import com.google.common.annotations.GwtCompatible; 23 import com.google.common.annotations.GwtIncompatible; 24 import com.google.common.collect.testing.IteratorTester; 25 import java.util.Iterator; 26 import java.util.NoSuchElementException; 27 import junit.framework.AssertionFailedError; 28 import junit.framework.TestCase; 29 30 /** Tests for {@link AbstractSequentialIterator}. */ 31 @GwtCompatible(emulated = true) 32 public class AbstractSequentialIteratorTest extends TestCase { 33 @GwtIncompatible // Too slow testDoublerExhaustive()34 public void testDoublerExhaustive() { 35 new IteratorTester<Integer>( 36 3, UNMODIFIABLE, ImmutableList.of(1, 2), IteratorTester.KnownOrder.KNOWN_ORDER) { 37 @Override 38 protected Iterator<Integer> newTargetIterator() { 39 return newDoubler(1, 2); 40 } 41 }.test(); 42 } 43 testDoubler()44 public void testDoubler() { 45 Iterable<Integer> doubled = 46 new Iterable<Integer>() { 47 @Override 48 public Iterator<Integer> iterator() { 49 return newDoubler(2, 32); 50 } 51 }; 52 assertThat(doubled).containsExactly(2, 4, 8, 16, 32).inOrder(); 53 } 54 testSampleCode()55 public void testSampleCode() { 56 Iterable<Integer> actual = 57 new Iterable<Integer>() { 58 @Override 59 public Iterator<Integer> iterator() { 60 Iterator<Integer> powersOfTwo = 61 new AbstractSequentialIterator<Integer>(1) { 62 protected Integer computeNext(Integer previous) { 63 return (previous == 1 << 30) ? null : previous * 2; 64 } 65 }; 66 return powersOfTwo; 67 } 68 }; 69 assertThat(actual) 70 .containsExactly( 71 1, 72 2, 73 4, 74 8, 75 16, 76 32, 77 64, 78 128, 79 256, 80 512, 81 1024, 82 2048, 83 4096, 84 8192, 85 16384, 86 32768, 87 65536, 88 131072, 89 262144, 90 524288, 91 1048576, 92 2097152, 93 4194304, 94 8388608, 95 16777216, 96 33554432, 97 67108864, 98 134217728, 99 268435456, 100 536870912, 101 1073741824) 102 .inOrder(); 103 } 104 testEmpty()105 public void testEmpty() { 106 Iterator<Object> empty = newEmpty(); 107 assertFalse(empty.hasNext()); 108 try { 109 empty.next(); 110 fail(); 111 } catch (NoSuchElementException expected) { 112 } 113 try { 114 empty.remove(); 115 fail(); 116 } catch (UnsupportedOperationException expected) { 117 } 118 } 119 testBroken()120 public void testBroken() { 121 Iterator<Object> broken = newBroken(); 122 assertTrue(broken.hasNext()); 123 // We can't retrieve even the known first element: 124 try { 125 broken.next(); 126 fail(); 127 } catch (MyException expected) { 128 } 129 try { 130 broken.next(); 131 fail(); 132 } catch (MyException expected) { 133 } 134 } 135 newDoubler(int first, final int last)136 private static Iterator<Integer> newDoubler(int first, final int last) { 137 return new AbstractSequentialIterator<Integer>(first) { 138 @Override 139 protected Integer computeNext(Integer previous) { 140 return (previous == last) ? null : previous * 2; 141 } 142 }; 143 } 144 145 private static <T> Iterator<T> newEmpty() { 146 return new AbstractSequentialIterator<T>(null) { 147 @Override 148 protected T computeNext(T previous) { 149 throw new AssertionFailedError(); 150 } 151 }; 152 } 153 154 private static Iterator<Object> newBroken() { 155 return new AbstractSequentialIterator<Object>("UNUSED") { 156 @Override 157 protected Object computeNext(Object previous) { 158 throw new MyException(); 159 } 160 }; 161 } 162 163 private static class MyException extends RuntimeException {} 164 } 165