1 /*
   2  * Copyright (c) 2018, 2019, 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 /*
  26  * @test
  27  * @summary test VarHandle on inline class array
  28  * @compile -XDallowWithFieldOperator Point.java
  29  * @compile -XDallowWithFieldOperator Line.java
  30  * @compile -XDallowWithFieldOperator MutablePath.java
  31  * @compile -XDallowWithFieldOperator MixedValues.java NonFlattenValue.java
  32  * @run testng/othervm -XX:+EnableValhalla -XX:ValueArrayElemMaxFlatSize=-1 ArrayElementVarHandleTest
  33  * @run testng/othervm -XX:+EnableValhalla -XX:ValueArrayElemMaxFlatSize=0  ArrayElementVarHandleTest
  34  */
  35 
  36 import java.lang.invoke.*;
  37 
  38 import org.testng.annotations.DataProvider;
  39 import org.testng.annotations.Test;
  40 import static org.testng.Assert.*;
  41 
  42 public class ArrayElementVarHandleTest {
  43     private static final Point P = Point.makePoint(10, 20);
  44     private static final Line L = Line.makeLine(10, 20, 30, 40);
  45     private static final MutablePath PATH = MutablePath.makePath(10, 20, 30, 40);
  46 
  47     @DataProvider(name="arrayTests")
  48     static Object[][] arrayTests() {
  49         return new Object[][]{
  50             new Object[] { Point[].class,
  51                            new Point[] { Point.makePoint(1, 2),
  52                                          Point.makePoint(10, 20),
  53                                          Point.makePoint(100, 200)}},
  54             new Object[] { Point[][].class,
  55                            new Point[][] { new Point[] { Point.makePoint(1, 2),
  56                                                          Point.makePoint(10, 20)}}},
  57             new Object[] { Line[].class,
  58                            new Line[] { Line.makeLine(1, 2, 3, 4),
  59                                         Line.makeLine(10, 20, 30, 40),
  60                                         Line.makeLine(15, 25, 35, 45),
  61                                         Line.makeLine(20, 30, 40, 50)}},
  62             new Object[] { MutablePath[].class,
  63                            new MutablePath[] { MutablePath.makePath(1, 2, 3, 4),
  64                                                MutablePath.makePath(10, 20, 30, 40),
  65                                                MutablePath.makePath(15, 25, 35, 45),
  66                                                MutablePath.makePath(20, 30, 40, 50)}},
  67             new Object[] { MixedValues[].class,
  68                            new MixedValues[] { new MixedValues(P, L, PATH, "mixed", "values")}},
  69             new Object[] { NonFlattenValue[].class,
  70                            new NonFlattenValue[] { NonFlattenValue.make(1, 2),
  71                                                    NonFlattenValue.make(10, 20),
  72                                                    NonFlattenValue.make(100, 200)}},
  73         };
  74     }
  75 
  76     @Test(dataProvider="arrayTests")
  77     public void set(Class<?> arrayType, Object[] elements) throws Throwable {
  78         VarHandle vh = MethodHandles.arrayElementVarHandle(arrayType);
  79         MethodHandle ctor = MethodHandles.arrayConstructor(arrayType);
  80         Object[] array = (Object[])ctor.invoke(elements.length);
  81         for (int i=0; i < elements.length; i++) {
  82             vh.set(array, i, elements[i]);
  83         }
  84         for (int i=0; i < elements.length; i++) {
  85             Object v = (Object)vh.get(array, i);
  86             assertEquals(v, elements[i]);
  87         }
  88     }
  89 
  90     @Test(dataProvider="arrayTests")
  91     public void setVolatile(Class<?> arrayType, Object[] elements) throws Throwable {
  92         VarHandle vh = MethodHandles.arrayElementVarHandle(arrayType);
  93         MethodHandle ctor = MethodHandles.arrayConstructor(arrayType);
  94         Object[] array = (Object[])ctor.invoke(elements.length);
  95         for (int i=0; i < elements.length; i++) {
  96             vh.setVolatile(array, i, elements[i]);
  97         }
  98         for (int i=0; i < elements.length; i++) {
  99             Object v = (Object)vh.getVolatile(array, i);
 100             assertEquals(v, elements[i]);
 101         }
 102     }
 103 
 104     @Test(dataProvider="arrayTests")
 105     public void setOpaque(Class<?> arrayType, Object[] elements) throws Throwable {
 106         VarHandle vh = MethodHandles.arrayElementVarHandle(arrayType);
 107         MethodHandle ctor = MethodHandles.arrayConstructor(arrayType);
 108         Object[] array = (Object[])ctor.invoke(elements.length);
 109         for (int i=0; i < elements.length; i++) {
 110             vh.setOpaque(array, i, elements[i]);
 111         }
 112         for (int i=0; i < elements.length; i++) {
 113             Object v = (Object)vh.getOpaque(array, i);
 114             assertEquals(v, elements[i]);
 115         }
 116     }
 117 
 118     @Test(dataProvider="arrayTests")
 119     public void getAndSet(Class<?> arrayType, Object[] elements) throws Throwable {
 120         VarHandle vh = MethodHandles.arrayElementVarHandle(arrayType);
 121         MethodHandle ctor = MethodHandles.arrayConstructor(arrayType);
 122         Object[] array = (Object[])ctor.invoke(elements.length);
 123         for (int i=0; i < elements.length; i++) {
 124             Object o = vh.getAndSet(array, i, elements[i]);
 125         }
 126         for (int i=0; i < elements.length; i++) {
 127             Object v = (Object)vh.get(array, i);
 128             assertEquals(v, elements[i]);
 129         }
 130     }
 131 }