guava
251 строка · 8.3 Кб
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
17package com.google.common.collect.testing.testers;
18
19import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
20import static com.google.common.collect.testing.features.CollectionSize.ONE;
21import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
22import static com.google.common.collect.testing.features.CollectionSize.ZERO;
23import static org.junit.Assert.assertThrows;
24
25import com.google.common.annotations.GwtIncompatible;
26import com.google.common.collect.testing.Helpers;
27import com.google.common.collect.testing.features.CollectionFeature;
28import com.google.common.collect.testing.features.CollectionSize;
29import java.lang.reflect.Method;
30import java.util.ArrayList;
31import java.util.Collections;
32import java.util.Iterator;
33import java.util.List;
34import java.util.NavigableSet;
35import java.util.TreeSet;
36import org.junit.Ignore;
37
38/**
39* A generic JUnit test which tests operations on a NavigableSet. Can't be invoked directly; please
40* see {@code NavigableSetTestSuiteBuilder}.
41*
42* @author Jesse Wilson
43* @author Louis Wasserman
44*/
45@GwtIncompatible
46@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
47public class NavigableSetNavigationTester<E> extends AbstractSetTester<E> {
48
49private NavigableSet<E> navigableSet;
50private List<E> values;
51private E a;
52private E b;
53private E c;
54
55@Override
56public void setUp() throws Exception {
57super.setUp();
58navigableSet = (NavigableSet<E>) getSet();
59values =
60Helpers.copyToList(
61getSubjectGenerator()
62.getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
63Collections.sort(values, navigableSet.comparator());
64
65// some tests assume SEVERAL == 3
66if (values.size() >= 1) {
67a = values.get(0);
68if (values.size() >= 3) {
69b = values.get(1);
70c = values.get(2);
71}
72}
73}
74
75/** Resets the contents of navigableSet to have elements a, c, for the navigation tests. */
76protected void resetWithHole() {
77super.resetContainer(getSubjectGenerator().create(a, c));
78navigableSet = (NavigableSet<E>) getSet();
79}
80
81@CollectionFeature.Require(SUPPORTS_REMOVE)
82@CollectionSize.Require(ZERO)
83public void testEmptySetPollFirst() {
84assertNull(navigableSet.pollFirst());
85}
86
87@CollectionSize.Require(ZERO)
88public void testEmptySetNearby() {
89assertNull(navigableSet.lower(e0()));
90assertNull(navigableSet.floor(e0()));
91assertNull(navigableSet.ceiling(e0()));
92assertNull(navigableSet.higher(e0()));
93}
94
95@CollectionFeature.Require(SUPPORTS_REMOVE)
96@CollectionSize.Require(ZERO)
97public void testEmptySetPollLast() {
98assertNull(navigableSet.pollLast());
99}
100
101@CollectionFeature.Require(SUPPORTS_REMOVE)
102@CollectionSize.Require(ONE)
103public void testSingletonSetPollFirst() {
104assertEquals(a, navigableSet.pollFirst());
105assertTrue(navigableSet.isEmpty());
106}
107
108@CollectionSize.Require(ONE)
109public void testSingletonSetNearby() {
110assertNull(navigableSet.lower(e0()));
111assertEquals(a, navigableSet.floor(e0()));
112assertEquals(a, navigableSet.ceiling(e0()));
113assertNull(navigableSet.higher(e0()));
114}
115
116@CollectionFeature.Require(SUPPORTS_REMOVE)
117@CollectionSize.Require(ONE)
118public void testSingletonSetPollLast() {
119assertEquals(a, navigableSet.pollLast());
120assertTrue(navigableSet.isEmpty());
121}
122
123@CollectionFeature.Require(SUPPORTS_REMOVE)
124@CollectionSize.Require(SEVERAL)
125public void testPollFirst() {
126assertEquals(a, navigableSet.pollFirst());
127assertEquals(values.subList(1, values.size()), Helpers.copyToList(navigableSet));
128}
129
130@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
131public void testPollFirstUnsupported() {
132assertThrows(UnsupportedOperationException.class, () -> navigableSet.pollFirst());
133}
134
135@CollectionSize.Require(SEVERAL)
136public void testLowerHole() {
137resetWithHole();
138assertEquals(null, navigableSet.lower(a));
139assertEquals(a, navigableSet.lower(b));
140assertEquals(a, navigableSet.lower(c));
141}
142
143@CollectionSize.Require(SEVERAL)
144public void testFloorHole() {
145resetWithHole();
146assertEquals(a, navigableSet.floor(a));
147assertEquals(a, navigableSet.floor(b));
148assertEquals(c, navigableSet.floor(c));
149}
150
151@CollectionSize.Require(SEVERAL)
152public void testCeilingHole() {
153resetWithHole();
154assertEquals(a, navigableSet.ceiling(a));
155assertEquals(c, navigableSet.ceiling(b));
156assertEquals(c, navigableSet.ceiling(c));
157}
158
159@CollectionSize.Require(SEVERAL)
160public void testHigherHole() {
161resetWithHole();
162assertEquals(c, navigableSet.higher(a));
163assertEquals(c, navigableSet.higher(b));
164assertEquals(null, navigableSet.higher(c));
165}
166
167/*
168* TODO(cpovirk): make "too small" and "too large" elements available for better navigation
169* testing. At that point, we may be able to eliminate the "hole" tests, which would mean that
170* ContiguousSet's tests would no longer need to suppress them.
171*/
172@CollectionSize.Require(SEVERAL)
173public void testLower() {
174assertEquals(null, navigableSet.lower(a));
175assertEquals(a, navigableSet.lower(b));
176assertEquals(b, navigableSet.lower(c));
177}
178
179@CollectionSize.Require(SEVERAL)
180public void testFloor() {
181assertEquals(a, navigableSet.floor(a));
182assertEquals(b, navigableSet.floor(b));
183assertEquals(c, navigableSet.floor(c));
184}
185
186@CollectionSize.Require(SEVERAL)
187public void testCeiling() {
188assertEquals(a, navigableSet.ceiling(a));
189assertEquals(b, navigableSet.ceiling(b));
190assertEquals(c, navigableSet.ceiling(c));
191}
192
193@CollectionSize.Require(SEVERAL)
194public void testHigher() {
195assertEquals(b, navigableSet.higher(a));
196assertEquals(c, navigableSet.higher(b));
197assertEquals(null, navigableSet.higher(c));
198}
199
200@CollectionFeature.Require(SUPPORTS_REMOVE)
201@CollectionSize.Require(SEVERAL)
202public void testPollLast() {
203assertEquals(c, navigableSet.pollLast());
204assertEquals(values.subList(0, values.size() - 1), Helpers.copyToList(navigableSet));
205}
206
207@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
208public void testPollLastUnsupported() {
209assertThrows(UnsupportedOperationException.class, () -> navigableSet.pollLast());
210}
211
212@CollectionSize.Require(SEVERAL)
213public void testDescendingNavigation() {
214List<E> descending = new ArrayList<>();
215for (Iterator<E> i = navigableSet.descendingIterator(); i.hasNext(); ) {
216descending.add(i.next());
217}
218Collections.reverse(descending);
219assertEquals(values, descending);
220}
221
222public void testEmptySubSet() {
223NavigableSet<E> empty = navigableSet.subSet(e0(), false, e0(), false);
224assertEquals(new TreeSet<E>(), empty);
225}
226
227/*
228* TODO(cpovirk): more testing of subSet/headSet/tailSet/descendingSet? and/or generate derived
229* suites?
230*/
231
232/**
233* Returns the {@link Method} instances for the test methods in this class that create a set with
234* a "hole" in it so that set tests of {@code ContiguousSet} can suppress them with {@code
235* FeatureSpecificTestSuiteBuilder.suppressing()}.
236*/
237/*
238* TODO(cpovirk): or we could make HOLES_FORBIDDEN a feature. Or we could declare that
239* implementations are permitted to throw IAE if a hole is requested, and we could update
240* test*Hole to permit IAE. (But might this ignore genuine bugs?) But see the TODO above
241* testLower, which could make this all unnecessary
242*/
243public static Method[] getHoleMethods() {
244return new Method[] {
245Helpers.getMethod(NavigableSetNavigationTester.class, "testLowerHole"),
246Helpers.getMethod(NavigableSetNavigationTester.class, "testFloorHole"),
247Helpers.getMethod(NavigableSetNavigationTester.class, "testCeilingHole"),
248Helpers.getMethod(NavigableSetNavigationTester.class, "testHigherHole"),
249};
250}
251}
252