1 /* 2 * Copyright (c) 2016, 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 -Xint -Xverify:none -XX:+EnableMVT -XX:+ValueArrayFlatten MVTTest 27 * @run testng/othervm -Xint -Xverify:none -XX:+EnableMVT -XX:-ValueArrayFlatten MVTTest 28 * @run testng/othervm -Xint -Xverify:none -XX:+EnableMVT -Dvalhalla.enableValueLambdaForms=true MVTTest 29 * @run testng/othervm -Xint -Xverify:none -XX:+EnableMVT -Dvalhalla.enableValueLambdaForms=true -Dvalhalla.enablePoolPatches=true MVTTest 30 */ 31 32 import jdk.experimental.value.ValueType; 33 import org.testng.annotations.Test; 34 import valhalla.shady.MinimalValueTypes_1_0; 35 36 import java.lang.invoke.MethodHandle; 37 import java.lang.invoke.MethodHandles; 38 import java.lang.reflect.Field; 39 40 import static java.lang.invoke.MethodType.methodType; 41 import static org.testng.Assert.assertEquals; 42 43 @Test 44 public class MVTTest { 45 static final Class<?> DVT; 46 47 static final ValueType<?> VT = ValueType.forClass(Point.class); 48 49 static final Class<?>[] FIELD_TYPES; 50 51 static final String[] FIELD_NAMES; 52 53 static String TEMPLATE = "Point[x=#x, y=#y, z=#z]"; 54 55 static final Object[] FIELD_VALUES = {42, (short) 43, (short) 44}; 56 57 static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); 58 59 static final MethodHandle PRINT_POINT; 60 61 static { 62 DVT = MinimalValueTypes_1_0.getValueTypeClass(Point.class); 63 64 Field[] fs = Point.class.getFields(); 65 66 FIELD_TYPES = new Class<?>[fs.length]; 67 FIELD_NAMES = new String[fs.length]; 68 69 for (int i = 0; i < fs.length; i++) { 70 FIELD_TYPES[i] = fs[i].getType(); 71 FIELD_NAMES[i] = fs[i].getName(); 72 } 73 74 try { 75 PRINT_POINT = LOOKUP.findStatic(MVTTest.class, "print", methodType(String.class, Point.class)) 76 .asType(methodType(String.class, DVT)); 77 } 78 catch (Exception e) { 79 throw new RuntimeException(e); 80 } 81 } 82 83 public void testDefaultValue() throws Throwable { 84 for (int i = 0; i < FIELD_NAMES.length; i++) { 85 MethodHandle getter = MethodHandles.collectArguments( 86 VT.findGetter(LOOKUP, FIELD_NAMES[i], FIELD_TYPES[i]), 87 0, 88 VT.defaultValueConstant()); 89 90 assertEquals((int) getter.invoke(), 0); 91 } 92 } 93 94 public void testWither() throws Throwable { 95 for (int i = 0; i < FIELD_NAMES.length; i++) { 96 MethodHandle wither = MethodHandles.collectArguments( 97 VT.findWither(LOOKUP, FIELD_NAMES[i], FIELD_TYPES[i]), 0, VT.defaultValueConstant()); 98 String expected = TEMPLATE.replace("#" + FIELD_NAMES[i], String.valueOf(FIELD_VALUES[i])) 99 .replaceAll("#[xyz]", "0"); 100 101 assertEquals(printReturn(wither).invoke(FIELD_VALUES[i]), expected); 102 } 103 } 104 105 public void testSubstitutability() throws Throwable { 106 Point[] pts = {new Point(1, (short) 6, (short) 3), new Point(1, (short) 2, (short) 3)}; 107 108 MethodHandle substTest = VT.substitutabilityTest(); 109 for (Point p1 : pts) { 110 for (Point p2 : pts) { 111 assertEquals((boolean) substTest.invoke(p1, p2), p1.equals(p2)); 112 } 113 } 114 115 MethodHandle hash = VT.substitutabilityHashCode(); 116 for (Point p1 : pts) { 117 for (Point p2 : pts) { 118 boolean vHashEq = (int) hash.invoke(p1) == (int) hash.invoke(p2); 119 boolean rHashEq = p1.hashCode() == p2.hashCode(); 120 assertEquals(vHashEq, rHashEq); 121 } 122 } 123 } 124 125 public void testIdentity() throws Throwable { 126 String actual = (String) printReturn(MethodHandles.identity(VT.valueClass())) 127 .invoke(new Point(1, (short) 2, (short) 3)); 128 assertEquals(actual, "Point[x=1, y=2, z=3]"); 129 } 130 131 public void testZero() throws Throwable { 132 String actual = (String) printReturn(MethodHandles.zero(VT.valueClass())) 133 .invoke(); 134 assertEquals(actual, "Point[x=0, y=0, z=0]"); 135 } 136 137 public void testEmpty() throws Throwable { 138 String actual = (String) printReturn(MethodHandles.empty(methodType(VT.valueClass(), int.class, String.class))) 139 .invoke(1, ""); 140 assertEquals(actual, "Point[x=0, y=0, z=0]"); 141 } 142 143 public void testArray1D() throws Throwable { 144 //test monodimensional array 145 Object arr = MethodHandles.arrayConstructor(VT.arrayValueClass()).invoke(10); 146 for (int i = 0; i < 10; i++) { 147 Point p = new Point(i, (short) 9, (short) 9); 148 MethodHandles.arrayElementSetter(VT.arrayValueClass()).invoke(arr, i, p); 149 } 150 for (int i = 0; i < 10; i++) { 151 String actual = (String) printReturn(MethodHandles.arrayElementGetter(VT.arrayValueClass())) 152 .invoke(arr, i); 153 String expected = TEMPLATE.replace("#x", String.valueOf(i)) 154 .replaceAll("#[yz]", "9"); 155 assertEquals(actual, expected); 156 } 157 } 158 159 public void testArray10D() throws Throwable { 160 //test multidimensional array 161 Object[] arr2 = (Object[]) MethodHandles.arrayConstructor(VT.arrayValueClass(2)).invoke(10); 162 for (int i = 0; i < 10; i++) { 163 Object innerArr = MethodHandles.arrayConstructor(VT.arrayValueClass()).invoke(10); 164 MethodHandles.arrayElementSetter(VT.arrayValueClass(2)).invoke(arr2, i, innerArr); 165 for (int j = 0; i < 10; i++) { 166 Point p = new Point(i, (short) j, (short) 9); 167 MethodHandles.arrayElementSetter(VT.arrayValueClass()).invoke(innerArr, i, p); 168 } 169 } 170 for (int i = 0; i < 10; i++) { 171 Object innerArr = MethodHandles.arrayElementGetter(VT.arrayValueClass(2)).invoke(arr2, i); 172 for (int j = 0; i < 10; i++) { 173 String actual = (String) printReturn(MethodHandles.arrayElementGetter(VT.arrayValueClass())) 174 .invoke(innerArr, i); 175 String expected = TEMPLATE.replace("#x", String.valueOf(i)) 176 .replace("#y", String.valueOf(j)) 177 .replace("#z", "9"); 178 assertEquals(actual, expected); 179 } 180 } 181 } 182 183 public void testMultiArray() throws Throwable { 184 Object[] arr43 = (Object[]) VT.newMultiArray(2).invoke(4, 3); 185 for (int i = 0; i < 4; i++) { 186 Object innerArr = arr43[i]; 187 for (int j = 0; i < 3; i++) { 188 Point p = new Point(i, (short) j, (short) 9); 189 MethodHandles.arrayElementSetter(VT.arrayValueClass()).invoke(innerArr, i, p); 190 } 191 } 192 for (int i = 0; i < 4; i++) { 193 Object innerArr = MethodHandles.arrayElementGetter(VT.arrayValueClass(2)).invoke(arr43, i); 194 for (int j = 0; i < 3; i++) { 195 String actual = (String) printReturn(MethodHandles.arrayElementGetter(VT.arrayValueClass())) 196 .invoke(innerArr, i); 197 String expected = TEMPLATE.replace("#x", String.valueOf(i)) 198 .replace("#y", String.valueOf(j)) 199 .replace("#z", "9"); 200 assertEquals(actual, expected); 201 } 202 } 203 } 204 205 public void testLoop() throws Throwable { 206 Object arr = MethodHandles.arrayConstructor(VT.arrayValueClass()).invoke(10); 207 for (int i = 0; i < 10; i++) { 208 Point p = new Point(i, (short) 9, (short) 9); 209 MethodHandles.arrayElementSetter(VT.arrayValueClass()).invoke(arr, i, p); 210 } 211 212 /* 213 iters -> (Point[] )int 214 215 init -> (Point[] )int 216 217 sum -> (int, int, int, int)int 218 a -> (int, Point, Point, Point)int 219 b -> (int, Point)int 220 c -> (int, Point[], int)int 221 body -> (int, int, Point[])int 222 */ 223 224 MethodHandle iters = MethodHandles.arrayLength(VT.arrayValueClass()); 225 226 MethodHandle init = MethodHandles.dropArguments(MethodHandles.constant(int.class, 0), 227 0, 228 VT.arrayValueClass()); 229 230 MethodHandle sum = LOOKUP.findStatic(MVTTest.class, 231 "sum", 232 methodType(int.class, int.class, int.class, short.class, short.class)); 233 234 MethodHandle a = MethodHandles.filterArguments(sum, 1, 235 VT.findGetter(LOOKUP, FIELD_NAMES[0], FIELD_TYPES[0]), 236 VT.findGetter(LOOKUP, FIELD_NAMES[1], FIELD_TYPES[1]), 237 VT.findGetter(LOOKUP, FIELD_NAMES[2], FIELD_TYPES[2])); 238 239 MethodHandle b = MethodHandles.permuteArguments(a, 240 methodType(int.class, int.class, VT.valueClass()), 241 0, 1, 1, 1); 242 243 MethodHandle c = MethodHandles.collectArguments(b, 244 1, 245 MethodHandles.arrayElementGetter(VT.arrayValueClass())); 246 247 MethodHandle body = MethodHandles.permuteArguments(c, 248 methodType(int.class, int.class, int.class, VT.arrayValueClass()), 249 0, 2, 1); 250 251 MethodHandle loop = MethodHandles.countedLoop(iters, init, body); 252 int actual = (int) loop.invoke(arr); 253 int expected = 9 * 10 * 2 + 10 * (0 + 9) / 2; 254 assertEquals(actual, expected); 255 } 256 257 static int sum(int v, int x, short y, short z) { 258 return v + x + y + z; 259 } 260 261 static MethodHandle printReturn(MethodHandle mh) { 262 return MethodHandles.filterReturnValue(mh, PRINT_POINT); 263 } 264 265 static String print(Point p) { 266 return String.format("Point[x=%d, y=%d, z=%d]", p.x, p.y, p.z); 267 } 268 }