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;
034
035/**
036 * A generic JUnit test which tests operations on a NavigableMap. Can't be invoked directly; please
037 * see {@code NavigableMapTestSuiteBuilder}.
038 *
039 * @author Jesse Wilson
040 * @author Louis Wasserman
041 */
042@GwtIncompatible
043public class NavigableMapNavigationTester<K, V> extends AbstractMapTester<K, V> {
044
045  private NavigableMap<K, V> navigableMap;
046  private List<Entry<K, V>> entries;
047  private Entry<K, V> a;
048  private Entry<K, V> b;
049  private Entry<K, V> c;
050
051  @Override
052  public void setUp() throws Exception {
053    super.setUp();
054    navigableMap = (NavigableMap<K, V>) getMap();
055    entries =
056        Helpers.copyToList(
057            getSubjectGenerator()
058                .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
059    Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator()));
060
061    // some tests assume SEVERAL == 3
062    if (entries.size() >= 1) {
063      a = entries.get(0);
064      if (entries.size() >= 3) {
065        b = entries.get(1);
066        c = entries.get(2);
067      }
068    }
069  }
070
071  /** Resets the contents of navigableMap to have entries a, c, for the navigation tests. */
072  @SuppressWarnings("unchecked") // Needed to stop Eclipse whining
073  private void resetWithHole() {
074    Entry<K, V>[] entries = new Entry[] {a, c};
075    super.resetMap(entries);
076    navigableMap = (NavigableMap<K, V>) getMap();
077  }
078
079  @CollectionSize.Require(ZERO)
080  public void testEmptyMapFirst() {
081    assertNull(navigableMap.firstEntry());
082  }
083
084  @MapFeature.Require(SUPPORTS_REMOVE)
085  @CollectionSize.Require(ZERO)
086  public void testEmptyMapPollFirst() {
087    assertNull(navigableMap.pollFirstEntry());
088  }
089
090  @CollectionSize.Require(ZERO)
091  public void testEmptyMapNearby() {
092    assertNull(navigableMap.lowerEntry(k0()));
093    assertNull(navigableMap.lowerKey(k0()));
094    assertNull(navigableMap.floorEntry(k0()));
095    assertNull(navigableMap.floorKey(k0()));
096    assertNull(navigableMap.ceilingEntry(k0()));
097    assertNull(navigableMap.ceilingKey(k0()));
098    assertNull(navigableMap.higherEntry(k0()));
099    assertNull(navigableMap.higherKey(k0()));
100  }
101
102  @CollectionSize.Require(ZERO)
103  public void testEmptyMapLast() {
104    assertNull(navigableMap.lastEntry());
105  }
106
107  @MapFeature.Require(SUPPORTS_REMOVE)
108  @CollectionSize.Require(ZERO)
109  public void testEmptyMapPollLast() {
110    assertNull(navigableMap.pollLastEntry());
111  }
112
113  @CollectionSize.Require(ONE)
114  public void testSingletonMapFirst() {
115    assertEquals(a, navigableMap.firstEntry());
116  }
117
118  @MapFeature.Require(SUPPORTS_REMOVE)
119  @CollectionSize.Require(ONE)
120  public void testSingletonMapPollFirst() {
121    assertEquals(a, navigableMap.pollFirstEntry());
122    assertTrue(navigableMap.isEmpty());
123  }
124
125  @CollectionSize.Require(ONE)
126  public void testSingletonMapNearby() {
127    assertNull(navigableMap.lowerEntry(k0()));
128    assertNull(navigableMap.lowerKey(k0()));
129    assertEquals(a, navigableMap.floorEntry(k0()));
130    assertEquals(a.getKey(), navigableMap.floorKey(k0()));
131    assertEquals(a, navigableMap.ceilingEntry(k0()));
132    assertEquals(a.getKey(), navigableMap.ceilingKey(k0()));
133    assertNull(navigableMap.higherEntry(k0()));
134    assertNull(navigableMap.higherKey(k0()));
135  }
136
137  @CollectionSize.Require(ONE)
138  public void testSingletonMapLast() {
139    assertEquals(a, navigableMap.lastEntry());
140  }
141
142  @MapFeature.Require(SUPPORTS_REMOVE)
143  @CollectionSize.Require(ONE)
144  public void testSingletonMapPollLast() {
145    assertEquals(a, navigableMap.pollLastEntry());
146    assertTrue(navigableMap.isEmpty());
147  }
148
149  @CollectionSize.Require(SEVERAL)
150  public void testFirst() {
151    assertEquals(a, navigableMap.firstEntry());
152  }
153
154  @MapFeature.Require(SUPPORTS_REMOVE)
155  @CollectionSize.Require(SEVERAL)
156  public void testPollFirst() {
157    assertEquals(a, navigableMap.pollFirstEntry());
158    assertEquals(entries.subList(1, entries.size()), Helpers.copyToList(navigableMap.entrySet()));
159  }
160
161  @MapFeature.Require(absent = SUPPORTS_REMOVE)
162  public void testPollFirstUnsupported() {
163    try {
164      navigableMap.pollFirstEntry();
165      fail();
166    } catch (UnsupportedOperationException e) {
167    }
168  }
169
170  @CollectionSize.Require(SEVERAL)
171  public void testLower() {
172    resetWithHole();
173    assertEquals(null, navigableMap.lowerEntry(a.getKey()));
174    assertEquals(null, navigableMap.lowerKey(a.getKey()));
175    assertEquals(a, navigableMap.lowerEntry(b.getKey()));
176    assertEquals(a.getKey(), navigableMap.lowerKey(b.getKey()));
177    assertEquals(a, navigableMap.lowerEntry(c.getKey()));
178    assertEquals(a.getKey(), navigableMap.lowerKey(c.getKey()));
179  }
180
181  @CollectionSize.Require(SEVERAL)
182  public void testFloor() {
183    resetWithHole();
184    assertEquals(a, navigableMap.floorEntry(a.getKey()));
185    assertEquals(a.getKey(), navigableMap.floorKey(a.getKey()));
186    assertEquals(a, navigableMap.floorEntry(b.getKey()));
187    assertEquals(a.getKey(), navigableMap.floorKey(b.getKey()));
188    assertEquals(c, navigableMap.floorEntry(c.getKey()));
189    assertEquals(c.getKey(), navigableMap.floorKey(c.getKey()));
190  }
191
192  @CollectionSize.Require(SEVERAL)
193  public void testCeiling() {
194    resetWithHole();
195    assertEquals(a, navigableMap.ceilingEntry(a.getKey()));
196    assertEquals(a.getKey(), navigableMap.ceilingKey(a.getKey()));
197    assertEquals(c, navigableMap.ceilingEntry(b.getKey()));
198    assertEquals(c.getKey(), navigableMap.ceilingKey(b.getKey()));
199    assertEquals(c, navigableMap.ceilingEntry(c.getKey()));
200    assertEquals(c.getKey(), navigableMap.ceilingKey(c.getKey()));
201  }
202
203  @CollectionSize.Require(SEVERAL)
204  public void testHigher() {
205    resetWithHole();
206    assertEquals(c, navigableMap.higherEntry(a.getKey()));
207    assertEquals(c.getKey(), navigableMap.higherKey(a.getKey()));
208    assertEquals(c, navigableMap.higherEntry(b.getKey()));
209    assertEquals(c.getKey(), navigableMap.higherKey(b.getKey()));
210    assertEquals(null, navigableMap.higherEntry(c.getKey()));
211    assertEquals(null, navigableMap.higherKey(c.getKey()));
212  }
213
214  @CollectionSize.Require(SEVERAL)
215  public void testLast() {
216    assertEquals(c, navigableMap.lastEntry());
217  }
218
219  @MapFeature.Require(SUPPORTS_REMOVE)
220  @CollectionSize.Require(SEVERAL)
221  public void testPollLast() {
222    assertEquals(c, navigableMap.pollLastEntry());
223    assertEquals(
224        entries.subList(0, entries.size() - 1), Helpers.copyToList(navigableMap.entrySet()));
225  }
226
227  @MapFeature.Require(absent = SUPPORTS_REMOVE)
228  @CollectionSize.Require(SEVERAL)
229  public void testPollLastUnsupported() {
230    try {
231      navigableMap.pollLastEntry();
232      fail();
233    } catch (UnsupportedOperationException e) {
234    }
235  }
236
237  @CollectionSize.Require(SEVERAL)
238  public void testDescendingNavigation() {
239    List<Entry<K, V>> descending = new ArrayList<>(navigableMap.descendingMap().entrySet());
240    Collections.reverse(descending);
241    assertEquals(entries, descending);
242  }
243
244  @CollectionSize.Require(absent = ZERO)
245  public void testHeadMapExclusive() {
246    assertFalse(navigableMap.headMap(a.getKey(), false).containsKey(a.getKey()));
247  }
248
249  @CollectionSize.Require(absent = ZERO)
250  public void testHeadMapInclusive() {
251    assertTrue(navigableMap.headMap(a.getKey(), true).containsKey(a.getKey()));
252  }
253
254  @CollectionSize.Require(absent = ZERO)
255  public void testTailMapExclusive() {
256    assertFalse(navigableMap.tailMap(a.getKey(), false).containsKey(a.getKey()));
257  }
258
259  @CollectionSize.Require(absent = ZERO)
260  public void testTailMapInclusive() {
261    assertTrue(navigableMap.tailMap(a.getKey(), true).containsKey(a.getKey()));
262  }
263}