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