1 /* 2 * Copyright (c) 1994, 2014, Oracle and/or its affiliates. All rights reserved. 3 * 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 import jdk.testlibrary.TestProxy; 27 28 import java.util.Objects; 29 30 /** 31 * @test 32 * @summary Unit tests for java.lang.HashArray internal class. 33 */ 34 public class HashArrayTest { 35 36 public static void main(String[] args) { 37 38 testPut(); 39 testPutIfAbsent(); 40 testRemove(); 41 42 System.out.println("All tests passed."); 43 } 44 45 @SuppressWarnings("UnnecessaryBoxing") 46 static final Integer i11 = new Integer(1); 47 48 @SuppressWarnings("UnnecessaryBoxing") 49 static final Integer i12 = new Integer(1); 50 51 @SuppressWarnings("UnnecessaryBoxing") 52 static final Integer i21 = new Integer(2); 53 54 @SuppressWarnings("UnnecessaryBoxing") 55 static final Integer i22 = new Integer(2); 56 57 static void testPut() { 58 HashArray<Integer> ha = newHashArray(); 59 60 assertSameObject(null, ha.put(i11)); 61 assertEquals(1, ha.size()); 62 63 assertSameObject(i11, ha.get(i11)); 64 assertSameObject(i11, ha.get(i12)); 65 66 assertSameObject(i11, ha.put(i12)); 67 assertEquals(1, ha.size()); 68 69 assertSameObject(i12, ha.get(i11)); 70 assertSameObject(i12, ha.get(i12)); 71 72 assertSameObject(null, ha.put(i21)); 73 assertEquals(2, ha.size()); 74 75 assertSameObject(i21, ha.get(i21)); 76 assertSameObject(i21, ha.get(i22)); 77 78 assertSameObject(i21, ha.put(i22)); 79 assertEquals(2, ha.size()); 80 81 assertSameObject(i22, ha.get(i21)); 82 assertSameObject(i22, ha.get(i22)); 83 } 84 85 static void testPutIfAbsent() { 86 HashArray<Integer> ha = newHashArray(); 87 88 assertSameObject(null, ha.putIfAbsent(i11)); 89 assertEquals(1, ha.size()); 90 91 assertSameObject(i11, ha.get(i11)); 92 assertSameObject(i11, ha.get(i12)); 93 94 assertSameObject(i11, ha.putIfAbsent(i12)); 95 assertEquals(1, ha.size()); 96 97 assertSameObject(i11, ha.get(i11)); 98 assertSameObject(i11, ha.get(i12)); 99 100 assertSameObject(null, ha.putIfAbsent(i21)); 101 assertEquals(2, ha.size()); 102 103 assertSameObject(i21, ha.get(i21)); 104 assertSameObject(i21, ha.get(i22)); 105 106 assertSameObject(i21, ha.putIfAbsent(i22)); 107 assertEquals(2, ha.size()); 108 109 assertSameObject(i21, ha.get(i21)); 110 assertSameObject(i21, ha.get(i22)); 111 } 112 113 static void testRemove() { 114 HashArray<Integer> ha = newHashArray(); 115 116 assertSameObject(null, ha.put(i11)); 117 assertSameObject(null, ha.put(i21)); 118 assertEquals(2, ha.size()); 119 120 assertSameObject(i11, ha.remove(i11)); 121 assertSameObject(null, ha.get(i11)); 122 assertEquals(1, ha.size()); 123 124 assertSameObject(i21, ha.remove(i21)); 125 assertSameObject(null, ha.get(i21)); 126 assertEquals(0, ha.size()); 127 128 // once again with equal elements 129 130 assertSameObject(null, ha.put(i12)); 131 assertSameObject(null, ha.put(i22)); 132 assertEquals(2, ha.size()); 133 134 assertSameObject(i12, ha.remove(i11)); 135 assertSameObject(null, ha.get(i11)); 136 assertEquals(1, ha.size()); 137 138 assertSameObject(i22, ha.remove(i21)); 139 assertSameObject(null, ha.get(i21)); 140 assertEquals(0, ha.size()); 141 } 142 143 144 // test infrastructure 145 146 static HashArray<Integer> newHashArray() { 147 HashArray<Integer> ha = HashArray.newInstance(1); 148 assertSameObject(null, ha.get(i11)); 149 assertSameObject(null, ha.get(i12)); 150 assertSameObject(null, ha.get(i21)); 151 assertSameObject(null, ha.get(i22)); 152 assertEquals(0, ha.size()); 153 return ha; 154 } 155 156 static void assertSameObject(Object expected, Object actual) { 157 if (expected != actual) { 158 throw new AssertionError("Expected:\n " + expected + 159 "\nActual:\n " + actual); 160 161 } 162 } 163 164 static void assertEquals(Object expected, Object actual) { 165 if (!Objects.equals(expected, actual)) { 166 throw new AssertionError("Expected:\n " + expected + 167 "\nActual:\n " + actual); 168 169 } 170 } 171 172 // test proxy for package-private java.lang.HashArray 173 174 interface HashArray<T> { 175 176 @SuppressWarnings("unchecked") 177 public static <T> HashArray<T> newInstance(int maxExpectedSize) { 178 return (HashArray<T>) 179 TestProxy.newInstance(HashArray.class, 180 "java.lang.HashArray", 181 ClassLoader.getSystemClassLoader(), 182 new Class<?>[]{int.class}, 183 maxExpectedSize); 184 } 185 186 public int size(); 187 188 public T get(Object lookupObj); 189 190 public T put(T newElement); 191 192 public T putIfAbsent(T newElement); 193 194 public T remove(Object lookupObj); 195 196 public void clear(); 197 } 198 }