Java

Форк
0
/
NavigableMapNavigationTester.java 
259 строк · 8.9 Кб
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
import static org.junit.Assert.assertThrows;
24

25
import com.google.common.annotations.GwtIncompatible;
26
import com.google.common.collect.testing.AbstractMapTester;
27
import com.google.common.collect.testing.Helpers;
28
import com.google.common.collect.testing.features.CollectionSize;
29
import com.google.common.collect.testing.features.MapFeature;
30
import java.util.ArrayList;
31
import java.util.Collections;
32
import java.util.List;
33
import java.util.Map.Entry;
34
import java.util.NavigableMap;
35
import org.junit.Ignore;
36

37
/**
38
 * A generic JUnit test which tests operations on a NavigableMap. Can't be invoked directly; please
39
 * see {@code NavigableMapTestSuiteBuilder}.
40
 *
41
 * @author Jesse Wilson
42
 * @author Louis Wasserman
43
 */
44
@GwtIncompatible
45
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
46
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
47
public class NavigableMapNavigationTester<K, V> extends AbstractMapTester<K, V> {
48

49
  private NavigableMap<K, V> navigableMap;
50
  private List<Entry<K, V>> entries;
51
  private Entry<K, V> a;
52
  private Entry<K, V> b;
53
  private Entry<K, V> c;
54

55
  @Override
56
  public void setUp() throws Exception {
57
    super.setUp();
58
    navigableMap = (NavigableMap<K, V>) getMap();
59
    entries =
60
        Helpers.copyToList(
61
            getSubjectGenerator()
62
                .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
63
    Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator()));
64

65
    // some tests assume SEVERAL == 3
66
    if (entries.size() >= 1) {
67
      a = entries.get(0);
68
      if (entries.size() >= 3) {
69
        b = entries.get(1);
70
        c = entries.get(2);
71
      }
72
    }
73
  }
74

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

83
  @CollectionSize.Require(ZERO)
84
  public void testEmptyMapFirst() {
85
    assertNull(navigableMap.firstEntry());
86
  }
87

88
  @MapFeature.Require(SUPPORTS_REMOVE)
89
  @CollectionSize.Require(ZERO)
90
  public void testEmptyMapPollFirst() {
91
    assertNull(navigableMap.pollFirstEntry());
92
  }
93

94
  @CollectionSize.Require(ZERO)
95
  public void testEmptyMapNearby() {
96
    assertNull(navigableMap.lowerEntry(k0()));
97
    assertNull(navigableMap.lowerKey(k0()));
98
    assertNull(navigableMap.floorEntry(k0()));
99
    assertNull(navigableMap.floorKey(k0()));
100
    assertNull(navigableMap.ceilingEntry(k0()));
101
    assertNull(navigableMap.ceilingKey(k0()));
102
    assertNull(navigableMap.higherEntry(k0()));
103
    assertNull(navigableMap.higherKey(k0()));
104
  }
105

106
  @CollectionSize.Require(ZERO)
107
  public void testEmptyMapLast() {
108
    assertNull(navigableMap.lastEntry());
109
  }
110

111
  @MapFeature.Require(SUPPORTS_REMOVE)
112
  @CollectionSize.Require(ZERO)
113
  public void testEmptyMapPollLast() {
114
    assertNull(navigableMap.pollLastEntry());
115
  }
116

117
  @CollectionSize.Require(ONE)
118
  public void testSingletonMapFirst() {
119
    assertEquals(a, navigableMap.firstEntry());
120
  }
121

122
  @MapFeature.Require(SUPPORTS_REMOVE)
123
  @CollectionSize.Require(ONE)
124
  public void testSingletonMapPollFirst() {
125
    assertEquals(a, navigableMap.pollFirstEntry());
126
    assertTrue(navigableMap.isEmpty());
127
  }
128

129
  @CollectionSize.Require(ONE)
130
  public void testSingletonMapNearby() {
131
    assertNull(navigableMap.lowerEntry(k0()));
132
    assertNull(navigableMap.lowerKey(k0()));
133
    assertEquals(a, navigableMap.floorEntry(k0()));
134
    assertEquals(a.getKey(), navigableMap.floorKey(k0()));
135
    assertEquals(a, navigableMap.ceilingEntry(k0()));
136
    assertEquals(a.getKey(), navigableMap.ceilingKey(k0()));
137
    assertNull(navigableMap.higherEntry(k0()));
138
    assertNull(navigableMap.higherKey(k0()));
139
  }
140

141
  @CollectionSize.Require(ONE)
142
  public void testSingletonMapLast() {
143
    assertEquals(a, navigableMap.lastEntry());
144
  }
145

146
  @MapFeature.Require(SUPPORTS_REMOVE)
147
  @CollectionSize.Require(ONE)
148
  public void testSingletonMapPollLast() {
149
    assertEquals(a, navigableMap.pollLastEntry());
150
    assertTrue(navigableMap.isEmpty());
151
  }
152

153
  @CollectionSize.Require(SEVERAL)
154
  public void testFirst() {
155
    assertEquals(a, navigableMap.firstEntry());
156
  }
157

158
  @MapFeature.Require(SUPPORTS_REMOVE)
159
  @CollectionSize.Require(SEVERAL)
160
  public void testPollFirst() {
161
    assertEquals(a, navigableMap.pollFirstEntry());
162
    assertEquals(entries.subList(1, entries.size()), Helpers.copyToList(navigableMap.entrySet()));
163
  }
164

165
  @MapFeature.Require(absent = SUPPORTS_REMOVE)
166
  public void testPollFirstUnsupported() {
167
    assertThrows(UnsupportedOperationException.class, () -> navigableMap.pollFirstEntry());
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
    assertThrows(UnsupportedOperationException.class, () -> navigableMap.pollLastEntry());
231
  }
232

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

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

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

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

255
  @CollectionSize.Require(absent = ZERO)
256
  public void testTailMapInclusive() {
257
    assertTrue(navigableMap.tailMap(a.getKey(), true).containsKey(a.getKey()));
258
  }
259
}
260

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.