Path: blob/master/test/jdk/java/util/HashMap/TreeBinAssert.java
41149 views
/*1* Copyright (c) 2018, 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 820539926* @summary Check for AssertionError from HashMap TreeBin after Iterator.remove27* @run testng/othervm -esa TreeBinAssert28*/2930import org.testng.annotations.DataProvider;31import org.testng.annotations.Test;32import org.testng.annotations.BeforeTest;33import java.util.Map;34import java.util.Set;35import java.util.Iterator;36import java.util.HashMap;37import java.util.LinkedHashSet;38import java.util.function.BiConsumer;39import java.util.function.Function;4041public class TreeBinAssert {42private static final int ITR_RM = -1; // Remove an item via Iterator43private static final int BIN352442_SIZE = 524288;44private static final int BIN552165_SIZE = 1048576;4546@DataProvider(name = "SizeAndHashes")47public Object[][] sizeAndHashes() {48return new Object[][] {49{ // Bin 35244250BIN352442_SIZE,51new int[] {522020958394,53631595194,541984782522,55419782842,56285565114,571432182970,58841310394,59320692410,60303390906,61ITR_RM,62ITR_RM,63ITR_RM,64ITR_RM,65ITR_RM,66ITR_RM,67ITR_RM,68ITR_RM,69519397562,70ITR_RM,71626352314,7210154002673}74},{ // Bin 55216575BIN552165_SIZE,76new int[] {77790129893,781214803173,791212706021,80608726245,812073586917,821433955557,83692612325,84370699493,852061004005,8648786661,87ITR_RM,88ITR_RM,891418226917,90ITR_RM,91ITR_RM,92ITR_RM,93ITR_RM,94ITR_RM,95ITR_RM,96ITR_RM,971487432933,98ITR_RM,99ITR_RM,1001880648933,101338193637102}103}104};105}106107@BeforeTest108public void checkAssertionStatus() {109if (!HashMap.class.desiredAssertionStatus()) {110System.out.println("*** Superficial test run. Test should be run with -esa ***\n");111return;112}113}114115@Test(dataProvider = "SizeAndHashes")116public void testMap(int size, int[] hashes) {117Map<Key,Integer> map = new HashMap<>(size);118119doTest(map, hashes,120(c,k) -> { ((Map<Key,Integer>)c).put(k,0); },121(c) -> { return ((Map<Key,Integer>)c).keySet().iterator(); }122);123}124125@Test(dataProvider = "SizeAndHashes")126public void testSet(int size, int[] hashes) {127Set<Key> set = new LinkedHashSet<>(size);128129doTest(set, hashes,130(c,k) -> { ((Set<Key>)c).add(k); },131(c) -> { return ((Set<Key>)c).iterator(); }132);133}134135private void doTest(Object collection, int[] hashes,136BiConsumer<Object,Key> addKey,137Function<Object,Iterator<Key>> mkItr) {138Iterator<Key> itr = null; // saved iterator, used for removals139for (int h : hashes) {140if (h == ITR_RM) {141if (itr == null) {142itr = mkItr.apply(collection);143}144itr.next();145itr.remove();146} else {147itr = null;148addKey.accept(collection, new Key(h));149}150}151}152153/**154* Class that will have specified hash code in a HashMap.155*/156static class Key implements Comparable<Key> {157final int hash;158159public Key(int desiredHash) {160// Account for processing done by HashMap161this.hash = desiredHash ^ (desiredHash >>> 16);162}163164@Override public int hashCode() { return this.hash; }165166@Override public boolean equals(Object o) {167return o.hashCode() == this.hashCode();168}169170@Override public int compareTo(Key k) {171return Integer.compare(this.hash, k.hash);172}173}174}175176177