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.features.CollectionSize.ONE;
020import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
021import static com.google.common.collect.testing.features.CollectionSize.ZERO;
022import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
023
024import com.google.common.annotations.GwtIncompatible;
025import com.google.common.collect.testing.AbstractMapTester;
026import com.google.common.collect.testing.Helpers;
027import com.google.common.collect.testing.features.CollectionSize;
028import com.google.common.collect.testing.features.MapFeature;
029import java.util.ArrayList;
030import java.util.Collections;
031import java.util.List;
032import java.util.Map.Entry;
033import java.util.NavigableMap;
034import org.junit.Ignore;
035
036/**
037 * A generic JUnit test which tests operations on a NavigableMap. Can't be invoked directly; please
038 * see {@code NavigableMapTestSuiteBuilder}.
039 *
040 * @author Jesse Wilson
041 * @author Louis Wasserman
042 */
043@GwtIncompatible
044@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
045public class NavigableMapNavigationTester<K, V> extends AbstractMapTester<K, V> {
046
047  private NavigableMap<K, V> navigableMap;
048  private List<Entry<K, V>> entries;
049  private Entry<K, V> a;
050  private Entry<K, V> b;
051  private Entry<K, V> c;
052
053  @Override
054  public void setUp() throws Exception {
055    super.setUp();
056    navigableMap = (NavigableMap<K, V>) getMap();
057    entries =
058        Helpers.copyToList(
059            getSubjectGenerator()
060                .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
061    Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator()));
062
063    // some tests assume SEVERAL == 3
064    if (entries.size() >= 1) {
065      a = entries.get(0);
066      if (entries.size() >= 3) {
067        b = entries.get(1);
068        c = entries.get(2);
069      }
070    }
071  }
072
073  /** Resets the contents of navigableMap to have entries a, c, for the navigation tests. */
074  @SuppressWarnings("unchecked") // Needed to stop Eclipse whining
075  private void resetWithHole() {
076    Entry<K, V>[] entries = new Entry[] {a, c};
077    super.resetMap(entries);
078    navigableMap = (NavigableMap<K, V>) getMap();
079  }
080
081  @CollectionSize.Require(ZERO)
082  public void testEmptyMapFirst() {
083    assertNull(navigableMap.firstEntry());
084  }
085
086  @MapFeature.Require(SUPPORTS_REMOVE)
087  @CollectionSize.Require(ZERO)
088  public void testEmptyMapPollFirst() {
089    assertNull(navigableMap.pollFirstEntry());
090  }
091
092  @CollectionSize.Require(ZERO)
093  public void testEmptyMapNearby() {
094    assertNull(navigableMap.lowerEntry(k0()));
095    assertNull(navigableMap.lowerKey(k0()));
096    assertNull(navigableMap.floorEntry(k0()));
097    assertNull(navigableMap.floorKey(k0()));
098    assertNull(navigableMap.ceilingEntry(k0()));
099    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}