Class IteratorTester<E>
- java.lang.Object
-
- com.google.common.collect.testing.AbstractIteratorTester<E,java.util.Iterator<E>>
-
- com.google.common.collect.testing.IteratorTester<E>
-
public abstract class IteratorTester<E> extends AbstractIteratorTester<E,java.util.Iterator<E>>
A utility for testing an Iterator implementation by comparing its behavior to that of a "known good" reference implementation. In order to accomplish this, it's important to test a great variety of sequences of theIterator.next()
,Iterator.hasNext()
andIterator.remove()
operations. This utility takes the brute-force approach of trying all possible sequences of these operations, up to a given number of steps. So, if the caller specifies to use n steps, a total of 3^n tests are actually performed.For instance, if steps is 5, one example sequence that will be tested is:
- remove();
- hasNext()
- hasNext();
- remove();
- next();
This particular order of operations may be unrealistic, and testing all 3^5 of them may be thought of as overkill; however, it's difficult to determine which proper subset of this massive set would be sufficient to expose any possible bug. Brute force is simpler.
To use this class the concrete subclass must implement the
AbstractIteratorTester.newTargetIterator()
method. This is because it's impossible to test an Iterator without changing its state, so the tester needs a steady supply of fresh Iterators.If your iterator supports modification through
remove()
, you may wish to override the verify() method, which is called after each sequence and is guaranteed to be called using the latest values obtained fromAbstractIteratorTester.newTargetIterator()
.The value you pass to the parameter
steps
should be greater than the length of your iterator, so that this class can check that your iterator behaves correctly when it is exhausted.For example, to test
Collections.unmodifiableList
's iterator:{@code List
expectedElements = Arrays.asList("a", "b", "c", "d", "e"); List actualElements = Collections.unmodifiableList( Arrays.asList("a", "b", "c", "d", "e")); IteratorTester iteratorTester = new IteratorTester ( 6, IteratorFeature.UNMODIFIABLE, expectedElements, IteratorTester.KnownOrder.KNOWN_ORDER) {
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.google.common.collect.testing.AbstractIteratorTester
AbstractIteratorTester.KnownOrder, AbstractIteratorTester.MultiExceptionListIterator, AbstractIteratorTester.Stimulus<E,T extends java.util.Iterator<E>>
-
-
Field Summary
-
Fields inherited from class com.google.common.collect.testing.AbstractIteratorTester
add, hasNext, hasPrevious, next, nextIndex, previous, previousIndex, remove, set
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
IteratorTester(int steps, java.lang.Iterable<? extends IteratorFeature> features, java.lang.Iterable<E> expectedElements, AbstractIteratorTester.KnownOrder knownOrder)
Creates an IteratorTester.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.Iterable<AbstractIteratorTester.Stimulus<E,java.util.Iterator<E>>>
getStimulusValues()
I'd like to make this a parameter to the constructor, but I can't because the stimulus instances refer tothis
.-
Methods inherited from class com.google.common.collect.testing.AbstractIteratorTester
iteratorStimuli, listIteratorStimuli, newTargetIterator, test, testForEachRemaining, verify
-
-
-
-
Constructor Detail
-
IteratorTester
protected IteratorTester(int steps, java.lang.Iterable<? extends IteratorFeature> features, java.lang.Iterable<E> expectedElements, AbstractIteratorTester.KnownOrder knownOrder)
Creates an IteratorTester.- Parameters:
steps
- how many operations to test for each tested pair of iteratorsfeatures
- the features supported by the iterator
-
-
Method Detail
-
getStimulusValues
protected final java.lang.Iterable<AbstractIteratorTester.Stimulus<E,java.util.Iterator<E>>> getStimulusValues()
Description copied from class:AbstractIteratorTester
I'd like to make this a parameter to the constructor, but I can't because the stimulus instances refer tothis
.- Specified by:
getStimulusValues
in classAbstractIteratorTester<E,java.util.Iterator<E>>
-
-