Path: blob/master/test/jdk/java/util/Map/EntryHashCode.java
41149 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* Portions Copyright (c) 2012 IBM Corporation25*/2627/*28* @test29* @bug 800095530* @summary Map.Entry implementations need to comply with Map.Entry.hashCode() defined behaviour.31* @author ngmr32*/33import java.util.*;34import java.util.concurrent.ConcurrentHashMap;35import java.util.concurrent.ConcurrentSkipListMap;3637public class EntryHashCode {38private static final int TEST_SIZE = 100;3940static final Object[][] entryData = {41new Object[TEST_SIZE],42new Object[TEST_SIZE]43};4445@SuppressWarnings("unchecked")46static final Map<Object,Object>[] maps = (Map<Object,Object>[])new Map[] {47new HashMap<>(),48new Hashtable<>(),49new IdentityHashMap<>(),50new LinkedHashMap<>(),51new TreeMap<>(),52new WeakHashMap<>(),53new ConcurrentHashMap<>(),54new ConcurrentSkipListMap<>()55};5657static {58for (int i = 0; i < entryData[0].length; i++) {59// key objects need to be Comparable for use in TreeMap60entryData[0][i] = new Comparable<Object>() {61public int compareTo(Object o) {62return (hashCode() - o.hashCode());63}64};65entryData[1][i] = new Object();66}67}6869private static void addTestData(Map<Object,Object> map) {70for (int i = 0; i < entryData[0].length; i++) {71map.put(entryData[0][i], entryData[1][i]);72}73}7475public static void main(String[] args) throws Exception {76Exception failure = null;77for (Map<Object,Object> map: maps) {78addTestData(map);7980try {81for (Map.Entry<Object,Object> e: map.entrySet()) {82Object key = e.getKey();83Object value = e.getValue();84int expectedEntryHashCode =85(Objects.hashCode(key) ^ Objects.hashCode(value));8687if (e.hashCode() != expectedEntryHashCode) {88throw new Exception("FAILURE: " +89e.getClass().getName() +90".hashCode() does not conform to defined" +91" behaviour of java.util.Map.Entry.hashCode()");92}93}94} catch (Exception e) {95if (failure == null) {96failure = e;97} else {98failure.addSuppressed(e);99}100} finally {101map.clear();102}103}104if (failure != null) {105throw failure;106}107}108}109110111