blob: d3efc8c7d56ceb4d2bc67516c147e84a18be3047 [file] [log] [blame]
ngmr4d97cc72012-10-17 13:35:22 +01001/*
katlemand08780c2012-12-20 16:24:50 -08002 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
ngmr4d97cc72012-10-17 13:35:22 +01003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * Portions Copyright (c) 2012 IBM Corporation
26 */
27
28/*
29 * @test
30 * @bug 8000955
31 * @summary Map.Entry implementations need to comply with Map.Entry.hashCode() defined behaviour.
32 * @author ngmr
33 */
34import java.util.*;
35import java.util.concurrent.ConcurrentHashMap;
36import java.util.concurrent.ConcurrentSkipListMap;
37
38public class EntryHashCode {
39 private static final int TEST_SIZE = 100;
40
41 static final Object[][] entryData = {
42 new Object[TEST_SIZE],
43 new Object[TEST_SIZE]
44 };
45
46 @SuppressWarnings("unchecked")
47 static final Map<Object,Object>[] maps = (Map<Object,Object>[])new Map[] {
48 new HashMap<>(),
49 new Hashtable<>(),
50 new IdentityHashMap<>(),
51 new LinkedHashMap<>(),
52 new TreeMap<>(),
53 new WeakHashMap<>(),
54 new ConcurrentHashMap<>(),
55 new ConcurrentSkipListMap<>()
56 };
57
58 static {
59 for (int i = 0; i < entryData[0].length; i++) {
60 // key objects need to be Comparable for use in TreeMap
61 entryData[0][i] = new Comparable<Object>() {
62 public int compareTo(Object o) {
63 return (hashCode() - o.hashCode());
64 }
65 };
66 entryData[1][i] = new Object();
67 }
68 }
69
70 private static void addTestData(Map<Object,Object> map) {
71 for (int i = 0; i < entryData[0].length; i++) {
72 map.put(entryData[0][i], entryData[1][i]);
73 }
74 }
75
76 public static void main(String[] args) throws Exception {
77 Exception failure = null;
78 for (Map<Object,Object> map: maps) {
79 addTestData(map);
80
81 try {
82 for (Map.Entry<Object,Object> e: map.entrySet()) {
83 Object key = e.getKey();
84 Object value = e.getValue();
85 int expectedEntryHashCode =
86 (Objects.hashCode(key) ^ Objects.hashCode(value));
87
88 if (e.hashCode() != expectedEntryHashCode) {
89 throw new Exception("FAILURE: " +
90 e.getClass().getName() +
91 ".hashCode() does not conform to defined" +
92 " behaviour of java.util.Map.Entry.hashCode()");
93 }
94 }
95 } catch (Exception e) {
96 if (failure == null) {
97 failure = e;
98 } else {
99 failure.addSuppressed(e);
100 }
101 } finally {
102 map.clear();
103 }
104 }
105 if (failure != null) {
106 throw failure;
107 }
108 }
109}