--- /dev/null 2014-11-30 10:11:31.639805301 +0100 +++ new/test/java/lang/Class/getMethods/HashArrayTest.java 2014-11-30 18:51:42.597538397 +0100 @@ -0,0 +1,198 @@ +/* + * Copyright (c) 1994, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import jdk.testlibrary.TestProxy; + +import java.util.Objects; + +/** + * @test + * @summary Unit tests for java.lang.HashArray internal class. + */ +public class HashArrayTest { + + public static void main(String[] args) { + + testPut(); + testPutIfAbsent(); + testRemove(); + + System.out.println("All tests passed."); + } + + @SuppressWarnings("UnnecessaryBoxing") + static final Integer i11 = new Integer(1); + + @SuppressWarnings("UnnecessaryBoxing") + static final Integer i12 = new Integer(1); + + @SuppressWarnings("UnnecessaryBoxing") + static final Integer i21 = new Integer(2); + + @SuppressWarnings("UnnecessaryBoxing") + static final Integer i22 = new Integer(2); + + static void testPut() { + HashArray ha = newHashArray(); + + assertSameObject(null, ha.put(i11)); + assertEquals(1, ha.size()); + + assertSameObject(i11, ha.get(i11)); + assertSameObject(i11, ha.get(i12)); + + assertSameObject(i11, ha.put(i12)); + assertEquals(1, ha.size()); + + assertSameObject(i12, ha.get(i11)); + assertSameObject(i12, ha.get(i12)); + + assertSameObject(null, ha.put(i21)); + assertEquals(2, ha.size()); + + assertSameObject(i21, ha.get(i21)); + assertSameObject(i21, ha.get(i22)); + + assertSameObject(i21, ha.put(i22)); + assertEquals(2, ha.size()); + + assertSameObject(i22, ha.get(i21)); + assertSameObject(i22, ha.get(i22)); + } + + static void testPutIfAbsent() { + HashArray ha = newHashArray(); + + assertSameObject(null, ha.putIfAbsent(i11)); + assertEquals(1, ha.size()); + + assertSameObject(i11, ha.get(i11)); + assertSameObject(i11, ha.get(i12)); + + assertSameObject(i11, ha.putIfAbsent(i12)); + assertEquals(1, ha.size()); + + assertSameObject(i11, ha.get(i11)); + assertSameObject(i11, ha.get(i12)); + + assertSameObject(null, ha.putIfAbsent(i21)); + assertEquals(2, ha.size()); + + assertSameObject(i21, ha.get(i21)); + assertSameObject(i21, ha.get(i22)); + + assertSameObject(i21, ha.putIfAbsent(i22)); + assertEquals(2, ha.size()); + + assertSameObject(i21, ha.get(i21)); + assertSameObject(i21, ha.get(i22)); + } + + static void testRemove() { + HashArray ha = newHashArray(); + + assertSameObject(null, ha.put(i11)); + assertSameObject(null, ha.put(i21)); + assertEquals(2, ha.size()); + + assertSameObject(i11, ha.remove(i11)); + assertSameObject(null, ha.get(i11)); + assertEquals(1, ha.size()); + + assertSameObject(i21, ha.remove(i21)); + assertSameObject(null, ha.get(i21)); + assertEquals(0, ha.size()); + + // once again with equal elements + + assertSameObject(null, ha.put(i12)); + assertSameObject(null, ha.put(i22)); + assertEquals(2, ha.size()); + + assertSameObject(i12, ha.remove(i11)); + assertSameObject(null, ha.get(i11)); + assertEquals(1, ha.size()); + + assertSameObject(i22, ha.remove(i21)); + assertSameObject(null, ha.get(i21)); + assertEquals(0, ha.size()); + } + + + // test infrastructure + + static HashArray newHashArray() { + HashArray ha = HashArray.newInstance(1); + assertSameObject(null, ha.get(i11)); + assertSameObject(null, ha.get(i12)); + assertSameObject(null, ha.get(i21)); + assertSameObject(null, ha.get(i22)); + assertEquals(0, ha.size()); + return ha; + } + + static void assertSameObject(Object expected, Object actual) { + if (expected != actual) { + throw new AssertionError("Expected:\n " + expected + + "\nActual:\n " + actual); + + } + } + + static void assertEquals(Object expected, Object actual) { + if (!Objects.equals(expected, actual)) { + throw new AssertionError("Expected:\n " + expected + + "\nActual:\n " + actual); + + } + } + + // test proxy for package-private java.lang.HashArray + + interface HashArray { + + @SuppressWarnings("unchecked") + public static HashArray newInstance(int maxExpectedSize) { + return (HashArray) + TestProxy.newInstance(HashArray.class, + "java.lang.HashArray", + ClassLoader.getSystemClassLoader(), + new Class[]{int.class}, + maxExpectedSize); + } + + public int size(); + + public T get(Object lookupObj); + + public T put(T newElement); + + public T putIfAbsent(T newElement); + + public T remove(Object lookupObj); + + public void clear(); + } +}