< prev index next >

test/valhalla/mvt/MVTTest.java

Print this page
rev 17237 : [mq]: mvt-test


   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 main/othervm -Xint -Xverify:none -XX:+EnableMVT -XX:+ValueArrayFlatten MVTTest
  29  * @run main/othervm -Xint -Xverify:none -XX:+EnableMVT -XX:-ValueArrayFlatten MVTTest
  30  * @run main/othervm -Xint -Xverify:none -XX:+EnableMVT -Dvalhalla.enableValueLambdaForms=true MVTTest
  31  * @run main/othervm -Xint -Xverify:none -XX:+EnableMVT -Dvalhalla.enableValueLambdaForms=true -Dvalhalla.enablePoolPatches=true MVTTest
  32  */
  33 
  34 import jdk.experimental.value.ValueType;


  35 
  36 import java.lang.invoke.MethodHandle;
  37 import java.lang.invoke.MethodHandles;
  38 import java.lang.invoke.MethodType;
  39 




  40 public class MVTTest {




























  41 
  42     public static void main(String[] args) throws Throwable {
  43         MethodHandles.Lookup lookup = MethodHandles.lookup();
  44         Class<?> VCC = Point.class;
  45         ValueType<?> VT = ValueType.forClass(VCC);
  46         Class<?> DVT = VT.valueClass();
  47         String[] fieldNames = {"x", "y", "z"};
  48         Class<?>[] fieldTypes = {int.class, short.class, short.class};
  49         Object[] fieldValues = {42, (short) 43, (short) 44};
  50 
  51         MethodHandle printPoint = lookup.findStatic(MVTTest.class, "print", MethodType.methodType(String.class, VCC))
  52                 .asType(MethodType.methodType(String.class, DVT));
  53 
  54         for (int i = 0; i < fieldNames.length; i++) {
  55             MethodHandle getter = MethodHandles.collectArguments(VT.findGetter(lookup, fieldNames[i], fieldTypes[i]), 0, VT.defaultValueConstant());
  56             assertTrue((int) getter.invoke() == 0);
  57         }
  58 
  59         String template = "Point[x=#x, y=#y, z=#z]";
  60         for (int i = 0; i < fieldNames.length; i++) {
  61             MethodHandle wither = MethodHandles.collectArguments(VT.findWither(lookup, fieldNames[i], fieldTypes[i]), 0, VT.defaultValueConstant());
  62             String expected = template.replace("#" + fieldNames[i], String.valueOf(fieldValues[i]))









  63                     .replaceAll("#[xyz]", "0");
  64 
  65             assertTrue(MethodHandles.filterReturnValue(wither, printPoint).invoke(fieldValues[i]).equals(expected));

  66         }
  67 

  68         Point[] pts = {new Point(1, (short) 6, (short) 3), new Point(1, (short) 2, (short) 3)};
  69 
  70         MethodHandle substTest = VT.substitutabilityTest();
  71         for (Point p1 : pts) {
  72             for (Point p2 : pts) {
  73                 assertTrue((boolean) substTest.invoke(p1, p2) == (p1.equals(p2)));
  74             }
  75         }
  76 
  77         MethodHandle hash = VT.substitutabilityHashCode();
  78         for (Point p1 : pts) {
  79             for (Point p2 : pts) {
  80                 boolean vHashEq = (int) hash.invoke(p1) == (int) hash.invoke(p2);
  81                 boolean rHashEq = p1.hashCode() == p2.hashCode();
  82                 assertTrue(vHashEq == rHashEq);

  83             }
  84         }
  85 
  86         MethodHandle vConstr = lookup.findConstructor(VT.valueClass(), MethodType.methodType(void.class, int.class, short.class, short.class));
  87         assertTrue(vConstr != null);
  88 
  89         assertTrue(MethodHandles.filterReturnValue(MethodHandles.identity(VT.valueClass()), printPoint)
  90                 .invoke(new Point(1, (short) 2, (short) 3)).equals("Point[x=1, y=2, z=3]")); //identity
  91 
  92         assertTrue(MethodHandles.filterReturnValue(MethodHandles.zero(VT.valueClass()), printPoint)
  93                 .invoke().equals("Point[x=0, y=0, z=0]")); //zero



  94 
  95         assertTrue(MethodHandles.filterReturnValue(MethodHandles.empty(MethodType.methodType(VT.valueClass(), int.class, String.class)), printPoint)
  96                 .invoke(1, "").equals("Point[x=0, y=0, z=0]")); //empty



  97 

  98         //test monodimensional array
  99         Object arr = MethodHandles.arrayConstructor(VT.arrayValueClass()).invoke(10);
 100         for (int i = 0; i < 10; i++) {
 101             Point p = new Point(i, (short) 9, (short) 9);
 102             MethodHandles.arrayElementSetter(VT.arrayValueClass()).invoke(arr, i, p);
 103         }
 104         for (int i = 0; i < 10; i++) {
 105             String expected = template.replace("#x", String.valueOf(i))


 106                     .replaceAll("#[yz]", "9");
 107             assertTrue(MethodHandles.filterReturnValue(MethodHandles.arrayElementGetter(VT.arrayValueClass()), printPoint)
 108                     .invoke(arr, i).equals(expected));
 109         }
 110 

 111         //test multidimensional array
 112         Object[] arr2 = (Object[]) MethodHandles.arrayConstructor(VT.arrayValueClass(2)).invoke(10);
 113         for (int i = 0; i < 10; i++) {
 114             Object innerArr = MethodHandles.arrayConstructor(VT.arrayValueClass()).invoke(10);
 115             MethodHandles.arrayElementSetter(VT.arrayValueClass(2)).invoke(arr2, i, innerArr);
 116             for (int j = 0; i < 10; i++) {
 117                 Point p = new Point(i, (short) j, (short) 9);
 118                 MethodHandles.arrayElementSetter(VT.arrayValueClass()).invoke(innerArr, i, p);
 119             }
 120         }
 121         for (int i = 0; i < 10; i++) {
 122             Object innerArr = MethodHandles.arrayElementGetter(VT.arrayValueClass(2)).invoke(arr2, i);
 123             for (int j = 0; i < 10; i++) {
 124                 String expected = template.replace("#x", String.valueOf(i))


 125                         .replace("#y", String.valueOf(j))
 126                         .replace("#z", "9");
 127                 assertTrue(MethodHandles.filterReturnValue(MethodHandles.arrayElementGetter(VT.arrayValueClass()), printPoint)
 128                         .invoke(innerArr, i).equals(expected));
 129             }
 130         }
 131 

 132         Object[] arr43 = (Object[]) VT.newMultiArray(2).invoke(4, 3);
 133         for (int i = 0; i < 4; i++) {
 134             Object innerArr = arr43[i];
 135             for (int j = 0; i < 3; i++) {
 136                 Point p = new Point(i, (short) j, (short) 9);
 137                 MethodHandles.arrayElementSetter(VT.arrayValueClass()).invoke(innerArr, i, p);
 138             }
 139         }
 140         for (int i = 0; i < 4; i++) {
 141             Object innerArr = MethodHandles.arrayElementGetter(VT.arrayValueClass(2)).invoke(arr2, i);
 142             for (int j = 0; i < 3; i++) {
 143                 String expected = template.replace("#x", String.valueOf(i))


 144                         .replace("#y", String.valueOf(j))
 145                         .replace("#z", "9");
 146                 assertTrue(MethodHandles.filterReturnValue(MethodHandles.arrayElementGetter(VT.arrayValueClass()), printPoint)
 147                         .invoke(innerArr, i).equals(expected));
 148             }
 149         }
 150 
 151         System.err.println("Checks executed: " + assertCount);




 152     }
 153 
 154     static int assertCount = 0;



 155 
 156     static void assertTrue(boolean cond) {
 157         assertCount++;
 158         if (!cond) {
 159             throw new AssertionError();


































 160         }







 161     }
 162 
 163     static String print(Point p) {
 164         return String.format("Point[x=%d, y=%d, z=%d]", p.x, p.y, p.z);
 165     }
 166 }


   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 }
< prev index next >