test/java/util/Map/BasicSerialization.java
Print this page
rev 6813 : 7143928: Optimize empty HashMap and ArrayList
Reviewed-by: mduigou
Contributed-by: Sergey Linetskiy <sergey.linetskiy@oracle.com>, John Rose <john.rose@oracle.com>, Mike Duigou <mike.duigou@oracle.com>
@@ -21,405 +21,201 @@
* questions.
*/
/*
* @test
- * @bug 7126277
- * @run main Collisions -shortrun
- * @run main/othervm -Djdk.map.althashing.threshold=0 Collisions -shortrun
- * @summary Ensure Maps behave well with lots of hashCode() collisions.
+ * @bug 7143928
+ * @run testng BasicSerialization
+ * @summary Ensure Maps can be serialized and deserialized.
* @author Mike Duigou
*/
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ByteArrayInputStream;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
-public class Collisions {
-
+import org.testng.annotations.Test;
+import org.testng.annotations.DataProvider;
+import static org.testng.Assert.fail;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertSame;
+
+public class BasicSerialization {
+
+ enum IntegerEnum {
+
+ e0, e1, e2, e3, e4, e5, e6, e7, e8, e9,
+ e10, e11, e12, e13, e14, e15, e16, e17, e18, e19,
+ e20, e21, e22, e23, e24, e25, e26, e27, e28, e29,
+ e30, e31, e32, e33, e34, e35, e36, e37, e38, e39,
+ e40, e41, e42, e43, e44, e45, e46, e47, e48, e49,
+ e50, e51, e52, e53, e54, e55, e56, e57, e58, e59,
+ e60, e61, e62, e63, e64, e65, e66, e67, e68, e69,
+ e70, e71, e72, e73, e74, e75, e76, e77, e78, e79,
+ e80, e81, e82, e83, e84, e85, e86, e87, e88, e89,
+ e90, e91, e92, e93, e94, e95, e96, e97, e98, e99,
+ EXTRA_KEY;
+ public static final int SIZE = values().length;
+ };
+ private static final int TEST_SIZE = IntegerEnum.SIZE - 1;
/**
- * Number of elements per map.
+ * Realized keys ensure that there is always a hard ref to all test objects.
*/
- private static final int TEST_SIZE = 5000;
-
- final static class HashableInteger implements Comparable<HashableInteger> {
-
- final int value;
- final int hashmask; //yes duplication
-
- HashableInteger(int value, int hashmask) {
- this.value = value;
- this.hashmask = hashmask;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (obj instanceof HashableInteger) {
- HashableInteger other = (HashableInteger) obj;
-
- return other.value == value;
- }
-
- return false;
- }
-
- @Override
- public int hashCode() {
- return value % hashmask;
- }
-
- @Override
- public int compareTo(HashableInteger o) {
- return value - o.value;
- }
-
- @Override
- public String toString() {
- return Integer.toString(value);
- }
- }
-
- private static Object[][] makeTestData(int size) {
- HashableInteger UNIQUE_OBJECTS[] = new HashableInteger[size];
- HashableInteger COLLIDING_OBJECTS[] = new HashableInteger[size];
- String UNIQUE_STRINGS[] = new String[size];
- String COLLIDING_STRINGS[] = new String[size];
-
- for (int i = 0; i < size; i++) {
- UNIQUE_OBJECTS[i] = new HashableInteger(i, Integer.MAX_VALUE);
- COLLIDING_OBJECTS[i] = new HashableInteger(i, 10);
- UNIQUE_STRINGS[i] = unhash(i);
- COLLIDING_STRINGS[i] = (0 == i % 2)
- ? UNIQUE_STRINGS[i / 2]
- : "\u0000\u0000\u0000\u0000\u0000" + COLLIDING_STRINGS[i - 1];
- }
-
- return new Object[][] {
- new Object[]{"Unique Objects", UNIQUE_OBJECTS},
- new Object[]{"Colliding Objects", COLLIDING_OBJECTS},
- new Object[]{"Unique Strings", UNIQUE_STRINGS},
- new Object[]{"Colliding Strings", COLLIDING_STRINGS}
- };
- }
-
+ private static final IntegerEnum[] KEYS = new IntegerEnum[TEST_SIZE];
/**
- * Returns a string with a hash equal to the argument.
- *
- * @return string with a hash equal to the argument.
+ * Realized values ensure that there is always a hard ref to all test
+ * objects.
*/
- public static String unhash(int target) {
- StringBuilder answer = new StringBuilder();
- if (target < 0) {
- // String with hash of Integer.MIN_VALUE, 0x80000000
- answer.append("\\u0915\\u0009\\u001e\\u000c\\u0002");
+ private static final String[] VALUES = new String[TEST_SIZE];
- if (target == Integer.MIN_VALUE) {
- return answer.toString();
+ static {
+ IntegerEnum[] keys = IntegerEnum.values();
+ for (int each = 0; each < TEST_SIZE; each++) {
+ KEYS[each] = keys[each];
+ VALUES[each] = keys[each].name();
}
- // Find target without sign bit set
- target = target & Integer.MAX_VALUE;
}
+ private static final IntegerEnum EXTRA_KEY = IntegerEnum.EXTRA_KEY;
+ private static final String EXTRA_VALUE = IntegerEnum.EXTRA_KEY.name();
- unhash0(answer, target);
- return answer.toString();
- }
+ public static <K, V> Map<K, V> mapClone(Map<K, V> map) {
+ Method cloneMethod;
- private static void unhash0(StringBuilder partial, int target) {
- int div = target / 31;
- int rem = target % 31;
-
- if (div <= Character.MAX_VALUE) {
- if (div != 0) {
- partial.append((char) div);
- }
- partial.append((char) rem);
- } else {
- unhash0(partial, div);
- partial.append((char) rem);
- }
+ try {
+ cloneMethod = map.getClass().getMethod("clone", new Class[]{});
+ } catch (NoSuchMethodException | SecurityException all) {
+ cloneMethod = null;
}
- private static void realMain(String[] args) throws Throwable {
- boolean shortRun = args.length > 0 && args[0].equals("-shortrun");
-
- Object[][] mapKeys = makeTestData(shortRun ? (TEST_SIZE / 2) : TEST_SIZE);
-
- // loop through data sets
- for (Object[] keys_desc : mapKeys) {
- Map<Object, Object>[] maps = (Map<Object, Object>[]) new Map[]{
- new HashMap<>(),
- new Hashtable<>(),
- new IdentityHashMap<>(),
- new LinkedHashMap<>(),
- new TreeMap<>(),
- new WeakHashMap<>(),
- new ConcurrentHashMap<>(),
- new ConcurrentSkipListMap<>()
- };
-
- // for each map type.
- for (Map<Object, Object> map : maps) {
- String desc = (String) keys_desc[0];
- Object[] keys = (Object[]) keys_desc[1];
+ if (null != cloneMethod) {
try {
- testMap(map, desc, keys);
- } catch(Exception all) {
- unexpected("Failed for " + map.getClass().getName() + " with " + desc, all);
- }
- }
+ Map<K, V> result = (Map<K, V>)cloneMethod.invoke(map, new Object[]{});
+ return result;
+ } catch (Exception all) {
+ fail("clone() failed " + map.getClass().getSimpleName(), all);
+ return null;
}
- }
-
- private static <T> void testMap(Map<T, T> map, String keys_desc, T[] keys) {
- System.out.println(map.getClass() + " : " + keys_desc);
- System.out.flush();
- testInsertion(map, keys_desc, keys);
-
- if (keys[0] instanceof HashableInteger) {
- testIntegerIteration((Map<HashableInteger, HashableInteger>) map, (HashableInteger[]) keys);
} else {
- testStringIteration((Map<String, String>) map, (String[]) keys);
- }
-
- testContainsKey(map, keys_desc, keys);
-
- testRemove(map, keys_desc, keys);
-
- map.clear();
- testInsertion(map, keys_desc, keys);
- testKeysIteratorRemove(map, keys_desc, keys);
-
- map.clear();
- testInsertion(map, keys_desc, keys);
- testValuesIteratorRemove(map, keys_desc, keys);
-
- map.clear();
- testInsertion(map, keys_desc, keys);
- testEntriesIteratorRemove(map, keys_desc, keys);
-
- check(map.isEmpty());
- }
-
- private static <T> void testInsertion(Map<T, T> map, String keys_desc, T[] keys) {
- check("map empty", (map.size() == 0) && map.isEmpty());
-
- for (int i = 0; i < keys.length; i++) {
- check(String.format("insertion: map expected size m%d != i%d", map.size(), i),
- map.size() == i);
- check(String.format("insertion: put(%s[%d])", keys_desc, i), null == map.put(keys[i], keys[i]));
- check(String.format("insertion: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i]));
- check(String.format("insertion: containsValue(%s[%d])", keys_desc, i), map.containsValue(keys[i]));
- }
-
- check(String.format("map expected size m%d != k%d", map.size(), keys.length),
- map.size() == keys.length);
- }
-
- private static void testIntegerIteration(Map<HashableInteger, HashableInteger> map, HashableInteger[] keys) {
- check(String.format("map expected size m%d != k%d", map.size(), keys.length),
- map.size() == keys.length);
-
- BitSet all = new BitSet(keys.length);
- for (Map.Entry<HashableInteger, HashableInteger> each : map.entrySet()) {
- check("Iteration: key already seen", !all.get(each.getKey().value));
- all.set(each.getKey().value);
- }
-
- all.flip(0, keys.length);
- check("Iteration: some keys not visited", all.isEmpty());
-
- for (HashableInteger each : map.keySet()) {
- check("Iteration: key already seen", !all.get(each.value));
- all.set(each.value);
- }
-
- all.flip(0, keys.length);
- check("Iteration: some keys not visited", all.isEmpty());
-
- int count = 0;
- for (HashableInteger each : map.values()) {
- count++;
- }
-
- check(String.format("Iteration: value count matches size m%d != c%d", map.size(), count),
- map.size() == count);
- }
-
- private static void testStringIteration(Map<String, String> map, String[] keys) {
- check(String.format("map expected size m%d != k%d", map.size(), keys.length),
- map.size() == keys.length);
-
- BitSet all = new BitSet(keys.length);
- for (Map.Entry<String, String> each : map.entrySet()) {
- String key = each.getKey();
- boolean longKey = key.length() > 5;
- int index = key.hashCode() + (longKey ? keys.length / 2 : 0);
- check("key already seen", !all.get(index));
- all.set(index);
- }
-
- all.flip(0, keys.length);
- check("some keys not visited", all.isEmpty());
-
- for (String each : map.keySet()) {
- boolean longKey = each.length() > 5;
- int index = each.hashCode() + (longKey ? keys.length / 2 : 0);
- check("key already seen", !all.get(index));
- all.set(index);
- }
-
- all.flip(0, keys.length);
- check("some keys not visited", all.isEmpty());
-
- int count = 0;
- for (String each : map.values()) {
- count++;
- }
-
- check(String.format("value count matches size m%d != k%d", map.size(), keys.length),
- map.size() == keys.length);
- }
-
- private static <T> void testContainsKey(Map<T, T> map, String keys_desc, T[] keys) {
- for (int i = 0; i < keys.length; i++) {
- T each = keys[i];
- check("containsKey: " + keys_desc + "[" + i + "]" + each, map.containsKey(each));
- }
- }
-
- private static <T> void testRemove(Map<T, T> map, String keys_desc, T[] keys) {
- check(String.format("remove: map expected size m%d != k%d", map.size(), keys.length),
- map.size() == keys.length);
-
- for (int i = 0; i < keys.length; i++) {
- T each = keys[i];
- check("remove: " + keys_desc + "[" + i + "]" + each, null != map.remove(each));
- }
-
- check(String.format("remove: map empty. size=%d", map.size()),
- (map.size() == 0) && map.isEmpty());
- }
-
- private static <T> void testKeysIteratorRemove(Map<T, T> map, String keys_desc, T[] keys) {
- check(String.format("remove: map expected size m%d != k%d", map.size(), keys.length),
- map.size() == keys.length);
-
- Iterator<T> each = map.keySet().iterator();
- while (each.hasNext()) {
- T t = each.next();
- each.remove();
- check("not removed: " + each, !map.containsKey(t) );
- }
-
- check(String.format("remove: map empty. size=%d", map.size()),
- (map.size() == 0) && map.isEmpty());
- }
-
- private static <T> void testValuesIteratorRemove(Map<T, T> map, String keys_desc, T[] keys) {
- check(String.format("remove: map expected size m%d != k%d", map.size(), keys.length),
- map.size() == keys.length);
-
- Iterator<T> each = map.values().iterator();
- while (each.hasNext()) {
- T t = each.next();
- each.remove();
- check("not removed: " + each, !map.containsValue(t) );
- }
-
- check(String.format("remove: map empty. size=%d", map.size()),
- (map.size() == 0) && map.isEmpty());
- }
-
- private static <T> void testEntriesIteratorRemove(Map<T, T> map, String keys_desc, T[] keys) {
- check(String.format("remove: map expected size m%d != k%d", map.size(), keys.length),
- map.size() == keys.length);
-
- Iterator<Map.Entry<T,T>> each = map.entrySet().iterator();
- while (each.hasNext()) {
- Map.Entry<T,T> t = each.next();
- T key = t.getKey();
- T value = t.getValue();
- each.remove();
- check("not removed: " + each, (map instanceof IdentityHashMap) || !map.entrySet().contains(t) );
- check("not removed: " + each, !map.containsKey(key) );
- check("not removed: " + each, !map.containsValue(value));
- }
-
- check(String.format("remove: map empty. size=%d", map.size()),
- (map.size() == 0) && map.isEmpty());
- }
-
- //--------------------- Infrastructure ---------------------------
- static volatile int passed = 0, failed = 0;
-
- static void pass() {
- passed++;
- }
+ Constructor<? extends Map> copyConstructor;
+ try {
+ copyConstructor = (Constructor<? extends Map>)map.getClass().getConstructor(new Class[]{Map.class});
- static void fail() {
- failed++;
- (new Error("Failure")).printStackTrace(System.err);
- }
+ Map<K, V> result = (Map<K, V>)copyConstructor.newInstance(new Object[]{map});
- static void fail(String msg) {
- failed++;
- (new Error("Failure: " + msg)).printStackTrace(System.err);
+ return result;
+ } catch (Exception all) {
+ return serialClone(map);
}
-
- static void abort() {
- fail();
- System.exit(1);
}
-
- static void abort(String msg) {
- fail(msg);
- System.exit(1);
}
- static void unexpected(String msg, Throwable t) {
- System.err.println("Unexpected: " + msg);
- unexpected(t);
+ @Test(dataProvider = "Map<IntegerEnum,String>")
+ public void testSerialization(String description, Map<IntegerEnum, String> map) {
+ Object foo = new Object();
+
+ Map<IntegerEnum, String> clone = mapClone(map);
+ Map<IntegerEnum, String> serialClone = serialClone(map);
+
+ assertEquals(map, map, description + ":should equal self");
+ assertEquals(clone, map, description + ":should equal clone");
+ assertEquals(map, clone, description + ": should equal orginal map");
+ assertEquals(serialClone, map, description + ": should equal deserialized clone");
+ assertEquals(map, serialClone, description + ": should equal original map");
+ assertEquals(serialClone, clone, description + ": deserialized clone should equal clone");
+ assertEquals(clone, serialClone, description + ": clone should equal deserialized clone");
+
+ assertFalse(map.containsKey(EXTRA_KEY), description + ":unexpected key");
+ assertFalse(clone.containsKey(EXTRA_KEY), description + ":unexpected key");
+ assertFalse(serialClone.containsKey(EXTRA_KEY), description + ":unexpected key");
+ map.put(EXTRA_KEY, EXTRA_VALUE);
+ clone.put(EXTRA_KEY, EXTRA_VALUE);
+ serialClone.put(EXTRA_KEY, EXTRA_VALUE);
+ assertTrue(map.containsKey(EXTRA_KEY), description + ":missing key");
+ assertTrue(clone.containsKey(EXTRA_KEY), description + ":missing key");
+ assertTrue(serialClone.containsKey(EXTRA_KEY), description + ":missing key");
+ assertSame(map.get(EXTRA_KEY), EXTRA_VALUE, description + ":wrong value");
+ assertSame(clone.get(EXTRA_KEY), EXTRA_VALUE, description + ":wrong value");
+ assertSame(serialClone.get(EXTRA_KEY), EXTRA_VALUE, description + ":wrong value");
+
+ assertEquals(map, map, description + ":should equal self");
+ assertEquals(clone, map, description + ":should equal clone");
+ assertEquals(map, clone, description + ": should equal orginal map");
+ assertEquals(serialClone, map, description + ": should equal deserialized clone");
+ assertEquals(map, serialClone, description + ": should equal original map");
+ assertEquals(serialClone, clone, description + ": deserialized clone should equal clone");
+ assertEquals(clone, serialClone, description + ": clone should equal deserialized clone");
}
- static void unexpected(Throwable t) {
- failed++;
- t.printStackTrace(System.err);
- }
-
- static void check(boolean cond) {
- if (cond) {
- pass();
- } else {
- fail();
+ static byte[] serializedForm(Object obj) {
+ try {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ new ObjectOutputStream(baos).writeObject(obj);
+ return baos.toByteArray();
+ } catch (IOException e) {
+ fail("Unexpected Exception", e);
+ return null;
}
}
- static void check(String desc, boolean cond) {
- if (cond) {
- pass();
- } else {
- fail(desc);
- }
+ static Object readObject(byte[] bytes) throws IOException, ClassNotFoundException {
+ InputStream is = new ByteArrayInputStream(bytes);
+ return new ObjectInputStream(is).readObject();
}
- static void equal(Object x, Object y) {
- if (Objects.equals(x, y)) {
- pass();
- } else {
- fail(x + " not equal to " + y);
+ @SuppressWarnings("unchecked")
+ static <T> T serialClone(T obj) {
+ try {
+ return (T)readObject(serializedForm(obj));
+ } catch (IOException | ClassNotFoundException e) {
+ fail("Unexpected Exception", e);
+ return null;
}
}
- public static void main(String[] args) throws Throwable {
- Thread.currentThread().setName(Collisions.class.getName());
-// Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
- try {
- realMain(args);
- } catch (Throwable t) {
- unexpected(t);
+ @DataProvider(name = "Map<IntegerEnum,String>", parallel = true)
+ private static Iterator<Object[]> makeMaps() {
+ return Arrays.asList(
+ // empty
+ new Object[]{"HashMap", new HashMap()},
+ new Object[]{"LinkedHashMap", new LinkedHashMap()},
+ new Object[]{"Collections.checkedMap(HashMap)", Collections.checkedMap(new HashMap(), IntegerEnum.class, String.class)},
+ new Object[]{"Collections.synchronizedMap(HashMap)", Collections.synchronizedMap(new HashMap())},
+ // null hostile
+ new Object[]{"EnumMap", new EnumMap(IntegerEnum.class)},
+ new Object[]{"Hashtable", new Hashtable()},
+ new Object[]{"TreeMap", new TreeMap()},
+ new Object[]{"ConcurrentHashMap", new ConcurrentHashMap()},
+ new Object[]{"ConcurrentSkipListMap", new ConcurrentSkipListMap()},
+ new Object[]{"Collections.checkedMap(ConcurrentHashMap)", Collections.checkedMap(new ConcurrentHashMap(), IntegerEnum.class, String.class)},
+ new Object[]{"Collections.synchronizedMap(EnumMap)", Collections.synchronizedMap(new EnumMap(IntegerEnum.class))},
+ // filled
+ new Object[]{"HashMap", fillMap(new HashMap())},
+ new Object[]{"LinkedHashMap", fillMap(new LinkedHashMap())},
+ new Object[]{"Collections.checkedMap(HashMap)", Collections.checkedMap(fillMap(new HashMap()), IntegerEnum.class, String.class)},
+ new Object[]{"Collections.synchronizedMap(HashMap)", Collections.synchronizedMap(fillMap(new HashMap()))},
+ // null hostile
+ new Object[]{"EnumMap", fillMap(new EnumMap(IntegerEnum.class))},
+ new Object[]{"Hashtable", fillMap(new Hashtable())},
+ new Object[]{"TreeMap", fillMap(new TreeMap())},
+ new Object[]{"ConcurrentHashMap", fillMap(new ConcurrentHashMap())},
+ new Object[]{"ConcurrentSkipListMap", fillMap(new ConcurrentSkipListMap())},
+ new Object[]{"Collections.checkedMap(ConcurrentHashMap)", Collections.checkedMap(fillMap(new ConcurrentHashMap()), IntegerEnum.class, String.class)},
+ new Object[]{"Collections.synchronizedMap(EnumMap)", Collections.synchronizedMap(fillMap(new EnumMap(IntegerEnum.class)))}).iterator();
+ }
+
+ private static Map<IntegerEnum, String> fillMap(Map<IntegerEnum, String> result) {
+ for (int each = 0; each < TEST_SIZE; each++) {
+ result.put(KEYS[each], VALUES[each]);
}
- System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
- if (failed > 0) {
- throw new Error("Some tests failed");
- }
+ return result;
}
}