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