--- jsr166/src/test/tck/ConcurrentHashMapTest.java 2009/11/21 02:07:26 1.17 +++ jsr166/src/test/tck/ConcurrentHashMapTest.java 2013/06/12 18:09:00 1.32 @@ -1,27 +1,25 @@ /* * 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/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ import junit.framework.*; import java.util.*; -import java.util.concurrent.*; -import java.util.Enumeration; -import java.io.*; +import java.util.concurrent.ConcurrentHashMap; 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); } /** - * 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 ConcurrentHashMap map5() { ConcurrentHashMap map = new ConcurrentHashMap(5); @@ -36,17 +34,119 @@ public class ConcurrentHashMapTest exten return map; } + // classes for testing Comparable fallbacks + static class BI implements Comparable { + private final int value; + BI(int value) { this.value = value; } + public int compareTo(BI other) { + return Integer.compare(value, other.value); + } + public boolean equals(Object x) { + return (x instanceof BI) && ((BI)x).value == value; + } + public int hashCode() { return 42; } + } + static class CI extends BI { CI(int value) { super(value); } } + static class DI extends BI { DI(int value) { super(value); } } + + static class BS implements Comparable { + private final String value; + BS(String value) { this.value = value; } + public int compareTo(BS other) { + return value.compareTo(other.value); + } + public boolean equals(Object x) { + return (x instanceof BS) && value.equals(((BS)x).value); + } + public int hashCode() { return 42; } + } + + static class LexicographicList> extends ArrayList + implements Comparable> { + LexicographicList(Collection c) { super(c); } + LexicographicList(E e) { super(Collections.singleton(e)); } + public int compareTo(LexicographicList other) { + int common = Math.min(size(), other.size()); + int r = 0; + for (int i = 0; i < common; i++) { + if ((r = get(i).compareTo(other.get(i))) != 0) + break; + } + if (r == 0) + r = Integer.compare(size(), other.size()); + return r; + } + private static final long serialVersionUID = 0; + } + + /** + * Inserted elements that are subclasses of the same Comparable + * class are found. + */ + public void testComparableFamily() { + ConcurrentHashMap m = + new ConcurrentHashMap(); + for (int i = 0; i < 1000; i++) { + assertTrue(m.put(new CI(i), true) == null); + } + for (int i = 0; i < 1000; i++) { + assertTrue(m.containsKey(new CI(i))); + assertTrue(m.containsKey(new DI(i))); + } + } + + /** + * Elements of classes with erased generic type parameters based + * on Comparable can be inserted and found. + */ + public void testGenericComparable() { + ConcurrentHashMap m = + new ConcurrentHashMap(); + for (int i = 0; i < 1000; i++) { + BI bi = new BI(i); + BS bs = new BS(String.valueOf(i)); + LexicographicList bis = new LexicographicList(bi); + LexicographicList bss = new LexicographicList(bs); + assertTrue(m.putIfAbsent(bis, true) == null); + assertTrue(m.containsKey(bis)); + if (m.putIfAbsent(bss, true) == null) + assertTrue(m.containsKey(bss)); + assertTrue(m.containsKey(bis)); + } + for (int i = 0; i < 1000; i++) { + assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); + } + } + /** - * clear removes all pairs + * Elements of non-comparable classes equal to those of classes + * with erased generic type parameters based on Comparable can be + * inserted and found. + */ + public void testGenericComparable2() { + ConcurrentHashMap m = + new ConcurrentHashMap(); + for (int i = 0; i < 1000; i++) { + m.put(new ArrayList(Collections.singleton(new BI(i))), true); + } + + for (int i = 0; i < 1000; i++) { + LexicographicList bis = new LexicographicList(new BI(i)); + assertTrue(m.containsKey(bis)); + } + } + + /** + * clear removes all pairs */ public void testClear() { ConcurrentHashMap map = map5(); map.clear(); - assertEquals(map.size(), 0); + assertEquals(0, map.size()); } /** - * Maps with same contents are equal + * Maps with same contents are equal */ public void testEquals() { ConcurrentHashMap map1 = map5(); @@ -59,7 +159,7 @@ public class ConcurrentHashMapTest exten } /** - * contains returns true for contained value + * contains returns true for contained value */ public void testContains() { ConcurrentHashMap map = map5(); @@ -68,7 +168,7 @@ public class ConcurrentHashMapTest exten } /** - * containsKey returns true for contained key + * containsKey returns true for contained key */ public void testContainsKey() { ConcurrentHashMap map = map5(); @@ -77,7 +177,7 @@ public class ConcurrentHashMapTest exten } /** - * containsValue returns true for held values + * containsValue returns true for held values */ public void testContainsValue() { ConcurrentHashMap map = map5(); @@ -86,8 +186,8 @@ public class ConcurrentHashMapTest exten } /** - * enumeration returns an enumeration containing the correct - * elements + * enumeration returns an enumeration containing the correct + * elements */ public void testEnumeration() { ConcurrentHashMap map = map5(); @@ -101,8 +201,8 @@ public class ConcurrentHashMapTest exten } /** - * 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() { ConcurrentHashMap map = map5(); @@ -112,7 +212,7 @@ public class ConcurrentHashMapTest exten } /** - * 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() { ConcurrentHashMap empty = new ConcurrentHashMap(); @@ -122,7 +222,7 @@ public class ConcurrentHashMapTest exten } /** - * keys returns an enumeration containing all the keys from the map + * keys returns an enumeration containing all the keys from the map */ public void testKeys() { ConcurrentHashMap map = map5(); @@ -136,7 +236,7 @@ public class ConcurrentHashMapTest exten } /** - * keySet returns a Set containing all the keys + * keySet returns a Set containing all the keys */ public void testKeySet() { ConcurrentHashMap map = map5(); @@ -150,7 +250,7 @@ public class ConcurrentHashMapTest exten } /** - * keySet.toArray returns contains all keys + * keySet.toArray returns contains all keys */ public void testKeySetToArray() { ConcurrentHashMap map = map5(); @@ -163,7 +263,7 @@ public class ConcurrentHashMapTest exten } /** - * Values.toArray contains all values + * Values.toArray contains all values */ public void testValuesToArray() { ConcurrentHashMap map = map5(); @@ -179,7 +279,7 @@ public class ConcurrentHashMapTest exten } /** - * entrySet.toArray contains all entries + * entrySet.toArray contains all entries */ public void testEntrySetToArray() { ConcurrentHashMap map = map5(); @@ -226,7 +326,7 @@ public class ConcurrentHashMapTest exten } /** - * putAll adds all key-value pairs from the given map + * putAll adds all key-value pairs from the given map */ public void testPutAll() { ConcurrentHashMap empty = new ConcurrentHashMap(); @@ -241,7 +341,7 @@ public class ConcurrentHashMapTest exten } /** - * putIfAbsent works when the given key is not present + * putIfAbsent works when the given key is not present */ public void testPutIfAbsent() { ConcurrentHashMap map = map5(); @@ -250,7 +350,7 @@ public class ConcurrentHashMapTest exten } /** - * 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() { ConcurrentHashMap map = map5(); @@ -258,7 +358,7 @@ public class ConcurrentHashMapTest exten } /** - * replace fails when the given key is not present + * replace fails when the given key is not present */ public void testReplace() { ConcurrentHashMap map = map5(); @@ -267,7 +367,7 @@ public class ConcurrentHashMapTest exten } /** - * replace succeeds if the key is already present + * replace succeeds if the key is already present */ public void testReplace2() { ConcurrentHashMap map = map5(); @@ -275,7 +375,6 @@ public class ConcurrentHashMapTest exten assertEquals("Z", map.get(one)); } - /** * replace value fails when the given key not mapped to expected value */ @@ -296,9 +395,8 @@ public class ConcurrentHashMapTest exten 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() { ConcurrentHashMap map = map5(); @@ -318,11 +416,10 @@ public class ConcurrentHashMapTest exten map.remove(four, "A"); assertEquals(4, map.size()); assertTrue(map.containsKey(four)); - } /** - * size returns the correct values + * size returns the correct values */ public void testSize() { ConcurrentHashMap map = map5(); @@ -338,7 +435,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap 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))); } } @@ -351,7 +448,7 @@ public class ConcurrentHashMapTest exten try { new ConcurrentHashMap(-1,0,1); shouldThrow(); - } catch (IllegalArgumentException e) {} + } catch (IllegalArgumentException success) {} } /** @@ -361,7 +458,7 @@ public class ConcurrentHashMapTest exten try { new ConcurrentHashMap(1,0,-1); shouldThrow(); - } catch (IllegalArgumentException e) {} + } catch (IllegalArgumentException success) {} } /** @@ -371,7 +468,7 @@ public class ConcurrentHashMapTest exten try { new ConcurrentHashMap(-1); shouldThrow(); - } catch (IllegalArgumentException e) {} + } catch (IllegalArgumentException success) {} } /** @@ -382,7 +479,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.get(null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** @@ -393,7 +490,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.containsKey(null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** @@ -404,7 +501,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.containsValue(null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** @@ -415,7 +512,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.contains(null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** @@ -426,7 +523,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.put(null, "whatever"); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** @@ -437,7 +534,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.put("whatever", null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** @@ -448,7 +545,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.putIfAbsent(null, "whatever"); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** @@ -459,7 +556,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace(null, "whatever"); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** @@ -470,7 +567,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace(null, one, "whatever"); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** @@ -481,10 +578,9 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.putIfAbsent("whatever", null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } - /** * replace(x, null) throws NPE */ @@ -493,7 +589,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** @@ -504,7 +600,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", null, "A"); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** @@ -515,10 +611,9 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", one, null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } - /** * remove(null) throws NPE */ @@ -528,7 +623,7 @@ public class ConcurrentHashMapTest exten c.put("sadsdf", "asdads"); c.remove(null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** @@ -540,47 +635,31 @@ 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() { - 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(); - } + public void testSerialization() throws Exception { + Map x = map5(); + Map y = serialClone(x); + + assertNotSame(x, y); + assertEquals(x.size(), y.size()); + assertEquals(x, y); + assertEquals(y, x); } - /** * SetValue of an EntrySet entry sets value in the map. */ @@ -592,16 +671,15 @@ public class ConcurrentHashMapTest exten 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 - // which just happens to cause entry1 to be cloned in map - map.remove(new Integer(16)); - entry1.setValue("XYZ"); - assertTrue(map.containsValue("XYZ")); // fails + // Unless it happens to be first (in which case remainder of + // test is skipped), remove a possibly-colliding key from map + // which, under some implementations, may cause entry1 to be + // cloned in map + if (!entry1.getKey().equals(new Integer(16))) { + map.remove(new Integer(16)); + entry1.setValue("XYZ"); + assertTrue(map.containsValue("XYZ")); // fails if write-through broken + } } }