Debian Codegraph @

guava-testlib/src/com/google/common/collect/testing/SortedMapInterfaceTest.java

raw
1 /*
2 * Copyright (C) 2009 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;
18
19 import com.google.common.annotations.GwtCompatible;
20 import java.util.Iterator;
21 import java.util.Map.Entry;
22 import java.util.SortedMap;
23
24 /**
25 * Tests representing the contract of {@link SortedMap}. Concrete subclasses of this base class test
26 * conformance of concrete {@link SortedMap} subclasses to that contract.
27 *
28 * @author Jared Levy
29 */
30 // TODO: Use this class to test classes besides ImmutableSortedMap.
31 @GwtCompatible
32 public abstract class SortedMapInterfaceTest<K, V> extends MapInterfaceTest<K, V> {
33
34 protected SortedMapInterfaceTest(
35 boolean allowsNullKeys,
36 boolean allowsNullValues,
37 boolean supportsPut,
38 boolean supportsRemove,
39 boolean supportsClear) {
40 super(allowsNullKeys, allowsNullValues, supportsPut, supportsRemove, supportsClear);
41 }
42
43 @Override
44 protected abstract SortedMap<K, V> makeEmptyMap() throws UnsupportedOperationException;
45
46 @Override
47 protected abstract SortedMap<K, V> makePopulatedMap() throws UnsupportedOperationException;
48
49 @Override
50 protected SortedMap<K, V> makeEitherMap() {
51 try {
52 return makePopulatedMap();
53 } catch (UnsupportedOperationException e) {
54 return makeEmptyMap();
55 }
56 }
57
58 public void testTailMapWriteThrough() {
59 SortedMap<K, V> map;
60 try {
61 map = makePopulatedMap();
62 } catch (UnsupportedOperationException e) {
63 return;
64 }
65 if (map.size() < 2 || !supportsPut) {
66 return;
67 }
68 Iterator<Entry<K, V>> iterator = map.entrySet().iterator();
69 Entry<K, V> firstEntry = iterator.next();
70 Entry<K, V> secondEntry = iterator.next();
71 K key = secondEntry.getKey();
72 SortedMap<K, V> subMap = map.tailMap(key);
73 V value = getValueNotInPopulatedMap();
74 subMap.put(key, value);
75 assertEquals(secondEntry.getValue(), value);
76 assertEquals(map.get(key), value);
77 try {
78 subMap.put(firstEntry.getKey(), value);
79 fail("Expected IllegalArgumentException");
80 } catch (IllegalArgumentException expected) {
81 }
82 }
83
84 public void testTailMapRemoveThrough() {
85 SortedMap<K, V> map;
86 try {
87 map = makePopulatedMap();
88 } catch (UnsupportedOperationException e) {
89 return;
90 }
91 int oldSize = map.size();
92 if (map.size() < 2 || !supportsRemove) {
93 return;
94 }
95 Iterator<Entry<K, V>> iterator = map.entrySet().iterator();
96 Entry<K, V> firstEntry = iterator.next();
97 Entry<K, V> secondEntry = iterator.next();
98 K key = secondEntry.getKey();
99 SortedMap<K, V> subMap = map.tailMap(key);
100 subMap.remove(key);
101 assertNull(subMap.remove(firstEntry.getKey()));
102 assertEquals(map.size(), oldSize - 1);
103 assertFalse(map.containsKey(key));
104 assertEquals(subMap.size(), oldSize - 2);
105 }
106
107 public void testTailMapClearThrough() {
108 SortedMap<K, V> map;
109 try {
110 map = makePopulatedMap();
111 } catch (UnsupportedOperationException e) {
112 return;
113 }
114 int oldSize = map.size();
115 if (map.size() < 2 || !supportsClear) {
116 return;
117 }
118 Iterator<Entry<K, V>> iterator = map.entrySet().iterator();
119 iterator.next(); // advance
120 Entry<K, V> secondEntry = iterator.next();
121 K key = secondEntry.getKey();
122 SortedMap<K, V> subMap = map.tailMap(key);
123 int subMapSize = subMap.size();
124 subMap.clear();
125 assertEquals(map.size(), oldSize - subMapSize);
126 assertTrue(subMap.isEmpty());
127 }
128 }