< prev index next >

test/java/util/Map/Collisions.java

Print this page
rev 11959 : 8078463: TEST_BUG: optimize java/util/Map/Collisions.java
Reviewed-by: XXX

*** 1,7 **** /* ! * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 196,351 **** 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; --- 196,337 ---- check(map.isEmpty()); } private static <T> void testInsertion(Map<T, T> map, String keys_desc, T[] keys) { ! check(map.size() == 0 && map.isEmpty(), "map empty"); for (int i = 0; i < keys.length; i++) { ! check(map.size() == i, "insertion: map expected size m%d != i%d", map.size(), i); ! check(null == map.put(keys[i], keys[i]), "insertion: put(%s[%d])", keys_desc, i); ! check(map.containsKey(keys[i]), "insertion: containsKey(%s[%d])", keys_desc, i); ! check(map.containsValue(keys[i]), "insertion: containsValue(%s[%d])", keys_desc, i); } ! check(map.size() == keys.length, "map expected size m%d != k%d", map.size(), keys.length); } private static void testIntegerIteration(Map<HashableInteger, HashableInteger> map, HashableInteger[] keys) { ! check(map.size() == keys.length, "map expected size m%d != k%d", map.size(), keys.length); BitSet all = new BitSet(keys.length); for (Map.Entry<HashableInteger, HashableInteger> each : map.entrySet()) { ! check(!all.get(each.getKey().value), "Iteration: key already seen"); all.set(each.getKey().value); } all.flip(0, keys.length); ! check(all.isEmpty(), "Iteration: some keys not visited"); for (HashableInteger each : map.keySet()) { ! check(!all.get(each.value), "Iteration: key already seen"); all.set(each.value); } all.flip(0, keys.length); ! check(all.isEmpty(), "Iteration: some keys not visited"); int count = 0; for (HashableInteger each : map.values()) { count++; } ! check(map.size() == count, "Iteration: value count matches size m%d != c%d", map.size(), count); } private static void testStringIteration(Map<String, String> map, String[] keys) { ! check(map.size() == keys.length, "map expected size m%d != k%d", 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(!all.get(index), "key already seen"); all.set(index); } all.flip(0, keys.length); ! check(all.isEmpty(), "some keys not visited"); for (String each : map.keySet()) { boolean longKey = each.length() > 5; int index = each.hashCode() + (longKey ? keys.length / 2 : 0); ! check(!all.get(index), "key already seen"); all.set(index); } all.flip(0, keys.length); ! check(all.isEmpty(), "some keys not visited"); int count = 0; for (String each : map.values()) { count++; } ! check(map.size() == keys.length, "value count matches size m%d != k%d", 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(map.containsKey(each), "containsKey: %s[%d]%s", keys_desc, i, each); } } private static <T> void testRemove(Map<T, T> map, String keys_desc, T[] keys) { ! check(map.size() == keys.length, "remove: map expected size m%d != k%d", map.size(), keys.length); for (int i = 0; i < keys.length; i++) { T each = keys[i]; ! check(null != map.remove(each), "remove: %s[%d]%s", keys_desc, i, each); } ! check(map.size() == 0 && map.isEmpty(), "remove: map empty. size=%d", map.size()); } private static <T> void testKeysIteratorRemove(Map<T, T> map, String keys_desc, T[] keys) { ! check(map.size() == keys.length, "remove: map expected size m%d != k%d", map.size(), keys.length); Iterator<T> each = map.keySet().iterator(); while (each.hasNext()) { T t = each.next(); each.remove(); ! check(!map.containsKey(t), "not removed: %s", each); } ! check(map.size() == 0 && map.isEmpty(), "remove: map empty. size=%d", map.size()); } private static <T> void testValuesIteratorRemove(Map<T, T> map, String keys_desc, T[] keys) { ! check(map.size() == keys.length, "remove: map expected size m%d != k%d", map.size(), keys.length); Iterator<T> each = map.values().iterator(); while (each.hasNext()) { T t = each.next(); each.remove(); ! check(!map.containsValue(t), "not removed: %s", each); } ! check(map.size() == 0 && map.isEmpty(), "remove: map empty. size=%d", map.size()); } private static <T> void testEntriesIteratorRemove(Map<T, T> map, String keys_desc, T[] keys) { ! check(map.size() == keys.length, "remove: map expected size m%d != k%d", 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((map instanceof IdentityHashMap) || !map.entrySet().contains(t), "not removed: %s", each); ! check(!map.containsKey(key), "not removed: %s", each); ! check(!map.containsValue(value), "not removed: %s", each); } ! check(map.size() == 0 && map.isEmpty(), "remove: map empty. size=%d", map.size()); } //--------------------- Infrastructure --------------------------- static volatile int passed = 0, failed = 0;
*** 389,406 **** } else { fail(); } } ! static void check(String desc, boolean cond) { if (cond) { pass(); } else { fail(desc); } } static void equal(Object x, Object y) { if (Objects.equals(x, y)) { pass(); } else { fail(x + " not equal to " + y); --- 375,432 ---- } else { fail(); } } ! static void check(boolean cond, String desc) { if (cond) { pass(); } else { fail(desc); } } + static void check(boolean cond, String fmt, int i) { + if (cond) { + pass(); + } else { + fail(String.format(fmt, i)); + } + } + + static void check(boolean cond, String fmt, Object o) { + if (cond) { + pass(); + } else { + fail(String.format(fmt, o)); + } + } + + static void check(boolean cond, String fmt, int i1, int i2) { + if (cond) { + pass(); + } else { + fail(String.format(fmt, i1, i2)); + } + } + + static void check(boolean cond, String fmt, String s, int i) { + if (cond) { + pass(); + } else { + fail(String.format(fmt, s, i)); + } + } + + static void check(boolean cond, String fmt, String s, int i, Object o) { + if (cond) { + pass(); + } else { + fail(String.format(fmt, s, i, o)); + } + } + static void equal(Object x, Object y) { if (Objects.equals(x, y)) { pass(); } else { fail(x + " not equal to " + y);
< prev index next >