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