Path: blob/master/test/jdk/java/util/Map/MapBinToFromTreeTest.java
41149 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*/2223import org.testng.annotations.DataProvider;24import org.testng.annotations.Test;2526import java.util.Collection;27import java.util.HashMap;28import java.util.LinkedHashMap;29import java.util.Map;30import java.util.concurrent.ConcurrentHashMap;31import java.util.function.BiConsumer;32import java.util.stream.Collector;33import java.util.stream.Collectors;34import java.util.stream.IntStream;3536import static org.testng.Assert.assertEquals;3738/*39* @test40* @bug 802346341* @summary Test the case where a bin is treeified and vice verser42* @run testng MapBinToFromTreeTest43*/4445@Test46public class MapBinToFromTreeTest {4748// Initial capacity of map49// Should be >= the map capacity for treeifiying, see HashMap/ConcurrentMap.MIN_TREEIFY_CAPACITY50static final int INITIAL_CAPACITY = 64;5152// Maximum size of map53// Should be > the treeify threshold, see HashMap/ConcurrentMap.TREEIFY_THRESHOLD54// Should be > INITIAL_CAPACITY to ensure resize occurs55static final int SIZE = 256;5657// Load factor of map58// A value 1.0 will ensure that a new threshold == capacity59static final float LOAD_FACTOR = 1.0f;6061@DataProvider(name = "maps")62static Object[][] mapProvider() {63return new Object[][] {64// Pass in the class name as a description for test reporting65// purposes66{ HashMap.class.getName(), new HashMap(INITIAL_CAPACITY, LOAD_FACTOR) },67{ LinkedHashMap.class.getName(), new LinkedHashMap(INITIAL_CAPACITY, LOAD_FACTOR) },68{ ConcurrentHashMap.class.getName(), new ConcurrentHashMap(INITIAL_CAPACITY, LOAD_FACTOR) },69};70}7172@Test(dataProvider = "maps")73public void testPutThenGet(String d, Map<HashCodeInteger, Integer> m) {74put(SIZE, m, (i, s) -> {75for (int j = 0; j < s; j++) {76assertEquals(m.get(new HashCodeInteger(j)).intValue(), j,77String.format("Map.get(%d)", j));78}79});80}8182@Test(dataProvider = "maps")83public void testPutThenTraverse(String d, Map<HashCodeInteger, Integer> m) {84Collector<Integer, ?, ? extends Collection<Integer>> c = getCollector(m);8586put(SIZE, m, (i, s) -> {87// Note that it is OK to collect to a Set (HashSet) as long as88// integer values are used since these tests only check for89// collisions and other tests will verify more general functionality90Collection<Integer> actual = m.keySet().stream().map(e -> e.value).collect(c);91Collection<Integer> expected = IntStream.range(0, s).boxed().collect(c);92assertEquals(actual, expected, "Map.keySet()");93});94}9596@Test(dataProvider = "maps")97public void testRemoveThenGet(String d, Map<HashCodeInteger, Integer> m) {98put(SIZE, m, (i, s) -> { });99100remove(m, (i, s) -> {101for (int j = i + 1; j < SIZE; j++) {102assertEquals(m.get(new HashCodeInteger(j)).intValue(), j,103String.format("Map.get(%d)", j));104}105});106}107108@Test(dataProvider = "maps")109public void testRemoveThenTraverse(String d, Map<HashCodeInteger, Integer> m) {110put(SIZE, m, (i, s) -> { });111112Collector<Integer, ?, ? extends Collection<Integer>> c = getCollector(m);113114remove(m, (i, s) -> {115Collection<Integer> actual = m.keySet().stream().map(e -> e.value).collect(c);116Collection<Integer> expected = IntStream.range(i + 1, SIZE).boxed().collect(c);117assertEquals(actual, expected, "Map.keySet()");118});119}120121@Test(dataProvider = "maps")122public void testUntreeifyOnResizeWithGet(String d, Map<HashCodeInteger, Integer> m) {123// Fill the map with 64 entries grouped into 4 buckets124put(INITIAL_CAPACITY, m, (i, s) -> { });125126for (int i = INITIAL_CAPACITY; i < SIZE; i++) {127// Add further entries in the 0'th bucket so as not to disturb128// other buckets, entries of which may be distributed and/or129// the bucket untreeified on resize130m.put(new HashCodeInteger(i, 0), i);131132for (int j = 0; j < INITIAL_CAPACITY; j++) {133assertEquals(m.get(new HashCodeInteger(j)).intValue(), j,134String.format("Map.get(%d) < INITIAL_CAPACITY", j));135}136for (int j = INITIAL_CAPACITY; j <= i; j++) {137assertEquals(m.get(new HashCodeInteger(j, 0)).intValue(), j,138String.format("Map.get(%d) >= INITIAL_CAPACITY", j));139}140}141}142143@Test(dataProvider = "maps")144public void testUntreeifyOnResizeWithTraverse(String d, Map<HashCodeInteger, Integer> m) {145// Fill the map with 64 entries grouped into 4 buckets146put(INITIAL_CAPACITY, m, (i, s) -> { });147148Collector<Integer, ?, ? extends Collection<Integer>> c = getCollector(m);149150for (int i = INITIAL_CAPACITY; i < SIZE; i++) {151// Add further entries in the 0'th bucket so as not to disturb152// other buckets, entries of which may be distributed and/or153// the bucket untreeified on resize154m.put(new HashCodeInteger(i, 0), i);155156Collection<Integer> actual = m.keySet().stream().map(e -> e.value).collect(c);157Collection<Integer> expected = IntStream.rangeClosed(0, i).boxed().collect(c);158assertEquals(actual, expected, "Key set");159}160}161162Collector<Integer, ?, ? extends Collection<Integer>> getCollector(Map<?, ?> m) {163Collector<Integer, ?, ? extends Collection<Integer>> collector = m instanceof LinkedHashMap164? Collectors.toList()165: Collectors.toSet();166return collector;167}168169void put(int size, Map<HashCodeInteger, Integer> m, BiConsumer<Integer, Integer> c) {170for (int i = 0; i < size; i++) {171m.put(new HashCodeInteger(i), i);172173c.accept(i, m.size());174}175}176177void remove(Map<HashCodeInteger, Integer> m, BiConsumer<Integer, Integer> c) {178int size = m.size();179// Remove all elements thus ensuring at some point trees will be180// converting back to bins181for (int i = 0; i < size; i++) {182m.remove(new HashCodeInteger(i));183184c.accept(i, m.size());185}186}187188static final class HashCodeInteger implements Comparable<HashCodeInteger> {189final int value;190191final int hashcode;192193HashCodeInteger(int value) {194this(value, hash(value));195}196197HashCodeInteger(int value, int hashcode) {198this.value = value;199this.hashcode = hashcode;200}201202static int hash(int i) {203// Assuming 64 entries with keys from 0 to 63 then a map:204// - of capacity 64 will have 4 buckets with 16 entries per-bucket205// - of capacity 128 will have 8 buckets with 8 entries per-bucket206// - of capacity 256 will have 16 buckets with 4 entries per-bucket207//208// Re-sizing will result in re-distribution, doubling the buckets209// and reducing the entries by half. This will result in210// untreeifying when the number of entries is less than untreeify211// threshold (see HashMap/ConcurrentMap.UNTREEIFY_THRESHOLD)212return (i % 4) + (i / 4) * INITIAL_CAPACITY;213}214215@Override216public boolean equals(Object obj) {217if (obj instanceof HashCodeInteger) {218HashCodeInteger other = (HashCodeInteger) obj;219return other.value == value;220}221return false;222}223224@Override225public int hashCode() {226return hashcode;227}228229@Override230public int compareTo(HashCodeInteger o) {231return value - o.value;232}233234@Override235public String toString() {236return Integer.toString(value);237}238}239}240241242