1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
/*
* Copyright (c) 2012, 2016, 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.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 7126277
* @run testng/othervm -Dtest.map.collisions.shortrun=true Collisions
* @summary Ensure Maps behave well with lots of hashCode() collisions.
*/
import java.util.BitSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Supplier;
import org.testng.annotations.Test;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
public class Collisions extends MapWithCollisionsProviders {
@Test(dataProvider = "mapsWithObjects")
void testIntegerIteration(String desc, Supplier<Map<IntKey, IntKey>> ms, IntKey val) {
Map<IntKey, IntKey> map = ms.get();
int mapSize = map.size();
BitSet all = new BitSet(mapSize);
for (Map.Entry<IntKey, IntKey> each : map.entrySet()) {
assertFalse(all.get(each.getKey().getValue()), "Iteration: key already seen");
all.set(each.getKey().getValue());
}
all.flip(0, mapSize);
assertTrue(all.isEmpty(), "Iteration: some keys not visited");
for (IntKey each : map.keySet()) {
assertFalse(all.get(each.getValue()), "Iteration: key already seen");
all.set(each.getValue());
}
all.flip(0, mapSize);
assertTrue(all.isEmpty(), "Iteration: some keys not visited");
int count = 0;
for (IntKey each : map.values()) {
count++;
}
assertEquals(map.size(), count,
String.format("Iteration: value count matches size m%d != c%d", map.size(), count));
}
@Test(dataProvider = "mapsWithStrings")
void testStringIteration(String desc, Supplier<Map<String, String>> ms, String val) {
Map<String, String> map = ms.get();
int mapSize = map.size();
BitSet all = new BitSet(mapSize);
for (Map.Entry<String, String> each : map.entrySet()) {
String key = each.getKey();
boolean longKey = key.length() > 5;
int index = key.hashCode() + (longKey ? mapSize / 2 : 0);
assertFalse(all.get(index), "key already seen");
all.set(index);
}
all.flip(0, mapSize);
assertTrue(all.isEmpty(), "some keys not visited");
for (String each : map.keySet()) {
boolean longKey = each.length() > 5;
int index = each.hashCode() + (longKey ? mapSize / 2 : 0);
assertFalse(all.get(index), "key already seen");
all.set(index);
}
all.flip(0, mapSize);
assertTrue(all.isEmpty(), "some keys not visited");
int count = 0;
for (String each : map.values()) {
count++;
}
assertEquals(map.size(), mapSize,
String.format("value count matches size m%d != k%d", map.size(), mapSize));
}
@Test(dataProvider = "mapsWithObjectsAndStrings")
void testRemove(String desc, Supplier<Map<Object, Object>> ms, Object val) {
Map<Object, Object> map = ms.get();
Object[] keys = map.keySet().toArray();
for (int i = 0; i < keys.length; i++) {
Object each = keys[i];
assertNotNull(map.remove(each),
String.format("remove: %s[%d]%s", desc, i, each));
}
assertTrue(map.size() == 0 && map.isEmpty(),
String.format("remove: map empty. size=%d", map.size()));
}
@Test(dataProvider = "mapsWithObjectsAndStrings")
void testKeysIteratorRemove(String desc, Supplier<Map<Object, Object>> ms, Object val) {
Map<Object, Object> map = ms.get();
Iterator<Object> each = map.keySet().iterator();
while (each.hasNext()) {
Object t = each.next();
each.remove();
assertFalse(map.containsKey(t), String.format("not removed: %s", each));
}
assertTrue(map.size() == 0 && map.isEmpty(),
String.format("remove: map empty. size=%d", map.size()));
}
@Test(dataProvider = "mapsWithObjectsAndStrings")
void testValuesIteratorRemove(String desc, Supplier<Map<Object, Object>> ms, Object val) {
Map<Object, Object> map = ms.get();
Iterator<Object> each = map.values().iterator();
while (each.hasNext()) {
Object t = each.next();
each.remove();
assertFalse(map.containsValue(t), String.format("not removed: %s", each));
}
assertTrue(map.size() == 0 && map.isEmpty(),
String.format("remove: map empty. size=%d", map.size()));
}
@Test(dataProvider = "mapsWithObjectsAndStrings")
void testEntriesIteratorRemove(String desc, Supplier<Map<Object, Object>> ms, Object val) {
Map<Object, Object> map = ms.get();
Iterator<Map.Entry<Object, Object>> each = map.entrySet().iterator();
while (each.hasNext()) {
Map.Entry<Object, Object> t = each.next();
Object key = t.getKey();
Object value = t.getValue();
each.remove();
assertTrue((map instanceof IdentityHashMap) || !map.entrySet().contains(t),
String.format("not removed: %s", each));
assertFalse(map.containsKey(key),
String.format("not removed: %s", each));
assertFalse(map.containsValue(value),
String.format("not removed: %s", each));
}
assertTrue(map.size() == 0 && map.isEmpty(),
String.format("remove: map empty. size=%d", map.size()));
}
}
|