001/* 002 * Copyright (C) 2010 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.testers; 018 019import static com.google.common.collect.testing.Helpers.copyToList; 020import static com.google.common.collect.testing.features.CollectionSize.ONE; 021import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 022import static com.google.common.collect.testing.features.CollectionSize.ZERO; 023import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 024import static java.util.Collections.sort; 025import static org.junit.Assert.assertThrows; 026 027import com.google.common.annotations.GwtIncompatible; 028import com.google.common.collect.testing.AbstractMapTester; 029import com.google.common.collect.testing.Helpers; 030import com.google.common.collect.testing.features.CollectionSize; 031import com.google.common.collect.testing.features.MapFeature; 032import java.util.ArrayList; 033import java.util.Collections; 034import java.util.List; 035import java.util.Map.Entry; 036import java.util.NavigableMap; 037import org.junit.Ignore; 038 039/** 040 * A generic JUnit test which tests operations on a NavigableMap. Can't be invoked directly; please 041 * see {@code NavigableMapTestSuiteBuilder}. 042 * 043 * @author Jesse Wilson 044 * @author Louis Wasserman 045 */ 046@GwtIncompatible 047@Ignore("test runners must not instantiate and run this directly, only via suites we build") 048// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 049@SuppressWarnings("JUnit4ClassUsedInJUnit3") 050public class NavigableMapNavigationTester<K, V> extends AbstractMapTester<K, V> { 051 052 private NavigableMap<K, V> navigableMap; 053 private List<Entry<K, V>> entries; 054 private Entry<K, V> a; 055 private Entry<K, V> b; 056 private Entry<K, V> c; 057 058 @Override 059 public void setUp() throws Exception { 060 super.setUp(); 061 navigableMap = (NavigableMap<K, V>) getMap(); 062 entries = 063 copyToList( 064 getSubjectGenerator() 065 .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements())); 066 sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator())); 067 068 // some tests assume SEVERAL == 3 069 if (entries.size() >= 1) { 070 a = entries.get(0); 071 if (entries.size() >= 3) { 072 b = entries.get(1); 073 c = entries.get(2); 074 } 075 } 076 } 077 078 /** Resets the contents of navigableMap to have entries a, c, for the navigation tests. */ 079 @SuppressWarnings("unchecked") // Needed to stop Eclipse whining 080 private void resetWithHole() { 081 Entry<K, V>[] entries = (Entry<K, V>[]) new Entry<?, ?>[] {a, c}; 082 super.resetMap(entries); 083 navigableMap = (NavigableMap<K, V>) getMap(); 084 } 085 086 @CollectionSize.Require(ZERO) 087 public void testEmptyMapFirst() { 088 assertNull(navigableMap.firstEntry()); 089 } 090 091 @MapFeature.Require(SUPPORTS_REMOVE) 092 @CollectionSize.Require(ZERO) 093 public void testEmptyMapPollFirst() { 094 assertNull(navigableMap.pollFirstEntry()); 095 } 096 097 @CollectionSize.Require(ZERO) 098 public void testEmptyMapNearby() { 099 assertNull(navigableMap.lowerEntry(k0())); 100 assertNull(navigableMap.lowerKey(k0())); 101 assertNull(navigableMap.floorEntry(k0())); 102 assertNull(navigableMap.floorKey(k0())); 103 assertNull(navigableMap.ceilingEntry(k0())); 104 assertNull(navigableMap.ceilingKey(k0())); 105 assertNull(navigableMap.higherEntry(k0())); 106 assertNull(navigableMap.higherKey(k0())); 107 } 108 109 @CollectionSize.Require(ZERO) 110 public void testEmptyMapLast() { 111 assertNull(navigableMap.lastEntry()); 112 } 113 114 @MapFeature.Require(SUPPORTS_REMOVE) 115 @CollectionSize.Require(ZERO) 116 public void testEmptyMapPollLast() { 117 assertNull(navigableMap.pollLastEntry()); 118 } 119 120 @CollectionSize.Require(ONE) 121 public void testSingletonMapFirst() { 122 assertEquals(a, navigableMap.firstEntry()); 123 } 124 125 @MapFeature.Require(SUPPORTS_REMOVE) 126 @CollectionSize.Require(ONE) 127 public void testSingletonMapPollFirst() { 128 assertEquals(a, navigableMap.pollFirstEntry()); 129 assertTrue(navigableMap.isEmpty()); 130 } 131 132 @CollectionSize.Require(ONE) 133 public void testSingletonMapNearby() { 134 assertNull(navigableMap.lowerEntry(k0())); 135 assertNull(navigableMap.lowerKey(k0())); 136 assertEquals(a, navigableMap.floorEntry(k0())); 137 assertEquals(a.getKey(), navigableMap.floorKey(k0())); 138 assertEquals(a, navigableMap.ceilingEntry(k0())); 139 assertEquals(a.getKey(), navigableMap.ceilingKey(k0())); 140 assertNull(navigableMap.higherEntry(k0())); 141 assertNull(navigableMap.higherKey(k0())); 142 } 143 144 @CollectionSize.Require(ONE) 145 public void testSingletonMapLast() { 146 assertEquals(a, navigableMap.lastEntry()); 147 } 148 149 @MapFeature.Require(SUPPORTS_REMOVE) 150 @CollectionSize.Require(ONE) 151 public void testSingletonMapPollLast() { 152 assertEquals(a, navigableMap.pollLastEntry()); 153 assertTrue(navigableMap.isEmpty()); 154 } 155 156 @CollectionSize.Require(SEVERAL) 157 public void testFirst() { 158 assertEquals(a, navigableMap.firstEntry()); 159 } 160 161 @MapFeature.Require(SUPPORTS_REMOVE) 162 @CollectionSize.Require(SEVERAL) 163 public void testPollFirst() { 164 assertEquals(a, navigableMap.pollFirstEntry()); 165 assertEquals(entries.subList(1, entries.size()), copyToList(navigableMap.entrySet())); 166 } 167 168 @MapFeature.Require(absent = SUPPORTS_REMOVE) 169 public void testPollFirstUnsupported() { 170 assertThrows(UnsupportedOperationException.class, () -> navigableMap.pollFirstEntry()); 171 } 172 173 @CollectionSize.Require(SEVERAL) 174 public void testLower() { 175 resetWithHole(); 176 assertEquals(null, navigableMap.lowerEntry(a.getKey())); 177 assertEquals(null, navigableMap.lowerKey(a.getKey())); 178 assertEquals(a, navigableMap.lowerEntry(b.getKey())); 179 assertEquals(a.getKey(), navigableMap.lowerKey(b.getKey())); 180 assertEquals(a, navigableMap.lowerEntry(c.getKey())); 181 assertEquals(a.getKey(), navigableMap.lowerKey(c.getKey())); 182 } 183 184 @CollectionSize.Require(SEVERAL) 185 public void testFloor() { 186 resetWithHole(); 187 assertEquals(a, navigableMap.floorEntry(a.getKey())); 188 assertEquals(a.getKey(), navigableMap.floorKey(a.getKey())); 189 assertEquals(a, navigableMap.floorEntry(b.getKey())); 190 assertEquals(a.getKey(), navigableMap.floorKey(b.getKey())); 191 assertEquals(c, navigableMap.floorEntry(c.getKey())); 192 assertEquals(c.getKey(), navigableMap.floorKey(c.getKey())); 193 } 194 195 @CollectionSize.Require(SEVERAL) 196 public void testCeiling() { 197 resetWithHole(); 198 assertEquals(a, navigableMap.ceilingEntry(a.getKey())); 199 assertEquals(a.getKey(), navigableMap.ceilingKey(a.getKey())); 200 assertEquals(c, navigableMap.ceilingEntry(b.getKey())); 201 assertEquals(c.getKey(), navigableMap.ceilingKey(b.getKey())); 202 assertEquals(c, navigableMap.ceilingEntry(c.getKey())); 203 assertEquals(c.getKey(), navigableMap.ceilingKey(c.getKey())); 204 } 205 206 @CollectionSize.Require(SEVERAL) 207 public void testHigher() { 208 resetWithHole(); 209 assertEquals(c, navigableMap.higherEntry(a.getKey())); 210 assertEquals(c.getKey(), navigableMap.higherKey(a.getKey())); 211 assertEquals(c, navigableMap.higherEntry(b.getKey())); 212 assertEquals(c.getKey(), navigableMap.higherKey(b.getKey())); 213 assertEquals(null, navigableMap.higherEntry(c.getKey())); 214 assertEquals(null, navigableMap.higherKey(c.getKey())); 215 } 216 217 @CollectionSize.Require(SEVERAL) 218 public void testLast() { 219 assertEquals(c, navigableMap.lastEntry()); 220 } 221 222 @MapFeature.Require(SUPPORTS_REMOVE) 223 @CollectionSize.Require(SEVERAL) 224 public void testPollLast() { 225 assertEquals(c, navigableMap.pollLastEntry()); 226 assertEquals(entries.subList(0, entries.size() - 1), copyToList(navigableMap.entrySet())); 227 } 228 229 @MapFeature.Require(absent = SUPPORTS_REMOVE) 230 @CollectionSize.Require(SEVERAL) 231 public void testPollLastUnsupported() { 232 assertThrows(UnsupportedOperationException.class, () -> navigableMap.pollLastEntry()); 233 } 234 235 @CollectionSize.Require(SEVERAL) 236 public void testDescendingNavigation() { 237 List<Entry<K, V>> descending = new ArrayList<>(navigableMap.descendingMap().entrySet()); 238 Collections.reverse(descending); 239 assertEquals(entries, descending); 240 } 241 242 @CollectionSize.Require(absent = ZERO) 243 public void testHeadMapExclusive() { 244 assertFalse(navigableMap.headMap(a.getKey(), false).containsKey(a.getKey())); 245 } 246 247 @CollectionSize.Require(absent = ZERO) 248 public void testHeadMapInclusive() { 249 assertTrue(navigableMap.headMap(a.getKey(), true).containsKey(a.getKey())); 250 } 251 252 @CollectionSize.Require(absent = ZERO) 253 public void testTailMapExclusive() { 254 assertFalse(navigableMap.tailMap(a.getKey(), false).containsKey(a.getKey())); 255 } 256 257 @CollectionSize.Require(absent = ZERO) 258 public void testTailMapInclusive() { 259 assertTrue(navigableMap.tailMap(a.getKey(), true).containsKey(a.getKey())); 260 } 261}