--- jsr166/src/test/tck/ConcurrentHashMapTest.java 2003/12/27 19:26:43 1.9 +++ jsr166/src/test/tck/ConcurrentHashMapTest.java 2009/11/02 20:28:31 1.14 @@ -2,8 +2,8 @@ * 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 - * Other contributors include Andrew Wright, Jeffrey Hayes, - * Pat Fisher, Mike Judd. + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ import junit.framework.*; @@ -14,7 +14,7 @@ import java.io.*; public class ConcurrentHashMapTest 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(ConcurrentHashMapTest.class); @@ -23,7 +23,7 @@ public class ConcurrentHashMapTest exten /** * Create a map from Integers 1-5 to Strings "A"-"E". */ - private static ConcurrentHashMap map5() { + private static ConcurrentHashMap map5() { ConcurrentHashMap map = new ConcurrentHashMap(5); assertTrue(map.isEmpty()); map.put(one, "A"); @@ -66,7 +66,7 @@ public class ConcurrentHashMapTest exten assertTrue(map.contains("A")); assertFalse(map.contains("Z")); } - + /** * containsKey returns true for contained key */ @@ -81,8 +81,8 @@ public class ConcurrentHashMapTest exten */ public void testContainsValue() { ConcurrentHashMap map = map5(); - assertTrue(map.contains("A")); - assertFalse(map.contains("Z")); + assertTrue(map.containsValue("A")); + assertFalse(map.containsValue("Z")); } /** @@ -101,15 +101,6 @@ public class ConcurrentHashMapTest exten } /** - * Clone creates an equal map - */ - public void testClone() { - ConcurrentHashMap map = map5(); - ConcurrentHashMap m2 = (ConcurrentHashMap)(map.clone()); - assertEquals(map, m2); - } - - /** * get returns the correct element at the given key, * or null if not present */ @@ -159,6 +150,49 @@ public class ConcurrentHashMapTest exten } /** + * keySet.toArray returns contains all keys + */ + public void testKeySetToArray() { + ConcurrentHashMap map = map5(); + Set s = map.keySet(); + Object[] ar = s.toArray(); + assertTrue(s.containsAll(Arrays.asList(ar))); + assertEquals(5, ar.length); + ar[0] = m10; + assertFalse(s.containsAll(Arrays.asList(ar))); + } + + /** + * Values.toArray contains all values + */ + public void testValuesToArray() { + ConcurrentHashMap 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")); + } + + /** + * entrySet.toArray contains all entries + */ + public void testEntrySetToArray() { + ConcurrentHashMap map = map5(); + Set s = map.entrySet(); + Object[] ar = s.toArray(); + assertEquals(5, ar.length); + for (int i = 0; i < 5; ++i) { + assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); + assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); + } + } + + /** * values collection contains all values */ public void testValues() { @@ -182,7 +216,7 @@ public class ConcurrentHashMapTest exten 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")) || @@ -306,12 +340,12 @@ public class ConcurrentHashMapTest exten for (int i = 1; i <= 5; ++i) { assertTrue(s.indexOf(String.valueOf(i)) >= 0); } - } + } // Exception tests - + /** - * Cannot create with negative capacity + * Cannot create with negative capacity */ public void testConstructor1() { try { @@ -510,6 +544,19 @@ public class ConcurrentHashMapTest exten } /** + * remove(x, null) returns false + */ + public void testRemove3() { + try { + ConcurrentHashMap c = new ConcurrentHashMap(5); + c.put("sadsdf", "asdads"); + assertFalse(c.remove("sadsdf", null)); + } catch(NullPointerException e){ + fail(); + } + } + + /** * A deserialized map equals original */ public void testSerialization() { @@ -538,23 +585,23 @@ public class ConcurrentHashMapTest exten * SetValue of an EntrySet entry sets value in the map. */ public void testSetValueWriteThrough() { - // Adapted from a bug report by Eric Zoerner + // Adapted from a bug report by Eric Zoerner ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1); assertTrue(map.isEmpty()); for (int i = 0; i < 20; i++) map.put(new Integer(i), new Integer(i)); assertFalse(map.isEmpty()); Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); - + // assert that entry1 is not 16 assertTrue("entry is 16, test not valid", !entry1.getKey().equals(new Integer(16))); - - // remove 16 (a different key) from map + + // remove 16 (a different key) from map // which just happens to cause entry1 to be cloned in map map.remove(new Integer(16)); entry1.setValue("XYZ"); assertTrue(map.containsValue("XYZ")); // fails } - + }