Path: blob/master/test/jdk/java/util/Map/Collisions.java
41149 views
/*1* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 712627726* @run testng/othervm -Dtest.map.collisions.shortrun=true Collisions27* @summary Ensure Maps behave well with lots of hashCode() collisions.28*/29import java.util.BitSet;30import java.util.IdentityHashMap;31import java.util.Iterator;32import java.util.Map;33import java.util.function.Supplier;3435import org.testng.annotations.Test;36import static org.testng.Assert.assertTrue;37import static org.testng.Assert.assertFalse;38import static org.testng.Assert.assertEquals;39import static org.testng.Assert.assertNotNull;4041public class Collisions extends MapWithCollisionsProviders {4243@Test(dataProvider = "mapsWithObjects")44void testIntegerIteration(String desc, Supplier<Map<IntKey, IntKey>> ms, IntKey val) {45Map<IntKey, IntKey> map = ms.get();46int mapSize = map.size();4748BitSet all = new BitSet(mapSize);49for (Map.Entry<IntKey, IntKey> each : map.entrySet()) {50assertFalse(all.get(each.getKey().getValue()), "Iteration: key already seen");51all.set(each.getKey().getValue());52}5354all.flip(0, mapSize);55assertTrue(all.isEmpty(), "Iteration: some keys not visited");5657for (IntKey each : map.keySet()) {58assertFalse(all.get(each.getValue()), "Iteration: key already seen");59all.set(each.getValue());60}6162all.flip(0, mapSize);63assertTrue(all.isEmpty(), "Iteration: some keys not visited");6465int count = 0;66for (IntKey each : map.values()) {67count++;68}6970assertEquals(map.size(), count,71String.format("Iteration: value count matches size m%d != c%d", map.size(), count));72}7374@Test(dataProvider = "mapsWithStrings")75void testStringIteration(String desc, Supplier<Map<String, String>> ms, String val) {76Map<String, String> map = ms.get();77int mapSize = map.size();7879BitSet all = new BitSet(mapSize);80for (Map.Entry<String, String> each : map.entrySet()) {81String key = each.getKey();82boolean longKey = key.length() > 5;83int index = key.hashCode() + (longKey ? mapSize / 2 : 0);84assertFalse(all.get(index), "key already seen");85all.set(index);86}8788all.flip(0, mapSize);89assertTrue(all.isEmpty(), "some keys not visited");9091for (String each : map.keySet()) {92boolean longKey = each.length() > 5;93int index = each.hashCode() + (longKey ? mapSize / 2 : 0);94assertFalse(all.get(index), "key already seen");95all.set(index);96}9798all.flip(0, mapSize);99assertTrue(all.isEmpty(), "some keys not visited");100101int count = 0;102for (String each : map.values()) {103count++;104}105106assertEquals(map.size(), mapSize,107String.format("value count matches size m%d != k%d", map.size(), mapSize));108}109110@Test(dataProvider = "mapsWithObjectsAndStrings")111void testRemove(String desc, Supplier<Map<Object, Object>> ms, Object val) {112Map<Object, Object> map = ms.get();113Object[] keys = map.keySet().toArray();114115for (int i = 0; i < keys.length; i++) {116Object each = keys[i];117assertNotNull(map.remove(each),118String.format("remove: %s[%d]%s", desc, i, each));119}120121assertTrue(map.size() == 0 && map.isEmpty(),122String.format("remove: map empty. size=%d", map.size()));123}124125@Test(dataProvider = "mapsWithObjectsAndStrings")126void testKeysIteratorRemove(String desc, Supplier<Map<Object, Object>> ms, Object val) {127Map<Object, Object> map = ms.get();128129Iterator<Object> each = map.keySet().iterator();130while (each.hasNext()) {131Object t = each.next();132each.remove();133assertFalse(map.containsKey(t), String.format("not removed: %s", each));134}135136assertTrue(map.size() == 0 && map.isEmpty(),137String.format("remove: map empty. size=%d", map.size()));138}139140@Test(dataProvider = "mapsWithObjectsAndStrings")141void testValuesIteratorRemove(String desc, Supplier<Map<Object, Object>> ms, Object val) {142Map<Object, Object> map = ms.get();143144Iterator<Object> each = map.values().iterator();145while (each.hasNext()) {146Object t = each.next();147each.remove();148assertFalse(map.containsValue(t), String.format("not removed: %s", each));149}150151assertTrue(map.size() == 0 && map.isEmpty(),152String.format("remove: map empty. size=%d", map.size()));153}154155@Test(dataProvider = "mapsWithObjectsAndStrings")156void testEntriesIteratorRemove(String desc, Supplier<Map<Object, Object>> ms, Object val) {157Map<Object, Object> map = ms.get();158159Iterator<Map.Entry<Object, Object>> each = map.entrySet().iterator();160while (each.hasNext()) {161Map.Entry<Object, Object> t = each.next();162Object key = t.getKey();163Object value = t.getValue();164each.remove();165assertTrue((map instanceof IdentityHashMap) || !map.entrySet().contains(t),166String.format("not removed: %s", each));167assertFalse(map.containsKey(key),168String.format("not removed: %s", each));169assertFalse(map.containsValue(value),170String.format("not removed: %s", each));171}172173assertTrue(map.size() == 0 && map.isEmpty(),174String.format("remove: map empty. size=%d", map.size()));175}176177}178179180