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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
10 |
< |
import java.io.*; |
7 |
> |
import java.util.Arrays; |
8 |
> |
import java.util.BitSet; |
9 |
> |
import java.util.Collection; |
10 |
> |
import java.util.Iterator; |
11 |
> |
import java.util.Map; |
12 |
> |
import java.util.NavigableMap; |
13 |
> |
import java.util.NavigableSet; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.Random; |
16 |
> |
import java.util.Set; |
17 |
> |
import java.util.TreeMap; |
18 |
> |
|
19 |
> |
import junit.framework.Test; |
20 |
> |
import junit.framework.TestSuite; |
21 |
|
|
22 |
|
public class TreeMapTest extends JSR166TestCase { |
23 |
|
public static void main(String[] args) { |
24 |
< |
junit.textui.TestRunner.run(suite()); |
24 |
> |
main(suite(), args); |
25 |
|
} |
26 |
|
public static Test suite() { |
27 |
|
return new TestSuite(TreeMapTest.class); |
28 |
|
} |
29 |
|
|
30 |
|
/** |
31 |
< |
* Create a map from Integers 1-5 to Strings "A"-"E". |
31 |
> |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
32 |
|
*/ |
33 |
|
private static TreeMap map5() { |
34 |
|
TreeMap map = new TreeMap(); |
49 |
|
public void testClear() { |
50 |
|
TreeMap map = map5(); |
51 |
|
map.clear(); |
52 |
< |
assertEquals(map.size(), 0); |
52 |
> |
assertEquals(0, map.size()); |
53 |
|
} |
54 |
|
|
55 |
|
/** |
56 |
< |
* |
56 |
> |
* copy constructor creates map equal to source map |
57 |
|
*/ |
58 |
|
public void testConstructFromSorted() { |
59 |
|
TreeMap map = map5(); |
129 |
|
assertEquals(five, map.lastKey()); |
130 |
|
} |
131 |
|
|
122 |
– |
|
132 |
|
/** |
133 |
|
* keySet.toArray returns contains all keys |
134 |
|
*/ |
185 |
|
last = k; |
186 |
|
++count; |
187 |
|
} |
188 |
< |
assertEquals(count ,5); |
188 |
> |
assertEquals(5, count); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
204 |
|
last = k; |
205 |
|
++count; |
206 |
|
} |
207 |
< |
assertEquals(count ,5); |
207 |
> |
assertEquals(5, count); |
208 |
|
} |
209 |
|
|
210 |
|
/** |
223 |
|
last = k; |
224 |
|
++count; |
225 |
|
} |
226 |
< |
assertEquals(count, 5); |
226 |
> |
assertEquals(5, count); |
227 |
|
} |
228 |
|
|
229 |
|
/** |
242 |
|
last = k; |
243 |
|
++count; |
244 |
|
} |
245 |
< |
assertEquals(count, 5); |
245 |
> |
assertEquals(5, count); |
246 |
|
} |
247 |
|
|
248 |
|
/** |
422 |
|
assertNull(e4); |
423 |
|
} |
424 |
|
|
416 |
– |
|
425 |
|
/** |
426 |
|
* lowerKey returns preceding element |
427 |
|
*/ |
565 |
|
TreeMap map = map5(); |
566 |
|
String s = map.toString(); |
567 |
|
for (int i = 1; i <= 5; ++i) { |
568 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
568 |
> |
assertTrue(s.contains(String.valueOf(i))); |
569 |
|
} |
570 |
|
} |
571 |
|
|
575 |
|
* get(null) of nonempty map throws NPE |
576 |
|
*/ |
577 |
|
public void testGet_NullPointerException() { |
578 |
+ |
TreeMap c = map5(); |
579 |
|
try { |
571 |
– |
TreeMap c = map5(); |
580 |
|
c.get(null); |
581 |
|
shouldThrow(); |
582 |
|
} catch (NullPointerException success) {} |
586 |
|
* containsKey(null) of nonempty map throws NPE |
587 |
|
*/ |
588 |
|
public void testContainsKey_NullPointerException() { |
589 |
+ |
TreeMap c = map5(); |
590 |
|
try { |
582 |
– |
TreeMap c = map5(); |
591 |
|
c.containsKey(null); |
592 |
|
shouldThrow(); |
593 |
|
} catch (NullPointerException success) {} |
597 |
|
* remove(null) throws NPE for nonempty map |
598 |
|
*/ |
599 |
|
public void testRemove1_NullPointerException() { |
600 |
+ |
TreeMap c = new TreeMap(); |
601 |
+ |
c.put("sadsdf", "asdads"); |
602 |
|
try { |
593 |
– |
TreeMap c = new TreeMap(); |
594 |
– |
c.put("sadsdf", "asdads"); |
603 |
|
c.remove(null); |
604 |
|
shouldThrow(); |
605 |
|
} catch (NullPointerException success) {} |
606 |
|
} |
607 |
|
|
608 |
|
/** |
609 |
< |
* A deserialized map equals original |
609 |
> |
* A deserialized/reserialized map equals original |
610 |
|
*/ |
611 |
|
public void testSerialization() throws Exception { |
612 |
< |
TreeMap q = map5(); |
612 |
> |
NavigableMap x = map5(); |
613 |
> |
NavigableMap y = serialClone(x); |
614 |
|
|
615 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
616 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
617 |
< |
out.writeObject(q); |
618 |
< |
out.close(); |
619 |
< |
|
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)); |
615 |
> |
assertNotSame(x, y); |
616 |
> |
assertEquals(x.size(), y.size()); |
617 |
> |
assertEquals(x.toString(), y.toString()); |
618 |
> |
assertEquals(x, y); |
619 |
> |
assertEquals(y, x); |
620 |
|
} |
621 |
|
|
622 |
|
/** |
783 |
|
* Submaps of submaps subdivide correctly |
784 |
|
*/ |
785 |
|
public void testRecursiveSubMaps() throws Exception { |
786 |
< |
int mapSize = 1000; |
786 |
> |
int mapSize = expensiveTests ? 1000 : 100; |
787 |
|
Class cl = TreeMap.class; |
788 |
|
NavigableMap<Integer, Integer> map = newMap(cl); |
789 |
|
bs = new BitSet(mapSize); |
802 |
|
|
803 |
|
static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception { |
804 |
|
NavigableMap<Integer, Integer> result |
805 |
< |
= (NavigableMap<Integer, Integer>) cl.newInstance(); |
806 |
< |
assertEquals(result.size(), 0); |
805 |
> |
= (NavigableMap<Integer, Integer>) cl.getConstructor().newInstance(); |
806 |
> |
assertEquals(0, result.size()); |
807 |
|
assertFalse(result.keySet().iterator().hasNext()); |
808 |
|
return result; |
809 |
|
} |
835 |
|
// Add entries till we're back to original size |
836 |
|
while (map.size() < size) { |
837 |
|
int key = min + rnd.nextInt(rangeSize); |
838 |
< |
assertTrue(key >= min && key<= max); |
838 |
> |
assertTrue(key >= min && key <= max); |
839 |
|
put(map, key); |
840 |
|
} |
841 |
|
} |
860 |
|
// Add entries till we're back to original size |
861 |
|
while (map.size() < size) { |
862 |
|
int key = min - 5 + rnd.nextInt(rangeSize + 10); |
863 |
< |
if (key >= min && key<= max) { |
863 |
> |
if (key >= min && key <= max) { |
864 |
|
put(map, key); |
865 |
|
} else { |
866 |
|
try { |
964 |
|
*/ |
965 |
|
void check(NavigableMap<Integer, Integer> map, |
966 |
|
final int min, final int max, final boolean ascending) { |
967 |
< |
class ReferenceSet { |
967 |
> |
class ReferenceSet { |
968 |
|
int lower(int key) { |
969 |
|
return ascending ? lowerAscending(key) : higherAscending(key); |
970 |
|
} |
1030 |
|
if (bsContainsI) |
1031 |
|
size++; |
1032 |
|
} |
1033 |
< |
assertEquals(map.size(), size); |
1033 |
> |
assertEquals(size, map.size()); |
1034 |
|
|
1035 |
|
// Test contents using contains keySet iterator |
1036 |
|
int size2 = 0; |