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