Debian Codegraph @

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

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