test/jdk/java/util/HashMap/ReplaceExisting.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 20199 jdk/test/java/util/HashMap/ReplaceExisting.java@0a9d1d17b076
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20199
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
     1
/*
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
     4
 *
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
     8
 *
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    13
 * accompanied this code).
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    14
 *
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    18
 *
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    21
 * questions.
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    22
 */
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    23
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    24
/**
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    25
 * @test
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    26
 * @bug 8025173
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    27
 * @summary Verify that replacing the value for an existing key does not
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    28
 * corrupt active iterators, in particular due to a resize() occurring and
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    29
 * not updating modCount.
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    30
 * @run main ReplaceExisting
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    31
 */
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    32
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    33
import java.util.ConcurrentModificationException;
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    34
import java.util.HashMap;
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    35
import java.util.HashSet;
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    36
import java.util.Iterator;
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    37
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    38
public class ReplaceExisting {
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    39
    /* Number of entries required to trigger a resize for cap=16, load=0.75*/
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    40
    private static int ENTRIES = 13;
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    41
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    42
    public static void main(String[] args) {
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    43
        for (int i = 0; i <= ENTRIES; i++) {
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    44
            HashMap<Integer,Integer> hm = prepHashMap();
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    45
            testItr(hm, i);
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    46
        }
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    47
    }
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    48
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    49
    /* Prepare a HashMap that will resize on next put() */
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    50
    private static HashMap<Integer,Integer> prepHashMap() {
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    51
        HashMap<Integer,Integer> hm = new HashMap<>(16, 0.75f);
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    52
        // Add items to one more than the resize threshold
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    53
        for (int i = 0; i < ENTRIES; i++) {
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    54
            hm.put(i*10, i*10);
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    55
        }
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    56
        return hm;
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    57
    }
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    58
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    59
    /* Iterate hm for elemBeforePut elements, then call put() to replace value
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    60
     * for existing key.  With bug 8025173, this will also cause a resize, but
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    61
     * not increase the modCount.
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    62
     * Finish the iteration to check for a corrupt iterator.
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    63
     */
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    64
    private static void testItr(HashMap<Integer,Integer> hm, int elemBeforePut) {
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    65
        if (elemBeforePut > hm.size()) {
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    66
            throw new IllegalArgumentException("Error in test: elemBeforePut must be <= HashMap size");
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    67
        }
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    68
        // Create a copy of the keys
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    69
        HashSet<Integer> keys = new HashSet<>(hm.size());
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    70
        keys.addAll(hm.keySet());
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    71
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    72
        HashSet<Integer> collected = new HashSet<>(hm.size());
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    73
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    74
        // Run itr for elemBeforePut items, collecting returned elems
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    75
        Iterator<Integer> itr = hm.keySet().iterator();
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    76
        for (int i = 0; i < elemBeforePut; i++) {
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    77
            Integer retVal = itr.next();
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    78
            if (!collected.add(retVal)) {
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    79
                throw new RuntimeException("Corrupt iterator: key " + retVal + " already encountered");
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    80
            }
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    81
        }
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    82
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    83
        // Do put() to replace entry (and resize table when bug present)
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    84
        if (null == hm.put(0, 100)) {
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    85
            throw new RuntimeException("Error in test: expected key 0 to be in the HashMap");
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    86
        }
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    87
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    88
        // Finish itr + collecting returned elems
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    89
        while(itr.hasNext()) {
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    90
            Integer retVal = itr.next();
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    91
            if (!collected.add(retVal)) {
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    92
                throw new RuntimeException("Corrupt iterator: key " + retVal + " already encountered");
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    93
            }
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    94
        }
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    95
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    96
        // Compare returned elems to original copy of keys
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    97
        if (!keys.equals(collected)) {
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    98
            throw new RuntimeException("Collected keys do not match original set of keys");
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
    99
        }
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
   100
    }
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents:
diff changeset
   101
}