--- jsr166/src/test/tck/TreeMapTest.java 2009/11/21 17:38:06 1.11 +++ jsr166/src/test/tck/TreeMapTest.java 2017/08/23 05:33:00 1.34 @@ -1,24 +1,45 @@ /* * 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; 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); + class Implementation implements MapImplementation { + public Class klazz() { return TreeMap.class; } + public Map emptyMap() { return new TreeMap(); } + public Object makeKey(int i) { return i; } + public Object makeValue(int i) { return i; } + public boolean isConcurrent() { return false; } + public boolean permitsNullKeys() { return false; } + public boolean permitsNullValues() { return true; } + public boolean supportsSetValue() { return true; } + } + return newTestSuite( + TreeMapTest.class, + MapTest.testSuite(new Implementation())); } /** - * 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 +55,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 +73,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 +86,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 +95,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 +104,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 +115,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 +125,7 @@ public class TreeMapTest extends JSR166T } /** - * firstKey returns first key + * firstKey returns first key */ public void testFirstKey() { TreeMap map = map5(); @@ -112,16 +133,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 +154,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 +167,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 +181,7 @@ public class TreeMapTest extends JSR166T } /** - * keySet is ordered + * keySet is ordered */ public void testKeySetOrder() { TreeMap map = map5(); @@ -176,7 +196,7 @@ public class TreeMapTest extends JSR166T last = k; ++count; } - assertEquals(count ,5); + assertEquals(5, count); } /** @@ -195,11 +215,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 +234,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 +253,7 @@ public class TreeMapTest extends JSR166T last = k; ++count; } - assertEquals(count, 5); + assertEquals(5, count); } /** @@ -289,7 +309,7 @@ public class TreeMapTest extends JSR166T } /** - * entrySet.toArray contains all entries + * entrySet.toArray contains all entries */ public void testEntrySetToArray() { TreeMap map = map5(); @@ -303,7 +323,7 @@ public class TreeMapTest extends JSR166T } /** - * descendingEntrySet.toArray contains all entries + * descendingEntrySet.toArray contains all entries */ public void testDescendingEntrySetToArray() { TreeMap map = map5(); @@ -317,7 +337,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 +352,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(); @@ -357,7 +377,6 @@ public class TreeMapTest extends JSR166T Map.Entry e4 = map.lowerEntry(zero); assertNull(e4); - } /** @@ -376,7 +395,6 @@ public class TreeMapTest extends JSR166T Map.Entry e4 = map.higherEntry(six); assertNull(e4); - } /** @@ -395,7 +413,6 @@ public class TreeMapTest extends JSR166T Map.Entry e4 = map.floorEntry(zero); assertNull(e4); - } /** @@ -414,10 +431,8 @@ public class TreeMapTest extends JSR166T Map.Entry e4 = map.ceilingEntry(six); assertNull(e4); - } - /** * lowerKey returns preceding element */ @@ -434,7 +449,6 @@ public class TreeMapTest extends JSR166T Object e4 = q.lowerKey(zero); assertNull(e4); - } /** @@ -453,7 +467,6 @@ public class TreeMapTest extends JSR166T Object e4 = q.higherKey(six); assertNull(e4); - } /** @@ -472,7 +485,6 @@ public class TreeMapTest extends JSR166T Object e4 = q.floorKey(zero); assertNull(e4); - } /** @@ -491,7 +503,6 @@ public class TreeMapTest extends JSR166T Object e4 = q.ceilingKey(six); assertNull(e4); - } /** @@ -549,7 +560,7 @@ public class TreeMapTest extends JSR166T } /** - * size returns the correct values + * size returns the correct values */ public void testSize() { TreeMap map = map5(); @@ -565,7 +576,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))); } } @@ -575,8 +586,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) {} @@ -586,8 +597,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) {} @@ -597,31 +608,26 @@ 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) {} } /** - * A deserialized map equals original + * A deserialized/reserialized 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); } /** @@ -660,7 +666,7 @@ public class TreeMapTest extends JSR166T assertEquals(1, sm.size()); assertEquals(three, sm.firstKey()); assertEquals(three, sm.lastKey()); - assertTrue(sm.remove(three) != null); + assertEquals("C", sm.remove(three)); assertTrue(sm.isEmpty()); assertEquals(3, map.size()); } @@ -693,7 +699,7 @@ public class TreeMapTest extends JSR166T assertEquals(4, map.size()); assertEquals(0, sm.size()); assertTrue(sm.isEmpty()); - assertTrue(sm.remove(three) == null); + assertSame(sm.remove(three), null); assertEquals(4, map.size()); } @@ -775,7 +781,7 @@ public class TreeMapTest extends JSR166T NavigableMap ssm = sm.tailMap(four, true); assertEquals(four, ssm.firstKey()); assertEquals(five, ssm.lastKey()); - assertTrue(ssm.remove(four) != null); + assertEquals("D", ssm.remove(four)); assertEquals(1, ssm.size()); assertEquals(3, sm.size()); assertEquals(4, map.size()); @@ -788,7 +794,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); @@ -807,8 +813,8 @@ public class TreeMapTest extends JSR166T static NavigableMap newMap(Class cl) throws Exception { NavigableMap result - = (NavigableMap) cl.newInstance(); - assertEquals(result.size(), 0); + = (NavigableMap) cl.getConstructor().newInstance(); + assertEquals(0, result.size()); assertFalse(result.keySet().iterator().hasNext()); return result; } @@ -840,7 +846,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); } } @@ -865,7 +871,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 { @@ -969,7 +975,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); } @@ -1000,7 +1006,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; @@ -1035,7 +1041,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;