--- jsr166/src/test/tck/ConcurrentHashMapTest.java 2005/05/20 14:31:06 1.12 +++ jsr166/src/test/tck/ConcurrentHashMapTest.java 2010/08/25 00:07:03 1.21 @@ -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.*; @@ -12,28 +12,28 @@ import java.util.concurrent.*; import java.util.Enumeration; import java.io.*; -public class ConcurrentHashMapTest extends JSR166TestCase{ +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); + return new TestSuite(ConcurrentHashMapTest.class); } /** * Create a map from Integers 1-5 to Strings "A"-"E". */ - private static ConcurrentHashMap map5() { - ConcurrentHashMap map = new ConcurrentHashMap(5); + private static ConcurrentHashMap map5() { + ConcurrentHashMap map = new ConcurrentHashMap(5); assertTrue(map.isEmpty()); - map.put(one, "A"); - map.put(two, "B"); - map.put(three, "C"); - map.put(four, "D"); - map.put(five, "E"); + map.put(one, "A"); + map.put(two, "B"); + map.put(three, "C"); + map.put(four, "D"); + map.put(five, "E"); assertFalse(map.isEmpty()); assertEquals(5, map.size()); - return map; + return map; } /** @@ -41,8 +41,8 @@ public class ConcurrentHashMapTest exten */ public void testClear() { ConcurrentHashMap map = map5(); - map.clear(); - assertEquals(map.size(), 0); + map.clear(); + assertEquals(map.size(), 0); } /** @@ -53,7 +53,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap map2 = map5(); assertEquals(map1, map2); assertEquals(map2, map1); - map1.clear(); + map1.clear(); assertFalse(map1.equals(map2)); assertFalse(map2.equals(map1)); } @@ -63,16 +63,16 @@ public class ConcurrentHashMapTest exten */ public void testContains() { ConcurrentHashMap map = map5(); - assertTrue(map.contains("A")); + assertTrue(map.contains("A")); assertFalse(map.contains("Z")); } - + /** * containsKey returns true for contained key */ public void testContainsKey() { ConcurrentHashMap map = map5(); - assertTrue(map.containsKey(one)); + assertTrue(map.containsKey(one)); assertFalse(map.containsKey(zero)); } @@ -81,7 +81,7 @@ public class ConcurrentHashMapTest exten */ public void testContainsValue() { ConcurrentHashMap map = map5(); - assertTrue(map.containsValue("A")); + assertTrue(map.containsValue("A")); assertFalse(map.containsValue("Z")); } @@ -91,13 +91,13 @@ public class ConcurrentHashMapTest exten */ public void testEnumeration() { ConcurrentHashMap map = map5(); - Enumeration e = map.elements(); - int count = 0; - while(e.hasMoreElements()){ - count++; - e.nextElement(); - } - assertEquals(5, count); + Enumeration e = map.elements(); + int count = 0; + while (e.hasMoreElements()) { + count++; + e.nextElement(); + } + assertEquals(5, count); } /** @@ -106,7 +106,7 @@ public class ConcurrentHashMapTest exten */ public void testGet() { ConcurrentHashMap map = map5(); - assertEquals("A", (String)map.get(one)); + assertEquals("A", (String)map.get(one)); ConcurrentHashMap empty = new ConcurrentHashMap(); assertNull(map.get("anything")); } @@ -117,7 +117,7 @@ public class ConcurrentHashMapTest exten public void testIsEmpty() { ConcurrentHashMap empty = new ConcurrentHashMap(); ConcurrentHashMap map = map5(); - assertTrue(empty.isEmpty()); + assertTrue(empty.isEmpty()); assertFalse(map.isEmpty()); } @@ -126,13 +126,13 @@ public class ConcurrentHashMapTest exten */ public void testKeys() { ConcurrentHashMap map = map5(); - Enumeration e = map.keys(); - int count = 0; - while(e.hasMoreElements()){ - count++; - e.nextElement(); - } - assertEquals(5, count); + Enumeration e = map.keys(); + int count = 0; + while (e.hasMoreElements()) { + count++; + e.nextElement(); + } + assertEquals(5, count); } /** @@ -140,13 +140,56 @@ public class ConcurrentHashMapTest exten */ public void testKeySet() { ConcurrentHashMap 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.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())); + } } /** @@ -154,13 +197,13 @@ public class ConcurrentHashMapTest exten */ public void testValues() { ConcurrentHashMap 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")); } /** @@ -168,12 +211,12 @@ public class ConcurrentHashMapTest exten */ public void testEntrySet() { ConcurrentHashMap 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")) || @@ -188,13 +231,13 @@ public class ConcurrentHashMapTest exten public void testPutAll() { ConcurrentHashMap empty = new ConcurrentHashMap(); ConcurrentHashMap 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)); } /** @@ -202,7 +245,7 @@ public class ConcurrentHashMapTest exten */ public void testPutIfAbsent() { ConcurrentHashMap map = map5(); - map.putIfAbsent(six, "Z"); + map.putIfAbsent(six, "Z"); assertTrue(map.containsKey(six)); } @@ -219,7 +262,7 @@ public class ConcurrentHashMapTest exten */ public void testReplace() { ConcurrentHashMap map = map5(); - assertNull(map.replace(six, "Z")); + assertNull(map.replace(six, "Z")); assertFalse(map.containsKey(six)); } @@ -239,7 +282,7 @@ public class ConcurrentHashMapTest exten public void testReplaceValue() { ConcurrentHashMap map = map5(); assertEquals("A", map.get(one)); - assertFalse(map.replace(one, "Z", "Z")); + assertFalse(map.replace(one, "Z", "Z")); assertEquals("A", map.get(one)); } @@ -249,7 +292,7 @@ public class ConcurrentHashMapTest exten public void testReplaceValue2() { ConcurrentHashMap map = map5(); assertEquals("A", map.get(one)); - assertTrue(map.replace(one, "A", "Z")); + assertTrue(map.replace(one, "A", "Z")); assertEquals("Z", map.get(one)); } @@ -259,9 +302,9 @@ public class ConcurrentHashMapTest exten */ public void testRemove() { ConcurrentHashMap map = map5(); - map.remove(five); - assertEquals(4, map.size()); - assertFalse(map.containsKey(five)); + map.remove(five); + assertEquals(4, map.size()); + assertFalse(map.containsKey(five)); } /** @@ -269,13 +312,12 @@ public class ConcurrentHashMapTest exten */ public void testRemove2() { ConcurrentHashMap map = map5(); - 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)); } /** @@ -284,8 +326,8 @@ public class ConcurrentHashMapTest exten public void testSize() { ConcurrentHashMap map = map5(); ConcurrentHashMap empty = new ConcurrentHashMap(); - assertEquals(0, empty.size()); - assertEquals(5, map.size()); + assertEquals(0, empty.size()); + assertEquals(5, map.size()); } /** @@ -297,18 +339,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 success) {} } /** @@ -318,7 +360,7 @@ public class ConcurrentHashMapTest exten try { new ConcurrentHashMap(1,0,-1); shouldThrow(); - } catch(IllegalArgumentException e){} + } catch (IllegalArgumentException success) {} } /** @@ -328,7 +370,7 @@ public class ConcurrentHashMapTest exten try { new ConcurrentHashMap(-1); shouldThrow(); - } catch(IllegalArgumentException e){} + } catch (IllegalArgumentException success) {} } /** @@ -339,7 +381,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.get(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -350,7 +392,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.containsKey(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -361,7 +403,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.containsValue(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -372,7 +414,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.contains(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -383,7 +425,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.put(null, "whatever"); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -394,7 +436,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.put("whatever", null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -405,7 +447,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.putIfAbsent(null, "whatever"); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -416,7 +458,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace(null, "whatever"); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -427,7 +469,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace(null, one, "whatever"); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -438,7 +480,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.putIfAbsent("whatever", null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } @@ -450,7 +492,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -461,7 +503,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", null, "A"); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -472,7 +514,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", one, null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } @@ -485,7 +527,7 @@ public class ConcurrentHashMapTest exten c.put("sadsdf", "asdads"); c.remove(null); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -497,44 +539,35 @@ public class ConcurrentHashMapTest exten c.put("sadsdf", "asdads"); c.remove(null, "whatever"); shouldThrow(); - } catch(NullPointerException e){} + } catch (NullPointerException success) {} } /** * 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(); - } + ConcurrentHashMap c = new ConcurrentHashMap(5); + c.put("sadsdf", "asdads"); + assertFalse(c.remove("sadsdf", null)); } /** * A deserialized map equals original */ - public void testSerialization() { + public void testSerialization() throws Exception { ConcurrentHashMap q = map5(); - 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)); - ConcurrentHashMap r = (ConcurrentHashMap)in.readObject(); - assertEquals(q.size(), r.size()); - assertTrue(q.equals(r)); - assertTrue(r.equals(q)); - } catch(Exception e){ - e.printStackTrace(); - unexpectedException(); - } + 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)); + ConcurrentHashMap r = (ConcurrentHashMap)in.readObject(); + assertEquals(q.size(), r.size()); + assertTrue(q.equals(r)); + assertTrue(r.equals(q)); } @@ -542,23 +575,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 } - + }