--- jsr166/src/test/tck/ConcurrentSkipListMapTest.java 2011/08/01 06:19:47 1.26 +++ jsr166/src/test/tck/ConcurrentSkipListMapTest.java 2017/08/04 03:00:20 1.40 @@ -4,20 +4,32 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ -import junit.framework.*; -import java.util.*; +import java.util.ArrayList; +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.concurrent.ConcurrentSkipListMap; +import junit.framework.Test; +import junit.framework.TestSuite; + public class ConcurrentSkipListMapTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ConcurrentSkipListMapTest.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 ConcurrentSkipListMap map5() { ConcurrentSkipListMap map = new ConcurrentSkipListMap(); @@ -38,11 +50,11 @@ public class ConcurrentSkipListMapTest e public void testClear() { ConcurrentSkipListMap map = map5(); map.clear(); - assertEquals(map.size(), 0); + assertEquals(0, map.size()); } /** - * + * copy constructor creates map equal to source map */ public void testConstructFromSorted() { ConcurrentSkipListMap map = map5(); @@ -174,7 +186,7 @@ public class ConcurrentSkipListMapTest e last = k; ++count; } - assertEquals(count ,5); + assertEquals(5, count); } /** @@ -193,7 +205,7 @@ public class ConcurrentSkipListMapTest e last = k; ++count; } - assertEquals(count ,5); + assertEquals(5, count); } /** @@ -212,7 +224,7 @@ public class ConcurrentSkipListMapTest e last = k; ++count; } - assertEquals(count, 5); + assertEquals(5, count); } /** @@ -231,7 +243,7 @@ public class ConcurrentSkipListMapTest e last = k; ++count; } - assertEquals(count, 5); + assertEquals(5, count); } /** @@ -682,8 +694,8 @@ public class ConcurrentSkipListMapTest e * get(null) of nonempty map throws NPE */ public void testGet_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.get(null); shouldThrow(); } catch (NullPointerException success) {} @@ -693,8 +705,8 @@ public class ConcurrentSkipListMapTest e * containsKey(null) of nonempty map throws NPE */ public void testContainsKey_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.containsKey(null); shouldThrow(); } catch (NullPointerException success) {} @@ -704,8 +716,8 @@ public class ConcurrentSkipListMapTest e * containsValue(null) throws NPE */ public void testContainsValue_NullPointerException() { + ConcurrentSkipListMap c = new ConcurrentSkipListMap(); try { - ConcurrentSkipListMap c = new ConcurrentSkipListMap(); c.containsValue(null); shouldThrow(); } catch (NullPointerException success) {} @@ -715,8 +727,8 @@ public class ConcurrentSkipListMapTest e * put(null,x) throws NPE */ public void testPut1_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.put(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -726,8 +738,8 @@ public class ConcurrentSkipListMapTest e * putIfAbsent(null, x) throws NPE */ public void testPutIfAbsent1_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.putIfAbsent(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -737,8 +749,8 @@ public class ConcurrentSkipListMapTest e * replace(null, x) throws NPE */ public void testReplace_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.replace(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -748,8 +760,8 @@ public class ConcurrentSkipListMapTest e * replace(null, x, y) throws NPE */ public void testReplaceValue_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.replace(null, one, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -759,9 +771,9 @@ public class ConcurrentSkipListMapTest e * remove(null) throws NPE */ public void testRemove1_NullPointerException() { + ConcurrentSkipListMap c = new ConcurrentSkipListMap(); + c.put("sadsdf", "asdads"); try { - ConcurrentSkipListMap c = new ConcurrentSkipListMap(); - c.put("sadsdf", "asdads"); c.remove(null); shouldThrow(); } catch (NullPointerException success) {} @@ -771,9 +783,9 @@ public class ConcurrentSkipListMapTest e * remove(null, x) throws NPE */ public void testRemove2_NullPointerException() { + ConcurrentSkipListMap c = new ConcurrentSkipListMap(); + c.put("sadsdf", "asdads"); try { - ConcurrentSkipListMap c = new ConcurrentSkipListMap(); - c.put("sadsdf", "asdads"); c.remove(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -789,17 +801,37 @@ public class ConcurrentSkipListMapTest e } /** - * A deserialized map equals original + * A cloned map equals original + */ + public void testClone() { + ConcurrentSkipListMap x = map5(); + ConcurrentSkipListMap y = x.clone(); + + assertNotSame(x, y); + assertEquals(x.size(), y.size()); + assertEquals(x.toString(), y.toString()); + assertEquals(x, y); + assertEquals(y, x); + y.clear(); + assertTrue(y.isEmpty()); + assertFalse(x.equals(y)); + } + + /** + * A deserialized/reserialized map equals original */ public void testSerialization() throws Exception { NavigableMap x = map5(); NavigableMap y = serialClone(x); - assertTrue(x != y); + assertNotSame(x, y); assertEquals(x.size(), y.size()); assertEquals(x.toString(), y.toString()); assertEquals(x, y); assertEquals(y, x); + y.clear(); + assertTrue(y.isEmpty()); + assertFalse(x.equals(y)); } /** @@ -985,8 +1017,8 @@ public class ConcurrentSkipListMapTest e 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; } @@ -1018,7 +1050,7 @@ public class ConcurrentSkipListMapTest e // 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); } } @@ -1043,7 +1075,7 @@ public class ConcurrentSkipListMapTest e // 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 { @@ -1213,7 +1245,7 @@ public class ConcurrentSkipListMapTest e if (bsContainsI) size++; } - assertEquals(map.size(), size); + assertEquals(size, map.size()); // Test contents using contains keySet iterator int size2 = 0; @@ -1261,7 +1293,7 @@ public class ConcurrentSkipListMapTest e } static boolean eq(Integer i, int j) { - return i == null ? j == -1 : i == j; + return (i == null) ? j == -1 : i == j; } }