1 /* 2 * Copyright (c) 2017, 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. 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 * @test 26 * @run testng/othervm -Xverify:none -XX:+EnableMVT TestValueInValue 27 */ 28 29 import jdk.incubator.mvt.ValueType; 30 31 import java.lang.invoke.MethodHandle; 32 import java.lang.invoke.MethodHandles; 33 import java.lang.invoke.MethodType; 34 35 import org.testng.annotations.Test; 36 37 import static org.testng.Assert.assertEquals; 38 39 @Test 40 public class TestValueInValue { 41 42 static final ValueType<?> VT_OUTER; 43 44 static final ValueType<?> VT_INNER; 45 46 static final Object[] TEST_OBJECTS; 47 48 static { 49 try { 50 MethodHandles.Lookup lookup = MethodHandles.lookup(); 51 VT_INNER = ValueType.make(lookup, "Inner", new String[] {"f_i1", "f_i2"}, new Class<?>[] {int.class, String.class}); 52 VT_OUTER = ValueType.make(lookup, "Outer", new String[] {"f_o1", "f_o2"}, new Class<?>[] {VT_INNER.valueClass(), double.class}); 53 MethodHandle c_i = lookup.findConstructor(VT_INNER.boxClass(), MethodType.methodType(void.class, int.class, String.class)); 54 MethodHandle c_o = lookup.findConstructor(VT_OUTER.boxClass(), MethodType.methodType(void.class, VT_INNER.valueClass(), double.class)); 55 TEST_OBJECTS = new Object[] { 56 c_o.invoke(c_i.invoke(42, "One"), 42d), 57 c_o.invoke(c_i.invoke(42, "Two"), 42d), 58 c_o.invoke(c_i.invoke(42, "Three"), 42d), 59 c_o.invoke(c_i.invoke(42, "Four"), 42d), 60 }; 61 } catch (Throwable t) { 62 throw new AssertionError(t); 63 } 64 } 65 66 public void testHashCode() throws Throwable { 67 for (int i = 0 ; i < TEST_OBJECTS.length ; i++) { 68 assertEquals((int)VT_OUTER.substitutabilityHashCode().invoke(TEST_OBJECTS[i]), TEST_OBJECTS[i].hashCode()); 69 } 70 } 71 72 public void testEquals() throws Throwable { 73 for (int i = 0 ; i < TEST_OBJECTS.length ; i++) { 74 for (int j = 0 ; j < TEST_OBJECTS.length ; j++) { 75 assertEquals((boolean)VT_OUTER.substitutabilityTest().invoke(TEST_OBJECTS[i], TEST_OBJECTS[j]), 76 TEST_OBJECTS[i].equals(TEST_OBJECTS[j])); 77 } 78 } 79 } 80 81 public void testArray() throws Throwable { 82 Object arr = MethodHandles.arrayConstructor(VT_OUTER.arrayValueClass()).invoke(TEST_OBJECTS.length); 83 int index = 0; 84 for (Object o : TEST_OBJECTS) { 85 MethodHandles.arrayElementSetter(VT_OUTER.arrayValueClass()).invoke(arr, index, o); 86 Object o2 = MethodHandles.arrayElementGetter(VT_OUTER.arrayValueClass()).invoke(arr, index); 87 assertEquals(o, o2); 88 index++; 89 } 90 } 91 }