1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/licenses/publicdomain |
5 |
*/ |
6 |
|
7 |
import junit.framework.*; |
8 |
import java.util.*; |
9 |
import java.util.concurrent.*; |
10 |
import java.io.*; |
11 |
|
12 |
public class ConcurrentSkipListSubMapTest extends JSR166TestCase { |
13 |
public static void main(String[] args) { |
14 |
junit.textui.TestRunner.run (suite()); |
15 |
} |
16 |
public static Test suite() { |
17 |
return new TestSuite(ConcurrentSkipListSubMapTest.class); |
18 |
} |
19 |
|
20 |
/** |
21 |
* Create a map from Integers 1-5 to Strings "A"-"E". |
22 |
*/ |
23 |
private static ConcurrentNavigableMap map5() { |
24 |
ConcurrentSkipListMap map = new ConcurrentSkipListMap(); |
25 |
assertTrue(map.isEmpty()); |
26 |
map.put(zero, "Z"); |
27 |
map.put(one, "A"); |
28 |
map.put(five, "E"); |
29 |
map.put(three, "C"); |
30 |
map.put(two, "B"); |
31 |
map.put(four, "D"); |
32 |
map.put(seven, "F"); |
33 |
assertFalse(map.isEmpty()); |
34 |
assertEquals(7, map.size()); |
35 |
return map.navigableSubMap(one, true, seven, false); |
36 |
} |
37 |
|
38 |
private static ConcurrentNavigableMap map0() { |
39 |
ConcurrentSkipListMap map = new ConcurrentSkipListMap(); |
40 |
assertTrue(map.isEmpty()); |
41 |
return map.navigableTailMap(one, true); |
42 |
} |
43 |
|
44 |
/** |
45 |
* clear removes all pairs |
46 |
*/ |
47 |
public void testClear() { |
48 |
ConcurrentNavigableMap map = map5(); |
49 |
map.clear(); |
50 |
assertEquals(map.size(), 0); |
51 |
} |
52 |
|
53 |
|
54 |
/** |
55 |
* Maps with same contents are equal |
56 |
*/ |
57 |
public void testEquals() { |
58 |
ConcurrentNavigableMap map1 = map5(); |
59 |
ConcurrentNavigableMap map2 = map5(); |
60 |
assertEquals(map1, map2); |
61 |
assertEquals(map2, map1); |
62 |
map1.clear(); |
63 |
assertFalse(map1.equals(map2)); |
64 |
assertFalse(map2.equals(map1)); |
65 |
} |
66 |
|
67 |
/** |
68 |
* containsKey returns true for contained key |
69 |
*/ |
70 |
public void testContainsKey() { |
71 |
ConcurrentNavigableMap map = map5(); |
72 |
assertTrue(map.containsKey(one)); |
73 |
assertFalse(map.containsKey(zero)); |
74 |
} |
75 |
|
76 |
/** |
77 |
* containsValue returns true for held values |
78 |
*/ |
79 |
public void testContainsValue() { |
80 |
ConcurrentNavigableMap map = map5(); |
81 |
assertTrue(map.containsValue("A")); |
82 |
assertFalse(map.containsValue("Z")); |
83 |
} |
84 |
|
85 |
/** |
86 |
* get returns the correct element at the given key, |
87 |
* or null if not present |
88 |
*/ |
89 |
public void testGet() { |
90 |
ConcurrentNavigableMap map = map5(); |
91 |
assertEquals("A", (String)map.get(one)); |
92 |
ConcurrentNavigableMap empty = map0(); |
93 |
assertNull(empty.get(one)); |
94 |
} |
95 |
|
96 |
/** |
97 |
* isEmpty is true of empty map and false for non-empty |
98 |
*/ |
99 |
public void testIsEmpty() { |
100 |
ConcurrentNavigableMap empty = map0(); |
101 |
ConcurrentNavigableMap map = map5(); |
102 |
assertTrue(empty.isEmpty()); |
103 |
assertFalse(map.isEmpty()); |
104 |
} |
105 |
|
106 |
/** |
107 |
* firstKey returns first key |
108 |
*/ |
109 |
public void testFirstKey() { |
110 |
ConcurrentNavigableMap map = map5(); |
111 |
assertEquals(one, map.firstKey()); |
112 |
} |
113 |
|
114 |
/** |
115 |
* lastKey returns last key |
116 |
*/ |
117 |
public void testLastKey() { |
118 |
ConcurrentNavigableMap map = map5(); |
119 |
assertEquals(five, map.lastKey()); |
120 |
} |
121 |
|
122 |
|
123 |
/** |
124 |
* keySet returns a Set containing all the keys |
125 |
*/ |
126 |
public void testKeySet() { |
127 |
ConcurrentNavigableMap map = map5(); |
128 |
Set s = map.keySet(); |
129 |
assertEquals(5, s.size()); |
130 |
assertTrue(s.contains(one)); |
131 |
assertTrue(s.contains(two)); |
132 |
assertTrue(s.contains(three)); |
133 |
assertTrue(s.contains(four)); |
134 |
assertTrue(s.contains(five)); |
135 |
} |
136 |
|
137 |
/** |
138 |
* keySet is ordered |
139 |
*/ |
140 |
public void testKeySetOrder() { |
141 |
ConcurrentNavigableMap map = map5(); |
142 |
Set s = map.keySet(); |
143 |
Iterator i = s.iterator(); |
144 |
Integer last = (Integer)i.next(); |
145 |
assertEquals(last, one); |
146 |
while (i.hasNext()) { |
147 |
Integer k = (Integer)i.next(); |
148 |
assertTrue(last.compareTo(k) < 0); |
149 |
last = k; |
150 |
} |
151 |
} |
152 |
|
153 |
/** |
154 |
* values collection contains all values |
155 |
*/ |
156 |
public void testValues() { |
157 |
ConcurrentNavigableMap map = map5(); |
158 |
Collection s = map.values(); |
159 |
assertEquals(5, s.size()); |
160 |
assertTrue(s.contains("A")); |
161 |
assertTrue(s.contains("B")); |
162 |
assertTrue(s.contains("C")); |
163 |
assertTrue(s.contains("D")); |
164 |
assertTrue(s.contains("E")); |
165 |
} |
166 |
|
167 |
/** |
168 |
* keySet.toArray returns contains all keys |
169 |
*/ |
170 |
public void testKeySetToArray() { |
171 |
ConcurrentNavigableMap map = map5(); |
172 |
Set s = map.keySet(); |
173 |
Object[] ar = s.toArray(); |
174 |
assertTrue(s.containsAll(Arrays.asList(ar))); |
175 |
assertEquals(5, ar.length); |
176 |
ar[0] = m10; |
177 |
assertFalse(s.containsAll(Arrays.asList(ar))); |
178 |
} |
179 |
|
180 |
/** |
181 |
* descendingkeySet.toArray returns contains all keys |
182 |
*/ |
183 |
public void testDescendingKeySetToArray() { |
184 |
ConcurrentNavigableMap map = map5(); |
185 |
Set s = map.descendingKeySet(); |
186 |
Object[] ar = s.toArray(); |
187 |
assertEquals(5, ar.length); |
188 |
assertTrue(s.containsAll(Arrays.asList(ar))); |
189 |
ar[0] = m10; |
190 |
assertFalse(s.containsAll(Arrays.asList(ar))); |
191 |
} |
192 |
|
193 |
/** |
194 |
* Values.toArray contains all values |
195 |
*/ |
196 |
public void testValuesToArray() { |
197 |
ConcurrentNavigableMap map = map5(); |
198 |
Collection v = map.values(); |
199 |
Object[] ar = v.toArray(); |
200 |
ArrayList s = new ArrayList(Arrays.asList(ar)); |
201 |
assertEquals(5, ar.length); |
202 |
assertTrue(s.contains("A")); |
203 |
assertTrue(s.contains("B")); |
204 |
assertTrue(s.contains("C")); |
205 |
assertTrue(s.contains("D")); |
206 |
assertTrue(s.contains("E")); |
207 |
} |
208 |
|
209 |
|
210 |
/** |
211 |
* entrySet contains all pairs |
212 |
*/ |
213 |
public void testEntrySet() { |
214 |
ConcurrentNavigableMap map = map5(); |
215 |
Set s = map.entrySet(); |
216 |
assertEquals(5, s.size()); |
217 |
Iterator it = s.iterator(); |
218 |
while (it.hasNext()) { |
219 |
Map.Entry e = (Map.Entry) it.next(); |
220 |
assertTrue( |
221 |
(e.getKey().equals(one) && e.getValue().equals("A")) || |
222 |
(e.getKey().equals(two) && e.getValue().equals("B")) || |
223 |
(e.getKey().equals(three) && e.getValue().equals("C")) || |
224 |
(e.getKey().equals(four) && e.getValue().equals("D")) || |
225 |
(e.getKey().equals(five) && e.getValue().equals("E"))); |
226 |
} |
227 |
} |
228 |
|
229 |
/** |
230 |
* putAll adds all key-value pairs from the given map |
231 |
*/ |
232 |
public void testPutAll() { |
233 |
ConcurrentNavigableMap empty = map0(); |
234 |
ConcurrentNavigableMap map = map5(); |
235 |
empty.putAll(map); |
236 |
assertEquals(5, empty.size()); |
237 |
assertTrue(empty.containsKey(one)); |
238 |
assertTrue(empty.containsKey(two)); |
239 |
assertTrue(empty.containsKey(three)); |
240 |
assertTrue(empty.containsKey(four)); |
241 |
assertTrue(empty.containsKey(five)); |
242 |
} |
243 |
|
244 |
/** |
245 |
* putIfAbsent works when the given key is not present |
246 |
*/ |
247 |
public void testPutIfAbsent() { |
248 |
ConcurrentNavigableMap map = map5(); |
249 |
map.putIfAbsent(six, "Z"); |
250 |
assertTrue(map.containsKey(six)); |
251 |
} |
252 |
|
253 |
/** |
254 |
* putIfAbsent does not add the pair if the key is already present |
255 |
*/ |
256 |
public void testPutIfAbsent2() { |
257 |
ConcurrentNavigableMap map = map5(); |
258 |
assertEquals("A", map.putIfAbsent(one, "Z")); |
259 |
} |
260 |
|
261 |
/** |
262 |
* replace fails when the given key is not present |
263 |
*/ |
264 |
public void testReplace() { |
265 |
ConcurrentNavigableMap map = map5(); |
266 |
assertNull(map.replace(six, "Z")); |
267 |
assertFalse(map.containsKey(six)); |
268 |
} |
269 |
|
270 |
/** |
271 |
* replace succeeds if the key is already present |
272 |
*/ |
273 |
public void testReplace2() { |
274 |
ConcurrentNavigableMap map = map5(); |
275 |
assertNotNull(map.replace(one, "Z")); |
276 |
assertEquals("Z", map.get(one)); |
277 |
} |
278 |
|
279 |
|
280 |
/** |
281 |
* replace value fails when the given key not mapped to expected value |
282 |
*/ |
283 |
public void testReplaceValue() { |
284 |
ConcurrentNavigableMap map = map5(); |
285 |
assertEquals("A", map.get(one)); |
286 |
assertFalse(map.replace(one, "Z", "Z")); |
287 |
assertEquals("A", map.get(one)); |
288 |
} |
289 |
|
290 |
/** |
291 |
* replace value succeeds when the given key mapped to expected value |
292 |
*/ |
293 |
public void testReplaceValue2() { |
294 |
ConcurrentNavigableMap map = map5(); |
295 |
assertEquals("A", map.get(one)); |
296 |
assertTrue(map.replace(one, "A", "Z")); |
297 |
assertEquals("Z", map.get(one)); |
298 |
} |
299 |
|
300 |
|
301 |
/** |
302 |
* remove removes the correct key-value pair from the map |
303 |
*/ |
304 |
public void testRemove() { |
305 |
ConcurrentNavigableMap map = map5(); |
306 |
map.remove(five); |
307 |
assertEquals(4, map.size()); |
308 |
assertFalse(map.containsKey(five)); |
309 |
} |
310 |
|
311 |
/** |
312 |
* remove(key,value) removes only if pair present |
313 |
*/ |
314 |
public void testRemove2() { |
315 |
ConcurrentNavigableMap map = map5(); |
316 |
assertTrue(map.containsKey(five)); |
317 |
assertEquals("E", map.get(five)); |
318 |
map.remove(five, "E"); |
319 |
assertEquals(4, map.size()); |
320 |
assertFalse(map.containsKey(five)); |
321 |
map.remove(four, "A"); |
322 |
assertEquals(4, map.size()); |
323 |
assertTrue(map.containsKey(four)); |
324 |
|
325 |
} |
326 |
|
327 |
/** |
328 |
* lowerEntry returns preceding entry. |
329 |
*/ |
330 |
public void testLowerEntry() { |
331 |
ConcurrentNavigableMap map = map5(); |
332 |
Map.Entry e1 = map.lowerEntry(three); |
333 |
assertEquals(two, e1.getKey()); |
334 |
|
335 |
Map.Entry e2 = map.lowerEntry(six); |
336 |
assertEquals(five, e2.getKey()); |
337 |
|
338 |
Map.Entry e3 = map.lowerEntry(one); |
339 |
assertNull(e3); |
340 |
|
341 |
Map.Entry e4 = map.lowerEntry(zero); |
342 |
assertNull(e4); |
343 |
|
344 |
} |
345 |
|
346 |
/** |
347 |
* higherEntry returns next entry. |
348 |
*/ |
349 |
public void testHigherEntry() { |
350 |
ConcurrentNavigableMap map = map5(); |
351 |
Map.Entry e1 = map.higherEntry(three); |
352 |
assertEquals(four, e1.getKey()); |
353 |
|
354 |
Map.Entry e2 = map.higherEntry(zero); |
355 |
assertEquals(one, e2.getKey()); |
356 |
|
357 |
Map.Entry e3 = map.higherEntry(five); |
358 |
assertNull(e3); |
359 |
|
360 |
Map.Entry e4 = map.higherEntry(six); |
361 |
assertNull(e4); |
362 |
|
363 |
} |
364 |
|
365 |
/** |
366 |
* floorEntry returns preceding entry. |
367 |
*/ |
368 |
public void testFloorEntry() { |
369 |
ConcurrentNavigableMap map = map5(); |
370 |
Map.Entry e1 = map.floorEntry(three); |
371 |
assertEquals(three, e1.getKey()); |
372 |
|
373 |
Map.Entry e2 = map.floorEntry(six); |
374 |
assertEquals(five, e2.getKey()); |
375 |
|
376 |
Map.Entry e3 = map.floorEntry(one); |
377 |
assertEquals(one, e3.getKey()); |
378 |
|
379 |
Map.Entry e4 = map.floorEntry(zero); |
380 |
assertNull(e4); |
381 |
|
382 |
} |
383 |
|
384 |
/** |
385 |
* ceilingEntry returns next entry. |
386 |
*/ |
387 |
public void testCeilingEntry() { |
388 |
ConcurrentNavigableMap map = map5(); |
389 |
Map.Entry e1 = map.ceilingEntry(three); |
390 |
assertEquals(three, e1.getKey()); |
391 |
|
392 |
Map.Entry e2 = map.ceilingEntry(zero); |
393 |
assertEquals(one, e2.getKey()); |
394 |
|
395 |
Map.Entry e3 = map.ceilingEntry(five); |
396 |
assertEquals(five, e3.getKey()); |
397 |
|
398 |
Map.Entry e4 = map.ceilingEntry(six); |
399 |
assertNull(e4); |
400 |
|
401 |
} |
402 |
|
403 |
/** |
404 |
* pollFirstEntry returns entries in order |
405 |
*/ |
406 |
public void testPollFirstEntry() { |
407 |
ConcurrentNavigableMap map = map5(); |
408 |
Map.Entry e = map.pollFirstEntry(); |
409 |
assertEquals(one, e.getKey()); |
410 |
assertEquals("A", e.getValue()); |
411 |
e = map.pollFirstEntry(); |
412 |
assertEquals(two, e.getKey()); |
413 |
map.put(one, "A"); |
414 |
e = map.pollFirstEntry(); |
415 |
assertEquals(one, e.getKey()); |
416 |
assertEquals("A", e.getValue()); |
417 |
e = map.pollFirstEntry(); |
418 |
assertEquals(three, e.getKey()); |
419 |
map.remove(four); |
420 |
e = map.pollFirstEntry(); |
421 |
assertEquals(five, e.getKey()); |
422 |
try { |
423 |
e.setValue("A"); |
424 |
shouldThrow(); |
425 |
} catch (Exception ok) { |
426 |
} |
427 |
e = map.pollFirstEntry(); |
428 |
assertNull(e); |
429 |
} |
430 |
|
431 |
/** |
432 |
* pollLastEntry returns entries in order |
433 |
*/ |
434 |
public void testPollLastEntry() { |
435 |
ConcurrentNavigableMap map = map5(); |
436 |
Map.Entry e = map.pollLastEntry(); |
437 |
assertEquals(five, e.getKey()); |
438 |
assertEquals("E", e.getValue()); |
439 |
e = map.pollLastEntry(); |
440 |
assertEquals(four, e.getKey()); |
441 |
map.put(five, "E"); |
442 |
e = map.pollLastEntry(); |
443 |
assertEquals(five, e.getKey()); |
444 |
assertEquals("E", e.getValue()); |
445 |
e = map.pollLastEntry(); |
446 |
assertEquals(three, e.getKey()); |
447 |
map.remove(two); |
448 |
e = map.pollLastEntry(); |
449 |
assertEquals(one, e.getKey()); |
450 |
try { |
451 |
e.setValue("E"); |
452 |
shouldThrow(); |
453 |
} catch (Exception ok) { |
454 |
} |
455 |
e = map.pollLastEntry(); |
456 |
assertNull(e); |
457 |
} |
458 |
|
459 |
/** |
460 |
* size returns the correct values |
461 |
*/ |
462 |
public void testSize() { |
463 |
ConcurrentNavigableMap map = map5(); |
464 |
ConcurrentNavigableMap empty = map0(); |
465 |
assertEquals(0, empty.size()); |
466 |
assertEquals(5, map.size()); |
467 |
} |
468 |
|
469 |
/** |
470 |
* toString contains toString of elements |
471 |
*/ |
472 |
public void testToString() { |
473 |
ConcurrentNavigableMap map = map5(); |
474 |
String s = map.toString(); |
475 |
for (int i = 1; i <= 5; ++i) { |
476 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
477 |
} |
478 |
} |
479 |
|
480 |
// Exception tests |
481 |
|
482 |
/** |
483 |
* get(null) of nonempty map throws NPE |
484 |
*/ |
485 |
public void testGet_NullPointerException() { |
486 |
try { |
487 |
ConcurrentNavigableMap c = map5(); |
488 |
c.get(null); |
489 |
shouldThrow(); |
490 |
} catch(NullPointerException e){} |
491 |
} |
492 |
|
493 |
/** |
494 |
* containsKey(null) of nonempty map throws NPE |
495 |
*/ |
496 |
public void testContainsKey_NullPointerException() { |
497 |
try { |
498 |
ConcurrentNavigableMap c = map5(); |
499 |
c.containsKey(null); |
500 |
shouldThrow(); |
501 |
} catch(NullPointerException e){} |
502 |
} |
503 |
|
504 |
/** |
505 |
* containsValue(null) throws NPE |
506 |
*/ |
507 |
public void testContainsValue_NullPointerException() { |
508 |
try { |
509 |
ConcurrentNavigableMap c = map0(); |
510 |
c.containsValue(null); |
511 |
shouldThrow(); |
512 |
} catch(NullPointerException e){} |
513 |
} |
514 |
|
515 |
|
516 |
/** |
517 |
* put(null,x) throws NPE |
518 |
*/ |
519 |
public void testPut1_NullPointerException() { |
520 |
try { |
521 |
ConcurrentNavigableMap c = map5(); |
522 |
c.put(null, "whatever"); |
523 |
shouldThrow(); |
524 |
} catch(NullPointerException e){} |
525 |
} |
526 |
|
527 |
/** |
528 |
* putIfAbsent(null, x) throws NPE |
529 |
*/ |
530 |
public void testPutIfAbsent1_NullPointerException() { |
531 |
try { |
532 |
ConcurrentNavigableMap c = map5(); |
533 |
c.putIfAbsent(null, "whatever"); |
534 |
shouldThrow(); |
535 |
} catch(NullPointerException e){} |
536 |
} |
537 |
|
538 |
/** |
539 |
* replace(null, x) throws NPE |
540 |
*/ |
541 |
public void testReplace_NullPointerException() { |
542 |
try { |
543 |
ConcurrentNavigableMap c = map5(); |
544 |
c.replace(null, "whatever"); |
545 |
shouldThrow(); |
546 |
} catch(NullPointerException e){} |
547 |
} |
548 |
|
549 |
/** |
550 |
* replace(null, x, y) throws NPE |
551 |
*/ |
552 |
public void testReplaceValue_NullPointerException() { |
553 |
try { |
554 |
ConcurrentNavigableMap c = map5(); |
555 |
c.replace(null, one, "whatever"); |
556 |
shouldThrow(); |
557 |
} catch(NullPointerException e){} |
558 |
} |
559 |
|
560 |
/** |
561 |
* remove(null) throws NPE |
562 |
*/ |
563 |
public void testRemove1_NullPointerException() { |
564 |
try { |
565 |
ConcurrentNavigableMap c = map5(); |
566 |
c.remove(null); |
567 |
shouldThrow(); |
568 |
} catch(NullPointerException e){} |
569 |
} |
570 |
|
571 |
/** |
572 |
* remove(null, x) throws NPE |
573 |
*/ |
574 |
public void testRemove2_NullPointerException() { |
575 |
try { |
576 |
ConcurrentNavigableMap c = map5(); |
577 |
c.remove(null, "whatever"); |
578 |
shouldThrow(); |
579 |
} catch(NullPointerException e){} |
580 |
} |
581 |
|
582 |
/** |
583 |
* A deserialized map equals original |
584 |
*/ |
585 |
public void testSerialization() { |
586 |
ConcurrentNavigableMap q = map5(); |
587 |
|
588 |
try { |
589 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
590 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
591 |
out.writeObject(q); |
592 |
out.close(); |
593 |
|
594 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
595 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
596 |
ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject(); |
597 |
assertEquals(q.size(), r.size()); |
598 |
assertTrue(q.equals(r)); |
599 |
assertTrue(r.equals(q)); |
600 |
} catch(Exception e){ |
601 |
e.printStackTrace(); |
602 |
unexpectedException(); |
603 |
} |
604 |
} |
605 |
|
606 |
|
607 |
|
608 |
/** |
609 |
* subMap returns map with keys in requested range |
610 |
*/ |
611 |
public void testSubMapContents() { |
612 |
ConcurrentNavigableMap map = map5(); |
613 |
SortedMap sm = map.subMap(two, four); |
614 |
assertEquals(two, sm.firstKey()); |
615 |
assertEquals(three, sm.lastKey()); |
616 |
assertEquals(2, sm.size()); |
617 |
assertFalse(sm.containsKey(one)); |
618 |
assertTrue(sm.containsKey(two)); |
619 |
assertTrue(sm.containsKey(three)); |
620 |
assertFalse(sm.containsKey(four)); |
621 |
assertFalse(sm.containsKey(five)); |
622 |
Iterator i = sm.keySet().iterator(); |
623 |
Object k; |
624 |
k = (Integer)(i.next()); |
625 |
assertEquals(two, k); |
626 |
k = (Integer)(i.next()); |
627 |
assertEquals(three, k); |
628 |
assertFalse(i.hasNext()); |
629 |
Iterator j = sm.keySet().iterator(); |
630 |
j.next(); |
631 |
j.remove(); |
632 |
assertFalse(map.containsKey(two)); |
633 |
assertEquals(4, map.size()); |
634 |
assertEquals(1, sm.size()); |
635 |
assertEquals(three, sm.firstKey()); |
636 |
assertEquals(three, sm.lastKey()); |
637 |
assertTrue(sm.remove(three) != null); |
638 |
assertTrue(sm.isEmpty()); |
639 |
assertEquals(3, map.size()); |
640 |
} |
641 |
|
642 |
public void testSubMapContents2() { |
643 |
ConcurrentNavigableMap map = map5(); |
644 |
SortedMap sm = map.subMap(two, three); |
645 |
assertEquals(1, sm.size()); |
646 |
assertEquals(two, sm.firstKey()); |
647 |
assertEquals(two, sm.lastKey()); |
648 |
assertFalse(sm.containsKey(one)); |
649 |
assertTrue(sm.containsKey(two)); |
650 |
assertFalse(sm.containsKey(three)); |
651 |
assertFalse(sm.containsKey(four)); |
652 |
assertFalse(sm.containsKey(five)); |
653 |
Iterator i = sm.keySet().iterator(); |
654 |
Object k; |
655 |
k = (Integer)(i.next()); |
656 |
assertEquals(two, k); |
657 |
assertFalse(i.hasNext()); |
658 |
Iterator j = sm.keySet().iterator(); |
659 |
j.next(); |
660 |
j.remove(); |
661 |
assertFalse(map.containsKey(two)); |
662 |
assertEquals(4, map.size()); |
663 |
assertEquals(0, sm.size()); |
664 |
assertTrue(sm.isEmpty()); |
665 |
assertTrue(sm.remove(three) == null); |
666 |
assertEquals(4, map.size()); |
667 |
} |
668 |
|
669 |
/** |
670 |
* headMap returns map with keys in requested range |
671 |
*/ |
672 |
public void testHeadMapContents() { |
673 |
ConcurrentNavigableMap map = map5(); |
674 |
SortedMap sm = map.headMap(four); |
675 |
assertTrue(sm.containsKey(one)); |
676 |
assertTrue(sm.containsKey(two)); |
677 |
assertTrue(sm.containsKey(three)); |
678 |
assertFalse(sm.containsKey(four)); |
679 |
assertFalse(sm.containsKey(five)); |
680 |
Iterator i = sm.keySet().iterator(); |
681 |
Object k; |
682 |
k = (Integer)(i.next()); |
683 |
assertEquals(one, k); |
684 |
k = (Integer)(i.next()); |
685 |
assertEquals(two, k); |
686 |
k = (Integer)(i.next()); |
687 |
assertEquals(three, k); |
688 |
assertFalse(i.hasNext()); |
689 |
sm.clear(); |
690 |
assertTrue(sm.isEmpty()); |
691 |
assertEquals(2, map.size()); |
692 |
assertEquals(four, map.firstKey()); |
693 |
} |
694 |
|
695 |
/** |
696 |
* headMap returns map with keys in requested range |
697 |
*/ |
698 |
public void testTailMapContents() { |
699 |
ConcurrentNavigableMap map = map5(); |
700 |
SortedMap sm = map.tailMap(two); |
701 |
assertFalse(sm.containsKey(one)); |
702 |
assertTrue(sm.containsKey(two)); |
703 |
assertTrue(sm.containsKey(three)); |
704 |
assertTrue(sm.containsKey(four)); |
705 |
assertTrue(sm.containsKey(five)); |
706 |
Iterator i = sm.keySet().iterator(); |
707 |
Object k; |
708 |
k = (Integer)(i.next()); |
709 |
assertEquals(two, k); |
710 |
k = (Integer)(i.next()); |
711 |
assertEquals(three, k); |
712 |
k = (Integer)(i.next()); |
713 |
assertEquals(four, k); |
714 |
k = (Integer)(i.next()); |
715 |
assertEquals(five, k); |
716 |
assertFalse(i.hasNext()); |
717 |
|
718 |
Iterator ei = sm.entrySet().iterator(); |
719 |
Map.Entry e; |
720 |
e = (Map.Entry)(ei.next()); |
721 |
assertEquals(two, e.getKey()); |
722 |
assertEquals("B", e.getValue()); |
723 |
e = (Map.Entry)(ei.next()); |
724 |
assertEquals(three, e.getKey()); |
725 |
assertEquals("C", e.getValue()); |
726 |
e = (Map.Entry)(ei.next()); |
727 |
assertEquals(four, e.getKey()); |
728 |
assertEquals("D", e.getValue()); |
729 |
e = (Map.Entry)(ei.next()); |
730 |
assertEquals(five, e.getKey()); |
731 |
assertEquals("E", e.getValue()); |
732 |
assertFalse(i.hasNext()); |
733 |
|
734 |
SortedMap ssm = sm.tailMap(four); |
735 |
assertEquals(four, ssm.firstKey()); |
736 |
assertEquals(five, ssm.lastKey()); |
737 |
assertTrue(ssm.remove(four) != null); |
738 |
assertEquals(1, ssm.size()); |
739 |
assertEquals(3, sm.size()); |
740 |
assertEquals(4, map.size()); |
741 |
} |
742 |
|
743 |
} |