author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 46146 | jdk/test/java/util/concurrent/tck/ConcurrentSkipListMapTest.java@b3e220a04d3f |
child 47304 | 3f5f9bc0bdc2 |
permissions | -rw-r--r-- |
35394 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 |
*/ |
|
33 |
||
34 |
import java.util.ArrayList; |
|
35 |
import java.util.Arrays; |
|
36 |
import java.util.BitSet; |
|
37 |
import java.util.Collection; |
|
38 |
import java.util.Iterator; |
|
39 |
import java.util.Map; |
|
40 |
import java.util.NavigableMap; |
|
41 |
import java.util.NavigableSet; |
|
42 |
import java.util.NoSuchElementException; |
|
43 |
import java.util.Random; |
|
44 |
import java.util.Set; |
|
45 |
import java.util.concurrent.ConcurrentSkipListMap; |
|
46 |
||
47 |
import junit.framework.Test; |
|
48 |
import junit.framework.TestSuite; |
|
49 |
||
50 |
public class ConcurrentSkipListMapTest extends JSR166TestCase { |
|
51 |
public static void main(String[] args) { |
|
52 |
main(suite(), args); |
|
53 |
} |
|
54 |
public static Test suite() { |
|
55 |
return new TestSuite(ConcurrentSkipListMapTest.class); |
|
56 |
} |
|
57 |
||
58 |
/** |
|
59 |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
|
60 |
*/ |
|
61 |
private static ConcurrentSkipListMap map5() { |
|
62 |
ConcurrentSkipListMap map = new ConcurrentSkipListMap(); |
|
63 |
assertTrue(map.isEmpty()); |
|
64 |
map.put(one, "A"); |
|
65 |
map.put(five, "E"); |
|
66 |
map.put(three, "C"); |
|
67 |
map.put(two, "B"); |
|
68 |
map.put(four, "D"); |
|
69 |
assertFalse(map.isEmpty()); |
|
70 |
assertEquals(5, map.size()); |
|
71 |
return map; |
|
72 |
} |
|
73 |
||
74 |
/** |
|
75 |
* clear removes all pairs |
|
76 |
*/ |
|
77 |
public void testClear() { |
|
78 |
ConcurrentSkipListMap map = map5(); |
|
79 |
map.clear(); |
|
80 |
assertEquals(0, map.size()); |
|
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* copy constructor creates map equal to source map |
|
85 |
*/ |
|
86 |
public void testConstructFromSorted() { |
|
87 |
ConcurrentSkipListMap map = map5(); |
|
88 |
ConcurrentSkipListMap map2 = new ConcurrentSkipListMap(map); |
|
89 |
assertEquals(map, map2); |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Maps with same contents are equal |
|
94 |
*/ |
|
95 |
public void testEquals() { |
|
96 |
ConcurrentSkipListMap map1 = map5(); |
|
97 |
ConcurrentSkipListMap map2 = map5(); |
|
98 |
assertEquals(map1, map2); |
|
99 |
assertEquals(map2, map1); |
|
100 |
map1.clear(); |
|
101 |
assertFalse(map1.equals(map2)); |
|
102 |
assertFalse(map2.equals(map1)); |
|
103 |
} |
|
104 |
||
105 |
/** |
|
106 |
* containsKey returns true for contained key |
|
107 |
*/ |
|
108 |
public void testContainsKey() { |
|
109 |
ConcurrentSkipListMap map = map5(); |
|
110 |
assertTrue(map.containsKey(one)); |
|
111 |
assertFalse(map.containsKey(zero)); |
|
112 |
} |
|
113 |
||
114 |
/** |
|
115 |
* containsValue returns true for held values |
|
116 |
*/ |
|
117 |
public void testContainsValue() { |
|
118 |
ConcurrentSkipListMap map = map5(); |
|
119 |
assertTrue(map.containsValue("A")); |
|
120 |
assertFalse(map.containsValue("Z")); |
|
121 |
} |
|
122 |
||
123 |
/** |
|
124 |
* get returns the correct element at the given key, |
|
125 |
* or null if not present |
|
126 |
*/ |
|
127 |
public void testGet() { |
|
128 |
ConcurrentSkipListMap map = map5(); |
|
129 |
assertEquals("A", (String)map.get(one)); |
|
130 |
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
|
131 |
assertNull(empty.get(one)); |
|
132 |
} |
|
133 |
||
134 |
/** |
|
135 |
* isEmpty is true of empty map and false for non-empty |
|
136 |
*/ |
|
137 |
public void testIsEmpty() { |
|
138 |
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
|
139 |
ConcurrentSkipListMap map = map5(); |
|
140 |
assertTrue(empty.isEmpty()); |
|
141 |
assertFalse(map.isEmpty()); |
|
142 |
} |
|
143 |
||
144 |
/** |
|
145 |
* firstKey returns first key |
|
146 |
*/ |
|
147 |
public void testFirstKey() { |
|
148 |
ConcurrentSkipListMap map = map5(); |
|
149 |
assertEquals(one, map.firstKey()); |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* lastKey returns last key |
|
154 |
*/ |
|
155 |
public void testLastKey() { |
|
156 |
ConcurrentSkipListMap map = map5(); |
|
157 |
assertEquals(five, map.lastKey()); |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* keySet.toArray returns contains all keys |
|
162 |
*/ |
|
163 |
public void testKeySetToArray() { |
|
164 |
ConcurrentSkipListMap map = map5(); |
|
165 |
Set s = map.keySet(); |
|
166 |
Object[] ar = s.toArray(); |
|
167 |
assertTrue(s.containsAll(Arrays.asList(ar))); |
|
168 |
assertEquals(5, ar.length); |
|
169 |
ar[0] = m10; |
|
170 |
assertFalse(s.containsAll(Arrays.asList(ar))); |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* descendingkeySet.toArray returns contains all keys |
|
175 |
*/ |
|
176 |
public void testDescendingKeySetToArray() { |
|
177 |
ConcurrentSkipListMap map = map5(); |
|
178 |
Set s = map.descendingKeySet(); |
|
179 |
Object[] ar = s.toArray(); |
|
180 |
assertEquals(5, ar.length); |
|
181 |
assertTrue(s.containsAll(Arrays.asList(ar))); |
|
182 |
ar[0] = m10; |
|
183 |
assertFalse(s.containsAll(Arrays.asList(ar))); |
|
184 |
} |
|
185 |
||
186 |
/** |
|
187 |
* keySet returns a Set containing all the keys |
|
188 |
*/ |
|
189 |
public void testKeySet() { |
|
190 |
ConcurrentSkipListMap map = map5(); |
|
191 |
Set s = map.keySet(); |
|
192 |
assertEquals(5, s.size()); |
|
193 |
assertTrue(s.contains(one)); |
|
194 |
assertTrue(s.contains(two)); |
|
195 |
assertTrue(s.contains(three)); |
|
196 |
assertTrue(s.contains(four)); |
|
197 |
assertTrue(s.contains(five)); |
|
198 |
} |
|
199 |
||
200 |
/** |
|
201 |
* keySet is ordered |
|
202 |
*/ |
|
203 |
public void testKeySetOrder() { |
|
204 |
ConcurrentSkipListMap map = map5(); |
|
205 |
Set s = map.keySet(); |
|
206 |
Iterator i = s.iterator(); |
|
207 |
Integer last = (Integer)i.next(); |
|
208 |
assertEquals(last, one); |
|
209 |
int count = 1; |
|
210 |
while (i.hasNext()) { |
|
211 |
Integer k = (Integer)i.next(); |
|
212 |
assertTrue(last.compareTo(k) < 0); |
|
213 |
last = k; |
|
214 |
++count; |
|
215 |
} |
|
216 |
assertEquals(5, count); |
|
217 |
} |
|
218 |
||
219 |
/** |
|
220 |
* descending iterator of key set is inverse ordered |
|
221 |
*/ |
|
222 |
public void testKeySetDescendingIteratorOrder() { |
|
223 |
ConcurrentSkipListMap map = map5(); |
|
224 |
NavigableSet s = map.navigableKeySet(); |
|
225 |
Iterator i = s.descendingIterator(); |
|
226 |
Integer last = (Integer)i.next(); |
|
227 |
assertEquals(last, five); |
|
228 |
int count = 1; |
|
229 |
while (i.hasNext()) { |
|
230 |
Integer k = (Integer)i.next(); |
|
231 |
assertTrue(last.compareTo(k) > 0); |
|
232 |
last = k; |
|
233 |
++count; |
|
234 |
} |
|
235 |
assertEquals(5, count); |
|
236 |
} |
|
237 |
||
238 |
/** |
|
239 |
* descendingKeySet is ordered |
|
240 |
*/ |
|
241 |
public void testDescendingKeySetOrder() { |
|
242 |
ConcurrentSkipListMap map = map5(); |
|
243 |
Set s = map.descendingKeySet(); |
|
244 |
Iterator i = s.iterator(); |
|
245 |
Integer last = (Integer)i.next(); |
|
246 |
assertEquals(last, five); |
|
247 |
int count = 1; |
|
248 |
while (i.hasNext()) { |
|
249 |
Integer k = (Integer)i.next(); |
|
250 |
assertTrue(last.compareTo(k) > 0); |
|
251 |
last = k; |
|
252 |
++count; |
|
253 |
} |
|
254 |
assertEquals(5, count); |
|
255 |
} |
|
256 |
||
257 |
/** |
|
258 |
* descending iterator of descendingKeySet is ordered |
|
259 |
*/ |
|
260 |
public void testDescendingKeySetDescendingIteratorOrder() { |
|
261 |
ConcurrentSkipListMap map = map5(); |
|
262 |
NavigableSet s = map.descendingKeySet(); |
|
263 |
Iterator i = s.descendingIterator(); |
|
264 |
Integer last = (Integer)i.next(); |
|
265 |
assertEquals(last, one); |
|
266 |
int count = 1; |
|
267 |
while (i.hasNext()) { |
|
268 |
Integer k = (Integer)i.next(); |
|
269 |
assertTrue(last.compareTo(k) < 0); |
|
270 |
last = k; |
|
271 |
++count; |
|
272 |
} |
|
273 |
assertEquals(5, count); |
|
274 |
} |
|
275 |
||
276 |
/** |
|
277 |
* Values.toArray contains all values |
|
278 |
*/ |
|
279 |
public void testValuesToArray() { |
|
280 |
ConcurrentSkipListMap map = map5(); |
|
281 |
Collection v = map.values(); |
|
282 |
Object[] ar = v.toArray(); |
|
283 |
ArrayList s = new ArrayList(Arrays.asList(ar)); |
|
284 |
assertEquals(5, ar.length); |
|
285 |
assertTrue(s.contains("A")); |
|
286 |
assertTrue(s.contains("B")); |
|
287 |
assertTrue(s.contains("C")); |
|
288 |
assertTrue(s.contains("D")); |
|
289 |
assertTrue(s.contains("E")); |
|
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* values collection contains all values |
|
294 |
*/ |
|
295 |
public void testValues() { |
|
296 |
ConcurrentSkipListMap map = map5(); |
|
297 |
Collection s = map.values(); |
|
298 |
assertEquals(5, s.size()); |
|
299 |
assertTrue(s.contains("A")); |
|
300 |
assertTrue(s.contains("B")); |
|
301 |
assertTrue(s.contains("C")); |
|
302 |
assertTrue(s.contains("D")); |
|
303 |
assertTrue(s.contains("E")); |
|
304 |
} |
|
305 |
||
306 |
/** |
|
307 |
* entrySet contains all pairs |
|
308 |
*/ |
|
309 |
public void testEntrySet() { |
|
310 |
ConcurrentSkipListMap map = map5(); |
|
311 |
Set s = map.entrySet(); |
|
312 |
assertEquals(5, s.size()); |
|
313 |
Iterator it = s.iterator(); |
|
314 |
while (it.hasNext()) { |
|
315 |
Map.Entry e = (Map.Entry) it.next(); |
|
316 |
assertTrue( |
|
317 |
(e.getKey().equals(one) && e.getValue().equals("A")) || |
|
318 |
(e.getKey().equals(two) && e.getValue().equals("B")) || |
|
319 |
(e.getKey().equals(three) && e.getValue().equals("C")) || |
|
320 |
(e.getKey().equals(four) && e.getValue().equals("D")) || |
|
321 |
(e.getKey().equals(five) && e.getValue().equals("E"))); |
|
322 |
} |
|
323 |
} |
|
324 |
||
325 |
/** |
|
326 |
* descendingEntrySet contains all pairs |
|
327 |
*/ |
|
328 |
public void testDescendingEntrySet() { |
|
329 |
ConcurrentSkipListMap map = map5(); |
|
330 |
Set s = map.descendingMap().entrySet(); |
|
331 |
assertEquals(5, s.size()); |
|
332 |
Iterator it = s.iterator(); |
|
333 |
while (it.hasNext()) { |
|
334 |
Map.Entry e = (Map.Entry) it.next(); |
|
335 |
assertTrue( |
|
336 |
(e.getKey().equals(one) && e.getValue().equals("A")) || |
|
337 |
(e.getKey().equals(two) && e.getValue().equals("B")) || |
|
338 |
(e.getKey().equals(three) && e.getValue().equals("C")) || |
|
339 |
(e.getKey().equals(four) && e.getValue().equals("D")) || |
|
340 |
(e.getKey().equals(five) && e.getValue().equals("E"))); |
|
341 |
} |
|
342 |
} |
|
343 |
||
344 |
/** |
|
345 |
* entrySet.toArray contains all entries |
|
346 |
*/ |
|
347 |
public void testEntrySetToArray() { |
|
348 |
ConcurrentSkipListMap map = map5(); |
|
349 |
Set s = map.entrySet(); |
|
350 |
Object[] ar = s.toArray(); |
|
351 |
assertEquals(5, ar.length); |
|
352 |
for (int i = 0; i < 5; ++i) { |
|
353 |
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); |
|
354 |
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); |
|
355 |
} |
|
356 |
} |
|
357 |
||
358 |
/** |
|
359 |
* descendingEntrySet.toArray contains all entries |
|
360 |
*/ |
|
361 |
public void testDescendingEntrySetToArray() { |
|
362 |
ConcurrentSkipListMap map = map5(); |
|
363 |
Set s = map.descendingMap().entrySet(); |
|
364 |
Object[] ar = s.toArray(); |
|
365 |
assertEquals(5, ar.length); |
|
366 |
for (int i = 0; i < 5; ++i) { |
|
367 |
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); |
|
368 |
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); |
|
369 |
} |
|
370 |
} |
|
371 |
||
372 |
/** |
|
373 |
* putAll adds all key-value pairs from the given map |
|
374 |
*/ |
|
375 |
public void testPutAll() { |
|
376 |
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
|
377 |
ConcurrentSkipListMap map = map5(); |
|
378 |
empty.putAll(map); |
|
379 |
assertEquals(5, empty.size()); |
|
380 |
assertTrue(empty.containsKey(one)); |
|
381 |
assertTrue(empty.containsKey(two)); |
|
382 |
assertTrue(empty.containsKey(three)); |
|
383 |
assertTrue(empty.containsKey(four)); |
|
384 |
assertTrue(empty.containsKey(five)); |
|
385 |
} |
|
386 |
||
387 |
/** |
|
388 |
* putIfAbsent works when the given key is not present |
|
389 |
*/ |
|
390 |
public void testPutIfAbsent() { |
|
391 |
ConcurrentSkipListMap map = map5(); |
|
392 |
map.putIfAbsent(six, "Z"); |
|
393 |
assertTrue(map.containsKey(six)); |
|
394 |
} |
|
395 |
||
396 |
/** |
|
397 |
* putIfAbsent does not add the pair if the key is already present |
|
398 |
*/ |
|
399 |
public void testPutIfAbsent2() { |
|
400 |
ConcurrentSkipListMap map = map5(); |
|
401 |
assertEquals("A", map.putIfAbsent(one, "Z")); |
|
402 |
} |
|
403 |
||
404 |
/** |
|
405 |
* replace fails when the given key is not present |
|
406 |
*/ |
|
407 |
public void testReplace() { |
|
408 |
ConcurrentSkipListMap map = map5(); |
|
409 |
assertNull(map.replace(six, "Z")); |
|
410 |
assertFalse(map.containsKey(six)); |
|
411 |
} |
|
412 |
||
413 |
/** |
|
414 |
* replace succeeds if the key is already present |
|
415 |
*/ |
|
416 |
public void testReplace2() { |
|
417 |
ConcurrentSkipListMap map = map5(); |
|
418 |
assertNotNull(map.replace(one, "Z")); |
|
419 |
assertEquals("Z", map.get(one)); |
|
420 |
} |
|
421 |
||
422 |
/** |
|
423 |
* replace value fails when the given key not mapped to expected value |
|
424 |
*/ |
|
425 |
public void testReplaceValue() { |
|
426 |
ConcurrentSkipListMap map = map5(); |
|
427 |
assertEquals("A", map.get(one)); |
|
428 |
assertFalse(map.replace(one, "Z", "Z")); |
|
429 |
assertEquals("A", map.get(one)); |
|
430 |
} |
|
431 |
||
432 |
/** |
|
433 |
* replace value succeeds when the given key mapped to expected value |
|
434 |
*/ |
|
435 |
public void testReplaceValue2() { |
|
436 |
ConcurrentSkipListMap map = map5(); |
|
437 |
assertEquals("A", map.get(one)); |
|
438 |
assertTrue(map.replace(one, "A", "Z")); |
|
439 |
assertEquals("Z", map.get(one)); |
|
440 |
} |
|
441 |
||
442 |
/** |
|
443 |
* remove removes the correct key-value pair from the map |
|
444 |
*/ |
|
445 |
public void testRemove() { |
|
446 |
ConcurrentSkipListMap map = map5(); |
|
447 |
map.remove(five); |
|
448 |
assertEquals(4, map.size()); |
|
449 |
assertFalse(map.containsKey(five)); |
|
450 |
} |
|
451 |
||
452 |
/** |
|
453 |
* remove(key,value) removes only if pair present |
|
454 |
*/ |
|
455 |
public void testRemove2() { |
|
456 |
ConcurrentSkipListMap map = map5(); |
|
457 |
assertTrue(map.containsKey(five)); |
|
458 |
assertEquals("E", map.get(five)); |
|
459 |
map.remove(five, "E"); |
|
460 |
assertEquals(4, map.size()); |
|
461 |
assertFalse(map.containsKey(five)); |
|
462 |
map.remove(four, "A"); |
|
463 |
assertEquals(4, map.size()); |
|
464 |
assertTrue(map.containsKey(four)); |
|
465 |
} |
|
466 |
||
467 |
/** |
|
468 |
* lowerEntry returns preceding entry. |
|
469 |
*/ |
|
470 |
public void testLowerEntry() { |
|
471 |
ConcurrentSkipListMap map = map5(); |
|
472 |
Map.Entry e1 = map.lowerEntry(three); |
|
473 |
assertEquals(two, e1.getKey()); |
|
474 |
||
475 |
Map.Entry e2 = map.lowerEntry(six); |
|
476 |
assertEquals(five, e2.getKey()); |
|
477 |
||
478 |
Map.Entry e3 = map.lowerEntry(one); |
|
479 |
assertNull(e3); |
|
480 |
||
481 |
Map.Entry e4 = map.lowerEntry(zero); |
|
482 |
assertNull(e4); |
|
483 |
} |
|
484 |
||
485 |
/** |
|
486 |
* higherEntry returns next entry. |
|
487 |
*/ |
|
488 |
public void testHigherEntry() { |
|
489 |
ConcurrentSkipListMap map = map5(); |
|
490 |
Map.Entry e1 = map.higherEntry(three); |
|
491 |
assertEquals(four, e1.getKey()); |
|
492 |
||
493 |
Map.Entry e2 = map.higherEntry(zero); |
|
494 |
assertEquals(one, e2.getKey()); |
|
495 |
||
496 |
Map.Entry e3 = map.higherEntry(five); |
|
497 |
assertNull(e3); |
|
498 |
||
499 |
Map.Entry e4 = map.higherEntry(six); |
|
500 |
assertNull(e4); |
|
501 |
} |
|
502 |
||
503 |
/** |
|
504 |
* floorEntry returns preceding entry. |
|
505 |
*/ |
|
506 |
public void testFloorEntry() { |
|
507 |
ConcurrentSkipListMap map = map5(); |
|
508 |
Map.Entry e1 = map.floorEntry(three); |
|
509 |
assertEquals(three, e1.getKey()); |
|
510 |
||
511 |
Map.Entry e2 = map.floorEntry(six); |
|
512 |
assertEquals(five, e2.getKey()); |
|
513 |
||
514 |
Map.Entry e3 = map.floorEntry(one); |
|
515 |
assertEquals(one, e3.getKey()); |
|
516 |
||
517 |
Map.Entry e4 = map.floorEntry(zero); |
|
518 |
assertNull(e4); |
|
519 |
} |
|
520 |
||
521 |
/** |
|
522 |
* ceilingEntry returns next entry. |
|
523 |
*/ |
|
524 |
public void testCeilingEntry() { |
|
525 |
ConcurrentSkipListMap map = map5(); |
|
526 |
Map.Entry e1 = map.ceilingEntry(three); |
|
527 |
assertEquals(three, e1.getKey()); |
|
528 |
||
529 |
Map.Entry e2 = map.ceilingEntry(zero); |
|
530 |
assertEquals(one, e2.getKey()); |
|
531 |
||
532 |
Map.Entry e3 = map.ceilingEntry(five); |
|
533 |
assertEquals(five, e3.getKey()); |
|
534 |
||
535 |
Map.Entry e4 = map.ceilingEntry(six); |
|
536 |
assertNull(e4); |
|
537 |
} |
|
538 |
||
539 |
/** |
|
540 |
* lowerEntry, higherEntry, ceilingEntry, and floorEntry return |
|
541 |
* immutable entries |
|
542 |
*/ |
|
543 |
public void testEntryImmutability() { |
|
544 |
ConcurrentSkipListMap map = map5(); |
|
545 |
Map.Entry e = map.lowerEntry(three); |
|
546 |
assertEquals(two, e.getKey()); |
|
547 |
try { |
|
548 |
e.setValue("X"); |
|
549 |
shouldThrow(); |
|
550 |
} catch (UnsupportedOperationException success) {} |
|
551 |
e = map.higherEntry(zero); |
|
552 |
assertEquals(one, e.getKey()); |
|
553 |
try { |
|
554 |
e.setValue("X"); |
|
555 |
shouldThrow(); |
|
556 |
} catch (UnsupportedOperationException success) {} |
|
557 |
e = map.floorEntry(one); |
|
558 |
assertEquals(one, e.getKey()); |
|
559 |
try { |
|
560 |
e.setValue("X"); |
|
561 |
shouldThrow(); |
|
562 |
} catch (UnsupportedOperationException success) {} |
|
563 |
e = map.ceilingEntry(five); |
|
564 |
assertEquals(five, e.getKey()); |
|
565 |
try { |
|
566 |
e.setValue("X"); |
|
567 |
shouldThrow(); |
|
568 |
} catch (UnsupportedOperationException success) {} |
|
569 |
} |
|
570 |
||
571 |
/** |
|
572 |
* lowerKey returns preceding element |
|
573 |
*/ |
|
574 |
public void testLowerKey() { |
|
575 |
ConcurrentSkipListMap q = map5(); |
|
576 |
Object e1 = q.lowerKey(three); |
|
577 |
assertEquals(two, e1); |
|
578 |
||
579 |
Object e2 = q.lowerKey(six); |
|
580 |
assertEquals(five, e2); |
|
581 |
||
582 |
Object e3 = q.lowerKey(one); |
|
583 |
assertNull(e3); |
|
584 |
||
585 |
Object e4 = q.lowerKey(zero); |
|
586 |
assertNull(e4); |
|
587 |
} |
|
588 |
||
589 |
/** |
|
590 |
* higherKey returns next element |
|
591 |
*/ |
|
592 |
public void testHigherKey() { |
|
593 |
ConcurrentSkipListMap q = map5(); |
|
594 |
Object e1 = q.higherKey(three); |
|
595 |
assertEquals(four, e1); |
|
596 |
||
597 |
Object e2 = q.higherKey(zero); |
|
598 |
assertEquals(one, e2); |
|
599 |
||
600 |
Object e3 = q.higherKey(five); |
|
601 |
assertNull(e3); |
|
602 |
||
603 |
Object e4 = q.higherKey(six); |
|
604 |
assertNull(e4); |
|
605 |
} |
|
606 |
||
607 |
/** |
|
608 |
* floorKey returns preceding element |
|
609 |
*/ |
|
610 |
public void testFloorKey() { |
|
611 |
ConcurrentSkipListMap q = map5(); |
|
612 |
Object e1 = q.floorKey(three); |
|
613 |
assertEquals(three, e1); |
|
614 |
||
615 |
Object e2 = q.floorKey(six); |
|
616 |
assertEquals(five, e2); |
|
617 |
||
618 |
Object e3 = q.floorKey(one); |
|
619 |
assertEquals(one, e3); |
|
620 |
||
621 |
Object e4 = q.floorKey(zero); |
|
622 |
assertNull(e4); |
|
623 |
} |
|
624 |
||
625 |
/** |
|
626 |
* ceilingKey returns next element |
|
627 |
*/ |
|
628 |
public void testCeilingKey() { |
|
629 |
ConcurrentSkipListMap q = map5(); |
|
630 |
Object e1 = q.ceilingKey(three); |
|
631 |
assertEquals(three, e1); |
|
632 |
||
633 |
Object e2 = q.ceilingKey(zero); |
|
634 |
assertEquals(one, e2); |
|
635 |
||
636 |
Object e3 = q.ceilingKey(five); |
|
637 |
assertEquals(five, e3); |
|
638 |
||
639 |
Object e4 = q.ceilingKey(six); |
|
640 |
assertNull(e4); |
|
641 |
} |
|
642 |
||
643 |
/** |
|
644 |
* pollFirstEntry returns entries in order |
|
645 |
*/ |
|
646 |
public void testPollFirstEntry() { |
|
647 |
ConcurrentSkipListMap map = map5(); |
|
648 |
Map.Entry e = map.pollFirstEntry(); |
|
649 |
assertEquals(one, e.getKey()); |
|
650 |
assertEquals("A", e.getValue()); |
|
651 |
e = map.pollFirstEntry(); |
|
652 |
assertEquals(two, e.getKey()); |
|
653 |
map.put(one, "A"); |
|
654 |
e = map.pollFirstEntry(); |
|
655 |
assertEquals(one, e.getKey()); |
|
656 |
assertEquals("A", e.getValue()); |
|
657 |
e = map.pollFirstEntry(); |
|
658 |
assertEquals(three, e.getKey()); |
|
659 |
map.remove(four); |
|
660 |
e = map.pollFirstEntry(); |
|
661 |
assertEquals(five, e.getKey()); |
|
662 |
try { |
|
663 |
e.setValue("A"); |
|
664 |
shouldThrow(); |
|
665 |
} catch (UnsupportedOperationException success) {} |
|
666 |
e = map.pollFirstEntry(); |
|
667 |
assertNull(e); |
|
668 |
} |
|
669 |
||
670 |
/** |
|
671 |
* pollLastEntry returns entries in order |
|
672 |
*/ |
|
673 |
public void testPollLastEntry() { |
|
674 |
ConcurrentSkipListMap map = map5(); |
|
675 |
Map.Entry e = map.pollLastEntry(); |
|
676 |
assertEquals(five, e.getKey()); |
|
677 |
assertEquals("E", e.getValue()); |
|
678 |
e = map.pollLastEntry(); |
|
679 |
assertEquals(four, e.getKey()); |
|
680 |
map.put(five, "E"); |
|
681 |
e = map.pollLastEntry(); |
|
682 |
assertEquals(five, e.getKey()); |
|
683 |
assertEquals("E", e.getValue()); |
|
684 |
e = map.pollLastEntry(); |
|
685 |
assertEquals(three, e.getKey()); |
|
686 |
map.remove(two); |
|
687 |
e = map.pollLastEntry(); |
|
688 |
assertEquals(one, e.getKey()); |
|
689 |
try { |
|
690 |
e.setValue("E"); |
|
691 |
shouldThrow(); |
|
692 |
} catch (UnsupportedOperationException success) {} |
|
693 |
e = map.pollLastEntry(); |
|
694 |
assertNull(e); |
|
695 |
} |
|
696 |
||
697 |
/** |
|
698 |
* size returns the correct values |
|
699 |
*/ |
|
700 |
public void testSize() { |
|
701 |
ConcurrentSkipListMap map = map5(); |
|
702 |
ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); |
|
703 |
assertEquals(0, empty.size()); |
|
704 |
assertEquals(5, map.size()); |
|
705 |
} |
|
706 |
||
707 |
/** |
|
708 |
* toString contains toString of elements |
|
709 |
*/ |
|
710 |
public void testToString() { |
|
711 |
ConcurrentSkipListMap map = map5(); |
|
712 |
String s = map.toString(); |
|
713 |
for (int i = 1; i <= 5; ++i) { |
|
714 |
assertTrue(s.contains(String.valueOf(i))); |
|
715 |
} |
|
716 |
} |
|
717 |
||
718 |
// Exception tests |
|
719 |
||
720 |
/** |
|
721 |
* get(null) of nonempty map throws NPE |
|
722 |
*/ |
|
723 |
public void testGet_NullPointerException() { |
|
724 |
ConcurrentSkipListMap c = map5(); |
|
725 |
try { |
|
726 |
c.get(null); |
|
727 |
shouldThrow(); |
|
728 |
} catch (NullPointerException success) {} |
|
729 |
} |
|
730 |
||
731 |
/** |
|
732 |
* containsKey(null) of nonempty map throws NPE |
|
733 |
*/ |
|
734 |
public void testContainsKey_NullPointerException() { |
|
735 |
ConcurrentSkipListMap c = map5(); |
|
736 |
try { |
|
737 |
c.containsKey(null); |
|
738 |
shouldThrow(); |
|
739 |
} catch (NullPointerException success) {} |
|
740 |
} |
|
741 |
||
742 |
/** |
|
743 |
* containsValue(null) throws NPE |
|
744 |
*/ |
|
745 |
public void testContainsValue_NullPointerException() { |
|
746 |
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
|
747 |
try { |
|
748 |
c.containsValue(null); |
|
749 |
shouldThrow(); |
|
750 |
} catch (NullPointerException success) {} |
|
751 |
} |
|
752 |
||
753 |
/** |
|
754 |
* put(null,x) throws NPE |
|
755 |
*/ |
|
756 |
public void testPut1_NullPointerException() { |
|
757 |
ConcurrentSkipListMap c = map5(); |
|
758 |
try { |
|
759 |
c.put(null, "whatever"); |
|
760 |
shouldThrow(); |
|
761 |
} catch (NullPointerException success) {} |
|
762 |
} |
|
763 |
||
764 |
/** |
|
765 |
* putIfAbsent(null, x) throws NPE |
|
766 |
*/ |
|
767 |
public void testPutIfAbsent1_NullPointerException() { |
|
768 |
ConcurrentSkipListMap c = map5(); |
|
769 |
try { |
|
770 |
c.putIfAbsent(null, "whatever"); |
|
771 |
shouldThrow(); |
|
772 |
} catch (NullPointerException success) {} |
|
773 |
} |
|
774 |
||
775 |
/** |
|
776 |
* replace(null, x) throws NPE |
|
777 |
*/ |
|
778 |
public void testReplace_NullPointerException() { |
|
779 |
ConcurrentSkipListMap c = map5(); |
|
780 |
try { |
|
781 |
c.replace(null, "whatever"); |
|
782 |
shouldThrow(); |
|
783 |
} catch (NullPointerException success) {} |
|
784 |
} |
|
785 |
||
786 |
/** |
|
787 |
* replace(null, x, y) throws NPE |
|
788 |
*/ |
|
789 |
public void testReplaceValue_NullPointerException() { |
|
790 |
ConcurrentSkipListMap c = map5(); |
|
791 |
try { |
|
792 |
c.replace(null, one, "whatever"); |
|
793 |
shouldThrow(); |
|
794 |
} catch (NullPointerException success) {} |
|
795 |
} |
|
796 |
||
797 |
/** |
|
798 |
* remove(null) throws NPE |
|
799 |
*/ |
|
800 |
public void testRemove1_NullPointerException() { |
|
801 |
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
|
802 |
c.put("sadsdf", "asdads"); |
|
803 |
try { |
|
804 |
c.remove(null); |
|
805 |
shouldThrow(); |
|
806 |
} catch (NullPointerException success) {} |
|
807 |
} |
|
808 |
||
809 |
/** |
|
810 |
* remove(null, x) throws NPE |
|
811 |
*/ |
|
812 |
public void testRemove2_NullPointerException() { |
|
813 |
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
|
814 |
c.put("sadsdf", "asdads"); |
|
815 |
try { |
|
816 |
c.remove(null, "whatever"); |
|
817 |
shouldThrow(); |
|
818 |
} catch (NullPointerException success) {} |
|
819 |
} |
|
820 |
||
821 |
/** |
|
822 |
* remove(x, null) returns false |
|
823 |
*/ |
|
824 |
public void testRemove3() { |
|
825 |
ConcurrentSkipListMap c = new ConcurrentSkipListMap(); |
|
826 |
c.put("sadsdf", "asdads"); |
|
827 |
assertFalse(c.remove("sadsdf", null)); |
|
828 |
} |
|
829 |
||
830 |
/** |
|
46146
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
831 |
* A cloned map equals original |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
832 |
*/ |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
833 |
public void testClone() { |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
834 |
ConcurrentSkipListMap x = map5(); |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
835 |
ConcurrentSkipListMap y = x.clone(); |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
836 |
|
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
837 |
assertNotSame(x, y); |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
838 |
assertEquals(x.size(), y.size()); |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
839 |
assertEquals(x.toString(), y.toString()); |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
840 |
assertEquals(x, y); |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
841 |
assertEquals(y, x); |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
842 |
y.clear(); |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
843 |
assertTrue(y.isEmpty()); |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
844 |
assertFalse(x.equals(y)); |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
845 |
} |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
846 |
|
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
847 |
/** |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
848 |
* A deserialized/reserialized map equals original |
35394 | 849 |
*/ |
850 |
public void testSerialization() throws Exception { |
|
851 |
NavigableMap x = map5(); |
|
852 |
NavigableMap y = serialClone(x); |
|
853 |
||
854 |
assertNotSame(x, y); |
|
855 |
assertEquals(x.size(), y.size()); |
|
856 |
assertEquals(x.toString(), y.toString()); |
|
857 |
assertEquals(x, y); |
|
858 |
assertEquals(y, x); |
|
46146
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
859 |
y.clear(); |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
860 |
assertTrue(y.isEmpty()); |
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
41131
diff
changeset
|
861 |
assertFalse(x.equals(y)); |
35394 | 862 |
} |
863 |
||
864 |
/** |
|
865 |
* subMap returns map with keys in requested range |
|
866 |
*/ |
|
867 |
public void testSubMapContents() { |
|
868 |
ConcurrentSkipListMap map = map5(); |
|
869 |
NavigableMap sm = map.subMap(two, true, four, false); |
|
870 |
assertEquals(two, sm.firstKey()); |
|
871 |
assertEquals(three, sm.lastKey()); |
|
872 |
assertEquals(2, sm.size()); |
|
873 |
assertFalse(sm.containsKey(one)); |
|
874 |
assertTrue(sm.containsKey(two)); |
|
875 |
assertTrue(sm.containsKey(three)); |
|
876 |
assertFalse(sm.containsKey(four)); |
|
877 |
assertFalse(sm.containsKey(five)); |
|
878 |
Iterator i = sm.keySet().iterator(); |
|
879 |
Object k; |
|
880 |
k = (Integer)(i.next()); |
|
881 |
assertEquals(two, k); |
|
882 |
k = (Integer)(i.next()); |
|
883 |
assertEquals(three, k); |
|
884 |
assertFalse(i.hasNext()); |
|
885 |
Iterator r = sm.descendingKeySet().iterator(); |
|
886 |
k = (Integer)(r.next()); |
|
887 |
assertEquals(three, k); |
|
888 |
k = (Integer)(r.next()); |
|
889 |
assertEquals(two, k); |
|
890 |
assertFalse(r.hasNext()); |
|
891 |
||
892 |
Iterator j = sm.keySet().iterator(); |
|
893 |
j.next(); |
|
894 |
j.remove(); |
|
895 |
assertFalse(map.containsKey(two)); |
|
896 |
assertEquals(4, map.size()); |
|
897 |
assertEquals(1, sm.size()); |
|
898 |
assertEquals(three, sm.firstKey()); |
|
899 |
assertEquals(three, sm.lastKey()); |
|
900 |
assertEquals("C", sm.remove(three)); |
|
901 |
assertTrue(sm.isEmpty()); |
|
902 |
assertEquals(3, map.size()); |
|
903 |
} |
|
904 |
||
905 |
public void testSubMapContents2() { |
|
906 |
ConcurrentSkipListMap map = map5(); |
|
907 |
NavigableMap sm = map.subMap(two, true, three, false); |
|
908 |
assertEquals(1, sm.size()); |
|
909 |
assertEquals(two, sm.firstKey()); |
|
910 |
assertEquals(two, sm.lastKey()); |
|
911 |
assertFalse(sm.containsKey(one)); |
|
912 |
assertTrue(sm.containsKey(two)); |
|
913 |
assertFalse(sm.containsKey(three)); |
|
914 |
assertFalse(sm.containsKey(four)); |
|
915 |
assertFalse(sm.containsKey(five)); |
|
916 |
Iterator i = sm.keySet().iterator(); |
|
917 |
Object k; |
|
918 |
k = (Integer)(i.next()); |
|
919 |
assertEquals(two, k); |
|
920 |
assertFalse(i.hasNext()); |
|
921 |
Iterator r = sm.descendingKeySet().iterator(); |
|
922 |
k = (Integer)(r.next()); |
|
923 |
assertEquals(two, k); |
|
924 |
assertFalse(r.hasNext()); |
|
925 |
||
926 |
Iterator j = sm.keySet().iterator(); |
|
927 |
j.next(); |
|
928 |
j.remove(); |
|
929 |
assertFalse(map.containsKey(two)); |
|
930 |
assertEquals(4, map.size()); |
|
931 |
assertEquals(0, sm.size()); |
|
932 |
assertTrue(sm.isEmpty()); |
|
933 |
assertSame(sm.remove(three), null); |
|
934 |
assertEquals(4, map.size()); |
|
935 |
} |
|
936 |
||
937 |
/** |
|
938 |
* headMap returns map with keys in requested range |
|
939 |
*/ |
|
940 |
public void testHeadMapContents() { |
|
941 |
ConcurrentSkipListMap map = map5(); |
|
942 |
NavigableMap sm = map.headMap(four, false); |
|
943 |
assertTrue(sm.containsKey(one)); |
|
944 |
assertTrue(sm.containsKey(two)); |
|
945 |
assertTrue(sm.containsKey(three)); |
|
946 |
assertFalse(sm.containsKey(four)); |
|
947 |
assertFalse(sm.containsKey(five)); |
|
948 |
Iterator i = sm.keySet().iterator(); |
|
949 |
Object k; |
|
950 |
k = (Integer)(i.next()); |
|
951 |
assertEquals(one, k); |
|
952 |
k = (Integer)(i.next()); |
|
953 |
assertEquals(two, k); |
|
954 |
k = (Integer)(i.next()); |
|
955 |
assertEquals(three, k); |
|
956 |
assertFalse(i.hasNext()); |
|
957 |
sm.clear(); |
|
958 |
assertTrue(sm.isEmpty()); |
|
959 |
assertEquals(2, map.size()); |
|
960 |
assertEquals(four, map.firstKey()); |
|
961 |
} |
|
962 |
||
963 |
/** |
|
964 |
* tailMap returns map with keys in requested range |
|
965 |
*/ |
|
966 |
public void testTailMapContents() { |
|
967 |
ConcurrentSkipListMap map = map5(); |
|
968 |
NavigableMap sm = map.tailMap(two, true); |
|
969 |
assertFalse(sm.containsKey(one)); |
|
970 |
assertTrue(sm.containsKey(two)); |
|
971 |
assertTrue(sm.containsKey(three)); |
|
972 |
assertTrue(sm.containsKey(four)); |
|
973 |
assertTrue(sm.containsKey(five)); |
|
974 |
Iterator i = sm.keySet().iterator(); |
|
975 |
Object k; |
|
976 |
k = (Integer)(i.next()); |
|
977 |
assertEquals(two, k); |
|
978 |
k = (Integer)(i.next()); |
|
979 |
assertEquals(three, k); |
|
980 |
k = (Integer)(i.next()); |
|
981 |
assertEquals(four, k); |
|
982 |
k = (Integer)(i.next()); |
|
983 |
assertEquals(five, k); |
|
984 |
assertFalse(i.hasNext()); |
|
985 |
Iterator r = sm.descendingKeySet().iterator(); |
|
986 |
k = (Integer)(r.next()); |
|
987 |
assertEquals(five, k); |
|
988 |
k = (Integer)(r.next()); |
|
989 |
assertEquals(four, k); |
|
990 |
k = (Integer)(r.next()); |
|
991 |
assertEquals(three, k); |
|
992 |
k = (Integer)(r.next()); |
|
993 |
assertEquals(two, k); |
|
994 |
assertFalse(r.hasNext()); |
|
995 |
||
996 |
Iterator ei = sm.entrySet().iterator(); |
|
997 |
Map.Entry e; |
|
998 |
e = (Map.Entry)(ei.next()); |
|
999 |
assertEquals(two, e.getKey()); |
|
1000 |
assertEquals("B", e.getValue()); |
|
1001 |
e = (Map.Entry)(ei.next()); |
|
1002 |
assertEquals(three, e.getKey()); |
|
1003 |
assertEquals("C", e.getValue()); |
|
1004 |
e = (Map.Entry)(ei.next()); |
|
1005 |
assertEquals(four, e.getKey()); |
|
1006 |
assertEquals("D", e.getValue()); |
|
1007 |
e = (Map.Entry)(ei.next()); |
|
1008 |
assertEquals(five, e.getKey()); |
|
1009 |
assertEquals("E", e.getValue()); |
|
1010 |
assertFalse(i.hasNext()); |
|
1011 |
||
1012 |
NavigableMap ssm = sm.tailMap(four, true); |
|
1013 |
assertEquals(four, ssm.firstKey()); |
|
1014 |
assertEquals(five, ssm.lastKey()); |
|
1015 |
assertEquals("D", ssm.remove(four)); |
|
1016 |
assertEquals(1, ssm.size()); |
|
1017 |
assertEquals(3, sm.size()); |
|
1018 |
assertEquals(4, map.size()); |
|
1019 |
} |
|
1020 |
||
1021 |
Random rnd = new Random(666); |
|
1022 |
BitSet bs; |
|
1023 |
||
1024 |
/** |
|
1025 |
* Submaps of submaps subdivide correctly |
|
1026 |
*/ |
|
1027 |
public void testRecursiveSubMaps() throws Exception { |
|
1028 |
int mapSize = expensiveTests ? 1000 : 100; |
|
1029 |
Class cl = ConcurrentSkipListMap.class; |
|
1030 |
NavigableMap<Integer, Integer> map = newMap(cl); |
|
1031 |
bs = new BitSet(mapSize); |
|
1032 |
||
1033 |
populate(map, mapSize); |
|
1034 |
check(map, 0, mapSize - 1, true); |
|
1035 |
check(map.descendingMap(), 0, mapSize - 1, false); |
|
1036 |
||
1037 |
mutateMap(map, 0, mapSize - 1); |
|
1038 |
check(map, 0, mapSize - 1, true); |
|
1039 |
check(map.descendingMap(), 0, mapSize - 1, false); |
|
1040 |
||
1041 |
bashSubMap(map.subMap(0, true, mapSize, false), |
|
1042 |
0, mapSize - 1, true); |
|
1043 |
} |
|
1044 |
||
1045 |
static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception { |
|
1046 |
NavigableMap<Integer, Integer> result = |
|
41131
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
35394
diff
changeset
|
1047 |
(NavigableMap<Integer, Integer>) cl.getConstructor().newInstance(); |
35394 | 1048 |
assertEquals(0, result.size()); |
1049 |
assertFalse(result.keySet().iterator().hasNext()); |
|
1050 |
return result; |
|
1051 |
} |
|
1052 |
||
1053 |
void populate(NavigableMap<Integer, Integer> map, int limit) { |
|
1054 |
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
|
1055 |
int key = rnd.nextInt(limit); |
|
1056 |
put(map, key); |
|
1057 |
} |
|
1058 |
} |
|
1059 |
||
1060 |
void mutateMap(NavigableMap<Integer, Integer> map, int min, int max) { |
|
1061 |
int size = map.size(); |
|
1062 |
int rangeSize = max - min + 1; |
|
1063 |
||
1064 |
// Remove a bunch of entries directly |
|
1065 |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
|
1066 |
remove(map, min - 5 + rnd.nextInt(rangeSize + 10)); |
|
1067 |
} |
|
1068 |
||
1069 |
// Remove a bunch of entries with iterator |
|
1070 |
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
|
1071 |
if (rnd.nextBoolean()) { |
|
1072 |
bs.clear(it.next()); |
|
1073 |
it.remove(); |
|
1074 |
} |
|
1075 |
} |
|
1076 |
||
1077 |
// Add entries till we're back to original size |
|
1078 |
while (map.size() < size) { |
|
1079 |
int key = min + rnd.nextInt(rangeSize); |
|
1080 |
assertTrue(key >= min && key <= max); |
|
1081 |
put(map, key); |
|
1082 |
} |
|
1083 |
} |
|
1084 |
||
1085 |
void mutateSubMap(NavigableMap<Integer, Integer> map, int min, int max) { |
|
1086 |
int size = map.size(); |
|
1087 |
int rangeSize = max - min + 1; |
|
1088 |
||
1089 |
// Remove a bunch of entries directly |
|
1090 |
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
|
1091 |
remove(map, min - 5 + rnd.nextInt(rangeSize + 10)); |
|
1092 |
} |
|
1093 |
||
1094 |
// Remove a bunch of entries with iterator |
|
1095 |
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
|
1096 |
if (rnd.nextBoolean()) { |
|
1097 |
bs.clear(it.next()); |
|
1098 |
it.remove(); |
|
1099 |
} |
|
1100 |
} |
|
1101 |
||
1102 |
// Add entries till we're back to original size |
|
1103 |
while (map.size() < size) { |
|
1104 |
int key = min - 5 + rnd.nextInt(rangeSize + 10); |
|
1105 |
if (key >= min && key <= max) { |
|
1106 |
put(map, key); |
|
1107 |
} else { |
|
1108 |
try { |
|
1109 |
map.put(key, 2 * key); |
|
1110 |
shouldThrow(); |
|
1111 |
} catch (IllegalArgumentException success) {} |
|
1112 |
} |
|
1113 |
} |
|
1114 |
} |
|
1115 |
||
1116 |
void put(NavigableMap<Integer, Integer> map, int key) { |
|
1117 |
if (map.put(key, 2 * key) == null) |
|
1118 |
bs.set(key); |
|
1119 |
} |
|
1120 |
||
1121 |
void remove(NavigableMap<Integer, Integer> map, int key) { |
|
1122 |
if (map.remove(key) != null) |
|
1123 |
bs.clear(key); |
|
1124 |
} |
|
1125 |
||
1126 |
void bashSubMap(NavigableMap<Integer, Integer> map, |
|
1127 |
int min, int max, boolean ascending) { |
|
1128 |
check(map, min, max, ascending); |
|
1129 |
check(map.descendingMap(), min, max, !ascending); |
|
1130 |
||
1131 |
mutateSubMap(map, min, max); |
|
1132 |
check(map, min, max, ascending); |
|
1133 |
check(map.descendingMap(), min, max, !ascending); |
|
1134 |
||
1135 |
// Recurse |
|
1136 |
if (max - min < 2) |
|
1137 |
return; |
|
1138 |
int midPoint = (min + max) / 2; |
|
1139 |
||
1140 |
// headMap - pick direction and endpoint inclusion randomly |
|
1141 |
boolean incl = rnd.nextBoolean(); |
|
1142 |
NavigableMap<Integer,Integer> hm = map.headMap(midPoint, incl); |
|
1143 |
if (ascending) { |
|
1144 |
if (rnd.nextBoolean()) |
|
1145 |
bashSubMap(hm, min, midPoint - (incl ? 0 : 1), true); |
|
1146 |
else |
|
1147 |
bashSubMap(hm.descendingMap(), min, midPoint - (incl ? 0 : 1), |
|
1148 |
false); |
|
1149 |
} else { |
|
1150 |
if (rnd.nextBoolean()) |
|
1151 |
bashSubMap(hm, midPoint + (incl ? 0 : 1), max, false); |
|
1152 |
else |
|
1153 |
bashSubMap(hm.descendingMap(), midPoint + (incl ? 0 : 1), max, |
|
1154 |
true); |
|
1155 |
} |
|
1156 |
||
1157 |
// tailMap - pick direction and endpoint inclusion randomly |
|
1158 |
incl = rnd.nextBoolean(); |
|
1159 |
NavigableMap<Integer,Integer> tm = map.tailMap(midPoint,incl); |
|
1160 |
if (ascending) { |
|
1161 |
if (rnd.nextBoolean()) |
|
1162 |
bashSubMap(tm, midPoint + (incl ? 0 : 1), max, true); |
|
1163 |
else |
|
1164 |
bashSubMap(tm.descendingMap(), midPoint + (incl ? 0 : 1), max, |
|
1165 |
false); |
|
1166 |
} else { |
|
1167 |
if (rnd.nextBoolean()) { |
|
1168 |
bashSubMap(tm, min, midPoint - (incl ? 0 : 1), false); |
|
1169 |
} else { |
|
1170 |
bashSubMap(tm.descendingMap(), min, midPoint - (incl ? 0 : 1), |
|
1171 |
true); |
|
1172 |
} |
|
1173 |
} |
|
1174 |
||
1175 |
// subMap - pick direction and endpoint inclusion randomly |
|
1176 |
int rangeSize = max - min + 1; |
|
1177 |
int[] endpoints = new int[2]; |
|
1178 |
endpoints[0] = min + rnd.nextInt(rangeSize); |
|
1179 |
endpoints[1] = min + rnd.nextInt(rangeSize); |
|
1180 |
Arrays.sort(endpoints); |
|
1181 |
boolean lowIncl = rnd.nextBoolean(); |
|
1182 |
boolean highIncl = rnd.nextBoolean(); |
|
1183 |
if (ascending) { |
|
1184 |
NavigableMap<Integer,Integer> sm = map.subMap( |
|
1185 |
endpoints[0], lowIncl, endpoints[1], highIncl); |
|
1186 |
if (rnd.nextBoolean()) |
|
1187 |
bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1), |
|
1188 |
endpoints[1] - (highIncl ? 0 : 1), true); |
|
1189 |
else |
|
1190 |
bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1), |
|
1191 |
endpoints[1] - (highIncl ? 0 : 1), false); |
|
1192 |
} else { |
|
1193 |
NavigableMap<Integer,Integer> sm = map.subMap( |
|
1194 |
endpoints[1], highIncl, endpoints[0], lowIncl); |
|
1195 |
if (rnd.nextBoolean()) |
|
1196 |
bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1), |
|
1197 |
endpoints[1] - (highIncl ? 0 : 1), false); |
|
1198 |
else |
|
1199 |
bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1), |
|
1200 |
endpoints[1] - (highIncl ? 0 : 1), true); |
|
1201 |
} |
|
1202 |
} |
|
1203 |
||
1204 |
/** |
|
1205 |
* min and max are both inclusive. If max < min, interval is empty. |
|
1206 |
*/ |
|
1207 |
void check(NavigableMap<Integer, Integer> map, |
|
1208 |
final int min, final int max, final boolean ascending) { |
|
1209 |
class ReferenceSet { |
|
1210 |
int lower(int key) { |
|
1211 |
return ascending ? lowerAscending(key) : higherAscending(key); |
|
1212 |
} |
|
1213 |
int floor(int key) { |
|
1214 |
return ascending ? floorAscending(key) : ceilingAscending(key); |
|
1215 |
} |
|
1216 |
int ceiling(int key) { |
|
1217 |
return ascending ? ceilingAscending(key) : floorAscending(key); |
|
1218 |
} |
|
1219 |
int higher(int key) { |
|
1220 |
return ascending ? higherAscending(key) : lowerAscending(key); |
|
1221 |
} |
|
1222 |
int first() { |
|
1223 |
return ascending ? firstAscending() : lastAscending(); |
|
1224 |
} |
|
1225 |
int last() { |
|
1226 |
return ascending ? lastAscending() : firstAscending(); |
|
1227 |
} |
|
1228 |
int lowerAscending(int key) { |
|
1229 |
return floorAscending(key - 1); |
|
1230 |
} |
|
1231 |
int floorAscending(int key) { |
|
1232 |
if (key < min) |
|
1233 |
return -1; |
|
1234 |
else if (key > max) |
|
1235 |
key = max; |
|
1236 |
||
1237 |
// BitSet should support this! Test would run much faster |
|
1238 |
while (key >= min) { |
|
1239 |
if (bs.get(key)) |
|
1240 |
return key; |
|
1241 |
key--; |
|
1242 |
} |
|
1243 |
return -1; |
|
1244 |
} |
|
1245 |
int ceilingAscending(int key) { |
|
1246 |
if (key < min) |
|
1247 |
key = min; |
|
1248 |
else if (key > max) |
|
1249 |
return -1; |
|
1250 |
int result = bs.nextSetBit(key); |
|
1251 |
return result > max ? -1 : result; |
|
1252 |
} |
|
1253 |
int higherAscending(int key) { |
|
1254 |
return ceilingAscending(key + 1); |
|
1255 |
} |
|
1256 |
private int firstAscending() { |
|
1257 |
int result = ceilingAscending(min); |
|
1258 |
return result > max ? -1 : result; |
|
1259 |
} |
|
1260 |
private int lastAscending() { |
|
1261 |
int result = floorAscending(max); |
|
1262 |
return result < min ? -1 : result; |
|
1263 |
} |
|
1264 |
} |
|
1265 |
ReferenceSet rs = new ReferenceSet(); |
|
1266 |
||
1267 |
// Test contents using containsKey |
|
1268 |
int size = 0; |
|
1269 |
for (int i = min; i <= max; i++) { |
|
1270 |
boolean bsContainsI = bs.get(i); |
|
1271 |
assertEquals(bsContainsI, map.containsKey(i)); |
|
1272 |
if (bsContainsI) |
|
1273 |
size++; |
|
1274 |
} |
|
1275 |
assertEquals(size, map.size()); |
|
1276 |
||
1277 |
// Test contents using contains keySet iterator |
|
1278 |
int size2 = 0; |
|
1279 |
int previousKey = -1; |
|
1280 |
for (int key : map.keySet()) { |
|
1281 |
assertTrue(bs.get(key)); |
|
1282 |
size2++; |
|
1283 |
assertTrue(previousKey < 0 || |
|
1284 |
(ascending ? key - previousKey > 0 : key - previousKey < 0)); |
|
1285 |
previousKey = key; |
|
1286 |
} |
|
1287 |
assertEquals(size2, size); |
|
1288 |
||
1289 |
// Test navigation ops |
|
1290 |
for (int key = min - 1; key <= max + 1; key++) { |
|
1291 |
assertEq(map.lowerKey(key), rs.lower(key)); |
|
1292 |
assertEq(map.floorKey(key), rs.floor(key)); |
|
1293 |
assertEq(map.higherKey(key), rs.higher(key)); |
|
1294 |
assertEq(map.ceilingKey(key), rs.ceiling(key)); |
|
1295 |
} |
|
1296 |
||
1297 |
// Test extrema |
|
1298 |
if (map.size() != 0) { |
|
1299 |
assertEq(map.firstKey(), rs.first()); |
|
1300 |
assertEq(map.lastKey(), rs.last()); |
|
1301 |
} else { |
|
1302 |
assertEq(rs.first(), -1); |
|
1303 |
assertEq(rs.last(), -1); |
|
1304 |
try { |
|
1305 |
map.firstKey(); |
|
1306 |
shouldThrow(); |
|
1307 |
} catch (NoSuchElementException success) {} |
|
1308 |
try { |
|
1309 |
map.lastKey(); |
|
1310 |
shouldThrow(); |
|
1311 |
} catch (NoSuchElementException success) {} |
|
1312 |
} |
|
1313 |
} |
|
1314 |
||
1315 |
static void assertEq(Integer i, int j) { |
|
1316 |
if (i == null) |
|
1317 |
assertEquals(j, -1); |
|
1318 |
else |
|
1319 |
assertEquals((int) i, j); |
|
1320 |
} |
|
1321 |
||
1322 |
static boolean eq(Integer i, int j) { |
|
1323 |
return (i == null) ? j == -1 : i == j; |
|
1324 |
} |
|
1325 |
||
1326 |
} |