--- jsr166/src/test/tck/ConcurrentSkipListMapTest.java 2009/11/21 17:38:05 1.14 +++ jsr166/src/test/tck/ConcurrentSkipListMapTest.java 2015/02/28 18:30:05 1.35 @@ -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()); + junit.textui.TestRunner.run(suite()); } 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,14 +507,13 @@ 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()); @@ -538,8 +541,6 @@ public class ConcurrentSkipListMapTest e } 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); - } /** @@ -671,7 +668,7 @@ public class ConcurrentSkipListMapTest e } /** - * size returns the correct values + * size returns the correct values */ public void testSize() { ConcurrentSkipListMap map = map5(); @@ -687,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))); } } @@ -697,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) {} @@ -708,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) {} @@ -719,20 +716,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) {} @@ -742,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) {} @@ -753,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) {} @@ -764,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) {} @@ -775,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) {} @@ -787,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) {} @@ -808,23 +804,16 @@ public class ConcurrentSkipListMapTest e * A deserialized map equals original */ public void testSerialization() throws Exception { - ConcurrentSkipListMap 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)); - 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); } - - /** * subMap returns map with keys in requested range */ @@ -861,7 +850,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()); } @@ -894,7 +883,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()); } @@ -976,7 +965,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()); @@ -989,7 +978,7 @@ public class ConcurrentSkipListMapTest e * Submaps of submaps subdivide correctly */ public void testRecursiveSubMaps() throws Exception { - int mapSize = 1000; + int mapSize = expensiveTests ? 1000 : 100; Class cl = ConcurrentSkipListMap.class; NavigableMap map = newMap(cl); bs = new BitSet(mapSize); @@ -1009,7 +998,7 @@ public class ConcurrentSkipListMapTest e static NavigableMap newMap(Class cl) throws Exception { NavigableMap result = (NavigableMap) cl.newInstance(); - assertEquals(result.size(), 0); + assertEquals(0, result.size()); assertFalse(result.keySet().iterator().hasNext()); return result; } @@ -1041,7 +1030,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); } } @@ -1066,7 +1055,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 { @@ -1170,7 +1159,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); } @@ -1201,7 +1190,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; @@ -1236,7 +1225,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;