test/jdk/java/util/concurrent/tck/MapTest.java
author dl
Sat, 14 Sep 2019 11:26:26 -0700
changeset 58138 1e4270f875ee
parent 54682 79d13fd7db9c
child 58892 35bac2745d04
permissions -rw-r--r--
8225490: Miscellaneous changes imported from jsr166 CVS 2019-09 Reviewed-by: martin, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     1
/*
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     3
 *
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     6
 * published by the Free Software Foundation.
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     7
 *
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    12
 * accompanied this code).
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    13
 *
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    17
 *
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    20
 * questions.
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    21
 */
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    22
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    23
/*
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    27
 * file:
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    28
 *
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    29
 * Written by Doug Lea and Martin Buchholz with assistance from
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    30
 * members of JCP JSR-166 Expert Group and released to the public
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    31
 * domain, as explained at
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    32
 * http://creativecommons.org/publicdomain/zero/1.0/
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    33
 */
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    34
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    35
import junit.framework.Test;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    36
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    37
import java.util.ArrayList;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    38
import java.util.Iterator;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    39
import java.util.List;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    40
import java.util.Map;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    41
import java.util.concurrent.ThreadLocalRandom;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    42
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    43
/**
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    44
 * Contains tests applicable to all Map implementations.
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    45
 */
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    46
public class MapTest extends JSR166TestCase {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    47
    final MapImplementation impl;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    48
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    49
    /** Tests are parameterized by a Map implementation. */
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    50
    MapTest(MapImplementation impl, String methodName) {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    51
        super(methodName);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    52
        this.impl = impl;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    53
    }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    54
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    55
    public static Test testSuite(MapImplementation impl) {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    56
        return newTestSuite(
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    57
            parameterizedTestSuite(MapTest.class,
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    58
                                   MapImplementation.class,
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    59
                                   impl));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    60
    }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    61
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    62
    public void testImplSanity() {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    63
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    64
        {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    65
            Map m = impl.emptyMap();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    66
            assertTrue(m.isEmpty());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    67
            assertEquals(0, m.size());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    68
            Object k = impl.makeKey(rnd.nextInt());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    69
            Object v = impl.makeValue(rnd.nextInt());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    70
            m.put(k, v);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    71
            assertFalse(m.isEmpty());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    72
            assertEquals(1, m.size());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    73
            assertTrue(m.containsKey(k));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    74
            assertTrue(m.containsValue(v));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    75
        }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    76
        {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    77
            Map m = impl.emptyMap();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    78
            Object v = impl.makeValue(rnd.nextInt());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    79
            if (impl.permitsNullKeys()) {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    80
                m.put(null, v);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    81
                assertTrue(m.containsKey(null));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    82
                assertTrue(m.containsValue(v));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    83
            } else {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    84
                assertThrows(NullPointerException.class, () -> m.put(null, v));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    85
            }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    86
        }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    87
        {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    88
            Map m = impl.emptyMap();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    89
            Object k = impl.makeKey(rnd.nextInt());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    90
            if (impl.permitsNullValues()) {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    91
                m.put(k, null);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    92
                assertTrue(m.containsKey(k));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    93
                assertTrue(m.containsValue(null));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    94
            } else {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    95
                assertThrows(NullPointerException.class, () -> m.put(k, null));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    96
            }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    97
        }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    98
        {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    99
            Map m = impl.emptyMap();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   100
            Object k = impl.makeKey(rnd.nextInt());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   101
            Object v1 = impl.makeValue(rnd.nextInt());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   102
            Object v2 = impl.makeValue(rnd.nextInt());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   103
            m.put(k, v1);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   104
            if (impl.supportsSetValue()) {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   105
                ((Map.Entry)(m.entrySet().iterator().next())).setValue(v2);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   106
                assertSame(v2, m.get(k));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   107
                assertTrue(m.containsKey(k));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   108
                assertTrue(m.containsValue(v2));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   109
                assertFalse(m.containsValue(v1));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   110
            } else {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   111
                assertThrows(UnsupportedOperationException.class,
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   112
                             () -> ((Map.Entry)(m.entrySet().iterator().next())).setValue(v2));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   113
            }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   114
        }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   115
    }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   116
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   117
    /**
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   118
     * Tests and extends the scenario reported in
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   119
     * https://bugs.openjdk.java.net/browse/JDK-8186171
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   120
     * HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   121
     * ant -Djsr166.tckTestClass=HashMapTest -Djsr166.methodFilter=testBug8186171 -Djsr166.runsPerTest=1000 tck
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   122
     */
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   123
    public void testBug8186171() {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   124
        if (!impl.supportsSetValue()) return;
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54682
diff changeset
   125
        if (!atLeastJava10()) return; // jdk9 is no longer maintained
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   126
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   127
        final boolean permitsNullValues = impl.permitsNullValues();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   128
        final Object v1 = (permitsNullValues && rnd.nextBoolean())
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   129
            ? null : impl.makeValue(1);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   130
        final Object v2 = (permitsNullValues && rnd.nextBoolean() && v1 != null)
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   131
            ? null : impl.makeValue(2);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   132
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47304
diff changeset
   133
        // If true, always lands in first bucket in hash tables.
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   134
        final boolean poorHash = rnd.nextBoolean();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   135
        class Key implements Comparable<Key> {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   136
            final int i;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   137
            Key(int i) { this.i = i; }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   138
            public int hashCode() { return poorHash ? 0 : super.hashCode(); }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   139
            public int compareTo(Key x) {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   140
                return Integer.compare(this.i, x.i);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   141
            }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   142
        }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   143
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   144
        // Both HashMap and ConcurrentHashMap have:
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   145
        // TREEIFY_THRESHOLD = 8; UNTREEIFY_THRESHOLD = 6;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   146
        final int size = rnd.nextInt(1, 25);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   147
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   148
        List<Key> keys = new ArrayList<>();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   149
        for (int i = size; i-->0; ) keys.add(new Key(i));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   150
        Key keyToFrob = keys.get(rnd.nextInt(keys.size()));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   151
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   152
        Map<Key, Object> m = impl.emptyMap();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   153
        for (Key key : keys) m.put(key, v1);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   154
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   155
        for (Iterator<Map.Entry<Key, Object>> it = m.entrySet().iterator();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   156
             it.hasNext(); ) {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   157
            Map.Entry<Key, Object> entry = it.next();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   158
            if (entry.getKey() == keyToFrob)
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   159
                entry.setValue(v2); // does this have the expected effect?
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   160
            else
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   161
                it.remove();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   162
        }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   163
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   164
        assertFalse(m.containsValue(v1));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   165
        assertTrue(m.containsValue(v2));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   166
        assertTrue(m.containsKey(keyToFrob));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   167
        assertEquals(1, m.size());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   168
    }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   169
53710
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   170
    /**
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   171
     * "Missing" test found while investigating JDK-8210280.
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   172
     * ant -Djsr166.tckTestClass=HashMapTest -Djsr166.methodFilter=testBug8210280 -Djsr166.runsPerTest=1000000 tck
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   173
     */
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   174
    public void testBug8210280() {
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   175
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   176
        final int size1 = rnd.nextInt(32);
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   177
        final int size2 = rnd.nextInt(128);
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   178
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   179
        final Map m1 = impl.emptyMap();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   180
        for (int i = 0; i < size1; i++) {
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   181
            int elt = rnd.nextInt(1024 * i, 1024 * (i + 1));
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   182
            assertNull(m1.put(impl.makeKey(elt), impl.makeValue(elt)));
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   183
        }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   184
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   185
        final Map m2 = impl.emptyMap();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   186
        for (int i = 0; i < size2; i++) {
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   187
            int elt = rnd.nextInt(Integer.MIN_VALUE + 1024 * i,
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   188
                                  Integer.MIN_VALUE + 1024 * (i + 1));
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   189
            assertNull(m2.put(impl.makeKey(elt), impl.makeValue(-elt)));
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   190
        }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   191
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   192
        final Map m1Copy = impl.emptyMap();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   193
        m1Copy.putAll(m1);
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   194
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   195
        m1.putAll(m2);
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   196
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   197
        for (Object elt : m2.keySet())
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   198
            assertEquals(m2.get(elt), m1.get(elt));
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   199
        for (Object elt : m1Copy.keySet())
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   200
            assertSame(m1Copy.get(elt), m1.get(elt));
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   201
        assertEquals(size1 + size2, m1.size());
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   202
    }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   203
54682
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   204
    /**
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   205
     * 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   206
     */
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   207
    public void testClone() {
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   208
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   209
        final int size = rnd.nextInt(4);
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   210
        final Map map = impl.emptyMap();
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   211
        for (int i = 0; i < size; i++)
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   212
            map.put(impl.makeKey(i), impl.makeValue(i));
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   213
        final Map clone = cloneableClone(map);
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   214
        if (clone == null) return;      // not cloneable?
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   215
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   216
        assertEquals(size, map.size());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   217
        assertEquals(size, clone.size());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   218
        assertEquals(map.isEmpty(), clone.isEmpty());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   219
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   220
        clone.put(impl.makeKey(-1), impl.makeValue(-1));
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   221
        assertEquals(size, map.size());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   222
        assertEquals(size + 1, clone.size());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   223
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   224
        clone.clear();
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   225
        assertEquals(size, map.size());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   226
        assertEquals(0, clone.size());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   227
        assertTrue(clone.isEmpty());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   228
    }
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   229
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   230
//     public void testFailsIntentionallyForDebugging() {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   231
//         fail(impl.klazz().getSimpleName());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   232
//     }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   233
}