--- jsr166/src/test/tck/ConcurrentSkipListSubMapTest.java 2009/12/23 00:47:16 1.14 +++ jsr166/src/test/tck/ConcurrentSkipListSubMapTest.java 2013/05/30 03:28:55 1.23 @@ -1,24 +1,24 @@ /* * 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.concurrent.ConcurrentNavigableMap; +import java.util.concurrent.ConcurrentSkipListMap; import java.util.*; -import java.util.concurrent.*; -import java.io.*; public class ConcurrentSkipListSubMapTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } public static Test suite() { return new TestSuite(ConcurrentSkipListSubMapTest.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 ConcurrentNavigableMap map5() { ConcurrentSkipListMap map = new ConcurrentSkipListMap(); @@ -36,7 +36,7 @@ public class ConcurrentSkipListSubMapTes } /** - * 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 ConcurrentNavigableMap dmap5() { ConcurrentSkipListMap map = new ConcurrentSkipListMap(); @@ -64,17 +64,16 @@ public class ConcurrentSkipListSubMapTes } /** - * clear removes all pairs + * clear removes all pairs */ public void testClear() { ConcurrentNavigableMap 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() { ConcurrentNavigableMap map1 = map5(); @@ -87,7 +86,7 @@ public class ConcurrentSkipListSubMapTes } /** - * containsKey returns true for contained key + * containsKey returns true for contained key */ public void testContainsKey() { ConcurrentNavigableMap map = map5(); @@ -96,7 +95,7 @@ public class ConcurrentSkipListSubMapTes } /** - * containsValue returns true for held values + * containsValue returns true for held values */ public void testContainsValue() { ConcurrentNavigableMap map = map5(); @@ -105,8 +104,8 @@ public class ConcurrentSkipListSubMapTes } /** - * 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() { ConcurrentNavigableMap map = map5(); @@ -116,7 +115,7 @@ public class ConcurrentSkipListSubMapTes } /** - * 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() { ConcurrentNavigableMap empty = map0(); @@ -126,7 +125,7 @@ public class ConcurrentSkipListSubMapTes } /** - * firstKey returns first key + * firstKey returns first key */ public void testFirstKey() { ConcurrentNavigableMap map = map5(); @@ -134,16 +133,15 @@ public class ConcurrentSkipListSubMapTes } /** - * lastKey returns last key + * lastKey returns last key */ public void testLastKey() { ConcurrentNavigableMap 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() { ConcurrentNavigableMap map = map5(); @@ -157,7 +155,7 @@ public class ConcurrentSkipListSubMapTes } /** - * keySet is ordered + * keySet is ordered */ public void testKeySetOrder() { ConcurrentNavigableMap map = map5(); @@ -187,7 +185,7 @@ public class ConcurrentSkipListSubMapTes } /** - * keySet.toArray returns contains all keys + * keySet.toArray returns contains all keys */ public void testKeySetToArray() { ConcurrentNavigableMap map = map5(); @@ -200,7 +198,7 @@ public class ConcurrentSkipListSubMapTes } /** - * descendingkeySet.toArray returns contains all keys + * descendingkeySet.toArray returns contains all keys */ public void testDescendingKeySetToArray() { ConcurrentNavigableMap map = map5(); @@ -213,7 +211,7 @@ public class ConcurrentSkipListSubMapTes } /** - * Values.toArray contains all values + * Values.toArray contains all values */ public void testValuesToArray() { ConcurrentNavigableMap map = map5(); @@ -228,7 +226,6 @@ public class ConcurrentSkipListSubMapTes assertTrue(s.contains("E")); } - /** * entrySet contains all pairs */ @@ -249,7 +246,7 @@ public class ConcurrentSkipListSubMapTes } /** - * putAll adds all key-value pairs from the given map + * putAll adds all key-value pairs from the given map */ public void testPutAll() { ConcurrentNavigableMap empty = map0(); @@ -264,7 +261,7 @@ public class ConcurrentSkipListSubMapTes } /** - * putIfAbsent works when the given key is not present + * putIfAbsent works when the given key is not present */ public void testPutIfAbsent() { ConcurrentNavigableMap map = map5(); @@ -273,7 +270,7 @@ public class ConcurrentSkipListSubMapTes } /** - * putIfAbsent does not add the pair if the key is already present + * putIfAbsent does not add the pair if the key is already present */ public void testPutIfAbsent2() { ConcurrentNavigableMap map = map5(); @@ -281,7 +278,7 @@ public class ConcurrentSkipListSubMapTes } /** - * replace fails when the given key is not present + * replace fails when the given key is not present */ public void testReplace() { ConcurrentNavigableMap map = map5(); @@ -290,7 +287,7 @@ public class ConcurrentSkipListSubMapTes } /** - * replace succeeds if the key is already present + * replace succeeds if the key is already present */ public void testReplace2() { ConcurrentNavigableMap map = map5(); @@ -298,7 +295,6 @@ public class ConcurrentSkipListSubMapTes assertEquals("Z", map.get(one)); } - /** * replace value fails when the given key not mapped to expected value */ @@ -319,9 +315,8 @@ public class ConcurrentSkipListSubMapTes assertEquals("Z", map.get(one)); } - /** - * remove removes the correct key-value pair from the map + * remove removes the correct key-value pair from the map */ public void testRemove() { ConcurrentNavigableMap map = map5(); @@ -472,7 +467,7 @@ public class ConcurrentSkipListSubMapTes } /** - * size returns the correct values + * size returns the correct values */ public void testSize() { ConcurrentNavigableMap map = map5(); @@ -488,7 +483,7 @@ public class ConcurrentSkipListSubMapTes ConcurrentNavigableMap 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))); } } @@ -527,7 +522,6 @@ public class ConcurrentSkipListSubMapTes } catch (NullPointerException success) {} } - /** * put(null,x) throws NPE */ @@ -598,23 +592,16 @@ public class ConcurrentSkipListSubMapTes * A deserialized map equals original */ public void testSerialization() throws Exception { - ConcurrentNavigableMap 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)); - ConcurrentNavigableMap r = (ConcurrentNavigableMap)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 */ @@ -751,17 +738,16 @@ public class ConcurrentSkipListSubMapTes } /** - * clear removes all pairs + * clear removes all pairs */ public void testDescendingClear() { ConcurrentNavigableMap 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() { ConcurrentNavigableMap map1 = dmap5(); @@ -774,7 +760,7 @@ public class ConcurrentSkipListSubMapTes } /** - * containsKey returns true for contained key + * containsKey returns true for contained key */ public void testDescendingContainsKey() { ConcurrentNavigableMap map = dmap5(); @@ -783,7 +769,7 @@ public class ConcurrentSkipListSubMapTes } /** - * containsValue returns true for held values + * containsValue returns true for held values */ public void testDescendingContainsValue() { ConcurrentNavigableMap map = dmap5(); @@ -792,8 +778,8 @@ public class ConcurrentSkipListSubMapTes } /** - * 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() { ConcurrentNavigableMap map = dmap5(); @@ -803,7 +789,7 @@ public class ConcurrentSkipListSubMapTes } /** - * 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() { ConcurrentNavigableMap empty = dmap0(); @@ -813,7 +799,7 @@ public class ConcurrentSkipListSubMapTes } /** - * firstKey returns first key + * firstKey returns first key */ public void testDescendingFirstKey() { ConcurrentNavigableMap map = dmap5(); @@ -821,16 +807,15 @@ public class ConcurrentSkipListSubMapTes } /** - * lastKey returns last key + * lastKey returns last key */ public void testDescendingLastKey() { ConcurrentNavigableMap 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() { ConcurrentNavigableMap map = dmap5(); @@ -844,7 +829,7 @@ public class ConcurrentSkipListSubMapTes } /** - * keySet is ordered + * keySet is ordered */ public void testDescendingKeySetOrder() { ConcurrentNavigableMap map = dmap5(); @@ -874,7 +859,7 @@ public class ConcurrentSkipListSubMapTes } /** - * keySet.toArray returns contains all keys + * keySet.toArray returns contains all keys */ public void testDescendingAscendingKeySetToArray() { ConcurrentNavigableMap map = dmap5(); @@ -887,7 +872,7 @@ public class ConcurrentSkipListSubMapTes } /** - * descendingkeySet.toArray returns contains all keys + * descendingkeySet.toArray returns contains all keys */ public void testDescendingDescendingKeySetToArray() { ConcurrentNavigableMap map = dmap5(); @@ -900,7 +885,7 @@ public class ConcurrentSkipListSubMapTes } /** - * Values.toArray contains all values + * Values.toArray contains all values */ public void testDescendingValuesToArray() { ConcurrentNavigableMap map = dmap5(); @@ -915,7 +900,6 @@ public class ConcurrentSkipListSubMapTes assertTrue(s.contains("E")); } - /** * entrySet contains all pairs */ @@ -936,7 +920,7 @@ public class ConcurrentSkipListSubMapTes } /** - * putAll adds all key-value pairs from the given map + * putAll adds all key-value pairs from the given map */ public void testDescendingPutAll() { ConcurrentNavigableMap empty = dmap0(); @@ -951,7 +935,7 @@ public class ConcurrentSkipListSubMapTes } /** - * putIfAbsent works when the given key is not present + * putIfAbsent works when the given key is not present */ public void testDescendingPutIfAbsent() { ConcurrentNavigableMap map = dmap5(); @@ -960,7 +944,7 @@ public class ConcurrentSkipListSubMapTes } /** - * putIfAbsent does not add the pair if the key is already present + * putIfAbsent does not add the pair if the key is already present */ public void testDescendingPutIfAbsent2() { ConcurrentNavigableMap map = dmap5(); @@ -968,7 +952,7 @@ public class ConcurrentSkipListSubMapTes } /** - * replace fails when the given key is not present + * replace fails when the given key is not present */ public void testDescendingReplace() { ConcurrentNavigableMap map = dmap5(); @@ -977,7 +961,7 @@ public class ConcurrentSkipListSubMapTes } /** - * replace succeeds if the key is already present + * replace succeeds if the key is already present */ public void testDescendingReplace2() { ConcurrentNavigableMap map = dmap5(); @@ -985,7 +969,6 @@ public class ConcurrentSkipListSubMapTes assertEquals("Z", map.get(m1)); } - /** * replace value fails when the given key not mapped to expected value */ @@ -1006,9 +989,8 @@ public class ConcurrentSkipListSubMapTes assertEquals("Z", map.get(m1)); } - /** - * remove removes the correct key-value pair from the map + * remove removes the correct key-value pair from the map */ public void testDescendingRemove() { ConcurrentNavigableMap map = dmap5(); @@ -1159,7 +1141,7 @@ public class ConcurrentSkipListSubMapTes } /** - * size returns the correct values + * size returns the correct values */ public void testDescendingSize() { ConcurrentNavigableMap map = dmap5(); @@ -1175,7 +1157,7 @@ public class ConcurrentSkipListSubMapTes ConcurrentNavigableMap 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))); } } @@ -1214,7 +1196,6 @@ public class ConcurrentSkipListSubMapTes } catch (NullPointerException success) {} } - /** * put(null,x) throws NPE */ @@ -1285,22 +1266,16 @@ public class ConcurrentSkipListSubMapTes * A deserialized map equals original */ public void testDescendingSerialization() throws Exception { - ConcurrentNavigableMap 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)); - ConcurrentNavigableMap r = (ConcurrentNavigableMap)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 */