--- jsr166/src/test/tck/TreeMapTest.java 2010/08/25 00:07:03 1.14 +++ jsr166/src/test/tck/TreeMapTest.java 2015/04/25 04:55:31 1.31 @@ -1,24 +1,34 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ -import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; -import java.io.*; +import java.util.Arrays; +import java.util.BitSet; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.NavigableMap; +import java.util.NavigableSet; +import java.util.NoSuchElementException; +import java.util.Random; +import java.util.Set; +import java.util.TreeMap; + +import junit.framework.Test; +import junit.framework.TestSuite; public class TreeMapTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(TreeMapTest.class); } /** - * Create a map from Integers 1-5 to Strings "A"-"E". + * Returns a new map from Integers 1-5 to Strings "A"-"E". */ private static TreeMap map5() { TreeMap map = new TreeMap(); @@ -34,16 +44,16 @@ public class TreeMapTest extends JSR166T } /** - * clear removes all pairs + * clear removes all pairs */ public void testClear() { TreeMap map = map5(); map.clear(); - assertEquals(map.size(), 0); + assertEquals(0, map.size()); } /** - * + * copy constructor creates map equal to source map */ public void testConstructFromSorted() { TreeMap map = map5(); @@ -52,7 +62,7 @@ public class TreeMapTest extends JSR166T } /** - * Maps with same contents are equal + * Maps with same contents are equal */ public void testEquals() { TreeMap map1 = map5(); @@ -65,7 +75,7 @@ public class TreeMapTest extends JSR166T } /** - * containsKey returns true for contained key + * containsKey returns true for contained key */ public void testContainsKey() { TreeMap map = map5(); @@ -74,7 +84,7 @@ public class TreeMapTest extends JSR166T } /** - * containsValue returns true for held values + * containsValue returns true for held values */ public void testContainsValue() { TreeMap map = map5(); @@ -83,8 +93,8 @@ public class TreeMapTest extends JSR166T } /** - * get returns the correct element at the given key, - * or null if not present + * get returns the correct element at the given key, + * or null if not present */ public void testGet() { TreeMap map = map5(); @@ -94,7 +104,7 @@ public class TreeMapTest extends JSR166T } /** - * isEmpty is true of empty map and false for non-empty + * isEmpty is true of empty map and false for non-empty */ public void testIsEmpty() { TreeMap empty = new TreeMap(); @@ -104,7 +114,7 @@ public class TreeMapTest extends JSR166T } /** - * firstKey returns first key + * firstKey returns first key */ public void testFirstKey() { TreeMap map = map5(); @@ -112,16 +122,15 @@ public class TreeMapTest extends JSR166T } /** - * lastKey returns last key + * lastKey returns last key */ public void testLastKey() { TreeMap map = map5(); assertEquals(five, map.lastKey()); } - /** - * keySet.toArray returns contains all keys + * keySet.toArray returns contains all keys */ public void testKeySetToArray() { TreeMap map = map5(); @@ -134,7 +143,7 @@ public class TreeMapTest extends JSR166T } /** - * descendingkeySet.toArray returns contains all keys + * descendingkeySet.toArray returns contains all keys */ public void testDescendingKeySetToArray() { TreeMap map = map5(); @@ -147,7 +156,7 @@ public class TreeMapTest extends JSR166T } /** - * keySet returns a Set containing all the keys + * keySet returns a Set containing all the keys */ public void testKeySet() { TreeMap map = map5(); @@ -161,7 +170,7 @@ public class TreeMapTest extends JSR166T } /** - * keySet is ordered + * keySet is ordered */ public void testKeySetOrder() { TreeMap map = map5(); @@ -176,7 +185,7 @@ public class TreeMapTest extends JSR166T last = k; ++count; } - assertEquals(count ,5); + assertEquals(5, count); } /** @@ -195,11 +204,11 @@ public class TreeMapTest extends JSR166T last = k; ++count; } - assertEquals(count ,5); + assertEquals(5, count); } /** - * descendingKeySet is ordered + * descendingKeySet is ordered */ public void testDescendingKeySetOrder() { TreeMap map = map5(); @@ -214,11 +223,11 @@ public class TreeMapTest extends JSR166T last = k; ++count; } - assertEquals(count, 5); + assertEquals(5, count); } /** - * descending iterator of descendingKeySet is ordered + * descending iterator of descendingKeySet is ordered */ public void testDescendingKeySetDescendingIteratorOrder() { TreeMap map = map5(); @@ -233,7 +242,7 @@ public class TreeMapTest extends JSR166T last = k; ++count; } - assertEquals(count, 5); + assertEquals(5, count); } /** @@ -289,7 +298,7 @@ public class TreeMapTest extends JSR166T } /** - * entrySet.toArray contains all entries + * entrySet.toArray contains all entries */ public void testEntrySetToArray() { TreeMap map = map5(); @@ -303,7 +312,7 @@ public class TreeMapTest extends JSR166T } /** - * descendingEntrySet.toArray contains all entries + * descendingEntrySet.toArray contains all entries */ public void testDescendingEntrySetToArray() { TreeMap map = map5(); @@ -317,7 +326,7 @@ public class TreeMapTest extends JSR166T } /** - * putAll adds all key-value pairs from the given map + * putAll adds all key-value pairs from the given map */ public void testPutAll() { TreeMap empty = new TreeMap(); @@ -332,7 +341,7 @@ public class TreeMapTest extends JSR166T } /** - * remove removes the correct key-value pair from the map + * remove removes the correct key-value pair from the map */ public void testRemove() { TreeMap map = map5(); @@ -413,7 +422,6 @@ public class TreeMapTest extends JSR166T assertNull(e4); } - /** * lowerKey returns preceding element */ @@ -541,7 +549,7 @@ public class TreeMapTest extends JSR166T } /** - * size returns the correct values + * size returns the correct values */ public void testSize() { TreeMap map = map5(); @@ -557,7 +565,7 @@ public class TreeMapTest extends JSR166T TreeMap map = map5(); String s = map.toString(); for (int i = 1; i <= 5; ++i) { - assertTrue(s.indexOf(String.valueOf(i)) >= 0); + assertTrue(s.contains(String.valueOf(i))); } } @@ -567,8 +575,8 @@ public class TreeMapTest extends JSR166T * get(null) of nonempty map throws NPE */ public void testGet_NullPointerException() { + TreeMap c = map5(); try { - TreeMap c = map5(); c.get(null); shouldThrow(); } catch (NullPointerException success) {} @@ -578,8 +586,8 @@ public class TreeMapTest extends JSR166T * containsKey(null) of nonempty map throws NPE */ public void testContainsKey_NullPointerException() { + TreeMap c = map5(); try { - TreeMap c = map5(); c.containsKey(null); shouldThrow(); } catch (NullPointerException success) {} @@ -589,9 +597,9 @@ public class TreeMapTest extends JSR166T * remove(null) throws NPE for nonempty map */ public void testRemove1_NullPointerException() { + TreeMap c = new TreeMap(); + c.put("sadsdf", "asdads"); try { - TreeMap c = new TreeMap(); - c.put("sadsdf", "asdads"); c.remove(null); shouldThrow(); } catch (NullPointerException success) {} @@ -601,19 +609,14 @@ public class TreeMapTest extends JSR166T * A deserialized map equals original */ public void testSerialization() throws Exception { - TreeMap q = map5(); + NavigableMap x = map5(); + NavigableMap y = serialClone(x); - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(q); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - TreeMap r = (TreeMap)in.readObject(); - assertEquals(q.size(), r.size()); - assertTrue(q.equals(r)); - assertTrue(r.equals(q)); + assertNotSame(x, y); + assertEquals(x.size(), y.size()); + assertEquals(x.toString(), y.toString()); + assertEquals(x, y); + assertEquals(y, x); } /** @@ -780,7 +783,7 @@ public class TreeMapTest extends JSR166T * Submaps of submaps subdivide correctly */ public void testRecursiveSubMaps() throws Exception { - int mapSize = 1000; + int mapSize = expensiveTests ? 1000 : 100; Class cl = TreeMap.class; NavigableMap map = newMap(cl); bs = new BitSet(mapSize); @@ -800,7 +803,7 @@ public class TreeMapTest extends JSR166T static NavigableMap newMap(Class cl) throws Exception { NavigableMap result = (NavigableMap) cl.newInstance(); - assertEquals(result.size(), 0); + assertEquals(0, result.size()); assertFalse(result.keySet().iterator().hasNext()); return result; } @@ -832,7 +835,7 @@ public class TreeMapTest extends JSR166T // Add entries till we're back to original size while (map.size() < size) { int key = min + rnd.nextInt(rangeSize); - assertTrue(key >= min && key<= max); + assertTrue(key >= min && key <= max); put(map, key); } } @@ -857,7 +860,7 @@ public class TreeMapTest extends JSR166T // Add entries till we're back to original size while (map.size() < size) { int key = min - 5 + rnd.nextInt(rangeSize + 10); - if (key >= min && key<= max) { + if (key >= min && key <= max) { put(map, key); } else { try { @@ -961,7 +964,7 @@ public class TreeMapTest extends JSR166T */ void check(NavigableMap map, final int min, final int max, final boolean ascending) { - class ReferenceSet { + class ReferenceSet { int lower(int key) { return ascending ? lowerAscending(key) : higherAscending(key); } @@ -992,7 +995,7 @@ public class TreeMapTest extends JSR166T // BitSet should support this! Test would run much faster while (key >= min) { if (bs.get(key)) - return(key); + return key; key--; } return -1; @@ -1027,7 +1030,7 @@ public class TreeMapTest extends JSR166T if (bsContainsI) size++; } - assertEquals(map.size(), size); + assertEquals(size, map.size()); // Test contents using contains keySet iterator int size2 = 0;