--- jsr166/src/test/tck/ConcurrentSkipListMapTest.java 2011/03/15 19:47:06 1.22 +++ jsr166/src/test/tck/ConcurrentSkipListMapTest.java 2019/09/29 20:40:48 1.42 @@ -4,21 +4,42 @@ * 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.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; 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); + class Implementation implements MapImplementation { + public Class klazz() { return ConcurrentSkipListMap.class; } + public Map emptyMap() { return new ConcurrentSkipListMap(); } + public boolean isConcurrent() { return true; } + public boolean remappingFunctionCalledAtMostOnce() { return false; }; + public boolean permitsNullKeys() { return false; } + public boolean permitsNullValues() { return false; } + public boolean supportsSetValue() { return false; } + } + return newTestSuite( + ConcurrentSkipListMapTest.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 ConcurrentSkipListMap map5() { ConcurrentSkipListMap map = new ConcurrentSkipListMap(); @@ -39,11 +60,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(); @@ -119,7 +140,6 @@ public class ConcurrentSkipListMapTest e assertEquals(five, map.lastKey()); } - /** * keySet.toArray returns contains all keys */ @@ -176,7 +196,7 @@ public class ConcurrentSkipListMapTest e last = k; ++count; } - assertEquals(count ,5); + assertEquals(5, count); } /** @@ -195,7 +215,7 @@ public class ConcurrentSkipListMapTest e last = k; ++count; } - assertEquals(count ,5); + assertEquals(5, count); } /** @@ -214,7 +234,7 @@ public class ConcurrentSkipListMapTest e last = k; ++count; } - assertEquals(count, 5); + assertEquals(5, count); } /** @@ -233,7 +253,7 @@ public class ConcurrentSkipListMapTest e last = k; ++count; } - assertEquals(count, 5); + assertEquals(5, count); } /** @@ -382,7 +402,6 @@ public class ConcurrentSkipListMapTest e assertEquals("Z", map.get(one)); } - /** * replace value fails when the given key not mapped to expected value */ @@ -403,7 +422,6 @@ public class ConcurrentSkipListMapTest e assertEquals("Z", map.get(one)); } - /** * remove removes the correct key-value pair from the map */ @@ -505,7 +523,7 @@ public class ConcurrentSkipListMapTest e * lowerEntry, higherEntry, ceilingEntry, and floorEntry return * immutable entries */ - public void testEntryImmutablity() { + public void testEntryImmutability() { ConcurrentSkipListMap map = map5(); Map.Entry e = map.lowerEntry(three); assertEquals(two, e.getKey()); @@ -533,8 +551,6 @@ public class ConcurrentSkipListMapTest e } catch (UnsupportedOperationException success) {} } - - /** * lowerKey returns preceding element */ @@ -678,7 +694,7 @@ public class ConcurrentSkipListMapTest e ConcurrentSkipListMap 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))); } } @@ -688,8 +704,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) {} @@ -699,8 +715,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) {} @@ -710,20 +726,19 @@ 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) {} } - /** * put(null,x) throws NPE */ public void testPut1_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.put(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -733,8 +748,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) {} @@ -744,8 +759,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) {} @@ -755,8 +770,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) {} @@ -766,9 +781,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) {} @@ -778,9 +793,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) {} @@ -796,25 +811,38 @@ public class ConcurrentSkipListMapTest e } /** - * A deserialized map equals original + * A cloned map equals original */ - public void testSerialization() throws Exception { - ConcurrentSkipListMap q = map5(); + public void testClone() { + ConcurrentSkipListMap x = map5(); + ConcurrentSkipListMap y = x.clone(); - 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)); - ConcurrentSkipListMap r = (ConcurrentSkipListMap)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); + 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); + 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)); + } /** * subMap returns map with keys in requested range @@ -999,8 +1027,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; } @@ -1032,7 +1060,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); } } @@ -1057,7 +1085,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 { @@ -1161,7 +1189,7 @@ public class ConcurrentSkipListMapTest e */ 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); } @@ -1227,7 +1255,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; @@ -1275,7 +1303,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; } }