aboutsummaryrefslogtreecommitdiff
path: root/android/guava-testlib/src/com/google/common/collect/testing/testers/NavigableMapNavigationTester.java
blob: 936783e77e64ca03293371116dab27c9560fb1bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * Copyright (C) 2010 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect.testing.testers;

import static com.google.common.collect.testing.features.CollectionSize.ONE;
import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
import static org.junit.Assert.assertThrows;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.testing.AbstractMapTester;
import com.google.common.collect.testing.Helpers;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.MapFeature;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.NavigableMap;
import org.junit.Ignore;

/**
 * A generic JUnit test which tests operations on a NavigableMap. Can't be invoked directly; please
 * see {@code NavigableMapTestSuiteBuilder}.
 *
 * @author Jesse Wilson
 * @author Louis Wasserman
 */
@GwtIncompatible
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
public class NavigableMapNavigationTester<K, V> extends AbstractMapTester<K, V> {

  private NavigableMap<K, V> navigableMap;
  private List<Entry<K, V>> entries;
  private Entry<K, V> a;
  private Entry<K, V> b;
  private Entry<K, V> c;

  @Override
  public void setUp() throws Exception {
    super.setUp();
    navigableMap = (NavigableMap<K, V>) getMap();
    entries =
        Helpers.copyToList(
            getSubjectGenerator()
                .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
    Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator()));

    // some tests assume SEVERAL == 3
    if (entries.size() >= 1) {
      a = entries.get(0);
      if (entries.size() >= 3) {
        b = entries.get(1);
        c = entries.get(2);
      }
    }
  }

  /** Resets the contents of navigableMap to have entries a, c, for the navigation tests. */
  @SuppressWarnings("unchecked") // Needed to stop Eclipse whining
  private void resetWithHole() {
    Entry<K, V>[] entries = new Entry[] {a, c};
    super.resetMap(entries);
    navigableMap = (NavigableMap<K, V>) getMap();
  }

  @CollectionSize.Require(ZERO)
  public void testEmptyMapFirst() {
    assertNull(navigableMap.firstEntry());
  }

  @MapFeature.Require(SUPPORTS_REMOVE)
  @CollectionSize.Require(ZERO)
  public void testEmptyMapPollFirst() {
    assertNull(navigableMap.pollFirstEntry());
  }

  @CollectionSize.Require(ZERO)
  public void testEmptyMapNearby() {
    assertNull(navigableMap.lowerEntry(k0()));
    assertNull(navigableMap.lowerKey(k0()));
    assertNull(navigableMap.floorEntry(k0()));
    assertNull(navigableMap.floorKey(k0()));
    assertNull(navigableMap.ceilingEntry(k0()));
    assertNull(navigableMap.ceilingKey(k0()));
    assertNull(navigableMap.higherEntry(k0()));
    assertNull(navigableMap.higherKey(k0()));
  }

  @CollectionSize.Require(ZERO)
  public void testEmptyMapLast() {
    assertNull(navigableMap.lastEntry());
  }

  @MapFeature.Require(SUPPORTS_REMOVE)
  @CollectionSize.Require(ZERO)
  public void testEmptyMapPollLast() {
    assertNull(navigableMap.pollLastEntry());
  }

  @CollectionSize.Require(ONE)
  public void testSingletonMapFirst() {
    assertEquals(a, navigableMap.firstEntry());
  }

  @MapFeature.Require(SUPPORTS_REMOVE)
  @CollectionSize.Require(ONE)
  public void testSingletonMapPollFirst() {
    assertEquals(a, navigableMap.pollFirstEntry());
    assertTrue(navigableMap.isEmpty());
  }

  @CollectionSize.Require(ONE)
  public void testSingletonMapNearby() {
    assertNull(navigableMap.lowerEntry(k0()));
    assertNull(navigableMap.lowerKey(k0()));
    assertEquals(a, navigableMap.floorEntry(k0()));
    assertEquals(a.getKey(), navigableMap.floorKey(k0()));
    assertEquals(a, navigableMap.ceilingEntry(k0()));
    assertEquals(a.getKey(), navigableMap.ceilingKey(k0()));
    assertNull(navigableMap.higherEntry(k0()));
    assertNull(navigableMap.higherKey(k0()));
  }

  @CollectionSize.Require(ONE)
  public void testSingletonMapLast() {
    assertEquals(a, navigableMap.lastEntry());
  }

  @MapFeature.Require(SUPPORTS_REMOVE)
  @CollectionSize.Require(ONE)
  public void testSingletonMapPollLast() {
    assertEquals(a, navigableMap.pollLastEntry());
    assertTrue(navigableMap.isEmpty());
  }

  @CollectionSize.Require(SEVERAL)
  public void testFirst() {
    assertEquals(a, navigableMap.firstEntry());
  }

  @MapFeature.Require(SUPPORTS_REMOVE)
  @CollectionSize.Require(SEVERAL)
  public void testPollFirst() {
    assertEquals(a, navigableMap.pollFirstEntry());
    assertEquals(entries.subList(1, entries.size()), Helpers.copyToList(navigableMap.entrySet()));
  }

  @MapFeature.Require(absent = SUPPORTS_REMOVE)
  public void testPollFirstUnsupported() {
    assertThrows(UnsupportedOperationException.class, () -> navigableMap.pollFirstEntry());
  }

  @CollectionSize.Require(SEVERAL)
  public void testLower() {
    resetWithHole();
    assertEquals(null, navigableMap.lowerEntry(a.getKey()));
    assertEquals(null, navigableMap.lowerKey(a.getKey()));
    assertEquals(a, navigableMap.lowerEntry(b.getKey()));
    assertEquals(a.getKey(), navigableMap.lowerKey(b.getKey()));
    assertEquals(a, navigableMap.lowerEntry(c.getKey()));
    assertEquals(a.getKey(), navigableMap.lowerKey(c.getKey()));
  }

  @CollectionSize.Require(SEVERAL)
  public void testFloor() {
    resetWithHole();
    assertEquals(a, navigableMap.floorEntry(a.getKey()));
    assertEquals(a.getKey(), navigableMap.floorKey(a.getKey()));
    assertEquals(a, navigableMap.floorEntry(b.getKey()));
    assertEquals(a.getKey(), navigableMap.floorKey(b.getKey()));
    assertEquals(c, navigableMap.floorEntry(c.getKey()));
    assertEquals(c.getKey(), navigableMap.floorKey(c.getKey()));
  }

  @CollectionSize.Require(SEVERAL)
  public void testCeiling() {
    resetWithHole();
    assertEquals(a, navigableMap.ceilingEntry(a.getKey()));
    assertEquals(a.getKey(), navigableMap.ceilingKey(a.getKey()));
    assertEquals(c, navigableMap.ceilingEntry(b.getKey()));
    assertEquals(c.getKey(), navigableMap.ceilingKey(b.getKey()));
    assertEquals(c, navigableMap.ceilingEntry(c.getKey()));
    assertEquals(c.getKey(), navigableMap.ceilingKey(c.getKey()));
  }

  @CollectionSize.Require(SEVERAL)
  public void testHigher() {
    resetWithHole();
    assertEquals(c, navigableMap.higherEntry(a.getKey()));
    assertEquals(c.getKey(), navigableMap.higherKey(a.getKey()));
    assertEquals(c, navigableMap.higherEntry(b.getKey()));
    assertEquals(c.getKey(), navigableMap.higherKey(b.getKey()));
    assertEquals(null, navigableMap.higherEntry(c.getKey()));
    assertEquals(null, navigableMap.higherKey(c.getKey()));
  }

  @CollectionSize.Require(SEVERAL)
  public void testLast() {
    assertEquals(c, navigableMap.lastEntry());
  }

  @MapFeature.Require(SUPPORTS_REMOVE)
  @CollectionSize.Require(SEVERAL)
  public void testPollLast() {
    assertEquals(c, navigableMap.pollLastEntry());
    assertEquals(
        entries.subList(0, entries.size() - 1), Helpers.copyToList(navigableMap.entrySet()));
  }

  @MapFeature.Require(absent = SUPPORTS_REMOVE)
  @CollectionSize.Require(SEVERAL)
  public void testPollLastUnsupported() {
    assertThrows(UnsupportedOperationException.class, () -> navigableMap.pollLastEntry());
  }

  @CollectionSize.Require(SEVERAL)
  public void testDescendingNavigation() {
    List<Entry<K, V>> descending = new ArrayList<>(navigableMap.descendingMap().entrySet());
    Collections.reverse(descending);
    assertEquals(entries, descending);
  }

  @CollectionSize.Require(absent = ZERO)
  public void testHeadMapExclusive() {
    assertFalse(navigableMap.headMap(a.getKey(), false).containsKey(a.getKey()));
  }

  @CollectionSize.Require(absent = ZERO)
  public void testHeadMapInclusive() {
    assertTrue(navigableMap.headMap(a.getKey(), true).containsKey(a.getKey()));
  }

  @CollectionSize.Require(absent = ZERO)
  public void testTailMapExclusive() {
    assertFalse(navigableMap.tailMap(a.getKey(), false).containsKey(a.getKey()));
  }

  @CollectionSize.Require(absent = ZERO)
  public void testTailMapInclusive() {
    assertTrue(navigableMap.tailMap(a.getKey(), true).containsKey(a.getKey()));
  }
}