--- jsr166/src/test/tck/ConcurrentHashMapTest.java 2004/07/02 10:26:28 1.11 +++ jsr166/src/test/tck/ConcurrentHashMapTest.java 2009/11/16 04:57:10 1.15 @@ -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 */ @@ -93,7 +93,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap map = map5(); Enumeration e = map.elements(); int count = 0; - while(e.hasMoreElements()){ + while (e.hasMoreElements()){ count++; e.nextElement(); } @@ -128,7 +128,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap map = map5(); Enumeration e = map.keys(); int count = 0; - while(e.hasMoreElements()){ + while (e.hasMoreElements()){ count++; e.nextElement(); } @@ -150,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() { @@ -173,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")) || @@ -297,18 +340,18 @@ 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 { new ConcurrentHashMap(-1,0,1); shouldThrow(); - } catch(IllegalArgumentException e){} + } catch (IllegalArgumentException e){} } /** @@ -318,7 +361,7 @@ public class ConcurrentHashMapTest exten try { new ConcurrentHashMap(1,0,-1); shouldThrow(); - } catch(IllegalArgumentException e){} + } catch (IllegalArgumentException e){} } /** @@ -328,7 +371,7 @@ public class ConcurrentHashMapTest exten try { new ConcurrentHashMap(-1); shouldThrow(); - } catch(IllegalArgumentException e){} + } catch (IllegalArgumentException e){} } /** @@ -339,7 +382,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.get(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } /** @@ -350,7 +393,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.containsKey(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } /** @@ -361,7 +404,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.containsValue(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } /** @@ -372,7 +415,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.contains(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } /** @@ -383,7 +426,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.put(null, "whatever"); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } /** @@ -394,7 +437,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.put("whatever", null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } /** @@ -405,7 +448,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.putIfAbsent(null, "whatever"); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } /** @@ -416,7 +459,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace(null, "whatever"); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } /** @@ -427,7 +470,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace(null, one, "whatever"); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } /** @@ -438,7 +481,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.putIfAbsent("whatever", null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } @@ -450,7 +493,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } /** @@ -461,7 +504,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", null, "A"); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } /** @@ -472,7 +515,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", one, null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } @@ -485,7 +528,7 @@ public class ConcurrentHashMapTest exten c.put("sadsdf", "asdads"); c.remove(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} } /** @@ -497,7 +540,20 @@ public class ConcurrentHashMapTest exten c.put("sadsdf", "asdads"); c.remove(null, "whatever"); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException e){} + } + + /** + * 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(); + } } /** @@ -518,7 +574,7 @@ public class ConcurrentHashMapTest exten assertEquals(q.size(), r.size()); assertTrue(q.equals(r)); assertTrue(r.equals(q)); - } catch(Exception e){ + } catch (Exception e){ e.printStackTrace(); unexpectedException(); } @@ -529,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 } - + }