Debian Codegraph @

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

raw
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.Helpers.assertEqualInOrder;
20 import static com.google.common.collect.testing.features.CollectionSize.ONE;
21 import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
22 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
23
24 import com.google.common.annotations.GwtCompatible;
25 import com.google.common.collect.testing.AbstractMapTester;
26 import com.google.common.collect.testing.Helpers;
27 import com.google.common.collect.testing.features.CollectionSize;
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map.Entry;
33 import java.util.NoSuchElementException;
34 import java.util.SortedMap;
35 import org.junit.Ignore;
36
37 /**
38 * A generic JUnit test which tests operations on a SortedMap. Can't be invoked directly; please see
39 * {@code SortedMapTestSuiteBuilder}.
40 *
41 * @author Jesse Wilson
42 * @author Louis Wasserman
43 */
44 @GwtCompatible
45 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
46 public class SortedMapNavigationTester<K, V> extends AbstractMapTester<K, V> {
47
48 private SortedMap<K, V> navigableMap;
49 private Entry<K, V> a;
50 private Entry<K, V> c;
51
52 @Override
53 public void setUp() throws Exception {
54 super.setUp();
55 navigableMap = (SortedMap<K, V>) getMap();
56 List<Entry<K, V>> entries =
57 Helpers.copyToList(
58 getSubjectGenerator()
59 .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
60 Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator()));
61
62 // some tests assume SEVERAL == 3
63 if (entries.size() >= 1) {
64 a = entries.get(0);
65 if (entries.size() >= 3) {
66 c = entries.get(2);
67 }
68 }
69 }
70
71 @CollectionSize.Require(ZERO)
72 public void testEmptyMapFirst() {
73 try {
74 navigableMap.firstKey();
75 fail();
76 } catch (NoSuchElementException e) {
77 }
78 }
79
80 @CollectionSize.Require(ZERO)
81 public void testEmptyMapLast() {
82 try {
83 assertNull(navigableMap.lastKey());
84 fail();
85 } catch (NoSuchElementException e) {
86 }
87 }
88
89 @CollectionSize.Require(ONE)
90 public void testSingletonMapFirst() {
91 assertEquals(a.getKey(), navigableMap.firstKey());
92 }
93
94 @CollectionSize.Require(ONE)
95 public void testSingletonMapLast() {
96 assertEquals(a.getKey(), navigableMap.lastKey());
97 }
98
99 @CollectionSize.Require(SEVERAL)
100 public void testFirst() {
101 assertEquals(a.getKey(), navigableMap.firstKey());
102 }
103
104 @CollectionSize.Require(SEVERAL)
105 public void testLast() {
106 assertEquals(c.getKey(), navigableMap.lastKey());
107 }
108
109 @CollectionSize.Require(absent = ZERO)
110 public void testHeadMapExclusive() {
111 assertFalse(navigableMap.headMap(a.getKey()).containsKey(a.getKey()));
112 }
113
114 @CollectionSize.Require(absent = ZERO)
115 public void testTailMapInclusive() {
116 assertTrue(navigableMap.tailMap(a.getKey()).containsKey(a.getKey()));
117 }
118
119 public void testHeadMap() {
120 List<Entry<K, V>> entries =
121 Helpers.copyToList(
122 getSubjectGenerator()
123 .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
124 Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator()));
125 for (int i = 0; i < entries.size(); i++) {
126 assertEqualInOrder(
127 entries.subList(0, i), navigableMap.headMap(entries.get(i).getKey()).entrySet());
128 }
129 }
130
131 public void testTailMap() {
132 List<Entry<K, V>> entries =
133 Helpers.copyToList(
134 getSubjectGenerator()
135 .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
136 Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator()));
137 for (int i = 0; i < entries.size(); i++) {
138 assertEqualInOrder(
139 entries.subList(i, entries.size()),
140 navigableMap.tailMap(entries.get(i).getKey()).entrySet());
141 }
142 }
143
144 public void testSubMap() {
145 List<Entry<K, V>> entries =
146 Helpers.copyToList(
147 getSubjectGenerator()
148 .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
149 Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator()));
150 for (int i = 0; i < entries.size(); i++) {
151 for (int j = i + 1; j < entries.size(); j++) {
152 assertEqualInOrder(
153 entries.subList(i, j),
154 navigableMap.subMap(entries.get(i).getKey(), entries.get(j).getKey()).entrySet());
155 }
156 }
157 }
158
159 @CollectionSize.Require(SEVERAL)
160 public void testSubMapIllegal() {
161 try {
162 navigableMap.subMap(c.getKey(), a.getKey());
163 fail("Expected IllegalArgumentException");
164 } catch (IllegalArgumentException expected) {
165 }
166 }
167
168 @CollectionSize.Require(absent = ZERO)
169 public void testOrderedByComparator() {
170 @SuppressWarnings("unchecked")
171 Comparator<? super K> comparator = navigableMap.comparator();
172 if (comparator == null) {
173 comparator =
174 new Comparator<K>() {
175 @SuppressWarnings("unchecked")
176 @Override
177 public int compare(K o1, K o2) {
178 return ((Comparable) o1).compareTo(o2);
179 }
180 };
181 }
182 Iterator<Entry<K, V>> entryItr = navigableMap.entrySet().iterator();
183 Entry<K, V> prevEntry = entryItr.next();
184 while (entryItr.hasNext()) {
185 Entry<K, V> nextEntry = entryItr.next();
186 assertTrue(comparator.compare(prevEntry.getKey(), nextEntry.getKey()) < 0);
187 prevEntry = nextEntry;
188 }
189 }
190 }