5380N/A/*
5380N/A * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
5380N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5380N/A *
5380N/A * This code is free software; you can redistribute it and/or modify it
5380N/A * under the terms of the GNU General Public License version 2 only, as
5380N/A * published by the Free Software Foundation.
5380N/A *
5380N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5380N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5380N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5380N/A * version 2 for more details (a copy is included in the LICENSE file that
5380N/A * accompanied this code).
5380N/A *
5380N/A * You should have received a copy of the GNU General Public License version
5380N/A * 2 along with this work; if not, write to the Free Software Foundation,
5380N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5380N/A *
5380N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5380N/A * or visit www.oracle.com if you need additional information or have any
5380N/A * questions.
5380N/A */
5380N/A
5380N/A/*
5380N/A * Portions Copyright (c) 2012 IBM Corporation
5380N/A */
5380N/A
5380N/A/*
5380N/A * @test
5380N/A * @bug 8000955
5380N/A * @summary Map.Entry implementations need to comply with Map.Entry.hashCode() defined behaviour.
5380N/A * @author ngmr
5380N/A * @run main/othervm -Djdk.map.althashing.threshold=1 EntryHashCode
5380N/A */
5380N/Aimport java.util.*;
5380N/Aimport java.util.concurrent.ConcurrentHashMap;
5380N/Aimport java.util.concurrent.ConcurrentSkipListMap;
5380N/A
5380N/Apublic class EntryHashCode {
5380N/A private static final int TEST_SIZE = 100;
5380N/A
5380N/A static final Object[][] entryData = {
5380N/A new Object[TEST_SIZE],
5380N/A new Object[TEST_SIZE]
5380N/A };
5380N/A
5380N/A @SuppressWarnings("unchecked")
5380N/A static final Map<Object,Object>[] maps = (Map<Object,Object>[])new Map[] {
5380N/A new HashMap<>(),
5380N/A new Hashtable<>(),
5380N/A new IdentityHashMap<>(),
5380N/A new LinkedHashMap<>(),
5380N/A new TreeMap<>(),
5380N/A new WeakHashMap<>(),
5380N/A new ConcurrentHashMap<>(),
5380N/A new ConcurrentSkipListMap<>()
5380N/A };
5380N/A
5380N/A static {
5380N/A for (int i = 0; i < entryData[0].length; i++) {
5380N/A // key objects need to be Comparable for use in TreeMap
5380N/A entryData[0][i] = new Comparable<Object>() {
5380N/A public int compareTo(Object o) {
5380N/A return (hashCode() - o.hashCode());
5380N/A }
5380N/A };
5380N/A entryData[1][i] = new Object();
5380N/A }
5380N/A }
5380N/A
5380N/A private static void addTestData(Map<Object,Object> map) {
5380N/A for (int i = 0; i < entryData[0].length; i++) {
5380N/A map.put(entryData[0][i], entryData[1][i]);
5380N/A }
5380N/A }
5380N/A
5380N/A public static void main(String[] args) throws Exception {
5380N/A Exception failure = null;
5380N/A for (Map<Object,Object> map: maps) {
5380N/A addTestData(map);
5380N/A
5380N/A try {
5380N/A for (Map.Entry<Object,Object> e: map.entrySet()) {
5380N/A Object key = e.getKey();
5380N/A Object value = e.getValue();
5380N/A int expectedEntryHashCode =
5380N/A (Objects.hashCode(key) ^ Objects.hashCode(value));
5380N/A
5380N/A if (e.hashCode() != expectedEntryHashCode) {
5380N/A throw new Exception("FAILURE: " +
5380N/A e.getClass().getName() +
5380N/A ".hashCode() does not conform to defined" +
5380N/A " behaviour of java.util.Map.Entry.hashCode()");
5380N/A }
5380N/A }
5380N/A } catch (Exception e) {
5380N/A if (failure == null) {
5380N/A failure = e;
5380N/A } else {
5380N/A failure.addSuppressed(e);
5380N/A }
5380N/A } finally {
5380N/A map.clear();
5380N/A }
5380N/A }
5380N/A if (failure != null) {
5380N/A throw failure;
5380N/A }
5380N/A }
5380N/A}