1 /*
   2  * Copyright (c) 2017, 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 package runtime.valhalla.valuetypes;
  25 
  26 import java.util.Arrays;
  27 
  28 import static jdk.test.lib.Asserts.*;
  29 
  30 /*
  31  * @test ValueTypeArray
  32  * @summary Plain array test for Value Types
  33  * @library /test/lib
  34  * @compile -XDenableValueTypes ValueTypeArray.java Point.java Long8Value.java Person.java
  35  * @run main/othervm -Xint -XX:+ValueArrayFlatten -XX:+EnableValhalla runtime.valhalla.valuetypes.ValueTypeArray
  36  */
  37 
  38 /*
  39  * @run main/othervm -Xint -XX:-ValueArrayFlatten -XX:+EnableValhalla runtime.valhalla.valuetypes.ValueTypeArray
  40  * @run main/othervm -Xcomp -XX:+ValueArrayFlatten -XX:+EnableValhalla runtime.valhalla.valuetypes.ValueTypeArray
  41  * @run main/othervm -Xcomp -XX:-ValueArrayFlatten -XX:+EnableValhalla runtime.valhalla.valuetypes.ValueTypeArray
  42  */
  43 public class ValueTypeArray {
  44     public static void main(String[] args) {
  45         ValueTypeArray valueTypeArray = new ValueTypeArray();
  46         valueTypeArray.run();
  47     }
  48 
  49     public void run() {
  50         // Class.forName does not support loading of DVT
  51         // - should call ValueType::arrayTypeClass instead?
  52         // testClassForName();
  53         testSimplePointArray();
  54         testLong8Array();
  55         // embedded oops not yet supported
  56         //testMixedPersonArray();
  57         testMultiDimPointArray();
  58         // Some design issues, ignore for now
  59         //testAtomicArray();
  60         //testArrayCopy();
  61         testReflectArray();
  62     }
  63 
  64     void testClassForName() {
  65         String arrayClsName = "[Qruntime.valhalla.valuetypes.Point;";
  66         try {
  67             Class<?> arrayCls = Class.forName(arrayClsName);
  68             assertTrue(arrayCls.isArray(), "Expected an array class");
  69             assertTrue(arrayCls.getComponentType() == Point.class,
  70                        "Expected component type of Point.class");
  71 
  72             arrayClsName = "[" + arrayClsName;
  73             Class<?> mulArrayCls = Class.forName(arrayClsName);
  74             assertTrue(mulArrayCls.isArray());
  75             assertTrue(mulArrayCls.getComponentType() == arrayCls);
  76         }
  77         catch (ClassNotFoundException cnfe) {
  78             fail("Class.forName(" + arrayClsName + ") failed", cnfe);
  79         }
  80     }
  81 
  82     void testSimplePointArray() {
  83         Point[] points = createSimplePointArray();
  84         checkSimplePointArray(points);
  85         System.gc(); // check that VTs survive GC
  86 
  87         assertTrue(points instanceof Point[], "Instance of");
  88 
  89         Point[] pointsCopy = new Point[points.length];
  90         System.arraycopy(points, 0, pointsCopy, 0, points.length);
  91         checkSimplePointArray(pointsCopy);
  92     }
  93 
  94     static Point[] createSimplePointArray() {
  95         Point[] ps = new Point[2];
  96         assertEquals(ps.length, 2, "Length");
  97         System.out.println(ps);
  98         ps[0] = Point.createPoint(1, 2);
  99         ps[1] = Point.createPoint(3, 4);
 100         System.gc(); // check that VTs survive GC
 101         return ps;
 102     }
 103 
 104     static void checkSimplePointArray(Point[] points) {
 105         assertEquals(points[0].x, 1, "invalid 0 point x value");
 106         assertEquals(points[0].y, 2, "invalid 0 point y value");
 107         assertEquals(points[1].x, 3, "invalid 1 point x value");
 108         assertEquals(points[1].y, 4, "invalid 1 point y value");
 109     }
 110 
 111     void testLong8Array() {
 112         Long8Value[] values = new Long8Value[3];
 113         assertEquals(values.length, 3, "length");
 114         System.out.println(values);
 115         Long8Value value = values[1];
 116         long zl = 0;
 117         Long8Value.check(value, zl, zl, zl, zl, zl, zl, zl, zl);
 118         values[1] = Long8Value.create(1, 2, 3, 4, 5, 6, 7, 8);
 119         value = values[1];
 120         Long8Value.check(value, 1, 2, 3, 4, 5, 6, 7, 8);
 121 
 122         Long8Value[] copy = new Long8Value[values.length];
 123         System.arraycopy(values, 0, copy, 0, values.length);
 124         value = copy[1];
 125         Long8Value.check(value, 1, 2, 3, 4, 5, 6, 7, 8);
 126     }
 127 
 128     void testMixedPersonArray() {
 129         Person[] people = new Person[3];
 130 
 131         people[0] = Person.create(1, "First", "Last");
 132         assertEquals(people[0].getId(), 1L, "Invalid Id");
 133         assertEquals(people[0].getFirstName(), "First", "Invalid First Name");
 134         assertEquals(people[0].getLastName(), "Last", "Invalid Last Name");
 135 
 136         people[1] = Person.create(2, "Jane", "Wayne");
 137         people[2] = Person.create(3, "Bob", "Dobalina");
 138 
 139         Person[] peopleCopy = new Person[people.length];
 140         System.arraycopy(people, 0, peopleCopy, 0, people.length);
 141         assertEquals(peopleCopy[2].getId(), 3L, "Invalid Id");
 142         assertEquals(peopleCopy[2].getFirstName(), "Bob", "Invalid First Name");
 143         assertEquals(peopleCopy[2].getLastName(), "Dobalina", "Invalid Last Name");
 144     }
 145 
 146     void testMultiDimPointArray() {
 147         Point[][][] multiPoints = new Point[2][3][4];
 148         assertEquals(multiPoints.length, 2, "1st dim length");
 149         assertEquals(multiPoints[0].length, 3, "2st dim length");
 150         assertEquals(multiPoints[0][0].length, 4, "3rd dim length");
 151 
 152         Point defaultPoint = multiPoints[1][2][3];
 153         assertEquals(defaultPoint.x, 0, "invalid point x value");
 154         assertEquals(defaultPoint.y, 0, "invalid point x value");
 155     }
 156 
 157 
 158     @SuppressWarnings("unchecked")
 159     void testArrayCopy() {
 160         // Test copy atomic vs relax combos...
 161         int testLength = 3;
 162         Long8Value long8Value = Long8Value.create(1, 2, 3, 4, 5, 6, 7, 8);
 163         Long8Value long8ValueZero = Long8Value.create(0, 0, 0, 0, 0, 0, 0, 0);
 164         Long8Value[] relaxedValues = new Long8Value[testLength];
 165         for (int i = 0; i < testLength; i++) {
 166             relaxedValues[i] = long8Value;
 167         }
 168 
 169         // relaxed -> relaxed
 170         Long8Value[] relaxedValues2 = new Long8Value[testLength];
 171         System.arraycopy(relaxedValues, 0, relaxedValues2, 0, testLength);
 172         Long8Value.check(relaxedValues2[testLength-1], 1, 2, 3, 4, 5, 6, 7, 8);
 173     }
 174 
 175     void testReflectArray() {
 176         // Check the java.lang.reflect.Array.newInstance methods...
 177         Class<?> cls = (Class<?>) Point[].class;
 178         Point[][] array = (Point[][]) java.lang.reflect.Array.newInstance(cls, 1);
 179         assertEquals(array.length, 1, "Incorrect length");
 180         assertTrue(array[0] == null, "Expected NULL");
 181 
 182         Point[][][] array3 = (Point[][][]) java.lang.reflect.Array.newInstance(cls, 1, 2);
 183         assertEquals(array3.length, 1, "Incorrect length");
 184         assertEquals(array3[0].length, 2, "Incorrect length");
 185         assertTrue(array3[0][0] == null, "Expected NULL");
 186 
 187         // Now create ObjArrays of ValueArray...
 188         cls = (Class<?>) Point.class;
 189         array = (Point[][]) java.lang.reflect.Array.newInstance(cls, 1, 2);
 190         assertEquals(array.length, 1, "Incorrect length");
 191         assertEquals(array[0].length, 2, "Incorrect length");
 192         Point p = array[0][1];
 193         int x = p.x;
 194         assertEquals(x, 0, "Bad Point Value");
 195     }
 196 
 197 }