Repository navigation
Expand file tree
/
Copy pathBTreeMapTest3.java
More file actions
273 lines (228 loc) · 9.37 KB
/
Copy pathBTreeMapTest3.java
File metadata and controls
273 lines (228 loc) · 9.37 KB
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
package org.mapdb;
import java.util.*;
import java.util.concurrent.ConcurrentNavigableMap;
/*
* This code comes from GoogleCollections, was modified for JDBM by Jan Kotek
*
* Tests representing the contract of {@link java.util.SortedMap}. Concrete subclasses of
* this base class test conformance of concrete {@link java.util.SortedMap} subclasses to
* that contract.
*
* @author Jared Levy
*
*/
public class BTreeMapTest3
extends ConcurrentMapInterfaceTest<Integer, String> {
public BTreeMapTest3() {
super(false, false, true, true, true, true, false);
}
@Override
protected Integer getKeyNotInPopulatedMap() throws UnsupportedOperationException {
return -100;
}
@Override
protected String getValueNotInPopulatedMap() throws UnsupportedOperationException {
return "XYZ";
}
@Override
protected String getSecondValueNotInPopulatedMap() throws UnsupportedOperationException {
return "ASD";
}
@Override
protected ConcurrentNavigableMap<Integer, String> makeEmptyMap() throws UnsupportedOperationException {
return DBMaker.memoryDB().transactionDisable().make().treeMap("test");
}
public static class Outside extends BTreeMapTest3{
@Override
protected ConcurrentNavigableMap<Integer, String> makeEmptyMap() throws UnsupportedOperationException {
return DBMaker.memoryDB().transactionDisable().make().treeMapCreate("test").valuesOutsideNodesEnable().make();
}
}
@Override
protected ConcurrentNavigableMap<Integer, String> makePopulatedMap() throws UnsupportedOperationException {
ConcurrentNavigableMap<Integer, String> map = makeEmptyMap();
for (int i = 0; i < 100; i++){
if(i%11==0||i%7==0) continue;
map.put(i, "aa" + i);
}
return map;
}
@Override
protected ConcurrentNavigableMap<Integer, String> makeEitherMap() {
try {
return makePopulatedMap();
} catch (UnsupportedOperationException e) {
return makeEmptyMap();
}
}
@SuppressWarnings({ "unchecked", "rawtypes" }) // Needed for null comparator
public void testOrdering() {
final SortedMap<Integer, String> map;
try {
map = makePopulatedMap();
} catch (UnsupportedOperationException e) {
return;
}
Iterator<Integer> iterator = map.keySet().iterator();
Integer prior = iterator.next();
Comparator<? super Integer> comparator = map.comparator();
while (iterator.hasNext()) {
Integer current = iterator.next();
if (comparator == null) {
Comparable comparable = (Comparable) prior;
assertTrue(comparable.compareTo(current) < 0);
} else {
assertTrue(map.comparator().compare(prior, current) < 0);
}
current = prior;
}
}
public void testFirstKeyNonEmpty() {
final SortedMap<Integer, String> map;
try {
map = makePopulatedMap();
} catch (UnsupportedOperationException e) {
return;
}
Integer expected = map.keySet().iterator().next();
assertEquals(expected, map.firstKey());
assertInvariants(map);
}
public void testLastKeyNonEmpty() {
final SortedMap<Integer, String> map;
try {
map = makePopulatedMap();
} catch (UnsupportedOperationException e) {
return;
}
Integer expected = null;
for (Integer key : map.keySet()) {
expected = key;
}
assertEquals(expected, map.lastKey());
assertInvariants(map);
}
private static <E> List<E> toList(Collection<E> collection) {
return new ArrayList<E>(collection);
}
private static <E> List<E> subListSnapshot(
List<E> list, int fromIndex, int toIndex) {
List<E> subList = new ArrayList<E>();
for (int i = fromIndex; i < toIndex; i++) {
subList.add(list.get(i));
}
return Collections.unmodifiableList(subList);
}
public void testHeadMap() {
final NavigableMap<Integer, String> map;
try {
map = makeEitherMap();
} catch (UnsupportedOperationException e) {
return;
}
List<Map.Entry<Integer, String>> list = toList(map.entrySet());
for (int i = 0; i < list.size(); i++) {
List<Map.Entry<Integer, String>> expected = subListSnapshot(list, 0, i);
SortedMap<Integer, String> headMap = map.headMap(list.get(i).getKey());
assertEquals(expected, toList(headMap.entrySet()));
}
for (int i = 0; i < list.size(); i++) {
List<Map.Entry<Integer, String>> expected = subListSnapshot(list, 0, i+1);
SortedMap<Integer, String> headMap = map.headMap(list.get(i).getKey(),true);
assertEquals(expected, toList(headMap.entrySet()));
}
for (int i = 0; i < list.size(); i++) {
List<Map.Entry<Integer, String>> expected = subListSnapshot(list, 0, i);
SortedMap<Integer, String> headMap = map.headMap(list.get(i).getKey(),false);
assertEquals(expected, toList(headMap.entrySet()));
}
}
public void testTailMap() {
final NavigableMap<Integer, String> map;
try {
map = makeEitherMap();
} catch (UnsupportedOperationException e) {
return;
}
List<Map.Entry<Integer, String>> list = toList(map.entrySet());
for (int i = 0; i < list.size(); i++) {
List<Map.Entry<Integer, String>> expected = subListSnapshot(list, i, list.size());
SortedMap<Integer, String> tailMap = map.tailMap(list.get(i).getKey());
assertEquals(expected, toList(tailMap.entrySet()));
}
for (int i = 0; i < list.size(); i++) {
List<Map.Entry<Integer, String>> expected = subListSnapshot(list, i, list.size());
SortedMap<Integer, String> tailMap = map.tailMap(list.get(i).getKey(),true);
assertEquals(expected, toList(tailMap.entrySet()));
}
for (int i = 0; i < list.size(); i++) {
List<Map.Entry<Integer, String>> expected = subListSnapshot(list, i+1, list.size());
SortedMap<Integer, String> tailMap = map.tailMap(list.get(i).getKey(),false);
assertEquals(expected, toList(tailMap.entrySet()));
}
}
public void testSubMap() {
final NavigableMap<Integer, String> map;
try {
map = makeEitherMap();
} catch (UnsupportedOperationException e) {
return;
}
List<Map.Entry<Integer, String>> list = toList(map.entrySet());
for (int i = 0; i < list.size(); i++) {
for (int j = i; j < list.size(); j++) {
List<Map.Entry<Integer, String>> expected = subListSnapshot(list, i, j);
SortedMap<Integer, String> subMap
= map.subMap(list.get(i).getKey(), list.get(j).getKey());
assertEquals(expected, toList(subMap.entrySet()));
assertEquals(expected.size(), subMap.size());
assertEquals(expected.size(), subMap.keySet().size());
assertEquals(expected.size(), subMap.entrySet().size());
assertEquals(expected.size(), subMap.values().size());
}
}
for (int i = 0; i < list.size(); i++) {
for (int j = i; j < list.size(); j++) {
List<Map.Entry<Integer, String>> expected = subListSnapshot(list, i, j+1);
SortedMap<Integer, String> subMap
= map.subMap(list.get(i).getKey(), true, list.get(j).getKey(), true);
assertEquals(expected, toList(subMap.entrySet()));
assertEquals(expected.size(), subMap.size());
assertEquals(expected.size(), subMap.keySet().size());
assertEquals(expected.size(), subMap.entrySet().size());
assertEquals(expected.size(), subMap.values().size());
}
}
for (int i = 0; i < list.size(); i++) {
for (int j = i; j < list.size(); j++) {
List<Map.Entry<Integer, String>> expected = subListSnapshot(list, i+1, j);
SortedMap<Integer, String> subMap
= map.subMap(list.get(i).getKey(), false, list.get(j).getKey(), false);
assertEquals(expected, toList(subMap.entrySet()));
assertEquals(expected.size(), subMap.size());
assertEquals(expected.size(), subMap.keySet().size());
assertEquals(expected.size(), subMap.entrySet().size());
assertEquals(expected.size(), subMap.values().size());
}
}
}
public void testSubMapIllegal() {
final SortedMap<Integer, String> map;
try {
map = makePopulatedMap();
} catch (UnsupportedOperationException e) {
return;
}
if (map.size() < 2) {
return;
}
Iterator<Integer> iterator = map.keySet().iterator();
Integer first = iterator.next();
Integer second = iterator.next();
try {
map.subMap(second, first);
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
}
}