--- jsr166/src/test/tck/ConcurrentSkipListMapTest.java 2005/05/22 10:56:03 1.3 +++ jsr166/src/test/tck/ConcurrentSkipListMapTest.java 2017/08/04 03:00:20 1.40 @@ -1,49 +1,60 @@ /* * 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); + 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(); + private static ConcurrentSkipListMap map5() { + ConcurrentSkipListMap map = new ConcurrentSkipListMap(); assertTrue(map.isEmpty()); - map.put(one, "A"); - map.put(five, "E"); - map.put(three, "C"); - map.put(two, "B"); - map.put(four, "D"); + map.put(one, "A"); + map.put(five, "E"); + map.put(three, "C"); + map.put(two, "B"); + map.put(four, "D"); assertFalse(map.isEmpty()); assertEquals(5, map.size()); - return map; + return map; } /** - * clear removes all pairs + * clear removes all pairs */ public void testClear() { ConcurrentSkipListMap map = map5(); - map.clear(); - assertEquals(map.size(), 0); + map.clear(); + assertEquals(0, map.size()); } /** - * + * copy constructor creates map equal to source map */ public void testConstructFromSorted() { ConcurrentSkipListMap map = map5(); @@ -52,144 +63,203 @@ public class ConcurrentSkipListMapTest e } /** - * Maps with same contents are equal + * Maps with same contents are equal */ public void testEquals() { ConcurrentSkipListMap map1 = map5(); ConcurrentSkipListMap map2 = map5(); assertEquals(map1, map2); assertEquals(map2, map1); - map1.clear(); + map1.clear(); assertFalse(map1.equals(map2)); assertFalse(map2.equals(map1)); } /** - * containsKey returns true for contained key + * containsKey returns true for contained key */ public void testContainsKey() { ConcurrentSkipListMap map = map5(); - assertTrue(map.containsKey(one)); + assertTrue(map.containsKey(one)); assertFalse(map.containsKey(zero)); } /** - * containsValue returns true for held values + * containsValue returns true for held values */ public void testContainsValue() { ConcurrentSkipListMap map = map5(); - assertTrue(map.containsValue("A")); + assertTrue(map.containsValue("A")); assertFalse(map.containsValue("Z")); } /** - * 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(); - assertEquals("A", (String)map.get(one)); + assertEquals("A", (String)map.get(one)); ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); assertNull(empty.get(one)); } /** - * 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(); ConcurrentSkipListMap map = map5(); - assertTrue(empty.isEmpty()); + assertTrue(empty.isEmpty()); assertFalse(map.isEmpty()); } /** - * firstKey returns first key + * firstKey returns first key */ public void testFirstKey() { ConcurrentSkipListMap map = map5(); - assertEquals(one, map.firstKey()); + assertEquals(one, map.firstKey()); } /** - * lastKey returns last key + * lastKey returns last key */ public void testLastKey() { ConcurrentSkipListMap map = map5(); - assertEquals(five, map.lastKey()); + assertEquals(five, map.lastKey()); } - /** - * keySet.toArray returns contains all keys + * keySet.toArray returns contains all keys */ public void testKeySetToArray() { ConcurrentSkipListMap map = map5(); - Set s = map.keySet(); + Set s = map.keySet(); Object[] ar = s.toArray(); assertTrue(s.containsAll(Arrays.asList(ar))); - assertEquals(5, ar.length); + assertEquals(5, ar.length); ar[0] = m10; assertFalse(s.containsAll(Arrays.asList(ar))); } /** - * descendingkeySet.toArray returns contains all keys + * descendingkeySet.toArray returns contains all keys */ public void testDescendingKeySetToArray() { ConcurrentSkipListMap map = map5(); - Set s = map.descendingKeySet(); + Set s = map.descendingKeySet(); Object[] ar = s.toArray(); - assertEquals(5, ar.length); + assertEquals(5, ar.length); assertTrue(s.containsAll(Arrays.asList(ar))); ar[0] = m10; assertFalse(s.containsAll(Arrays.asList(ar))); } /** - * keySet returns a Set containing all the keys + * keySet returns a Set containing all the keys */ public void testKeySet() { ConcurrentSkipListMap map = map5(); - Set s = map.keySet(); - assertEquals(5, s.size()); - assertTrue(s.contains(one)); - assertTrue(s.contains(two)); - assertTrue(s.contains(three)); - assertTrue(s.contains(four)); - assertTrue(s.contains(five)); + Set s = map.keySet(); + assertEquals(5, s.size()); + assertTrue(s.contains(one)); + assertTrue(s.contains(two)); + assertTrue(s.contains(three)); + assertTrue(s.contains(four)); + assertTrue(s.contains(five)); } /** - * keySet is ordered + * keySet is ordered */ public void testKeySetOrder() { ConcurrentSkipListMap map = map5(); - Set s = map.keySet(); + Set s = map.keySet(); Iterator i = s.iterator(); Integer last = (Integer)i.next(); assertEquals(last, one); + int count = 1; while (i.hasNext()) { Integer k = (Integer)i.next(); assertTrue(last.compareTo(k) < 0); last = k; + ++count; + } + assertEquals(5, count); + } + + /** + * descending iterator of key set is inverse ordered + */ + public void testKeySetDescendingIteratorOrder() { + ConcurrentSkipListMap map = map5(); + NavigableSet s = map.navigableKeySet(); + Iterator i = s.descendingIterator(); + Integer last = (Integer)i.next(); + assertEquals(last, five); + int count = 1; + while (i.hasNext()) { + Integer k = (Integer)i.next(); + assertTrue(last.compareTo(k) > 0); + last = k; + ++count; } + assertEquals(5, count); } /** - * descendingKeySet is ordered + * descendingKeySet is ordered */ public void testDescendingKeySetOrder() { ConcurrentSkipListMap map = map5(); - Set s = map.descendingKeySet(); + Set s = map.descendingKeySet(); Iterator i = s.iterator(); Integer last = (Integer)i.next(); assertEquals(last, five); + int count = 1; while (i.hasNext()) { Integer k = (Integer)i.next(); assertTrue(last.compareTo(k) > 0); last = k; + ++count; } + assertEquals(5, count); + } + + /** + * descending iterator of descendingKeySet is ordered + */ + public void testDescendingKeySetDescendingIteratorOrder() { + ConcurrentSkipListMap map = map5(); + NavigableSet s = map.descendingKeySet(); + Iterator i = s.descendingIterator(); + Integer last = (Integer)i.next(); + assertEquals(last, one); + int count = 1; + while (i.hasNext()) { + Integer k = (Integer)i.next(); + assertTrue(last.compareTo(k) < 0); + last = k; + ++count; + } + assertEquals(5, count); + } + + /** + * Values.toArray contains all values + */ + public void testValuesToArray() { + ConcurrentSkipListMap map = map5(); + Collection v = map.values(); + Object[] ar = v.toArray(); + ArrayList s = new ArrayList(Arrays.asList(ar)); + assertEquals(5, ar.length); + assertTrue(s.contains("A")); + assertTrue(s.contains("B")); + assertTrue(s.contains("C")); + assertTrue(s.contains("D")); + assertTrue(s.contains("E")); } /** @@ -197,13 +267,13 @@ public class ConcurrentSkipListMapTest e */ public void testValues() { ConcurrentSkipListMap map = map5(); - Collection s = map.values(); - assertEquals(5, s.size()); - assertTrue(s.contains("A")); - assertTrue(s.contains("B")); - assertTrue(s.contains("C")); - assertTrue(s.contains("D")); - assertTrue(s.contains("E")); + Collection s = map.values(); + assertEquals(5, s.size()); + assertTrue(s.contains("A")); + assertTrue(s.contains("B")); + assertTrue(s.contains("C")); + assertTrue(s.contains("D")); + assertTrue(s.contains("E")); } /** @@ -211,12 +281,12 @@ public class ConcurrentSkipListMapTest e */ public void testEntrySet() { ConcurrentSkipListMap map = map5(); - Set s = map.entrySet(); - assertEquals(5, s.size()); + Set s = map.entrySet(); + assertEquals(5, s.size()); Iterator it = s.iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry) it.next(); - assertTrue( + assertTrue( (e.getKey().equals(one) && e.getValue().equals("A")) || (e.getKey().equals(two) && e.getValue().equals("B")) || (e.getKey().equals(three) && e.getValue().equals("C")) || @@ -230,12 +300,12 @@ public class ConcurrentSkipListMapTest e */ public void testDescendingEntrySet() { ConcurrentSkipListMap map = map5(); - Set s = map.descendingEntrySet(); - assertEquals(5, s.size()); + Set s = map.descendingMap().entrySet(); + assertEquals(5, s.size()); Iterator it = s.iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry) it.next(); - assertTrue( + assertTrue( (e.getKey().equals(one) && e.getValue().equals("A")) || (e.getKey().equals(two) && e.getValue().equals("B")) || (e.getKey().equals(three) && e.getValue().equals("C")) || @@ -245,11 +315,11 @@ public class ConcurrentSkipListMapTest e } /** - * entrySet.toArray contains all entries + * entrySet.toArray contains all entries */ public void testEntrySetToArray() { ConcurrentSkipListMap map = map5(); - Set s = map.entrySet(); + Set s = map.entrySet(); Object[] ar = s.toArray(); assertEquals(5, ar.length); for (int i = 0; i < 5; ++i) { @@ -259,11 +329,11 @@ public class ConcurrentSkipListMapTest e } /** - * descendingEntrySet.toArray contains all entries + * descendingEntrySet.toArray contains all entries */ public void testDescendingEntrySetToArray() { ConcurrentSkipListMap map = map5(); - Set s = map.descendingEntrySet(); + Set s = map.descendingMap().entrySet(); Object[] ar = s.toArray(); assertEquals(5, ar.length); for (int i = 0; i < 5; ++i) { @@ -273,31 +343,31 @@ 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(); ConcurrentSkipListMap map = map5(); - empty.putAll(map); - assertEquals(5, empty.size()); - assertTrue(empty.containsKey(one)); - assertTrue(empty.containsKey(two)); - assertTrue(empty.containsKey(three)); - assertTrue(empty.containsKey(four)); - assertTrue(empty.containsKey(five)); + empty.putAll(map); + assertEquals(5, empty.size()); + assertTrue(empty.containsKey(one)); + assertTrue(empty.containsKey(two)); + assertTrue(empty.containsKey(three)); + assertTrue(empty.containsKey(four)); + assertTrue(empty.containsKey(five)); } /** - * putIfAbsent works when the given key is not present + * putIfAbsent works when the given key is not present */ public void testPutIfAbsent() { ConcurrentSkipListMap map = map5(); - map.putIfAbsent(six, "Z"); + map.putIfAbsent(six, "Z"); assertTrue(map.containsKey(six)); } /** - * 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(); @@ -305,16 +375,16 @@ 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(); - assertNull(map.replace(six, "Z")); + assertNull(map.replace(six, "Z")); assertFalse(map.containsKey(six)); } /** - * replace succeeds if the key is already present + * replace succeeds if the key is already present */ public void testReplace2() { ConcurrentSkipListMap map = map5(); @@ -322,14 +392,13 @@ public class ConcurrentSkipListMapTest e assertEquals("Z", map.get(one)); } - /** * replace value fails when the given key not mapped to expected value */ public void testReplaceValue() { ConcurrentSkipListMap map = map5(); assertEquals("A", map.get(one)); - assertFalse(map.replace(one, "Z", "Z")); + assertFalse(map.replace(one, "Z", "Z")); assertEquals("A", map.get(one)); } @@ -339,19 +408,18 @@ public class ConcurrentSkipListMapTest e public void testReplaceValue2() { ConcurrentSkipListMap map = map5(); assertEquals("A", map.get(one)); - assertTrue(map.replace(one, "A", "Z")); + assertTrue(map.replace(one, "A", "Z")); 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(); - map.remove(five); - assertEquals(4, map.size()); - assertFalse(map.containsKey(five)); + map.remove(five); + assertEquals(4, map.size()); + assertFalse(map.containsKey(five)); } /** @@ -359,15 +427,14 @@ public class ConcurrentSkipListMapTest e */ public void testRemove2() { ConcurrentSkipListMap map = map5(); - assertTrue(map.containsKey(five)); + assertTrue(map.containsKey(five)); assertEquals("E", map.get(five)); - map.remove(five, "E"); - assertEquals(4, map.size()); - assertFalse(map.containsKey(five)); - map.remove(four, "A"); - assertEquals(4, map.size()); - assertTrue(map.containsKey(four)); - + map.remove(five, "E"); + assertEquals(4, map.size()); + assertFalse(map.containsKey(five)); + map.remove(four, "A"); + assertEquals(4, map.size()); + assertTrue(map.containsKey(four)); } /** @@ -386,7 +453,6 @@ public class ConcurrentSkipListMapTest e Map.Entry e4 = map.lowerEntry(zero); assertNull(e4); - } /** @@ -405,7 +471,6 @@ public class ConcurrentSkipListMapTest e Map.Entry e4 = map.higherEntry(six); assertNull(e4); - } /** @@ -424,7 +489,6 @@ public class ConcurrentSkipListMapTest e Map.Entry e4 = map.floorEntry(zero); assertNull(e4); - } /** @@ -443,9 +507,39 @@ public class ConcurrentSkipListMapTest e Map.Entry e4 = map.ceilingEntry(six); assertNull(e4); - } + /** + * lowerEntry, higherEntry, ceilingEntry, and floorEntry return + * immutable entries + */ + public void testEntryImmutability() { + ConcurrentSkipListMap map = map5(); + Map.Entry e = map.lowerEntry(three); + assertEquals(two, e.getKey()); + try { + e.setValue("X"); + shouldThrow(); + } catch (UnsupportedOperationException success) {} + e = map.higherEntry(zero); + assertEquals(one, e.getKey()); + try { + e.setValue("X"); + shouldThrow(); + } catch (UnsupportedOperationException success) {} + e = map.floorEntry(one); + assertEquals(one, e.getKey()); + try { + e.setValue("X"); + shouldThrow(); + } catch (UnsupportedOperationException success) {} + e = map.ceilingEntry(five); + assertEquals(five, e.getKey()); + try { + e.setValue("X"); + shouldThrow(); + } catch (UnsupportedOperationException success) {} + } /** * lowerKey returns preceding element @@ -463,7 +557,6 @@ public class ConcurrentSkipListMapTest e Object e4 = q.lowerKey(zero); assertNull(e4); - } /** @@ -482,7 +575,6 @@ public class ConcurrentSkipListMapTest e Object e4 = q.higherKey(six); assertNull(e4); - } /** @@ -501,7 +593,6 @@ public class ConcurrentSkipListMapTest e Object e4 = q.floorKey(zero); assertNull(e4); - } /** @@ -520,7 +611,6 @@ public class ConcurrentSkipListMapTest e Object e4 = q.ceilingKey(six); assertNull(e4); - } /** @@ -545,8 +635,7 @@ public class ConcurrentSkipListMapTest e try { e.setValue("A"); shouldThrow(); - } catch (Exception ok) { - } + } catch (UnsupportedOperationException success) {} e = map.pollFirstEntry(); assertNull(e); } @@ -573,20 +662,19 @@ 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(); ConcurrentSkipListMap empty = new ConcurrentSkipListMap(); - assertEquals(0, empty.size()); - assertEquals(5, map.size()); + assertEquals(0, empty.size()); + assertEquals(5, map.size()); } /** @@ -596,9 +684,9 @@ 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))); } - } + } // Exception tests @@ -606,149 +694,152 @@ 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 */ public void testSubMapContents() { ConcurrentSkipListMap map = map5(); - NavigableMap sm = map.navigableSubMap(two, four); + NavigableMap sm = map.subMap(two, true, four, false); assertEquals(two, sm.firstKey()); assertEquals(three, sm.lastKey()); assertEquals(2, sm.size()); @@ -770,7 +861,7 @@ public class ConcurrentSkipListMapTest e k = (Integer)(r.next()); assertEquals(two, k); assertFalse(r.hasNext()); - + Iterator j = sm.keySet().iterator(); j.next(); j.remove(); @@ -779,14 +870,14 @@ 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()); } public void testSubMapContents2() { ConcurrentSkipListMap map = map5(); - NavigableMap sm = map.navigableSubMap(two, three); + NavigableMap sm = map.subMap(two, true, three, false); assertEquals(1, sm.size()); assertEquals(two, sm.firstKey()); assertEquals(two, sm.lastKey()); @@ -804,7 +895,7 @@ public class ConcurrentSkipListMapTest e k = (Integer)(r.next()); assertEquals(two, k); assertFalse(r.hasNext()); - + Iterator j = sm.keySet().iterator(); j.next(); j.remove(); @@ -812,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()); } @@ -821,7 +912,7 @@ public class ConcurrentSkipListMapTest e */ public void testHeadMapContents() { ConcurrentSkipListMap map = map5(); - NavigableMap sm = map.navigableHeadMap(four); + NavigableMap sm = map.headMap(four, false); assertTrue(sm.containsKey(one)); assertTrue(sm.containsKey(two)); assertTrue(sm.containsKey(three)); @@ -843,11 +934,11 @@ public class ConcurrentSkipListMapTest e } /** - * headMap returns map with keys in requested range + * tailMap returns map with keys in requested range */ public void testTailMapContents() { ConcurrentSkipListMap map = map5(); - NavigableMap sm = map.navigableTailMap(two); + NavigableMap sm = map.tailMap(two, true); assertFalse(sm.containsKey(one)); assertTrue(sm.containsKey(two)); assertTrue(sm.containsKey(three)); @@ -891,13 +982,318 @@ public class ConcurrentSkipListMapTest e assertEquals("E", e.getValue()); assertFalse(i.hasNext()); - NavigableMap ssm = sm.navigableTailMap(four); + 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()); } - + + Random rnd = new Random(666); + BitSet bs; + + /** + * Submaps of submaps subdivide correctly + */ + public void testRecursiveSubMaps() throws Exception { + int mapSize = expensiveTests ? 1000 : 100; + Class cl = ConcurrentSkipListMap.class; + NavigableMap map = newMap(cl); + bs = new BitSet(mapSize); + + populate(map, mapSize); + check(map, 0, mapSize - 1, true); + check(map.descendingMap(), 0, mapSize - 1, false); + + mutateMap(map, 0, mapSize - 1); + check(map, 0, mapSize - 1, true); + check(map.descendingMap(), 0, mapSize - 1, false); + + bashSubMap(map.subMap(0, true, mapSize, false), + 0, mapSize - 1, true); + } + + static NavigableMap newMap(Class cl) throws Exception { + NavigableMap result = + (NavigableMap) cl.getConstructor().newInstance(); + assertEquals(0, result.size()); + assertFalse(result.keySet().iterator().hasNext()); + return result; + } + + void populate(NavigableMap map, int limit) { + for (int i = 0, n = 2 * limit / 3; i < n; i++) { + int key = rnd.nextInt(limit); + put(map, key); + } + } + + void mutateMap(NavigableMap map, int min, int max) { + int size = map.size(); + int rangeSize = max - min + 1; + + // Remove a bunch of entries directly + for (int i = 0, n = rangeSize / 2; i < n; i++) { + remove(map, min - 5 + rnd.nextInt(rangeSize + 10)); + } + + // Remove a bunch of entries with iterator + for (Iterator it = map.keySet().iterator(); it.hasNext(); ) { + if (rnd.nextBoolean()) { + bs.clear(it.next()); + it.remove(); + } + } + + // Add entries till we're back to original size + while (map.size() < size) { + int key = min + rnd.nextInt(rangeSize); + assertTrue(key >= min && key <= max); + put(map, key); + } + } + + void mutateSubMap(NavigableMap map, int min, int max) { + int size = map.size(); + int rangeSize = max - min + 1; + + // Remove a bunch of entries directly + for (int i = 0, n = rangeSize / 2; i < n; i++) { + remove(map, min - 5 + rnd.nextInt(rangeSize + 10)); + } + + // Remove a bunch of entries with iterator + for (Iterator it = map.keySet().iterator(); it.hasNext(); ) { + if (rnd.nextBoolean()) { + bs.clear(it.next()); + it.remove(); + } + } + + // 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) { + put(map, key); + } else { + try { + map.put(key, 2 * key); + shouldThrow(); + } catch (IllegalArgumentException success) {} + } + } + } + + void put(NavigableMap map, int key) { + if (map.put(key, 2 * key) == null) + bs.set(key); + } + + void remove(NavigableMap map, int key) { + if (map.remove(key) != null) + bs.clear(key); + } + + void bashSubMap(NavigableMap map, + int min, int max, boolean ascending) { + check(map, min, max, ascending); + check(map.descendingMap(), min, max, !ascending); + + mutateSubMap(map, min, max); + check(map, min, max, ascending); + check(map.descendingMap(), min, max, !ascending); + + // Recurse + if (max - min < 2) + return; + int midPoint = (min + max) / 2; + + // headMap - pick direction and endpoint inclusion randomly + boolean incl = rnd.nextBoolean(); + NavigableMap hm = map.headMap(midPoint, incl); + if (ascending) { + if (rnd.nextBoolean()) + bashSubMap(hm, min, midPoint - (incl ? 0 : 1), true); + else + bashSubMap(hm.descendingMap(), min, midPoint - (incl ? 0 : 1), + false); + } else { + if (rnd.nextBoolean()) + bashSubMap(hm, midPoint + (incl ? 0 : 1), max, false); + else + bashSubMap(hm.descendingMap(), midPoint + (incl ? 0 : 1), max, + true); + } + + // tailMap - pick direction and endpoint inclusion randomly + incl = rnd.nextBoolean(); + NavigableMap tm = map.tailMap(midPoint,incl); + if (ascending) { + if (rnd.nextBoolean()) + bashSubMap(tm, midPoint + (incl ? 0 : 1), max, true); + else + bashSubMap(tm.descendingMap(), midPoint + (incl ? 0 : 1), max, + false); + } else { + if (rnd.nextBoolean()) { + bashSubMap(tm, min, midPoint - (incl ? 0 : 1), false); + } else { + bashSubMap(tm.descendingMap(), min, midPoint - (incl ? 0 : 1), + true); + } + } + + // subMap - pick direction and endpoint inclusion randomly + int rangeSize = max - min + 1; + int[] endpoints = new int[2]; + endpoints[0] = min + rnd.nextInt(rangeSize); + endpoints[1] = min + rnd.nextInt(rangeSize); + Arrays.sort(endpoints); + boolean lowIncl = rnd.nextBoolean(); + boolean highIncl = rnd.nextBoolean(); + if (ascending) { + NavigableMap sm = map.subMap( + endpoints[0], lowIncl, endpoints[1], highIncl); + if (rnd.nextBoolean()) + bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1), + endpoints[1] - (highIncl ? 0 : 1), true); + else + bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1), + endpoints[1] - (highIncl ? 0 : 1), false); + } else { + NavigableMap sm = map.subMap( + endpoints[1], highIncl, endpoints[0], lowIncl); + if (rnd.nextBoolean()) + bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1), + endpoints[1] - (highIncl ? 0 : 1), false); + else + bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1), + endpoints[1] - (highIncl ? 0 : 1), true); + } + } + + /** + * min and max are both inclusive. If max < min, interval is empty. + */ + void check(NavigableMap map, + final int min, final int max, final boolean ascending) { + class ReferenceSet { + int lower(int key) { + return ascending ? lowerAscending(key) : higherAscending(key); + } + int floor(int key) { + return ascending ? floorAscending(key) : ceilingAscending(key); + } + int ceiling(int key) { + return ascending ? ceilingAscending(key) : floorAscending(key); + } + int higher(int key) { + return ascending ? higherAscending(key) : lowerAscending(key); + } + int first() { + return ascending ? firstAscending() : lastAscending(); + } + int last() { + return ascending ? lastAscending() : firstAscending(); + } + int lowerAscending(int key) { + return floorAscending(key - 1); + } + int floorAscending(int key) { + if (key < min) + return -1; + else if (key > max) + key = max; + + // BitSet should support this! Test would run much faster + while (key >= min) { + if (bs.get(key)) + return key; + key--; + } + return -1; + } + int ceilingAscending(int key) { + if (key < min) + key = min; + else if (key > max) + return -1; + int result = bs.nextSetBit(key); + return result > max ? -1 : result; + } + int higherAscending(int key) { + return ceilingAscending(key + 1); + } + private int firstAscending() { + int result = ceilingAscending(min); + return result > max ? -1 : result; + } + private int lastAscending() { + int result = floorAscending(max); + return result < min ? -1 : result; + } + } + ReferenceSet rs = new ReferenceSet(); + + // Test contents using containsKey + int size = 0; + for (int i = min; i <= max; i++) { + boolean bsContainsI = bs.get(i); + assertEquals(bsContainsI, map.containsKey(i)); + if (bsContainsI) + size++; + } + assertEquals(size, map.size()); + + // Test contents using contains keySet iterator + int size2 = 0; + int previousKey = -1; + for (int key : map.keySet()) { + assertTrue(bs.get(key)); + size2++; + assertTrue(previousKey < 0 || + (ascending ? key - previousKey > 0 : key - previousKey < 0)); + previousKey = key; + } + assertEquals(size2, size); + + // Test navigation ops + for (int key = min - 1; key <= max + 1; key++) { + assertEq(map.lowerKey(key), rs.lower(key)); + assertEq(map.floorKey(key), rs.floor(key)); + assertEq(map.higherKey(key), rs.higher(key)); + assertEq(map.ceilingKey(key), rs.ceiling(key)); + } + + // Test extrema + if (map.size() != 0) { + assertEq(map.firstKey(), rs.first()); + assertEq(map.lastKey(), rs.last()); + } else { + assertEq(rs.first(), -1); + assertEq(rs.last(), -1); + try { + map.firstKey(); + shouldThrow(); + } catch (NoSuchElementException success) {} + try { + map.lastKey(); + shouldThrow(); + } catch (NoSuchElementException success) {} + } + } + + static void assertEq(Integer i, int j) { + if (i == null) + assertEquals(j, -1); + else + assertEquals((int) i, j); + } + + static boolean eq(Integer i, int j) { + return (i == null) ? j == -1 : i == j; + } + }