11 |
|
|
12 |
|
public class TreeMapTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
< |
junit.textui.TestRunner.run (suite()); |
14 |
> |
junit.textui.TestRunner.run(suite()); |
15 |
|
} |
16 |
|
public static Test suite() { |
17 |
|
return new TestSuite(TreeMapTest.class); |
34 |
|
} |
35 |
|
|
36 |
|
/** |
37 |
< |
* clear removes all pairs |
37 |
> |
* clear removes all pairs |
38 |
|
*/ |
39 |
|
public void testClear() { |
40 |
|
TreeMap map = map5(); |
52 |
|
} |
53 |
|
|
54 |
|
/** |
55 |
< |
* Maps with same contents are equal |
55 |
> |
* Maps with same contents are equal |
56 |
|
*/ |
57 |
|
public void testEquals() { |
58 |
|
TreeMap map1 = map5(); |
65 |
|
} |
66 |
|
|
67 |
|
/** |
68 |
< |
* containsKey returns true for contained key |
68 |
> |
* containsKey returns true for contained key |
69 |
|
*/ |
70 |
|
public void testContainsKey() { |
71 |
|
TreeMap map = map5(); |
74 |
|
} |
75 |
|
|
76 |
|
/** |
77 |
< |
* containsValue returns true for held values |
77 |
> |
* containsValue returns true for held values |
78 |
|
*/ |
79 |
|
public void testContainsValue() { |
80 |
|
TreeMap map = map5(); |
83 |
|
} |
84 |
|
|
85 |
|
/** |
86 |
< |
* get returns the correct element at the given key, |
87 |
< |
* or null if not present |
86 |
> |
* get returns the correct element at the given key, |
87 |
> |
* or null if not present |
88 |
|
*/ |
89 |
|
public void testGet() { |
90 |
|
TreeMap map = map5(); |
94 |
|
} |
95 |
|
|
96 |
|
/** |
97 |
< |
* isEmpty is true of empty map and false for non-empty |
97 |
> |
* isEmpty is true of empty map and false for non-empty |
98 |
|
*/ |
99 |
|
public void testIsEmpty() { |
100 |
|
TreeMap empty = new TreeMap(); |
104 |
|
} |
105 |
|
|
106 |
|
/** |
107 |
< |
* firstKey returns first key |
107 |
> |
* firstKey returns first key |
108 |
|
*/ |
109 |
|
public void testFirstKey() { |
110 |
|
TreeMap map = map5(); |
112 |
|
} |
113 |
|
|
114 |
|
/** |
115 |
< |
* lastKey returns last key |
115 |
> |
* lastKey returns last key |
116 |
|
*/ |
117 |
|
public void testLastKey() { |
118 |
|
TreeMap map = map5(); |
121 |
|
|
122 |
|
|
123 |
|
/** |
124 |
< |
* keySet.toArray returns contains all keys |
124 |
> |
* keySet.toArray returns contains all keys |
125 |
|
*/ |
126 |
|
public void testKeySetToArray() { |
127 |
|
TreeMap map = map5(); |
134 |
|
} |
135 |
|
|
136 |
|
/** |
137 |
< |
* descendingkeySet.toArray returns contains all keys |
137 |
> |
* descendingkeySet.toArray returns contains all keys |
138 |
|
*/ |
139 |
|
public void testDescendingKeySetToArray() { |
140 |
|
TreeMap map = map5(); |
147 |
|
} |
148 |
|
|
149 |
|
/** |
150 |
< |
* keySet returns a Set containing all the keys |
150 |
> |
* keySet returns a Set containing all the keys |
151 |
|
*/ |
152 |
|
public void testKeySet() { |
153 |
|
TreeMap map = map5(); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
< |
* keySet is ordered |
164 |
> |
* keySet is ordered |
165 |
|
*/ |
166 |
|
public void testKeySetOrder() { |
167 |
|
TreeMap map = map5(); |
199 |
|
} |
200 |
|
|
201 |
|
/** |
202 |
< |
* descendingKeySet is ordered |
202 |
> |
* descendingKeySet is ordered |
203 |
|
*/ |
204 |
|
public void testDescendingKeySetOrder() { |
205 |
|
TreeMap map = map5(); |
218 |
|
} |
219 |
|
|
220 |
|
/** |
221 |
< |
* descending iterator of descendingKeySet is ordered |
221 |
> |
* descending iterator of descendingKeySet is ordered |
222 |
|
*/ |
223 |
|
public void testDescendingKeySetDescendingIteratorOrder() { |
224 |
|
TreeMap map = map5(); |
289 |
|
} |
290 |
|
|
291 |
|
/** |
292 |
< |
* entrySet.toArray contains all entries |
292 |
> |
* entrySet.toArray contains all entries |
293 |
|
*/ |
294 |
|
public void testEntrySetToArray() { |
295 |
|
TreeMap map = map5(); |
303 |
|
} |
304 |
|
|
305 |
|
/** |
306 |
< |
* descendingEntrySet.toArray contains all entries |
306 |
> |
* descendingEntrySet.toArray contains all entries |
307 |
|
*/ |
308 |
|
public void testDescendingEntrySetToArray() { |
309 |
|
TreeMap map = map5(); |
317 |
|
} |
318 |
|
|
319 |
|
/** |
320 |
< |
* putAll adds all key-value pairs from the given map |
320 |
> |
* putAll adds all key-value pairs from the given map |
321 |
|
*/ |
322 |
|
public void testPutAll() { |
323 |
|
TreeMap empty = new TreeMap(); |
332 |
|
} |
333 |
|
|
334 |
|
/** |
335 |
< |
* remove removes the correct key-value pair from the map |
335 |
> |
* remove removes the correct key-value pair from the map |
336 |
|
*/ |
337 |
|
public void testRemove() { |
338 |
|
TreeMap map = map5(); |
357 |
|
|
358 |
|
Map.Entry e4 = map.lowerEntry(zero); |
359 |
|
assertNull(e4); |
360 |
– |
|
360 |
|
} |
361 |
|
|
362 |
|
/** |
375 |
|
|
376 |
|
Map.Entry e4 = map.higherEntry(six); |
377 |
|
assertNull(e4); |
379 |
– |
|
378 |
|
} |
379 |
|
|
380 |
|
/** |
393 |
|
|
394 |
|
Map.Entry e4 = map.floorEntry(zero); |
395 |
|
assertNull(e4); |
398 |
– |
|
396 |
|
} |
397 |
|
|
398 |
|
/** |
411 |
|
|
412 |
|
Map.Entry e4 = map.ceilingEntry(six); |
413 |
|
assertNull(e4); |
417 |
– |
|
414 |
|
} |
415 |
|
|
416 |
|
|
430 |
|
|
431 |
|
Object e4 = q.lowerKey(zero); |
432 |
|
assertNull(e4); |
437 |
– |
|
433 |
|
} |
434 |
|
|
435 |
|
/** |
448 |
|
|
449 |
|
Object e4 = q.higherKey(six); |
450 |
|
assertNull(e4); |
456 |
– |
|
451 |
|
} |
452 |
|
|
453 |
|
/** |
466 |
|
|
467 |
|
Object e4 = q.floorKey(zero); |
468 |
|
assertNull(e4); |
475 |
– |
|
469 |
|
} |
470 |
|
|
471 |
|
/** |
484 |
|
|
485 |
|
Object e4 = q.ceilingKey(six); |
486 |
|
assertNull(e4); |
494 |
– |
|
487 |
|
} |
488 |
|
|
489 |
|
/** |
508 |
|
try { |
509 |
|
e.setValue("A"); |
510 |
|
shouldThrow(); |
511 |
< |
} catch (Exception ok) { |
520 |
< |
} |
511 |
> |
} catch (UnsupportedOperationException success) {} |
512 |
|
e = map.pollFirstEntry(); |
513 |
|
assertNull(e); |
514 |
|
} |
535 |
|
try { |
536 |
|
e.setValue("E"); |
537 |
|
shouldThrow(); |
538 |
< |
} catch (Exception ok) { |
548 |
< |
} |
538 |
> |
} catch (UnsupportedOperationException success) {} |
539 |
|
e = map.pollLastEntry(); |
540 |
|
assertNull(e); |
541 |
|
} |
542 |
|
|
543 |
|
/** |
544 |
< |
* size returns the correct values |
544 |
> |
* size returns the correct values |
545 |
|
*/ |
546 |
|
public void testSize() { |
547 |
|
TreeMap map = map5(); |
571 |
|
TreeMap c = map5(); |
572 |
|
c.get(null); |
573 |
|
shouldThrow(); |
574 |
< |
} catch (NullPointerException e) {} |
574 |
> |
} catch (NullPointerException success) {} |
575 |
|
} |
576 |
|
|
577 |
|
/** |
582 |
|
TreeMap c = map5(); |
583 |
|
c.containsKey(null); |
584 |
|
shouldThrow(); |
585 |
< |
} catch (NullPointerException e) {} |
585 |
> |
} catch (NullPointerException success) {} |
586 |
|
} |
587 |
|
|
588 |
|
/** |
594 |
|
c.put("sadsdf", "asdads"); |
595 |
|
c.remove(null); |
596 |
|
shouldThrow(); |
597 |
< |
} catch (NullPointerException e) {} |
597 |
> |
} catch (NullPointerException success) {} |
598 |
|
} |
599 |
|
|
600 |
|
/** |
601 |
|
* A deserialized map equals original |
602 |
|
*/ |
603 |
< |
public void testSerialization() { |
603 |
> |
public void testSerialization() throws Exception { |
604 |
|
TreeMap q = map5(); |
605 |
|
|
606 |
< |
try { |
607 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
608 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
609 |
< |
out.writeObject(q); |
610 |
< |
out.close(); |
611 |
< |
|
612 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
613 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
614 |
< |
TreeMap r = (TreeMap)in.readObject(); |
615 |
< |
assertEquals(q.size(), r.size()); |
616 |
< |
assertTrue(q.equals(r)); |
627 |
< |
assertTrue(r.equals(q)); |
628 |
< |
} catch (Exception e) { |
629 |
< |
e.printStackTrace(); |
630 |
< |
unexpectedException(); |
631 |
< |
} |
606 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
607 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
608 |
> |
out.writeObject(q); |
609 |
> |
out.close(); |
610 |
> |
|
611 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
612 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
613 |
> |
TreeMap r = (TreeMap)in.readObject(); |
614 |
> |
assertEquals(q.size(), r.size()); |
615 |
> |
assertTrue(q.equals(r)); |
616 |
> |
assertTrue(r.equals(q)); |
617 |
|
} |
618 |
|
|
619 |
|
/** |
652 |
|
assertEquals(1, sm.size()); |
653 |
|
assertEquals(three, sm.firstKey()); |
654 |
|
assertEquals(three, sm.lastKey()); |
655 |
< |
assertTrue(sm.remove(three) != null); |
655 |
> |
assertEquals("C", sm.remove(three)); |
656 |
|
assertTrue(sm.isEmpty()); |
657 |
|
assertEquals(3, map.size()); |
658 |
|
} |
685 |
|
assertEquals(4, map.size()); |
686 |
|
assertEquals(0, sm.size()); |
687 |
|
assertTrue(sm.isEmpty()); |
688 |
< |
assertTrue(sm.remove(three) == null); |
688 |
> |
assertSame(sm.remove(three), null); |
689 |
|
assertEquals(4, map.size()); |
690 |
|
} |
691 |
|
|
767 |
|
NavigableMap ssm = sm.tailMap(four, true); |
768 |
|
assertEquals(four, ssm.firstKey()); |
769 |
|
assertEquals(five, ssm.lastKey()); |
770 |
< |
assertTrue(ssm.remove(four) != null); |
770 |
> |
assertEquals("D", ssm.remove(four)); |
771 |
|
assertEquals(1, ssm.size()); |
772 |
|
assertEquals(3, sm.size()); |
773 |
|
assertEquals(4, map.size()); |
779 |
|
/** |
780 |
|
* Submaps of submaps subdivide correctly |
781 |
|
*/ |
782 |
< |
public void testRecursiveSubMaps() { |
783 |
< |
int mapSize = 1000; |
782 |
> |
public void testRecursiveSubMaps() throws Exception { |
783 |
> |
int mapSize = expensiveTests ? 1000 : 100; |
784 |
|
Class cl = TreeMap.class; |
785 |
|
NavigableMap<Integer, Integer> map = newMap(cl); |
786 |
|
bs = new BitSet(mapSize); |
797 |
|
0, mapSize - 1, true); |
798 |
|
} |
799 |
|
|
800 |
< |
static NavigableMap<Integer, Integer> newMap(Class cl) { |
801 |
< |
NavigableMap<Integer, Integer> result = null; |
802 |
< |
try { |
818 |
< |
result = (NavigableMap<Integer, Integer>) cl.newInstance(); |
819 |
< |
} catch (Exception e) { |
820 |
< |
fail(); |
821 |
< |
} |
800 |
> |
static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception { |
801 |
> |
NavigableMap<Integer, Integer> result |
802 |
> |
= (NavigableMap<Integer, Integer>) cl.newInstance(); |
803 |
|
assertEquals(result.size(), 0); |
804 |
|
assertFalse(result.keySet().iterator().hasNext()); |
805 |
|
return result; |
862 |
|
} else { |
863 |
|
try { |
864 |
|
map.put(key, 2 * key); |
865 |
< |
fail(); |
866 |
< |
} catch (IllegalArgumentException e) { |
886 |
< |
// expected |
887 |
< |
} |
865 |
> |
shouldThrow(); |
866 |
> |
} catch (IllegalArgumentException success) {} |
867 |
|
} |
868 |
|
} |
869 |
|
} |
992 |
|
// BitSet should support this! Test would run much faster |
993 |
|
while (key >= min) { |
994 |
|
if (bs.get(key)) |
995 |
< |
return(key); |
995 |
> |
return key; |
996 |
|
key--; |
997 |
|
} |
998 |
|
return -1; |
1058 |
|
assertEq(rs.last(), -1); |
1059 |
|
try { |
1060 |
|
map.firstKey(); |
1061 |
< |
fail(); |
1062 |
< |
} catch (NoSuchElementException e) { |
1084 |
< |
// expected |
1085 |
< |
} |
1061 |
> |
shouldThrow(); |
1062 |
> |
} catch (NoSuchElementException success) {} |
1063 |
|
try { |
1064 |
|
map.lastKey(); |
1065 |
< |
fail(); |
1066 |
< |
} catch (NoSuchElementException e) { |
1090 |
< |
// expected |
1091 |
< |
} |
1065 |
> |
shouldThrow(); |
1066 |
> |
} catch (NoSuchElementException success) {} |
1067 |
|
} |
1068 |
|
} |
1069 |
|
|