Java
259 строк · 8.9 Кб
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.CollectionSize.ONE;
20import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
21import static com.google.common.collect.testing.features.CollectionSize.ZERO;
22import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
23import static org.junit.Assert.assertThrows;
24
25import com.google.common.annotations.GwtIncompatible;
26import com.google.common.collect.testing.AbstractMapTester;
27import com.google.common.collect.testing.Helpers;
28import com.google.common.collect.testing.features.CollectionSize;
29import com.google.common.collect.testing.features.MapFeature;
30import java.util.ArrayList;
31import java.util.Collections;
32import java.util.List;
33import java.util.Map.Entry;
34import java.util.NavigableMap;
35import org.junit.Ignore;
36
37/**
38* A generic JUnit test which tests operations on a NavigableMap. Can't be invoked directly; please
39* see {@code NavigableMapTestSuiteBuilder}.
40*
41* @author Jesse Wilson
42* @author Louis Wasserman
43*/
44@GwtIncompatible
45@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
46@SuppressWarnings("JUnit4ClassUsedInJUnit3")
47public class NavigableMapNavigationTester<K, V> extends AbstractMapTester<K, V> {
48
49private NavigableMap<K, V> navigableMap;
50private List<Entry<K, V>> entries;
51private Entry<K, V> a;
52private Entry<K, V> b;
53private Entry<K, V> c;
54
55@Override
56public void setUp() throws Exception {
57super.setUp();
58navigableMap = (NavigableMap<K, V>) getMap();
59entries =
60Helpers.copyToList(
61getSubjectGenerator()
62.getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
63Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator()));
64
65// some tests assume SEVERAL == 3
66if (entries.size() >= 1) {
67a = entries.get(0);
68if (entries.size() >= 3) {
69b = entries.get(1);
70c = entries.get(2);
71}
72}
73}
74
75/** Resets the contents of navigableMap to have entries a, c, for the navigation tests. */
76@SuppressWarnings("unchecked") // Needed to stop Eclipse whining
77private void resetWithHole() {
78Entry<K, V>[] entries = (Entry<K, V>[]) new Entry<?, ?>[] {a, c};
79super.resetMap(entries);
80navigableMap = (NavigableMap<K, V>) getMap();
81}
82
83@CollectionSize.Require(ZERO)
84public void testEmptyMapFirst() {
85assertNull(navigableMap.firstEntry());
86}
87
88@MapFeature.Require(SUPPORTS_REMOVE)
89@CollectionSize.Require(ZERO)
90public void testEmptyMapPollFirst() {
91assertNull(navigableMap.pollFirstEntry());
92}
93
94@CollectionSize.Require(ZERO)
95public void testEmptyMapNearby() {
96assertNull(navigableMap.lowerEntry(k0()));
97assertNull(navigableMap.lowerKey(k0()));
98assertNull(navigableMap.floorEntry(k0()));
99assertNull(navigableMap.floorKey(k0()));
100assertNull(navigableMap.ceilingEntry(k0()));
101assertNull(navigableMap.ceilingKey(k0()));
102assertNull(navigableMap.higherEntry(k0()));
103assertNull(navigableMap.higherKey(k0()));
104}
105
106@CollectionSize.Require(ZERO)
107public void testEmptyMapLast() {
108assertNull(navigableMap.lastEntry());
109}
110
111@MapFeature.Require(SUPPORTS_REMOVE)
112@CollectionSize.Require(ZERO)
113public void testEmptyMapPollLast() {
114assertNull(navigableMap.pollLastEntry());
115}
116
117@CollectionSize.Require(ONE)
118public void testSingletonMapFirst() {
119assertEquals(a, navigableMap.firstEntry());
120}
121
122@MapFeature.Require(SUPPORTS_REMOVE)
123@CollectionSize.Require(ONE)
124public void testSingletonMapPollFirst() {
125assertEquals(a, navigableMap.pollFirstEntry());
126assertTrue(navigableMap.isEmpty());
127}
128
129@CollectionSize.Require(ONE)
130public void testSingletonMapNearby() {
131assertNull(navigableMap.lowerEntry(k0()));
132assertNull(navigableMap.lowerKey(k0()));
133assertEquals(a, navigableMap.floorEntry(k0()));
134assertEquals(a.getKey(), navigableMap.floorKey(k0()));
135assertEquals(a, navigableMap.ceilingEntry(k0()));
136assertEquals(a.getKey(), navigableMap.ceilingKey(k0()));
137assertNull(navigableMap.higherEntry(k0()));
138assertNull(navigableMap.higherKey(k0()));
139}
140
141@CollectionSize.Require(ONE)
142public void testSingletonMapLast() {
143assertEquals(a, navigableMap.lastEntry());
144}
145
146@MapFeature.Require(SUPPORTS_REMOVE)
147@CollectionSize.Require(ONE)
148public void testSingletonMapPollLast() {
149assertEquals(a, navigableMap.pollLastEntry());
150assertTrue(navigableMap.isEmpty());
151}
152
153@CollectionSize.Require(SEVERAL)
154public void testFirst() {
155assertEquals(a, navigableMap.firstEntry());
156}
157
158@MapFeature.Require(SUPPORTS_REMOVE)
159@CollectionSize.Require(SEVERAL)
160public void testPollFirst() {
161assertEquals(a, navigableMap.pollFirstEntry());
162assertEquals(entries.subList(1, entries.size()), Helpers.copyToList(navigableMap.entrySet()));
163}
164
165@MapFeature.Require(absent = SUPPORTS_REMOVE)
166public void testPollFirstUnsupported() {
167assertThrows(UnsupportedOperationException.class, () -> navigableMap.pollFirstEntry());
168}
169
170@CollectionSize.Require(SEVERAL)
171public void testLower() {
172resetWithHole();
173assertEquals(null, navigableMap.lowerEntry(a.getKey()));
174assertEquals(null, navigableMap.lowerKey(a.getKey()));
175assertEquals(a, navigableMap.lowerEntry(b.getKey()));
176assertEquals(a.getKey(), navigableMap.lowerKey(b.getKey()));
177assertEquals(a, navigableMap.lowerEntry(c.getKey()));
178assertEquals(a.getKey(), navigableMap.lowerKey(c.getKey()));
179}
180
181@CollectionSize.Require(SEVERAL)
182public void testFloor() {
183resetWithHole();
184assertEquals(a, navigableMap.floorEntry(a.getKey()));
185assertEquals(a.getKey(), navigableMap.floorKey(a.getKey()));
186assertEquals(a, navigableMap.floorEntry(b.getKey()));
187assertEquals(a.getKey(), navigableMap.floorKey(b.getKey()));
188assertEquals(c, navigableMap.floorEntry(c.getKey()));
189assertEquals(c.getKey(), navigableMap.floorKey(c.getKey()));
190}
191
192@CollectionSize.Require(SEVERAL)
193public void testCeiling() {
194resetWithHole();
195assertEquals(a, navigableMap.ceilingEntry(a.getKey()));
196assertEquals(a.getKey(), navigableMap.ceilingKey(a.getKey()));
197assertEquals(c, navigableMap.ceilingEntry(b.getKey()));
198assertEquals(c.getKey(), navigableMap.ceilingKey(b.getKey()));
199assertEquals(c, navigableMap.ceilingEntry(c.getKey()));
200assertEquals(c.getKey(), navigableMap.ceilingKey(c.getKey()));
201}
202
203@CollectionSize.Require(SEVERAL)
204public void testHigher() {
205resetWithHole();
206assertEquals(c, navigableMap.higherEntry(a.getKey()));
207assertEquals(c.getKey(), navigableMap.higherKey(a.getKey()));
208assertEquals(c, navigableMap.higherEntry(b.getKey()));
209assertEquals(c.getKey(), navigableMap.higherKey(b.getKey()));
210assertEquals(null, navigableMap.higherEntry(c.getKey()));
211assertEquals(null, navigableMap.higherKey(c.getKey()));
212}
213
214@CollectionSize.Require(SEVERAL)
215public void testLast() {
216assertEquals(c, navigableMap.lastEntry());
217}
218
219@MapFeature.Require(SUPPORTS_REMOVE)
220@CollectionSize.Require(SEVERAL)
221public void testPollLast() {
222assertEquals(c, navigableMap.pollLastEntry());
223assertEquals(
224entries.subList(0, entries.size() - 1), Helpers.copyToList(navigableMap.entrySet()));
225}
226
227@MapFeature.Require(absent = SUPPORTS_REMOVE)
228@CollectionSize.Require(SEVERAL)
229public void testPollLastUnsupported() {
230assertThrows(UnsupportedOperationException.class, () -> navigableMap.pollLastEntry());
231}
232
233@CollectionSize.Require(SEVERAL)
234public void testDescendingNavigation() {
235List<Entry<K, V>> descending = new ArrayList<>(navigableMap.descendingMap().entrySet());
236Collections.reverse(descending);
237assertEquals(entries, descending);
238}
239
240@CollectionSize.Require(absent = ZERO)
241public void testHeadMapExclusive() {
242assertFalse(navigableMap.headMap(a.getKey(), false).containsKey(a.getKey()));
243}
244
245@CollectionSize.Require(absent = ZERO)
246public void testHeadMapInclusive() {
247assertTrue(navigableMap.headMap(a.getKey(), true).containsKey(a.getKey()));
248}
249
250@CollectionSize.Require(absent = ZERO)
251public void testTailMapExclusive() {
252assertFalse(navigableMap.tailMap(a.getKey(), false).containsKey(a.getKey()));
253}
254
255@CollectionSize.Require(absent = ZERO)
256public void testTailMapInclusive() {
257assertTrue(navigableMap.tailMap(a.getKey(), true).containsKey(a.getKey()));
258}
259}
260