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