aboutsummaryrefslogtreecommitdiffstats
path: root/guava-tests/test/com/google/common/collect/ForwardingNavigableMapTest.java
blob: 16e2400f86e3d155d2c80d6412930d1182b19c04 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Copyright (C) 2011 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;

import static com.google.common.collect.Maps.immutableEntry;

import com.google.common.collect.testing.NavigableMapTestSuiteBuilder;
import com.google.common.collect.testing.SafeTreeMap;
import com.google.common.collect.testing.TestStringSortedMapGenerator;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.MapFeature;

import junit.framework.Test;
import junit.framework.TestSuite;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Set;
import java.util.SortedMap;

/**
 * Tests for {@code ForwardingNavigableMap}.
 *
 * @author Robert Konigsberg
 * @author Louis Wasserman
 */
public class ForwardingNavigableMapTest extends ForwardingSortedMapTest {
  static class StandardImplForwardingNavigableMap<K, V>
      extends ForwardingNavigableMap<K, V> {
    private final NavigableMap<K, V> backingMap;

    StandardImplForwardingNavigableMap(NavigableMap<K, V> backingMap) {
      this.backingMap = backingMap;
    }

    @Override protected NavigableMap<K, V> delegate() {
      return backingMap;
    }

    @Override public boolean containsKey(Object key) {
      return standardContainsKey(key);
    }

    @Override public boolean containsValue(Object value) {
      return standardContainsValue(value);
    }

    @Override public void putAll(Map<? extends K, ? extends V> map) {
      standardPutAll(map);
    }

    @Override public V remove(Object object) {
      return standardRemove(object);
    }

    @Override public boolean equals(Object object) {
      return standardEquals(object);
    }

    @Override public int hashCode() {
      return standardHashCode();
    }

    @Override public Set<K> keySet() {
      /*
       * We can't use StandardKeySet, as NavigableMapTestSuiteBuilder assumes that our keySet is a
       * NavigableSet. We test StandardKeySet in the superclass, so it's still covered.
       */
      return navigableKeySet();
    }

    @Override public Collection<V> values() {
      return new StandardValues();
    }

    @Override public String toString() {
      return standardToString();
    }

    @Override public Set<Entry<K, V>> entrySet() {
      return new StandardEntrySet() {
        @Override
        public Iterator<Entry<K, V>> iterator() {
          return backingMap.entrySet().iterator();
        }
      };
    }

    @Override public void clear() {
      standardClear();
    }

    @Override public boolean isEmpty() {
      return standardIsEmpty();
    }

    @Override public SortedMap<K, V> subMap(K fromKey, K toKey) {
      return standardSubMap(fromKey, toKey);
    }

    @Override
    public Entry<K, V> lowerEntry(K key) {
      return standardLowerEntry(key);
    }

    @Override
    public K lowerKey(K key) {
      return standardLowerKey(key);
    }

    @Override
    public Entry<K, V> floorEntry(K key) {
      return standardFloorEntry(key);
    }

    @Override
    public K floorKey(K key) {
      return standardFloorKey(key);
    }

    @Override
    public Entry<K, V> ceilingEntry(K key) {
      return standardCeilingEntry(key);
    }

    @Override
    public K ceilingKey(K key) {
      return standardCeilingKey(key);
    }

    @Override
    public Entry<K, V> higherEntry(K key) {
      return standardHigherEntry(key);
    }

    @Override
    public K higherKey(K key) {
      return standardHigherKey(key);
    }

    @Override
    public Entry<K, V> firstEntry() {
      return standardFirstEntry();
    }

    /*
     * We can't override lastEntry to delegate to standardLastEntry, as it would create an infinite
     * loop. Instead, we test standardLastEntry manually below.
     */

    @Override
    public Entry<K, V> pollFirstEntry() {
      return standardPollFirstEntry();
    }

    @Override
    public Entry<K, V> pollLastEntry() {
      return standardPollLastEntry();
    }

    @Override
    public NavigableMap<K, V> descendingMap() {
      return new StandardDescendingMap();
    }

    @Override
    public NavigableSet<K> navigableKeySet() {
      return new StandardNavigableKeySet();
    }

    @Override
    public NavigableSet<K> descendingKeySet() {
      return standardDescendingKeySet();
    }

    @Override
    public K firstKey() {
      return standardFirstKey();
    }

    @Override
    public SortedMap<K, V> headMap(K toKey) {
      return standardHeadMap(toKey);
    }

    @Override
    public K lastKey() {
      return standardLastKey();
    }

    @Override
    public SortedMap<K, V> tailMap(K fromKey) {
      return standardTailMap(fromKey);
    }
  }

  static class StandardLastEntryForwardingNavigableMap<K, V>
      extends ForwardingNavigableMap<K, V> {
    private final NavigableMap<K, V> backingMap;

    StandardLastEntryForwardingNavigableMap(NavigableMap<K, V> backingMap) {
      this.backingMap = backingMap;
    }

    @Override protected NavigableMap<K, V> delegate() {
      return backingMap;
    }

    @Override
    public Entry<K, V> lastEntry() {
      return standardLastEntry();
    }
  }

  public static Test suite() {
    TestSuite suite = new TestSuite();

    suite.addTestSuite(ForwardingNavigableMapTest.class);
    suite.addTest(NavigableMapTestSuiteBuilder.using(new TestStringSortedMapGenerator() {
      @Override protected SortedMap<String, String> create(
          Entry<String, String>[] entries) {
        NavigableMap<String, String> map = new SafeTreeMap<String, String>();
        for (Entry<String, String> entry : entries) {
          map.put(entry.getKey(), entry.getValue());
        }
        return new StandardImplForwardingNavigableMap<String, String>(map);
      }
    }).named("ForwardingNavigableMap[SafeTreeMap] with no comparator and standard "
        + "implementations").withFeatures(CollectionSize.ANY,
        CollectionFeature.KNOWN_ORDER, MapFeature.ALLOWS_NULL_VALUES,
        MapFeature.GENERAL_PURPOSE).createTestSuite());
    // TODO(user): add forwarding-to-ImmutableSortedMap test
    return suite;
  }

  @Override public void setUp() throws Exception {
    super.setUp();
    /*
     * Class parameters must be raw, so we can't create a proxy with generic
     * type arguments. The created proxy only records calls and returns null, so
     * the type is irrelevant at runtime.
     */
    @SuppressWarnings("unchecked")
    final NavigableMap<String, Boolean> sortedMap =
        createProxyInstance(NavigableMap.class);
    forward = new ForwardingNavigableMap<String, Boolean>() {
      @Override protected NavigableMap<String, Boolean> delegate() {
        return sortedMap;
      }
    };
  }

  public void testStandardLastEntry() {
    NavigableMap<String, Integer> forwarding =
        new StandardLastEntryForwardingNavigableMap<String, Integer>(
            new SafeTreeMap<String, Integer>());
    assertNull(forwarding.lastEntry());
    forwarding.put("b", 2);
    assertEquals(immutableEntry("b", 2), forwarding.lastEntry());
    forwarding.put("c", 3);
    assertEquals(immutableEntry("c", 3), forwarding.lastEntry());
    forwarding.put("a", 1);
    assertEquals(immutableEntry("c", 3), forwarding.lastEntry());
    forwarding.remove("c");
    assertEquals(immutableEntry("b", 2), forwarding.lastEntry());
  }

  public void testLowerEntry() {
    forward().lowerEntry("key");
    assertEquals("[lowerEntry(Object)]", getCalls());
  }

  public void testLowerKey() {
    forward().lowerKey("key");
    assertEquals("[lowerKey(Object)]", getCalls());
  }

  public void testFloorEntry() {
    forward().floorEntry("key");
    assertEquals("[floorEntry(Object)]", getCalls());
  }

  public void testFloorKey() {
    forward().floorKey("key");
    assertEquals("[floorKey(Object)]", getCalls());
  }

  public void testCeilingEntry() {
    forward().ceilingEntry("key");
    assertEquals("[ceilingEntry(Object)]", getCalls());
  }

  public void testCeilingKey() {
    forward().ceilingKey("key");
    assertEquals("[ceilingKey(Object)]", getCalls());
  }

  public void testHigherEntry() {
    forward().higherEntry("key");
    assertEquals("[higherEntry(Object)]", getCalls());
  }

  public void testHigherKey() {
    forward().higherKey("key");
    assertEquals("[higherKey(Object)]", getCalls());
  }

  public void testPollFirstEntry() {
    forward().pollFirstEntry();
    assertEquals("[pollFirstEntry]", getCalls());
  }

  public void testPollLastEntry() {
    forward().pollLastEntry();
    assertEquals("[pollLastEntry]", getCalls());
  }

  public void testFirstEntry() {
    forward().firstEntry();
    assertEquals("[firstEntry]", getCalls());
  }

  public void testLastEntry() {
    forward().lastEntry();
    assertEquals("[lastEntry]", getCalls());
  }

  public void testDescendingMap() {
    forward().descendingMap();
    assertEquals("[descendingMap]", getCalls());
  }

  public void testNavigableKeySet() {
    forward().navigableKeySet();
    assertEquals("[navigableKeySet]", getCalls());
  }

  public void testDescendingKeySet() {
    forward().descendingKeySet();
    assertEquals("[descendingKeySet]", getCalls());
  }

  public void testSubMap_K_Bool_K_Bool() {
    forward().subMap("a", false, "b", true);
    assertEquals("[subMap(Object,boolean,Object,boolean)]", getCalls());
  }

  public void testHeadMap_K_Bool() {
    forward().headMap("a", false);
    assertEquals("[headMap(Object,boolean)]", getCalls());
  }

  public void testTailMap_K_Bool() {
    forward().tailMap("a", false);
    assertEquals("[tailMap(Object,boolean)]", getCalls());
  }

  @Override NavigableMap<String, Boolean> forward() {
    return (NavigableMap<String, Boolean>) super.forward();
  }
}