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