Java
1/*
2* Copyright (C) 2007 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
17package com.google.common.collect.testing;18
19import com.google.common.annotations.GwtCompatible;20import java.util.Collections;21import java.util.Iterator;22import org.checkerframework.checker.nullness.qual.Nullable;23
24/**
25* A utility for testing an Iterator implementation by comparing its behavior to that of a "known
26* good" reference implementation. In order to accomplish this, it's important to test a great
27* variety of sequences of the {@link Iterator#next}, {@link Iterator#hasNext} and {@link
28* Iterator#remove} operations. This utility takes the brute-force approach of trying <i>all</i>
29* possible sequences of these operations, up to a given number of steps. So, if the caller
30* specifies to use <i>n</i> steps, a total of <i>3^n</i> tests are actually performed.
31*
32* <p>For instance, if <i>steps</i> is 5, one example sequence that will be tested is:
33*
34* <ol>
35* <li>remove();
36* <li>hasNext()
37* <li>hasNext();
38* <li>remove();
39* <li>next();
40* </ol>
41*
42* <p>This particular order of operations may be unrealistic, and testing all 3^5 of them may be
43* thought of as overkill; however, it's difficult to determine which proper subset of this massive
44* set would be sufficient to expose any possible bug. Brute force is simpler.
45*
46* <p>To use this class the concrete subclass must implement the {@link
47* IteratorTester#newTargetIterator()} method. This is because it's impossible to test an Iterator
48* without changing its state, so the tester needs a steady supply of fresh Iterators.
49*
50* <p>If your iterator supports modification through {@code remove()}, you may wish to override the
51* verify() method, which is called <em>after</em> each sequence and is guaranteed to be called
52* using the latest values obtained from {@link IteratorTester#newTargetIterator()}.
53*
54* <p>The value you pass to the parameter {@code steps} should be greater than the length of your
55* iterator, so that this class can check that your iterator behaves correctly when it is exhausted.
56*
57* <p>For example, to test {@link java.util.Collections#unmodifiableList(java.util.List)
58* Collections.unmodifiableList}'s iterator:
59*
60* <pre>{@code
61* List<String> expectedElements =
62* Arrays.asList("a", "b", "c", "d", "e");
63* List<String> actualElements =
64* Collections.unmodifiableList(
65* Arrays.asList("a", "b", "c", "d", "e"));
66* IteratorTester<String> iteratorTester =
67* new IteratorTester<String>(
68* 6,
69* IteratorFeature.UNMODIFIABLE,
70* expectedElements,
71* IteratorTester.KnownOrder.KNOWN_ORDER) {
72* @Override
73* protected Iterator<String> newTargetIterator() {
74* return actualElements.iterator();
75* }
76* };
77* iteratorTester.test();
78* iteratorTester.testForEachRemaining();
79* }</pre>
80*
81* <p><b>Note</b>: It is necessary to use {@code IteratorTester.KnownOrder} as shown above, rather
82* than {@code KnownOrder} directly, because otherwise the code cannot be compiled.
83*
84* @author Kevin Bourrillion
85* @author Chris Povirk
86*/
87@GwtCompatible
88@ElementTypesAreNonnullByDefault
89public abstract class IteratorTester<E extends @Nullable Object>90extends AbstractIteratorTester<E, Iterator<E>> {91/**92* Creates an IteratorTester.
93*
94* @param steps how many operations to test for each tested pair of iterators
95* @param features the features supported by the iterator
96*/
97protected IteratorTester(98int steps,99Iterable<? extends IteratorFeature> features,100Iterable<E> expectedElements,101KnownOrder knownOrder) {102super(steps, Collections.<E>singleton(null), features, expectedElements, knownOrder, 0);103}104
105@Override106protected final Iterable<Stimulus<E, Iterator<E>>> getStimulusValues() {107return iteratorStimuli();108}109}
110