--- jsr166/src/test/tck/TreeSubMapTest.java 2010/10/09 19:30:35 1.13 +++ jsr166/src/test/tck/TreeSubMapTest.java 2015/02/28 18:32:12 1.22 @@ -1,13 +1,21 @@ /* * 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) { @@ -18,7 +26,7 @@ public class TreeSubMapTest extends JSR1 } /** - * 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(); @@ -69,10 +77,9 @@ public class TreeSubMapTest extends JSR1 public void testClear() { NavigableMap map = map5(); map.clear(); - assertEquals(map.size(), 0); + assertEquals(0, map.size()); } - /** * Maps with same contents are equal */ @@ -141,7 +148,6 @@ public class TreeSubMapTest extends JSR1 assertEquals(five, map.lastKey()); } - /** * keySet returns a Set containing all the keys */ @@ -376,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))); } } @@ -386,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) {} @@ -397,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) {} @@ -408,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) {} @@ -419,8 +425,8 @@ 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) {} @@ -430,24 +436,16 @@ public class TreeSubMapTest extends JSR1 * A deserialized 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 */ @@ -589,10 +587,9 @@ public class TreeSubMapTest extends JSR1 public void testDescendingClear() { NavigableMap map = dmap5(); map.clear(); - assertEquals(map.size(), 0); + assertEquals(0, map.size()); } - /** * Maps with same contents are equal */ @@ -661,7 +658,6 @@ public class TreeSubMapTest extends JSR1 assertEquals(m5, map.lastKey()); } - /** * keySet returns a Set containing all the keys */ @@ -748,7 +744,6 @@ public class TreeSubMapTest extends JSR1 assertTrue(s.contains("E")); } - /** * entrySet contains all pairs */ @@ -783,7 +778,6 @@ public class TreeSubMapTest extends JSR1 assertTrue(empty.containsKey(m5)); } - /** * remove removes the correct key-value pair from the map */ @@ -937,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))); } } @@ -947,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) {} @@ -958,8 +952,8 @@ 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) {} @@ -969,22 +963,16 @@ public class TreeSubMapTest extends JSR1 * A deserialized 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 */