--- jsr166/src/test/tck/ConcurrentSkipListMapTest.java 2009/11/21 02:07:26 1.12 +++ jsr166/src/test/tck/ConcurrentSkipListMapTest.java 2017/08/04 03:00:20 1.40 @@ -1,24 +1,35 @@ /* * 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.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(); @@ -34,16 +45,16 @@ public class ConcurrentSkipListMapTest e } /** - * clear removes all pairs + * clear removes all pairs */ 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(); @@ -52,7 +63,7 @@ public class ConcurrentSkipListMapTest e } /** - * Maps with same contents are equal + * Maps with same contents are equal */ public void testEquals() { ConcurrentSkipListMap map1 = map5(); @@ -65,7 +76,7 @@ public class ConcurrentSkipListMapTest e } /** - * containsKey returns true for contained key + * containsKey returns true for contained key */ public void testContainsKey() { ConcurrentSkipListMap map = map5(); @@ -74,7 +85,7 @@ public class ConcurrentSkipListMapTest e } /** - * containsValue returns true for held values + * containsValue returns true for held values */ public void testContainsValue() { ConcurrentSkipListMap map = map5(); @@ -83,8 +94,8 @@ public class ConcurrentSkipListMapTest e } /** - * 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() { ConcurrentSkipListMap map = map5(); @@ -94,7 +105,7 @@ public class ConcurrentSkipListMapTest e } /** - * 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() { ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); @@ -104,7 +115,7 @@ public class ConcurrentSkipListMapTest e } /** - * firstKey returns first key + * firstKey returns first key */ public void testFirstKey() { ConcurrentSkipListMap map = map5(); @@ -112,16 +123,15 @@ public class ConcurrentSkipListMapTest e } /** - * lastKey returns last key + * lastKey returns last key */ public void testLastKey() { ConcurrentSkipListMap map = map5(); assertEquals(five, map.lastKey()); } - /** - * keySet.toArray returns contains all keys + * keySet.toArray returns contains all keys */ public void testKeySetToArray() { ConcurrentSkipListMap map = map5(); @@ -134,7 +144,7 @@ public class ConcurrentSkipListMapTest e } /** - * descendingkeySet.toArray returns contains all keys + * descendingkeySet.toArray returns contains all keys */ public void testDescendingKeySetToArray() { ConcurrentSkipListMap map = map5(); @@ -147,7 +157,7 @@ public class ConcurrentSkipListMapTest e } /** - * keySet returns a Set containing all the keys + * keySet returns a Set containing all the keys */ public void testKeySet() { ConcurrentSkipListMap map = map5(); @@ -161,7 +171,7 @@ public class ConcurrentSkipListMapTest e } /** - * keySet is ordered + * keySet is ordered */ public void testKeySetOrder() { ConcurrentSkipListMap map = map5(); @@ -176,7 +186,7 @@ public class ConcurrentSkipListMapTest e last = k; ++count; } - assertEquals(count ,5); + assertEquals(5, count); } /** @@ -195,11 +205,11 @@ public class ConcurrentSkipListMapTest e last = k; ++count; } - assertEquals(count ,5); + assertEquals(5, count); } /** - * descendingKeySet is ordered + * descendingKeySet is ordered */ public void testDescendingKeySetOrder() { ConcurrentSkipListMap map = map5(); @@ -214,11 +224,11 @@ public class ConcurrentSkipListMapTest e last = k; ++count; } - assertEquals(count, 5); + assertEquals(5, count); } /** - * descending iterator of descendingKeySet is ordered + * descending iterator of descendingKeySet is ordered */ public void testDescendingKeySetDescendingIteratorOrder() { ConcurrentSkipListMap map = map5(); @@ -233,11 +243,11 @@ public class ConcurrentSkipListMapTest e last = k; ++count; } - assertEquals(count, 5); + assertEquals(5, count); } /** - * Values.toArray contains all values + * Values.toArray contains all values */ public void testValuesToArray() { ConcurrentSkipListMap map = map5(); @@ -305,7 +315,7 @@ public class ConcurrentSkipListMapTest e } /** - * entrySet.toArray contains all entries + * entrySet.toArray contains all entries */ public void testEntrySetToArray() { ConcurrentSkipListMap map = map5(); @@ -319,7 +329,7 @@ public class ConcurrentSkipListMapTest e } /** - * descendingEntrySet.toArray contains all entries + * descendingEntrySet.toArray contains all entries */ public void testDescendingEntrySetToArray() { ConcurrentSkipListMap map = map5(); @@ -333,7 +343,7 @@ public class ConcurrentSkipListMapTest e } /** - * putAll adds all key-value pairs from the given map + * putAll adds all key-value pairs from the given map */ public void testPutAll() { ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); @@ -348,7 +358,7 @@ public class ConcurrentSkipListMapTest e } /** - * putIfAbsent works when the given key is not present + * putIfAbsent works when the given key is not present */ public void testPutIfAbsent() { ConcurrentSkipListMap map = map5(); @@ -357,7 +367,7 @@ public class ConcurrentSkipListMapTest e } /** - * 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() { ConcurrentSkipListMap map = map5(); @@ -365,7 +375,7 @@ public class ConcurrentSkipListMapTest e } /** - * replace fails when the given key is not present + * replace fails when the given key is not present */ public void testReplace() { ConcurrentSkipListMap map = map5(); @@ -374,7 +384,7 @@ public class ConcurrentSkipListMapTest e } /** - * replace succeeds if the key is already present + * replace succeeds if the key is already present */ public void testReplace2() { ConcurrentSkipListMap map = map5(); @@ -382,7 +392,6 @@ public class ConcurrentSkipListMapTest e assertEquals("Z", map.get(one)); } - /** * replace value fails when the given key not mapped to expected value */ @@ -403,9 +412,8 @@ public class ConcurrentSkipListMapTest e 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() { ConcurrentSkipListMap map = map5(); @@ -427,7 +435,6 @@ public class ConcurrentSkipListMapTest e map.remove(four, "A"); assertEquals(4, map.size()); assertTrue(map.containsKey(four)); - } /** @@ -446,7 +453,6 @@ public class ConcurrentSkipListMapTest e Map.Entry e4 = map.lowerEntry(zero); assertNull(e4); - } /** @@ -465,7 +471,6 @@ public class ConcurrentSkipListMapTest e Map.Entry e4 = map.higherEntry(six); assertNull(e4); - } /** @@ -484,7 +489,6 @@ public class ConcurrentSkipListMapTest e Map.Entry e4 = map.floorEntry(zero); assertNull(e4); - } /** @@ -503,43 +507,40 @@ public class ConcurrentSkipListMapTest e Map.Entry e4 = map.ceilingEntry(six); assertNull(e4); - } /** * lowerEntry, higherEntry, ceilingEntry, and floorEntry return - * imutable entries + * immutable entries */ - public void testEntryImmutablity() { + public void testEntryImmutability() { ConcurrentSkipListMap map = map5(); Map.Entry e = map.lowerEntry(three); assertEquals(two, e.getKey()); try { e.setValue("X"); - fail(); + shouldThrow(); } catch (UnsupportedOperationException success) {} e = map.higherEntry(zero); assertEquals(one, e.getKey()); try { e.setValue("X"); - fail(); + shouldThrow(); } catch (UnsupportedOperationException success) {} e = map.floorEntry(one); assertEquals(one, e.getKey()); try { e.setValue("X"); - fail(); + shouldThrow(); } catch (UnsupportedOperationException success) {} e = map.ceilingEntry(five); assertEquals(five, e.getKey()); try { e.setValue("X"); - fail(); + shouldThrow(); } catch (UnsupportedOperationException success) {} } - - /** * lowerKey returns preceding element */ @@ -556,7 +557,6 @@ public class ConcurrentSkipListMapTest e Object e4 = q.lowerKey(zero); assertNull(e4); - } /** @@ -575,7 +575,6 @@ public class ConcurrentSkipListMapTest e Object e4 = q.higherKey(six); assertNull(e4); - } /** @@ -594,7 +593,6 @@ public class ConcurrentSkipListMapTest e Object e4 = q.floorKey(zero); assertNull(e4); - } /** @@ -613,7 +611,6 @@ public class ConcurrentSkipListMapTest e Object e4 = q.ceilingKey(six); assertNull(e4); - } /** @@ -638,8 +635,7 @@ public class ConcurrentSkipListMapTest e try { e.setValue("A"); shouldThrow(); - } catch (Exception ok) { - } + } catch (UnsupportedOperationException success) {} e = map.pollFirstEntry(); assertNull(e); } @@ -666,14 +662,13 @@ public class ConcurrentSkipListMapTest e try { e.setValue("E"); shouldThrow(); - } catch (Exception ok) { - } + } catch (UnsupportedOperationException success) {} e = map.pollLastEntry(); assertNull(e); } /** - * size returns the correct values + * size returns the correct values */ public void testSize() { ConcurrentSkipListMap map = map5(); @@ -689,7 +684,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))); } } @@ -699,142 +694,145 @@ 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 e) {} + } catch (NullPointerException success) {} } /** * containsKey(null) of nonempty map throws NPE */ public void testContainsKey_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.containsKey(null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** * containsValue(null) throws NPE */ public void testContainsValue_NullPointerException() { + ConcurrentSkipListMap c = new ConcurrentSkipListMap(); try { - ConcurrentSkipListMap c = new ConcurrentSkipListMap(); c.containsValue(null); shouldThrow(); - } catch (NullPointerException e) {} + } 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 e) {} + } catch (NullPointerException success) {} } /** * putIfAbsent(null, x) throws NPE */ public void testPutIfAbsent1_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.putIfAbsent(null, "whatever"); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** * replace(null, x) throws NPE */ public void testReplace_NullPointerException() { + ConcurrentSkipListMap c = map5(); try { - ConcurrentSkipListMap c = map5(); c.replace(null, "whatever"); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** * 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 e) {} + } catch (NullPointerException success) {} } /** * 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 e) {} + } catch (NullPointerException success) {} } /** * 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 e) {} + } catch (NullPointerException success) {} } /** * remove(x, null) returns false */ public void testRemove3() { - try { - ConcurrentSkipListMap c = new ConcurrentSkipListMap(); - c.put("sadsdf", "asdads"); - assertFalse(c.remove("sadsdf", null)); - } catch (NullPointerException e) { - fail(); - } + ConcurrentSkipListMap c = new ConcurrentSkipListMap(); + c.put("sadsdf", "asdads"); + assertFalse(c.remove("sadsdf", null)); } /** - * A deserialized map equals original + * A cloned map equals original */ - public void testSerialization() { - ConcurrentSkipListMap q = map5(); + public void testClone() { + ConcurrentSkipListMap x = map5(); + ConcurrentSkipListMap y = x.clone(); - try { - 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)); - } catch (Exception e) { - e.printStackTrace(); - unexpectedException(); - } + 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 @@ -872,7 +870,7 @@ public class ConcurrentSkipListMapTest e assertEquals(1, sm.size()); assertEquals(three, sm.firstKey()); assertEquals(three, sm.lastKey()); - assertTrue(sm.remove(three) != null); + assertEquals("C", sm.remove(three)); assertTrue(sm.isEmpty()); assertEquals(3, map.size()); } @@ -905,7 +903,7 @@ public class ConcurrentSkipListMapTest e assertEquals(4, map.size()); assertEquals(0, sm.size()); assertTrue(sm.isEmpty()); - assertTrue(sm.remove(three) == null); + assertSame(sm.remove(three), null); assertEquals(4, map.size()); } @@ -987,7 +985,7 @@ public class ConcurrentSkipListMapTest e NavigableMap ssm = sm.tailMap(four, true); assertEquals(four, ssm.firstKey()); assertEquals(five, ssm.lastKey()); - assertTrue(ssm.remove(four) != null); + assertEquals("D", ssm.remove(four)); assertEquals(1, ssm.size()); assertEquals(3, sm.size()); assertEquals(4, map.size()); @@ -999,8 +997,8 @@ public class ConcurrentSkipListMapTest e /** * Submaps of submaps subdivide correctly */ - public void testRecursiveSubMaps() { - int mapSize = 1000; + public void testRecursiveSubMaps() throws Exception { + int mapSize = expensiveTests ? 1000 : 100; Class cl = ConcurrentSkipListMap.class; NavigableMap map = newMap(cl); bs = new BitSet(mapSize); @@ -1017,14 +1015,10 @@ public class ConcurrentSkipListMapTest e 0, mapSize - 1, true); } - static NavigableMap newMap(Class cl) { - NavigableMap result = null; - try { - result = (NavigableMap) cl.newInstance(); - } catch (Exception e) { - fail(); - } - assertEquals(result.size(), 0); + static NavigableMap newMap(Class cl) throws Exception { + NavigableMap result = + (NavigableMap) cl.getConstructor().newInstance(); + assertEquals(0, result.size()); assertFalse(result.keySet().iterator().hasNext()); return result; } @@ -1056,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); } } @@ -1081,15 +1075,13 @@ 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 { map.put(key, 2 * key); - fail(); - } catch (IllegalArgumentException e) { - // expected - } + shouldThrow(); + } catch (IllegalArgumentException success) {} } } } @@ -1187,7 +1179,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); } @@ -1218,7 +1210,7 @@ public class ConcurrentSkipListMapTest e // BitSet should support this! Test would run much faster while (key >= min) { if (bs.get(key)) - return(key); + return key; key--; } return -1; @@ -1253,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; @@ -1284,16 +1276,12 @@ public class ConcurrentSkipListMapTest e assertEq(rs.last(), -1); try { map.firstKey(); - fail(); - } catch (NoSuchElementException e) { - // expected - } + shouldThrow(); + } catch (NoSuchElementException success) {} try { map.lastKey(); - fail(); - } catch (NoSuchElementException e) { - // expected - } + shouldThrow(); + } catch (NoSuchElementException success) {} } } @@ -1305,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; } }