# HG changeset patch # User mduigou # Date 1339790498 25200 # Node ID e070f58ad775ca33272cea207f7dc0b90dde9252 # Parent ada1a7c54e845ceaff93e139a4ab628e9da8494c 7175758: Improve unit test of Map iterators and Iterator.remove() Summary: Adds additional tests of Map iterators and Iterator.remove() Reviewed-by: lancea diff -r ada1a7c54e84 -r e070f58ad775 jdk/test/java/util/Map/Collisions.java --- a/jdk/test/java/util/Map/Collisions.java Fri Jun 15 17:16:25 2012 +0100 +++ b/jdk/test/java/util/Map/Collisions.java Fri Jun 15 13:01:38 2012 -0700 @@ -68,7 +68,7 @@ return Integer.toString(value); } } - private static final int ITEMS = 10000; + private static final int ITEMS = 5000; private static final Object KEYS[][]; static { @@ -133,8 +133,8 @@ private static void realMain(String[] args) throws Throwable { for (Object[] keys_desc : KEYS) { - Map[] MAPS = (Map[]) new Map[]{ -// new Hashtable<>(), + Map[] MAPS = (Map[]) new Map[]{ + new Hashtable<>(), new HashMap<>(), new IdentityHashMap<>(), new LinkedHashMap<>(), @@ -144,51 +144,69 @@ new ConcurrentSkipListMap<>() }; - for (Map map : MAPS) { + for (Map map : MAPS) { String desc = (String) keys_desc[0]; Object[] keys = (Object[]) keys_desc[1]; + try { testMap(map, desc, keys); + } catch(Exception all) { + unexpected("Failed for " + map.getClass().getName() + " with " + desc, all); + } } } } - private static void testMap(Map map, String keys_desc, T[] keys) { - System.err.println(map.getClass() + " : " + keys_desc); + private static void testMap(Map 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) map, (HashableInteger[]) keys); + testIntegerIteration((Map) map, (HashableInteger[]) keys); } else { - testStringIteration((Map) map, (String[]) keys); + testStringIteration((Map) 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 void testInsertion(Map map, String keys_desc, T[] keys) { + private static void testInsertion(Map 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], true)); + 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 map, HashableInteger[] keys) { + private static void testIntegerIteration(Map 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 each : map.entrySet()) { + for (Map.Entry each : map.entrySet()) { check("Iteration: key already seen", !all.get(each.getKey().value)); all.set(each.getKey().value); } @@ -205,7 +223,7 @@ check("Iteration: some keys not visited", all.isEmpty()); int count = 0; - for (Boolean each : map.values()) { + for (HashableInteger each : map.values()) { count++; } @@ -213,12 +231,12 @@ map.size() == count); } - private static void testStringIteration(Map map, String[] keys) { + private static void testStringIteration(Map 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 each : map.entrySet()) { + for (Map.Entry each : map.entrySet()) { String key = each.getKey(); boolean longKey = key.length() > 5; int index = key.hashCode() + (longKey ? keys.length / 2 : 0); @@ -240,7 +258,7 @@ check("some keys not visited", all.isEmpty()); int count = 0; - for (Boolean each : map.values()) { + for (String each : map.values()) { count++; } @@ -248,14 +266,14 @@ map.size() == keys.length); } - private static void testContainsKey(Map map, String keys_desc, T[] keys) { + private static void testContainsKey(Map 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 void testRemove(Map map, String keys_desc, T[] keys) { + private static void testRemove(Map 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); @@ -267,6 +285,56 @@ check(String.format("remove: map empty. size=%d", map.size()), (map.size() == 0) && map.isEmpty()); } + + private static void testKeysIteratorRemove(Map 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 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 void testValuesIteratorRemove(Map 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 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 void testEntriesIteratorRemove(Map 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> each = map.entrySet().iterator(); + while (each.hasNext()) { + Map.Entry 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;