--- jsr166/src/test/tck/TreeSubMapTest.java 2009/12/01 06:03:49 1.10 +++ jsr166/src/test/tck/TreeSubMapTest.java 2017/08/04 03:30:21 1.24 @@ -1,24 +1,32 @@ /* * 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.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.NavigableMap; +import java.util.Set; +import java.util.SortedMap; +import java.util.TreeMap; + +import junit.framework.Test; +import junit.framework.TestSuite; public class TreeSubMapTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(TreeSubMapTest.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 NavigableMap map5() { TreeMap map = new TreeMap(); @@ -42,7 +50,7 @@ public class TreeSubMapTest extends JSR1 } /** - * Create a map from Integers -5 to -1 to Strings "A"-"E". + * Returns a new map from Integers -5 to -1 to Strings "A"-"E". */ private static NavigableMap dmap5() { TreeMap map = new TreeMap(); @@ -64,17 +72,16 @@ public class TreeSubMapTest extends JSR1 } /** - * clear removes all pairs + * clear removes all pairs */ public void testClear() { NavigableMap map = map5(); map.clear(); - assertEquals(map.size(), 0); + assertEquals(0, map.size()); } - /** - * Maps with same contents are equal + * Maps with same contents are equal */ public void testEquals() { NavigableMap map1 = map5(); @@ -87,7 +94,7 @@ public class TreeSubMapTest extends JSR1 } /** - * containsKey returns true for contained key + * containsKey returns true for contained key */ public void testContainsKey() { NavigableMap map = map5(); @@ -96,7 +103,7 @@ public class TreeSubMapTest extends JSR1 } /** - * containsValue returns true for held values + * containsValue returns true for held values */ public void testContainsValue() { NavigableMap map = map5(); @@ -105,8 +112,8 @@ public class TreeSubMapTest extends JSR1 } /** - * 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() { NavigableMap map = map5(); @@ -116,7 +123,7 @@ public class TreeSubMapTest extends JSR1 } /** - * 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() { NavigableMap empty = map0(); @@ -126,7 +133,7 @@ public class TreeSubMapTest extends JSR1 } /** - * firstKey returns first key + * firstKey returns first key */ public void testFirstKey() { NavigableMap map = map5(); @@ -134,16 +141,15 @@ public class TreeSubMapTest extends JSR1 } /** - * lastKey returns last key + * lastKey returns last key */ public void testLastKey() { NavigableMap map = map5(); assertEquals(five, map.lastKey()); } - /** - * keySet returns a Set containing all the keys + * keySet returns a Set containing all the keys */ public void testKeySet() { NavigableMap map = map5(); @@ -157,7 +163,7 @@ public class TreeSubMapTest extends JSR1 } /** - * keySet is ordered + * keySet is ordered */ public void testKeySetOrder() { NavigableMap map = map5(); @@ -206,7 +212,7 @@ public class TreeSubMapTest extends JSR1 } /** - * putAll adds all key-value pairs from the given map + * putAll adds all key-value pairs from the given map */ public void testPutAll() { NavigableMap empty = map0(); @@ -221,7 +227,7 @@ public class TreeSubMapTest extends JSR1 } /** - * remove removes the correct key-value pair from the map + * remove removes the correct key-value pair from the map */ public void testRemove() { NavigableMap map = map5(); @@ -246,7 +252,6 @@ public class TreeSubMapTest extends JSR1 Map.Entry e4 = map.lowerEntry(zero); assertNull(e4); - } /** @@ -265,7 +270,6 @@ public class TreeSubMapTest extends JSR1 Map.Entry e4 = map.higherEntry(six); assertNull(e4); - } /** @@ -284,7 +288,6 @@ public class TreeSubMapTest extends JSR1 Map.Entry e4 = map.floorEntry(zero); assertNull(e4); - } /** @@ -303,7 +306,6 @@ public class TreeSubMapTest extends JSR1 Map.Entry e4 = map.ceilingEntry(six); assertNull(e4); - } /** @@ -364,7 +366,7 @@ public class TreeSubMapTest extends JSR1 } /** - * size returns the correct values + * size returns the correct values */ public void testSize() { NavigableMap map = map5(); @@ -380,7 +382,7 @@ public class TreeSubMapTest extends JSR1 NavigableMap 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))); } } @@ -390,8 +392,8 @@ public class TreeSubMapTest extends JSR1 * get(null) of nonempty map throws NPE */ public void testGet_NullPointerException() { + NavigableMap c = map5(); try { - NavigableMap c = map5(); c.get(null); shouldThrow(); } catch (NullPointerException success) {} @@ -401,8 +403,8 @@ public class TreeSubMapTest extends JSR1 * containsKey(null) of nonempty map throws NPE */ public void testContainsKey_NullPointerException() { + NavigableMap c = map5(); try { - NavigableMap c = map5(); c.containsKey(null); shouldThrow(); } catch (NullPointerException success) {} @@ -412,8 +414,8 @@ public class TreeSubMapTest extends JSR1 * put(null,x) throws NPE */ public void testPut1_NullPointerException() { + NavigableMap c = map5(); try { - NavigableMap c = map5(); c.put(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -423,35 +425,27 @@ public class TreeSubMapTest extends JSR1 * remove(null) throws NPE */ public void testRemove1_NullPointerException() { + NavigableMap c = map5(); try { - NavigableMap c = map5(); c.remove(null); shouldThrow(); } catch (NullPointerException success) {} } /** - * A deserialized map equals original + * A deserialized/reserialized map equals original */ public void testSerialization() throws Exception { - NavigableMap 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)); - NavigableMap r = (NavigableMap)in.readObject(); - assertFalse(r.isEmpty()); - 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); } - - /** * subMap returns map with keys in requested range */ @@ -588,17 +582,16 @@ public class TreeSubMapTest extends JSR1 } /** - * clear removes all pairs + * clear removes all pairs */ public void testDescendingClear() { NavigableMap map = dmap5(); map.clear(); - assertEquals(map.size(), 0); + assertEquals(0, map.size()); } - /** - * Maps with same contents are equal + * Maps with same contents are equal */ public void testDescendingEquals() { NavigableMap map1 = dmap5(); @@ -611,7 +604,7 @@ public class TreeSubMapTest extends JSR1 } /** - * containsKey returns true for contained key + * containsKey returns true for contained key */ public void testDescendingContainsKey() { NavigableMap map = dmap5(); @@ -620,7 +613,7 @@ public class TreeSubMapTest extends JSR1 } /** - * containsValue returns true for held values + * containsValue returns true for held values */ public void testDescendingContainsValue() { NavigableMap map = dmap5(); @@ -629,8 +622,8 @@ public class TreeSubMapTest extends JSR1 } /** - * 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 testDescendingGet() { NavigableMap map = dmap5(); @@ -640,7 +633,7 @@ public class TreeSubMapTest extends JSR1 } /** - * isEmpty is true of empty map and false for non-empty + * isEmpty is true of empty map and false for non-empty */ public void testDescendingIsEmpty() { NavigableMap empty = dmap0(); @@ -650,7 +643,7 @@ public class TreeSubMapTest extends JSR1 } /** - * firstKey returns first key + * firstKey returns first key */ public void testDescendingFirstKey() { NavigableMap map = dmap5(); @@ -658,16 +651,15 @@ public class TreeSubMapTest extends JSR1 } /** - * lastKey returns last key + * lastKey returns last key */ public void testDescendingLastKey() { NavigableMap map = dmap5(); assertEquals(m5, map.lastKey()); } - /** - * keySet returns a Set containing all the keys + * keySet returns a Set containing all the keys */ public void testDescendingKeySet() { NavigableMap map = dmap5(); @@ -681,7 +673,7 @@ public class TreeSubMapTest extends JSR1 } /** - * keySet is ordered + * keySet is ordered */ public void testDescendingKeySetOrder() { NavigableMap map = dmap5(); @@ -711,7 +703,7 @@ public class TreeSubMapTest extends JSR1 } /** - * keySet.toArray returns contains all keys + * keySet.toArray returns contains all keys */ public void testDescendingAscendingKeySetToArray() { NavigableMap map = dmap5(); @@ -724,7 +716,7 @@ public class TreeSubMapTest extends JSR1 } /** - * descendingkeySet.toArray returns contains all keys + * descendingkeySet.toArray returns contains all keys */ public void testDescendingDescendingKeySetToArray() { NavigableMap map = dmap5(); @@ -737,7 +729,7 @@ public class TreeSubMapTest extends JSR1 } /** - * Values.toArray contains all values + * Values.toArray contains all values */ public void testDescendingValuesToArray() { NavigableMap map = dmap5(); @@ -752,7 +744,6 @@ public class TreeSubMapTest extends JSR1 assertTrue(s.contains("E")); } - /** * entrySet contains all pairs */ @@ -773,7 +764,7 @@ public class TreeSubMapTest extends JSR1 } /** - * putAll adds all key-value pairs from the given map + * putAll adds all key-value pairs from the given map */ public void testDescendingPutAll() { NavigableMap empty = dmap0(); @@ -787,9 +778,8 @@ public class TreeSubMapTest extends JSR1 assertTrue(empty.containsKey(m5)); } - /** - * remove removes the correct key-value pair from the map + * remove removes the correct key-value pair from the map */ public void testDescendingRemove() { NavigableMap map = dmap5(); @@ -814,7 +804,6 @@ public class TreeSubMapTest extends JSR1 Map.Entry e4 = map.lowerEntry(zero); assertNull(e4); - } /** @@ -833,7 +822,6 @@ public class TreeSubMapTest extends JSR1 Map.Entry e4 = map.higherEntry(m6); assertNull(e4); - } /** @@ -852,7 +840,6 @@ public class TreeSubMapTest extends JSR1 Map.Entry e4 = map.floorEntry(zero); assertNull(e4); - } /** @@ -871,7 +858,6 @@ public class TreeSubMapTest extends JSR1 Map.Entry e4 = map.ceilingEntry(m6); assertNull(e4); - } /** @@ -929,7 +915,7 @@ public class TreeSubMapTest extends JSR1 } /** - * size returns the correct values + * size returns the correct values */ public void testDescendingSize() { NavigableMap map = dmap5(); @@ -945,7 +931,7 @@ public class TreeSubMapTest extends JSR1 NavigableMap map = dmap5(); String s = map.toString(); for (int i = 1; i <= 5; ++i) { - assertTrue(s.indexOf(String.valueOf(i)) >= 0); + assertTrue(s.contains(String.valueOf(i))); } } @@ -955,8 +941,8 @@ public class TreeSubMapTest extends JSR1 * get(null) of nonempty map throws NPE */ public void testDescendingGet_NullPointerException() { + NavigableMap c = dmap5(); try { - NavigableMap c = dmap5(); c.get(null); shouldThrow(); } catch (NullPointerException success) {} @@ -966,33 +952,27 @@ public class TreeSubMapTest extends JSR1 * put(null,x) throws NPE */ public void testDescendingPut1_NullPointerException() { + NavigableMap c = dmap5(); try { - NavigableMap c = dmap5(); c.put(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} } /** - * A deserialized map equals original + * A deserialized/reserialized map equals original */ public void testDescendingSerialization() throws Exception { - NavigableMap q = dmap5(); + NavigableMap x = dmap5(); + 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)); - NavigableMap r = (NavigableMap)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); } - /** * subMap returns map with keys in requested range */