--- jsr166/src/test/tck/ConcurrentHashMapTest.java 2013/07/20 16:53:40 1.35 +++ jsr166/src/test/tck/ConcurrentHashMapTest.java 2013/07/21 22:24:18 1.36 @@ -21,8 +21,8 @@ public class ConcurrentHashMapTest exten /** * Returns a new 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"); @@ -212,6 +212,18 @@ public class ConcurrentHashMapTest exten assertFalse(map2.equals(map1)); } + + /** + * hashCode() equals sum of each key.hashCode ^ value.hashCode + */ + public void testHashCode() { + ConcurrentHashMap map = map5(); + int sum = 0; + for (Map.Entry e : map.entrySet()) + sum += e.getKey().hashCode() ^ e.getValue().hashCode(); + assertEquals(sum, map.hashCode()); + } + /** * contains returns true for contained value */ @@ -496,35 +508,76 @@ public class ConcurrentHashMapTest exten // Exception tests /** - * Cannot create with negative capacity + * Cannot create with only negative capacity */ public void testConstructor1() { try { - new ConcurrentHashMap(-1,0,1); + new ConcurrentHashMap(-1); shouldThrow(); } catch (IllegalArgumentException success) {} } /** - * Cannot create with negative concurrency level - */ + * Constructor (initialCapacity, loadFactor) throws + * IllegalArgumentException if either argument is negative + */ public void testConstructor2() { try { - new ConcurrentHashMap(1,0,-1); + new ConcurrentHashMap(-1, .75f); shouldThrow(); - } catch (IllegalArgumentException success) {} + } catch (IllegalArgumentException e) {} + + try { + new ConcurrentHashMap(16, -1); + shouldThrow(); + } catch (IllegalArgumentException e) {} } + + /** + * Constructor (initialCapacity, loadFactor, concurrencyLevel) + * throws IllegalArgumentException if any argument is negative + */ + public void testConstructor3() { + try { + new ConcurrentHashMap(-1, .75f, 1); + shouldThrow(); + } catch (IllegalArgumentException e) {} + + try { + new ConcurrentHashMap(16, -1, 1); + shouldThrow(); + } catch (IllegalArgumentException e) {} + + try { + new ConcurrentHashMap(16, .75f, -1); + shouldThrow(); + } catch (IllegalArgumentException e) {} + } /** - * Cannot create with only negative capacity + * ConcurrentHashMap(map) throws NullPointerException if the given + * map is null */ - public void testConstructor3() { + public void testConstructor4() { try { - new ConcurrentHashMap(-1); + new ConcurrentHashMap(null); shouldThrow(); - } catch (IllegalArgumentException success) {} + } catch (NullPointerException e) {} + } + + /** + * ConcurrentHashMap(map) creates a new map with the same mappings + * as the given map + */ + public void testConstructor5() { + ConcurrentHashMap map1 = map5(); + ConcurrentHashMap map2 = new ConcurrentHashMap(map5()); + assertTrue(map2.equals(map1)); + map2.put(one, "F"); + assertFalse(map2.equals(map1)); } + /** * get(null) throws NPE */