Path: blob/master/test/jdk/java/util/Map/MapWithCollisionsProviders.java
41149 views
/*1* Copyright (c) 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*/2223import java.util.ArrayList;24import java.util.Collection;25import java.util.HashMap;26import java.util.Hashtable;27import java.util.IdentityHashMap;28import java.util.Iterator;29import java.util.LinkedHashMap;30import java.util.Map;31import java.util.TreeMap;32import java.util.WeakHashMap;33import java.util.concurrent.ConcurrentHashMap;34import java.util.concurrent.ConcurrentSkipListMap;35import java.util.function.Supplier;3637import org.testng.annotations.DataProvider;38import static org.testng.Assert.assertTrue;39import static org.testng.Assert.assertEquals;4041public class MapWithCollisionsProviders {4243private static final int TEST_SIZE44= Boolean.valueOf(System.getProperty("test.map.collisions.shortrun"))45? 250046: 5000;4748private static final IntKey EXTRA_INTKEY_VAL49= new IntKey(TEST_SIZE, Integer.MAX_VALUE);5051private static final String EXTRA_STRING_VAL = "Extra Value";5253public static final class IntKey implements Comparable<IntKey> {5455private final int value;56private final int hashmask; //yes duplication5758IntKey(int value, int hashmask) {59this.value = value;60this.hashmask = hashmask;61}6263@Override64public boolean equals(Object obj) {65if (obj instanceof IntKey) {66IntKey other = (IntKey) obj;6768return other.value == value;69}7071return false;72}7374@Override75public int hashCode() {76return value % hashmask;77}7879@Override80public int compareTo(IntKey o) {81return value - o.value;82}8384@Override85public String toString() {86return Integer.toString(value);87}8889public int getValue() {90return value;91}92}9394private static Object[] createUniqueObjectKeys() {95IntKey UNIQUE_OBJECTS[] = new IntKey[TEST_SIZE];96for (int i = 0; i < TEST_SIZE; i++) {97UNIQUE_OBJECTS[i] = new IntKey(i, Integer.MAX_VALUE);98}99return UNIQUE_OBJECTS;100}101102private static Object[] createUniqueStringKeys() {103String UNIQUE_STRINGS[] = new String[TEST_SIZE];104for (int i = 0; i < TEST_SIZE; i++) {105UNIQUE_STRINGS[i] = unhash(i);106}107return UNIQUE_STRINGS;108}109110private static Object[] createCollidingObjectKeys() {111IntKey COLLIDING_OBJECTS[] = new IntKey[TEST_SIZE];112for (int i = 0; i < TEST_SIZE; i++) {113COLLIDING_OBJECTS[i] = new IntKey(i, 10);114}115return COLLIDING_OBJECTS;116}117118private static Object[] createCollidingStringKeys() {119String COLLIDING_STRINGS[] = new String[TEST_SIZE];120String UNIQUE_STRINGS[] = new String[TEST_SIZE];121for (int i = 0; i < TEST_SIZE; i++) {122UNIQUE_STRINGS[i] = unhash(i);123COLLIDING_STRINGS[i] = (0 == i % 2)124? UNIQUE_STRINGS[i / 2]125: "\u0000\u0000\u0000\u0000\u0000" + COLLIDING_STRINGS[i - 1];126}127return COLLIDING_STRINGS;128}129130/**131* Returns a string with a hash equal to the argument.132*133* @return string with a hash equal to the argument.134*/135private static String unhash(int target) {136StringBuilder answer = new StringBuilder();137if (target < 0) {138// String with hash of Integer.MIN_VALUE, 0x80000000139answer.append("\\u0915\\u0009\\u001e\\u000c\\u0002");140141if (target == Integer.MIN_VALUE) {142return answer.toString();143}144// Find target without sign bit set145target = target & Integer.MAX_VALUE;146}147148unhash0(answer, target);149return answer.toString();150}151152private static void unhash0(StringBuilder partial, int target) {153int div = target / 31;154int rem = target % 31;155156if (div <= Character.MAX_VALUE) {157if (div != 0) {158partial.append((char) div);159}160partial.append((char) rem);161} else {162unhash0(partial, div);163partial.append((char) rem);164}165}166167private static <T> Map<T, T> fillMap(Map<T, T> m, T[] keys) {168for (T k : keys) {169m.put(k, k);170assertTrue(m.containsKey(k));171assertTrue(m.containsValue(k));172}173assertEquals(m.size(), keys.length);174return m;175}176177private static <T> Supplier<Map<T, T>> createMap(Map<T, T> m, T[] keys) {178return () -> fillMap(m, keys);179}180181private static <T> Object[] createCase(String desc, Map<T, T> m, T[] keys, T val) {182return new Object[]{desc, createMap(m, keys), val};183}184185private static <T> Collection<Object[]> makeMapsMoreTypes(String desc,186T[] keys,187T val) {188Collection<Object[]> cases = new ArrayList<>();189cases.add(createCase("Hashtable with " + desc,190new Hashtable<>(), keys, val));191cases.add(createCase("IdentityHashMap with " + desc,192new IdentityHashMap<>(), keys, val));193cases.add(createCase("TreeMap with " + desc,194new TreeMap<>(), keys, val));195cases.add(createCase("Descending TreeMap with " + desc,196new TreeMap<>().descendingMap(), keys, val));197cases.add(createCase("WeakHashMap with " + desc,198new WeakHashMap<>(), keys, val));199cases.add(createCase("ConcurrentHashMap with " + desc,200new ConcurrentHashMap<>(), keys, val));201cases.add(createCase("ConcurrentSkipListMap with " + desc,202new ConcurrentSkipListMap<>(), keys, val));203return cases;204}205206private static <T> Collection<Object[]> makeMapsHashMap(String desc,207T[] keys,208T val) {209Collection<Object[]> cases = new ArrayList<>();210cases.add(createCase("HashMap with " + desc,211new HashMap<>(), keys, val));212cases.add(createCase("LinkedHashMap with " + desc,213new LinkedHashMap<>(), keys, val));214return cases;215}216217private static <T> Collection<Object[]> makeMaps(String desc, T[] keys, T val) {218Collection<Object[]> cases = new ArrayList<>();219cases.addAll(makeMapsHashMap(desc, keys, val));220cases.addAll(makeMapsMoreTypes(desc, keys, val));221return cases;222}223224private static <T> Collection<Object[]> makeObjectsCases(String desc, T[] keys) {225return makeMaps(desc, keys, EXTRA_INTKEY_VAL);226}227228private static <T> Collection<Object[]> makeStringsCases(String desc,229T[] keys) {230return makeMaps(desc, keys, EXTRA_STRING_VAL);231}232233private static final Collection<Object[]> mapsWithObjectsCases234= new ArrayList<>() {235{236addAll(makeObjectsCases("unique objects", createUniqueObjectKeys()));237addAll(makeObjectsCases("colliding objects", createCollidingObjectKeys()));238}239};240241private static final Collection<Object[]> mapsWithStringsCases242= new ArrayList<>() {243{244addAll(makeStringsCases("unique strings", createUniqueStringKeys()));245addAll(makeStringsCases("colliding strings", createCollidingStringKeys()));246}247};248249private static final Collection<Object[]> mapsWithObjectsAndStringsCases250= new ArrayList<>() {251{252addAll(mapsWithObjectsCases);253addAll(mapsWithStringsCases);254}255};256257private static final Collection<Object[]> hashMapsWithObjectsCases258= new ArrayList<>() {259{260addAll(makeMapsHashMap("unique objects",261createUniqueObjectKeys(), EXTRA_INTKEY_VAL));262addAll(makeMapsHashMap("collisions objects",263createCollidingObjectKeys(), EXTRA_INTKEY_VAL));264}265};266267@DataProvider268public Iterator<Object[]> mapsWithObjects() {269return mapsWithObjectsCases.iterator();270}271272@DataProvider273public Iterator<Object[]> mapsWithStrings() {274return mapsWithStringsCases.iterator();275}276277@DataProvider278public Iterator<Object[]> mapsWithObjectsAndStrings() {279return mapsWithObjectsAndStringsCases.iterator();280}281282@DataProvider283public Iterator<Object[]> hashMapsWithObjects() {284return hashMapsWithObjectsCases.iterator();285}286287}288289290