Debian Codegraph @

guava-testlib/src/com/google/common/collect/testing/testers/CollectionIteratorTester.java

raw
1 /*
2 * Copyright (C) 2008 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.testing.testers;
18
19 import static com.google.common.collect.testing.Helpers.mapEntry;
20 import static com.google.common.collect.testing.IteratorFeature.MODIFIABLE;
21 import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE;
22 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
23 import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
24 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
25 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
26 import static java.util.Arrays.asList;
27
28 import com.google.common.annotations.GwtCompatible;
29 import com.google.common.collect.testing.AbstractCollectionTester;
30 import com.google.common.collect.testing.Helpers;
31 import com.google.common.collect.testing.IteratorFeature;
32 import com.google.common.collect.testing.IteratorTester;
33 import com.google.common.collect.testing.features.CollectionFeature;
34 import com.google.common.collect.testing.features.CollectionSize;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Map.Entry;
40 import java.util.NoSuchElementException;
41 import java.util.Set;
42 import org.junit.Ignore;
43
44 /**
45 * A generic JUnit test which tests {@code iterator} operations on a collection. Can't be invoked
46 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
47 *
48 * @author Chris Povirk
49 */
50 @GwtCompatible(emulated = true)
51 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
52 public class CollectionIteratorTester<E> extends AbstractCollectionTester<E> {
53 public void testIterator() {
54 List<E> iteratorElements = new ArrayList<>();
55 for (E element : collection) { // uses iterator()
56 iteratorElements.add(element);
57 }
58 Helpers.assertEqualIgnoringOrder(Arrays.asList(createSamplesArray()), iteratorElements);
59 }
60
61 @CollectionFeature.Require(KNOWN_ORDER)
62 public void testIterationOrdering() {
63 List<E> iteratorElements = new ArrayList<>();
64 for (E element : collection) { // uses iterator()
65 iteratorElements.add(element);
66 }
67 List<E> expected = Helpers.copyToList(getOrderedElements());
68 assertEquals("Different ordered iteration", expected, iteratorElements);
69 }
70
71 @CollectionFeature.Require(ALLOWS_NULL_VALUES)
72 @CollectionSize.Require(absent = ZERO)
73 public void testIterator_nullElement() {
74 initCollectionWithNullElement();
75 List<E> iteratorElements = new ArrayList<>();
76 for (E element : collection) { // uses iterator()
77 iteratorElements.add(element);
78 }
79 Helpers.assertEqualIgnoringOrder(asList(createArrayWithNullElement()), iteratorElements);
80 }
81
82 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
83 @CollectionSize.Require(absent = ZERO)
84 public void testIterator_removeAffectsBackingCollection() {
85 int originalSize = collection.size();
86 Iterator<E> iterator = collection.iterator();
87 Object element = iterator.next();
88 // If it's an Entry, it may become invalid once it's removed from the Map. Copy it.
89 if (element instanceof Entry) {
90 Entry<?, ?> entry = (Entry<?, ?>) element;
91 element = mapEntry(entry.getKey(), entry.getValue());
92 }
93 assertTrue(collection.contains(element)); // sanity check
94 iterator.remove();
95 assertFalse(collection.contains(element));
96 assertEquals(originalSize - 1, collection.size());
97 }
98
99 @CollectionFeature.Require({KNOWN_ORDER, SUPPORTS_ITERATOR_REMOVE})
100 public void testIterator_knownOrderRemoveSupported() {
101 runIteratorTest(MODIFIABLE, IteratorTester.KnownOrder.KNOWN_ORDER, getOrderedElements());
102 }
103
104 @CollectionFeature.Require(value = KNOWN_ORDER, absent = SUPPORTS_ITERATOR_REMOVE)
105 public void testIterator_knownOrderRemoveUnsupported() {
106 runIteratorTest(UNMODIFIABLE, IteratorTester.KnownOrder.KNOWN_ORDER, getOrderedElements());
107 }
108
109 @CollectionFeature.Require(absent = KNOWN_ORDER, value = SUPPORTS_ITERATOR_REMOVE)
110 public void testIterator_unknownOrderRemoveSupported() {
111 runIteratorTest(MODIFIABLE, IteratorTester.KnownOrder.UNKNOWN_ORDER, getSampleElements());
112 }
113
114 @CollectionFeature.Require(absent = {KNOWN_ORDER, SUPPORTS_ITERATOR_REMOVE})
115 public void testIterator_unknownOrderRemoveUnsupported() {
116 runIteratorTest(UNMODIFIABLE, IteratorTester.KnownOrder.UNKNOWN_ORDER, getSampleElements());
117 }
118
119 private void runIteratorTest(
120 Set<IteratorFeature> features, IteratorTester.KnownOrder knownOrder, Iterable<E> elements) {
121 new IteratorTester<E>(
122 Platform.collectionIteratorTesterNumIterations(), features, elements, knownOrder) {
123 @Override
124 protected Iterator<E> newTargetIterator() {
125 resetCollection();
126 return collection.iterator();
127 }
128
129 @Override
130 protected void verify(List<E> elements) {
131 expectContents(elements);
132 }
133 }.test();
134 }
135
136 public void testIteratorNoSuchElementException() {
137 Iterator<E> iterator = collection.iterator();
138 while (iterator.hasNext()) {
139 iterator.next();
140 }
141
142 try {
143 iterator.next();
144 fail("iterator.next() should throw NoSuchElementException");
145 } catch (NoSuchElementException expected) {
146 }
147 }
148 }