0N/A/*
553N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6585904
0N/A * @summary Checked collections with underlying maps with identity comparisons
0N/A */
0N/A
288N/Aimport java.util.*;
0N/Aimport static java.util.Collections.*;
0N/A
0N/Apublic class CheckedIdentityMap {
0N/A void test(String[] args) throws Throwable {
0N/A Map<Integer, Integer> m1 = checkedMap(
0N/A new IdentityHashMap<Integer, Integer>(),
0N/A Integer.class, Integer.class);
0N/A Map<Integer, Integer> m2 = checkedMap(
0N/A new IdentityHashMap<Integer, Integer>(),
0N/A Integer.class, Integer.class);
0N/A m1.put(new Integer(1), new Integer(1));
0N/A m2.put(new Integer(1), new Integer(1));
0N/A
0N/A Map.Entry<Integer, Integer> e1 = m1.entrySet().iterator().next();
0N/A Map.Entry<Integer, Integer> e2 = m2.entrySet().iterator().next();
0N/A check(! e1.equals(e2));
0N/A check(e1.hashCode() == hashCode(e1));
0N/A check(e2.hashCode() == hashCode(e2));
0N/A }
int hashCode(Map.Entry<?,?> e) {
return (System.identityHashCode(e.getKey()) ^
System.identityHashCode(e.getValue()));
}
//--------------------- Infrastructure ---------------------------
volatile int passed = 0, failed = 0;
void pass() {passed++;}
void fail() {failed++; Thread.dumpStack();}
void fail(String msg) {System.err.println(msg); fail();}
void unexpected(Throwable t) {failed++; t.printStackTrace();}
void check(boolean cond) {if (cond) pass(); else fail();}
void equal(Object x, Object y) {
if (x == null ? y == null : x.equals(y)) pass();
else fail(x + " not equal to " + y);}
public static void main(String[] args) throws Throwable {
new CheckedIdentityMap().instanceMain(args);}
void instanceMain(String[] args) throws Throwable {
try {test(args);} catch (Throwable t) {unexpected(t);}
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new AssertionError("Some tests failed");}
abstract class F {abstract void f() throws Throwable;}
void THROWS(Class<? extends Throwable> k, F... fs) {
for (F f : fs)
try {f.f(); fail("Expected " + k.getName() + " not thrown");}
catch (Throwable t) {
if (k.isAssignableFrom(t.getClass())) pass();
else unexpected(t);}}
Thread checkedThread(final Runnable r) {
return new Thread() {public void run() {
try {r.run();} catch (Throwable t) {unexpected(t);}}};}
}