Path: blob/master/test/jdk/java/util/HashMap/ReplaceExisting.java
41152 views
/*1* Copyright (c) 2013, 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 802517326* @summary Verify that replacing the value for an existing key does not27* corrupt active iterators, in particular due to a resize() occurring and28* not updating modCount.29* @run main ReplaceExisting30*/3132import java.util.ConcurrentModificationException;33import java.util.HashMap;34import java.util.HashSet;35import java.util.Iterator;3637public class ReplaceExisting {38/* Number of entries required to trigger a resize for cap=16, load=0.75*/39private static int ENTRIES = 13;4041public static void main(String[] args) {42for (int i = 0; i <= ENTRIES; i++) {43HashMap<Integer,Integer> hm = prepHashMap();44testItr(hm, i);45}46}4748/* Prepare a HashMap that will resize on next put() */49private static HashMap<Integer,Integer> prepHashMap() {50HashMap<Integer,Integer> hm = new HashMap<>(16, 0.75f);51// Add items to one more than the resize threshold52for (int i = 0; i < ENTRIES; i++) {53hm.put(i*10, i*10);54}55return hm;56}5758/* Iterate hm for elemBeforePut elements, then call put() to replace value59* for existing key. With bug 8025173, this will also cause a resize, but60* not increase the modCount.61* Finish the iteration to check for a corrupt iterator.62*/63private static void testItr(HashMap<Integer,Integer> hm, int elemBeforePut) {64if (elemBeforePut > hm.size()) {65throw new IllegalArgumentException("Error in test: elemBeforePut must be <= HashMap size");66}67// Create a copy of the keys68HashSet<Integer> keys = new HashSet<>(hm.size());69keys.addAll(hm.keySet());7071HashSet<Integer> collected = new HashSet<>(hm.size());7273// Run itr for elemBeforePut items, collecting returned elems74Iterator<Integer> itr = hm.keySet().iterator();75for (int i = 0; i < elemBeforePut; i++) {76Integer retVal = itr.next();77if (!collected.add(retVal)) {78throw new RuntimeException("Corrupt iterator: key " + retVal + " already encountered");79}80}8182// Do put() to replace entry (and resize table when bug present)83if (null == hm.put(0, 100)) {84throw new RuntimeException("Error in test: expected key 0 to be in the HashMap");85}8687// Finish itr + collecting returned elems88while(itr.hasNext()) {89Integer retVal = itr.next();90if (!collected.add(retVal)) {91throw new RuntimeException("Corrupt iterator: key " + retVal + " already encountered");92}93}9495// Compare returned elems to original copy of keys96if (!keys.equals(collected)) {97throw new RuntimeException("Collected keys do not match original set of keys");98}99}100}101102103