jdk/test/java/util/Map/InPlaceOpsCollisions.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 19806 dda89341ee2d
child 42669 0b8c4fd7cc85
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     1
/*
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     4
 *
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     8
 *
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    13
 * accompanied this code).
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    14
 *
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    18
 *
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    21
 * questions.
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    22
 */
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    23
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    24
/*
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    25
 * @test
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
    26
 * @bug 8005698
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
    27
 * @run main InPlaceOpsCollisions -shortrun
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
    28
 * @summary Ensure overrides of in-place operations in Maps behave well with lots of collisions.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
    29
 * @author Brent Christian
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    30
 */
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    31
import java.util.*;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
    32
import java.util.function.*;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    33
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
    34
public class InPlaceOpsCollisions {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    35
13805
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
    36
    /**
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
    37
     * Number of elements per map.
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
    38
     */
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
    39
    private static final int TEST_SIZE = 5000;
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
    40
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 19806
diff changeset
    41
    static final class HashableInteger implements Comparable<HashableInteger> {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    42
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    43
        final int value;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    44
        final int hashmask; //yes duplication
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    45
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    46
        HashableInteger(int value, int hashmask) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    47
            this.value = value;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    48
            this.hashmask = hashmask;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    49
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    50
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    51
        @Override
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    52
        public boolean equals(Object obj) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    53
            if (obj instanceof HashableInteger) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    54
                HashableInteger other = (HashableInteger) obj;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    55
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    56
                return other.value == value;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    57
            }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    58
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    59
            return false;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    60
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    61
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    62
        @Override
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    63
        public int hashCode() {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    64
            return value % hashmask;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    65
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    66
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    67
        @Override
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    68
        public int compareTo(HashableInteger o) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    69
            return value - o.value;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    70
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    71
13805
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
    72
        @Override
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    73
        public String toString() {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    74
            return Integer.toString(value);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    75
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    76
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    77
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
    78
    static HashableInteger EXTRA_INT_VAL;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
    79
    static String EXTRA_STRING_VAL;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
    80
13805
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
    81
    private static Object[][] makeTestData(int size) {
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
    82
        HashableInteger UNIQUE_OBJECTS[] = new HashableInteger[size];
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
    83
        HashableInteger COLLIDING_OBJECTS[] = new HashableInteger[size];
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
    84
        String UNIQUE_STRINGS[] = new String[size];
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
    85
        String COLLIDING_STRINGS[] = new String[size];
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    86
13805
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
    87
        for (int i = 0; i < size; i++) {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    88
            UNIQUE_OBJECTS[i] = new HashableInteger(i, Integer.MAX_VALUE);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    89
            COLLIDING_OBJECTS[i] = new HashableInteger(i, 10);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    90
            UNIQUE_STRINGS[i] = unhash(i);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    91
            COLLIDING_STRINGS[i] = (0 == i % 2)
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    92
                    ? UNIQUE_STRINGS[i / 2]
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    93
                    : "\u0000\u0000\u0000\u0000\u0000" + COLLIDING_STRINGS[i - 1];
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    94
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
    95
        EXTRA_INT_VAL = new HashableInteger(size, Integer.MAX_VALUE);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
    96
        EXTRA_STRING_VAL = new String ("Extra Value");
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    97
13805
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
    98
     return new Object[][] {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    99
            new Object[]{"Unique Objects", UNIQUE_OBJECTS},
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   100
            new Object[]{"Colliding Objects", COLLIDING_OBJECTS},
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   101
            new Object[]{"Unique Strings", UNIQUE_STRINGS},
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   102
            new Object[]{"Colliding Strings", COLLIDING_STRINGS}
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   103
        };
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   104
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   105
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   106
    /**
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   107
     * Returns a string with a hash equal to the argument.
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   108
     *
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   109
     * @return string with a hash equal to the argument.
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   110
     */
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   111
    public static String unhash(int target) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   112
        StringBuilder answer = new StringBuilder();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   113
        if (target < 0) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   114
            // String with hash of Integer.MIN_VALUE, 0x80000000
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   115
            answer.append("\\u0915\\u0009\\u001e\\u000c\\u0002");
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   116
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   117
            if (target == Integer.MIN_VALUE) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   118
                return answer.toString();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   119
            }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   120
            // Find target without sign bit set
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   121
            target = target & Integer.MAX_VALUE;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   122
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   123
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   124
        unhash0(answer, target);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   125
        return answer.toString();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   126
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   127
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   128
    private static void unhash0(StringBuilder partial, int target) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   129
        int div = target / 31;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   130
        int rem = target % 31;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   131
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   132
        if (div <= Character.MAX_VALUE) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   133
            if (div != 0) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   134
                partial.append((char) div);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   135
            }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   136
            partial.append((char) rem);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   137
        } else {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   138
            unhash0(partial, div);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   139
            partial.append((char) rem);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   140
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   141
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   142
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   143
    private static void realMain(String[] args) throws Throwable {
13805
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
   144
        boolean shortRun = args.length > 0 && args[0].equals("-shortrun");
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
   145
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
   146
        Object[][] mapKeys = makeTestData(shortRun ? (TEST_SIZE / 2) : TEST_SIZE);
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
   147
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
   148
        // loop through data sets
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
   149
        for (Object[] keys_desc : mapKeys) {
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
   150
            Map<Object, Object>[] maps = (Map<Object, Object>[]) new Map[]{
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
   151
                        new HashMap<>(),
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   152
                        new LinkedHashMap<>(),
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   153
                    };
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   154
13805
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
   155
            // for each map type.
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
   156
            for (Map<Object, Object> map : maps) {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   157
                String desc = (String) keys_desc[0];
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   158
                Object[] keys = (Object[]) keys_desc[1];
13025
e070f58ad775 7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents: 12859
diff changeset
   159
                try {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   160
                    testInPlaceOps(map, desc, keys);
13025
e070f58ad775 7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents: 12859
diff changeset
   161
                } catch(Exception all) {
e070f58ad775 7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents: 12859
diff changeset
   162
                    unexpected("Failed for " + map.getClass().getName() + " with " + desc, all);
e070f58ad775 7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents: 12859
diff changeset
   163
                }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   164
            }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   165
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   166
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   167
13025
e070f58ad775 7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents: 12859
diff changeset
   168
    private static <T> void testInsertion(Map<T, T> map, String keys_desc, T[] keys) {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   169
        check("map empty", (map.size() == 0) && map.isEmpty());
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   170
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   171
        for (int i = 0; i < keys.length; i++) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   172
            check(String.format("insertion: map expected size m%d != i%d", map.size(), i),
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   173
                    map.size() == i);
13025
e070f58ad775 7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents: 12859
diff changeset
   174
            check(String.format("insertion: put(%s[%d])", keys_desc, i), null == map.put(keys[i], keys[i]));
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   175
            check(String.format("insertion: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i]));
13025
e070f58ad775 7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents: 12859
diff changeset
   176
            check(String.format("insertion: containsValue(%s[%d])", keys_desc, i), map.containsValue(keys[i]));
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   177
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   178
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   179
        check(String.format("map expected size m%d != k%d", map.size(), keys.length),
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   180
                map.size() == keys.length);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   181
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   182
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   183
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   184
    private static <T> void testInPlaceOps(Map<T, T> map, String keys_desc, T[] keys) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   185
        System.out.println(map.getClass() + " : " + keys_desc + ", testInPlaceOps");
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   186
        System.out.flush();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   187
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   188
        testInsertion(map, keys_desc, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   189
        testPutIfAbsent(map, keys_desc, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   190
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   191
        map.clear();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   192
        testInsertion(map, keys_desc, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   193
        testRemoveMapping(map, keys_desc, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   194
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   195
        map.clear();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   196
        testInsertion(map, keys_desc, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   197
        testReplaceOldValue(map, keys_desc, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   198
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   199
        map.clear();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   200
        testInsertion(map, keys_desc, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   201
        testReplaceIfMapped(map, keys_desc, keys);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   202
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   203
        map.clear();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   204
        testInsertion(map, keys_desc, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   205
        testComputeIfAbsent(map, keys_desc, keys, (k) -> getExtraVal(keys[0]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   206
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   207
        map.clear();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   208
        testInsertion(map, keys_desc, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   209
        testComputeIfAbsent(map, keys_desc, keys, (k) -> null);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   210
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   211
        map.clear();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   212
        testInsertion(map, keys_desc, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   213
        testComputeIfPresent(map, keys_desc, keys, (k, v) -> getExtraVal(keys[0]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   214
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   215
        map.clear();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   216
        testInsertion(map, keys_desc, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   217
        testComputeIfPresent(map, keys_desc, keys, (k, v) -> null);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   218
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   219
        if (!keys_desc.contains("Strings")) { // avoid parseInt() number format error
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   220
            map.clear();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   221
            testInsertion(map, keys_desc, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   222
            testComputeNonNull(map, keys_desc, keys);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   223
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   224
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   225
        map.clear();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   226
        testInsertion(map, keys_desc, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   227
        testComputeNull(map, keys_desc, keys);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   228
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   229
        if (!keys_desc.contains("Strings")) { // avoid parseInt() number format error
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   230
            map.clear();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   231
            testInsertion(map, keys_desc, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   232
            testMergeNonNull(map, keys_desc, keys);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   233
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   234
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   235
        map.clear();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   236
        testInsertion(map, keys_desc, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   237
        testMergeNull(map, keys_desc, keys);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   238
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   239
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   240
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   241
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   242
    private static <T> void testPutIfAbsent(Map<T, T> map, String keys_desc, T[] keys) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   243
        T extraVal = getExtraVal(keys[0]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   244
        T retVal;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   245
        removeOddKeys(map, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   246
        for (int i = 0; i < keys.length; i++) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   247
            retVal = map.putIfAbsent(keys[i], extraVal);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   248
            if (i % 2 == 0) { // even: not absent, not put
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   249
                check(String.format("putIfAbsent: (%s[%d]) retVal", keys_desc, i), retVal == keys[i]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   250
                check(String.format("putIfAbsent: get(%s[%d])", keys_desc, i), keys[i] == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   251
                check(String.format("putIfAbsent: containsValue(%s[%d])", keys_desc, i), map.containsValue(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   252
            } else { // odd: absent, was put
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   253
                check(String.format("putIfAbsent: (%s[%d]) retVal", keys_desc, i), retVal == null);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   254
                check(String.format("putIfAbsent: get(%s[%d])", keys_desc, i), extraVal == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   255
                check(String.format("putIfAbsent: !containsValue(%s[%d])", keys_desc, i), !map.containsValue(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   256
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   257
            check(String.format("insertion: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   258
        }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   259
        check(String.format("map expected size m%d != k%d", map.size(), keys.length),
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   260
                map.size() == keys.length);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   261
    }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   262
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   263
    private static <T> void testRemoveMapping(Map<T, T> map, String keys_desc, T[] keys) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   264
        T extraVal = getExtraVal(keys[0]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   265
        boolean removed;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   266
        int removes = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   267
        remapOddKeys(map, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   268
        for (int i = 0; i < keys.length; i++) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   269
            removed = map.remove(keys[i], keys[i]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   270
            if (i % 2 == 0) { // even: original mapping, should be removed
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   271
                check(String.format("removeMapping: retVal(%s[%d])", keys_desc, i), removed);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   272
                check(String.format("removeMapping: get(%s[%d])", keys_desc, i), null == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   273
                check(String.format("removeMapping: !containsKey(%s[%d])", keys_desc, i), !map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   274
                check(String.format("removeMapping: !containsValue(%s[%d])", keys_desc, i), !map.containsValue(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   275
                removes++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   276
            } else { // odd: new mapping, not removed
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   277
                check(String.format("removeMapping: retVal(%s[%d])", keys_desc, i), !removed);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   278
                check(String.format("removeMapping: get(%s[%d])", keys_desc, i), extraVal == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   279
                check(String.format("removeMapping: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   280
                check(String.format("removeMapping: containsValue(%s[%d])", keys_desc, i), map.containsValue(extraVal));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   281
            }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   282
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   283
        check(String.format("map expected size m%d != k%d", map.size(), keys.length - removes),
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   284
                map.size() == keys.length - removes);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   285
    }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   286
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   287
    private static <T> void testReplaceOldValue(Map<T, T> map, String keys_desc, T[] keys) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   288
        // remap odds to extraVal
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   289
        // call replace to replace for extraVal, for all keys
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   290
        // check that all keys map to value from keys array
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   291
        T extraVal = getExtraVal(keys[0]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   292
        boolean replaced;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   293
        remapOddKeys(map, keys);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   294
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   295
        for (int i = 0; i < keys.length; i++) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   296
            replaced = map.replace(keys[i], extraVal, keys[i]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   297
            if (i % 2 == 0) { // even: original mapping, should not be replaced
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   298
                check(String.format("replaceOldValue: retVal(%s[%d])", keys_desc, i), !replaced);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   299
            } else { // odd: new mapping, should be replaced
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   300
                check(String.format("replaceOldValue: get(%s[%d])", keys_desc, i), replaced);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   301
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   302
            check(String.format("replaceOldValue: get(%s[%d])", keys_desc, i), keys[i] == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   303
            check(String.format("replaceOldValue: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   304
            check(String.format("replaceOldValue: containsValue(%s[%d])", keys_desc, i), map.containsValue(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   305
//            removes++;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   306
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   307
        check(String.format("replaceOldValue: !containsValue(%s[%s])", keys_desc, extraVal.toString()), !map.containsValue(extraVal));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   308
        check(String.format("map expected size m%d != k%d", map.size(), keys.length),
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   309
                map.size() == keys.length);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   310
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   311
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   312
    // TODO: Test case for key mapped to null value
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   313
    private static <T> void testReplaceIfMapped(Map<T, T> map, String keys_desc, T[] keys) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   314
        // remove odd keys
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   315
        // call replace for all keys[]
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   316
        // odd keys should remain absent, even keys should be mapped to EXTRA, no value from keys[] should be in map
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   317
        T extraVal = getExtraVal(keys[0]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   318
        int expectedSize1 = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   319
        removeOddKeys(map, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   320
        int expectedSize2 = map.size();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   321
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   322
        for (int i = 0; i < keys.length; i++) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   323
            T retVal = map.replace(keys[i], extraVal);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   324
            if (i % 2 == 0) { // even: still in map, should be replaced
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   325
                check(String.format("replaceIfMapped: retVal(%s[%d])", keys_desc, i), retVal == keys[i]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   326
                check(String.format("replaceIfMapped: get(%s[%d])", keys_desc, i), extraVal == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   327
                check(String.format("replaceIfMapped: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   328
                expectedSize1++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   329
            } else { // odd: was removed, should not be replaced
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   330
                check(String.format("replaceIfMapped: retVal(%s[%d])", keys_desc, i), retVal == null);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   331
                check(String.format("replaceIfMapped: get(%s[%d])", keys_desc, i), null == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   332
                check(String.format("replaceIfMapped: containsKey(%s[%d])", keys_desc, i), !map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   333
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   334
            check(String.format("replaceIfMapped: !containsValue(%s[%d])", keys_desc, i), !map.containsValue(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   335
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   336
        check(String.format("replaceIfMapped: containsValue(%s[%s])", keys_desc, extraVal.toString()), map.containsValue(extraVal));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   337
        check(String.format("map expected size#1 m%d != k%d", map.size(), expectedSize1),
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   338
                map.size() == expectedSize1);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   339
        check(String.format("map expected size#2 m%d != k%d", map.size(), expectedSize2),
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   340
                map.size() == expectedSize2);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   341
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   342
    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   343
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   344
    private static <T> void testComputeIfAbsent(Map<T, T> map, String keys_desc, T[] keys,
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   345
                                                Function<T,T> mappingFunction) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   346
        // remove a third of the keys
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   347
        // call computeIfAbsent for all keys, func returns EXTRA
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   348
        // check that removed keys now -> EXTRA, other keys -> original val
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   349
        T expectedVal = mappingFunction.apply(keys[0]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   350
        T retVal;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   351
        int expectedSize = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   352
        removeThirdKeys(map, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   353
        for (int i = 0; i < keys.length; i++) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   354
            retVal = map.computeIfAbsent(keys[i], mappingFunction);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   355
            if (i % 3 != 2) { // key present, not computed
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   356
                check(String.format("computeIfAbsent: (%s[%d]) retVal", keys_desc, i), retVal == keys[i]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   357
                check(String.format("computeIfAbsent: get(%s[%d])", keys_desc, i), keys[i] == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   358
                check(String.format("computeIfAbsent: containsValue(%s[%d])", keys_desc, i), map.containsValue(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   359
                check(String.format("insertion: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   360
                expectedSize++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   361
            } else { // key absent, computed unless function return null
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   362
                check(String.format("computeIfAbsent: (%s[%d]) retVal", keys_desc, i), retVal == expectedVal);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   363
                check(String.format("computeIfAbsent: get(%s[%d])", keys_desc, i), expectedVal == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   364
                check(String.format("computeIfAbsent: !containsValue(%s[%d])", keys_desc, i), !map.containsValue(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   365
                // mapping should not be added if function returns null
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   366
                check(String.format("insertion: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i]) != (expectedVal == null));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   367
                if (expectedVal != null) { expectedSize++; }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   368
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   369
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   370
        if (expectedVal != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   371
            check(String.format("computeIfAbsent: containsValue(%s[%s])", keys_desc, expectedVal), map.containsValue(expectedVal));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   372
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   373
        check(String.format("map expected size m%d != k%d", map.size(), expectedSize),
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   374
                map.size() == expectedSize);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   375
    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   376
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   377
    private static <T> void testComputeIfPresent(Map<T, T> map, String keys_desc, T[] keys,
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   378
                                                BiFunction<T,T,T> mappingFunction) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   379
        // remove a third of the keys
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   380
        // call testComputeIfPresent for all keys[]
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   381
        // removed keys should remain absent, even keys should be mapped to $RESULT
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   382
        // no value from keys[] should be in map
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   383
        T funcResult = mappingFunction.apply(keys[0], keys[0]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   384
        int expectedSize1 = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   385
        removeThirdKeys(map, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   386
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   387
        for (int i = 0; i < keys.length; i++) {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   388
            T retVal = map.computeIfPresent(keys[i], mappingFunction);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   389
            if (i % 3 != 2) { // key present
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   390
                if (funcResult == null) { // was removed
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   391
                    check(String.format("replaceIfMapped: containsKey(%s[%d])", keys_desc, i), !map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   392
                } else { // value was replaced
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   393
                    check(String.format("replaceIfMapped: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   394
                    expectedSize1++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   395
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   396
                check(String.format("computeIfPresent: retVal(%s[%s])", keys_desc, i), retVal == funcResult);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   397
                check(String.format("replaceIfMapped: get(%s[%d])", keys_desc, i), funcResult == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   398
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   399
            } else { // odd: was removed, should not be replaced
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   400
                check(String.format("replaceIfMapped: retVal(%s[%d])", keys_desc, i), retVal == null);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   401
                check(String.format("replaceIfMapped: get(%s[%d])", keys_desc, i), null == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   402
                check(String.format("replaceIfMapped: containsKey(%s[%d])", keys_desc, i), !map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   403
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   404
            check(String.format("replaceIfMapped: !containsValue(%s[%d])", keys_desc, i), !map.containsValue(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   405
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   406
        check(String.format("map expected size#1 m%d != k%d", map.size(), expectedSize1),
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   407
                map.size() == expectedSize1);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   408
    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   409
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   410
    private static <T> void testComputeNonNull(Map<T, T> map, String keys_desc, T[] keys) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   411
        // remove a third of the keys
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   412
        // call compute() for all keys[]
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   413
        // all keys should be present: removed keys -> EXTRA, others to k-1
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   414
        BiFunction<T,T,T> mappingFunction = (k, v) -> {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   415
                if (v == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   416
                    return getExtraVal(keys[0]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   417
                } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   418
                    return keys[Integer.parseInt(k.toString()) - 1];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   419
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   420
            };
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   421
        T extraVal = getExtraVal(keys[0]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   422
        removeThirdKeys(map, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   423
        for (int i = 1; i < keys.length; i++) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   424
            T retVal = map.compute(keys[i], mappingFunction);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   425
            if (i % 3 != 2) { // key present, should be mapped to k-1
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   426
                check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == keys[i-1]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   427
                check(String.format("compute: get(%s[%d])", keys_desc, i), keys[i-1] == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   428
            } else { // odd: was removed, should be replaced with EXTRA
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   429
                check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == extraVal);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   430
                check(String.format("compute: get(%s[%d])", keys_desc, i), extraVal == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   431
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   432
            check(String.format("compute: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   433
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   434
        check(String.format("map expected size#1 m%d != k%d", map.size(), keys.length),
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   435
                map.size() == keys.length);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   436
        check(String.format("compute: containsValue(%s[%s])", keys_desc, extraVal.toString()), map.containsValue(extraVal));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   437
        check(String.format("compute: !containsValue(%s,[null])", keys_desc), !map.containsValue(null));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   438
    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   439
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   440
    private static <T> void testComputeNull(Map<T, T> map, String keys_desc, T[] keys) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   441
        // remove a third of the keys
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   442
        // call compute() for all keys[]
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   443
        // removed keys should -> EXTRA
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   444
        // for other keys: func returns null, should have no mapping
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   445
        BiFunction<T,T,T> mappingFunction = (k, v) -> {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   446
            // if absent/null -> EXTRA
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   447
            // if present -> null
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   448
            if (v == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   449
                return getExtraVal(keys[0]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   450
            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   451
                return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   452
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   453
        };
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   454
        T extraVal = getExtraVal(keys[0]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   455
        int expectedSize = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   456
        removeThirdKeys(map, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   457
        for (int i = 0; i < keys.length; i++) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   458
            T retVal = map.compute(keys[i], mappingFunction);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   459
            if (i % 3 != 2) { // key present, func returned null, should be absent from map
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   460
                check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == null);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   461
                check(String.format("compute: get(%s[%d])", keys_desc, i), null == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   462
                check(String.format("compute: containsKey(%s[%d])", keys_desc, i), !map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   463
                check(String.format("compute: containsValue(%s[%s])", keys_desc, i), !map.containsValue(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   464
            } else { // odd: was removed, should now be mapped to EXTRA
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   465
                check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == extraVal);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   466
                check(String.format("compute: get(%s[%d])", keys_desc, i), extraVal == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   467
                check(String.format("compute: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   468
                expectedSize++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   469
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   470
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   471
        check(String.format("compute: containsValue(%s[%s])", keys_desc, extraVal.toString()), map.containsValue(extraVal));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   472
        check(String.format("map expected size#1 m%d != k%d", map.size(), expectedSize),
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   473
                map.size() == expectedSize);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   474
    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   475
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   476
    private static <T> void testMergeNonNull(Map<T, T> map, String keys_desc, T[] keys) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   477
        // remove a third of the keys
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   478
        // call merge() for all keys[]
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   479
        // all keys should be present: removed keys now -> EXTRA, other keys -> k-1
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   480
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   481
        // Map to preceding key
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   482
        BiFunction<T,T,T> mappingFunction = (k, v) -> keys[Integer.parseInt(k.toString()) - 1];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   483
        T extraVal = getExtraVal(keys[0]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   484
        removeThirdKeys(map, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   485
        for (int i = 1; i < keys.length; i++) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   486
            T retVal = map.merge(keys[i], extraVal, mappingFunction);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   487
            if (i % 3 != 2) { // key present, should be mapped to k-1
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   488
                check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == keys[i-1]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   489
                check(String.format("compute: get(%s[%d])", keys_desc, i), keys[i-1] == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   490
            } else { // odd: was removed, should be replaced with EXTRA
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   491
                check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == extraVal);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   492
                check(String.format("compute: get(%s[%d])", keys_desc, i), extraVal == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   493
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   494
            check(String.format("compute: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   495
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   496
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   497
        check(String.format("map expected size#1 m%d != k%d", map.size(), keys.length),
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   498
                map.size() == keys.length);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   499
        check(String.format("compute: containsValue(%s[%s])", keys_desc, extraVal.toString()), map.containsValue(extraVal));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   500
        check(String.format("compute: !containsValue(%s,[null])", keys_desc), !map.containsValue(null));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   501
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   502
    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   503
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   504
    private static <T> void testMergeNull(Map<T, T> map, String keys_desc, T[] keys) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   505
        // remove a third of the keys
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   506
        // call merge() for all keys[]
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   507
        // result: removed keys -> EXTRA, other keys absent
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   508
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   509
        BiFunction<T,T,T> mappingFunction = (k, v) -> null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   510
        T extraVal = getExtraVal(keys[0]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   511
        int expectedSize = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   512
        removeThirdKeys(map, keys);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   513
        for (int i = 0; i < keys.length; i++) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   514
            T retVal = map.merge(keys[i], extraVal, mappingFunction);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   515
            if (i % 3 != 2) { // key present, func returned null, should be absent from map
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   516
                check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == null);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   517
                check(String.format("compute: get(%s[%d])", keys_desc, i), null == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   518
                check(String.format("compute: containsKey(%s[%d])", keys_desc, i), !map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   519
            } else { // odd: was removed, should now be mapped to EXTRA
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   520
                check(String.format("compute: retVal(%s[%d])", keys_desc, i), retVal == extraVal);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   521
                check(String.format("compute: get(%s[%d])", keys_desc, i), extraVal == map.get(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   522
                check(String.format("compute: containsKey(%s[%d])", keys_desc, i), map.containsKey(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   523
                expectedSize++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   524
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   525
            check(String.format("compute: containsValue(%s[%s])", keys_desc, i), !map.containsValue(keys[i]));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   526
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   527
        check(String.format("compute: containsValue(%s[%s])", keys_desc, extraVal.toString()), map.containsValue(extraVal));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   528
        check(String.format("map expected size#1 m%d != k%d", map.size(), expectedSize),
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   529
                map.size() == expectedSize);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   530
    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   531
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   532
    /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   533
     * Return the EXTRA val for the key type being used
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   534
     */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   535
    private static <T> T getExtraVal(T key) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   536
        if (key instanceof HashableInteger) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   537
            return (T)EXTRA_INT_VAL;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   538
        } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   539
            return (T)EXTRA_STRING_VAL;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   540
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   541
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   542
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   543
    /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   544
     * Remove half of the keys
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   545
     */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   546
    private static <T> void removeOddKeys(Map<T, T> map, /*String keys_desc, */ T[] keys) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   547
        int removes = 0;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   548
        for (int i = 0; i < keys.length; i++) {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   549
            if (i % 2 != 0) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   550
                map.remove(keys[i]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   551
                removes++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   552
            }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   553
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   554
        check(String.format("map expected size m%d != k%d", map.size(), keys.length - removes),
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   555
                map.size() == keys.length - removes);
13025
e070f58ad775 7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents: 12859
diff changeset
   556
    }
e070f58ad775 7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents: 12859
diff changeset
   557
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   558
    /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   559
     * Remove every third key
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   560
     * This will hopefully leave some removed keys in TreeBins for, e.g., computeIfAbsent
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   561
     * w/ a func that returns null.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   562
     *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   563
     * TODO: consider using this in other tests (and maybe adding a remapThirdKeys)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   564
     */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   565
    private static <T> void removeThirdKeys(Map<T, T> map, /*String keys_desc, */ T[] keys) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   566
        int removes = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   567
        for (int i = 0; i < keys.length; i++) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   568
            if (i % 3 == 2) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   569
                map.remove(keys[i]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   570
                removes++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   571
            }
13025
e070f58ad775 7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents: 12859
diff changeset
   572
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   573
        check(String.format("map expected size m%d != k%d", map.size(), keys.length - removes),
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   574
                map.size() == keys.length - removes);
13025
e070f58ad775 7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents: 12859
diff changeset
   575
    }
e070f58ad775 7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents: 12859
diff changeset
   576
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   577
    /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   578
     * Re-map the odd-numbered keys to map to the EXTRA value
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   579
     */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   580
    private static <T> void remapOddKeys(Map<T, T> map, /*String keys_desc, */ T[] keys) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   581
        T extraVal = getExtraVal(keys[0]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   582
        for (int i = 0; i < keys.length; i++) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   583
            if (i % 2 != 0) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   584
                map.put(keys[i], extraVal);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 13817
diff changeset
   585
            }
13025
e070f58ad775 7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents: 12859
diff changeset
   586
        }
e070f58ad775 7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents: 12859
diff changeset
   587
    }
e070f58ad775 7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents: 12859
diff changeset
   588
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   589
    //--------------------- Infrastructure ---------------------------
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   590
    static volatile int passed = 0, failed = 0;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   591
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   592
    static void pass() {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   593
        passed++;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   594
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   595
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   596
    static void fail() {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   597
        failed++;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   598
        (new Error("Failure")).printStackTrace(System.err);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   599
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   600
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   601
    static void fail(String msg) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   602
        failed++;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   603
        (new Error("Failure: " + msg)).printStackTrace(System.err);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   604
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   605
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   606
    static void abort() {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   607
        fail();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   608
        System.exit(1);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   609
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   610
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   611
    static void abort(String msg) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   612
        fail(msg);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   613
        System.exit(1);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   614
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   615
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   616
    static void unexpected(String msg, Throwable t) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   617
        System.err.println("Unexpected: " + msg);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   618
        unexpected(t);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   619
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   620
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   621
    static void unexpected(Throwable t) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   622
        failed++;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   623
        t.printStackTrace(System.err);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   624
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   625
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   626
    static void check(boolean cond) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   627
        if (cond) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   628
            pass();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   629
        } else {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   630
            fail();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   631
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   632
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   633
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   634
    static void check(String desc, boolean cond) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   635
        if (cond) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   636
            pass();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   637
        } else {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   638
            fail(desc);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   639
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   640
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   641
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   642
    static void equal(Object x, Object y) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   643
        if (Objects.equals(x, y)) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   644
            pass();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   645
        } else {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   646
            fail(x + " not equal to " + y);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   647
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   648
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   649
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   650
    public static void main(String[] args) throws Throwable {
13805
29e474ceeddc 7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents: 13025
diff changeset
   651
        Thread.currentThread().setName(Collisions.class.getName());
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   652
//        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   653
        try {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   654
            realMain(args);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   655
        } catch (Throwable t) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   656
            unexpected(t);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   657
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   658
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   659
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   660
        if (failed > 0) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   661
            throw new Error("Some tests failed");
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   662
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   663
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
   664
}