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 package runtime.valhalla.valuetypes;
  25 
  26 import java.lang.reflect.Array;
  27 import java.util.Arrays;
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 
  31 import static jdk.test.lib.Asserts.*;
  32 
  33 /*
  34  * @test ValueTypeArray
  35  * @summary Plain array test for Value Types
  36  * @library /test/lib
  37  * @compile -XDemitQtypes -XDenableValueTypes -XDallowWithFieldOperator -XDallowFlattenabilityModifiers -XDallowGenericsOverValues ValueTypeArray.java Point.java Long8Value.java Person.java
  38  * @run main/othervm -Xint  -XX:ValueArrayElemMaxFlatSize=-1 -XX:+EnableValhalla runtime.valhalla.valuetypes.ValueTypeArray
  39  * @run main/othervm -Xint  -XX:ValueArrayElemMaxFlatSize=0  -XX:+EnableValhalla runtime.valhalla.valuetypes.ValueTypeArray
  40  * @run main/othervm -Xcomp -XX:ValueArrayElemMaxFlatSize=-1 -XX:+EnableValhalla runtime.valhalla.valuetypes.ValueTypeArray
  41  * @run main/othervm -Xcomp -XX:ValueArrayElemMaxFlatSize=0  -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         testClassForName();
  51         testSimplePointArray();
  52         testLong8Array();
  53         testMixedPersonArray();
  54         testMultiDimPointArray();
  55         testComposition();
  56 
  57         testSanityCheckcasts();
  58         testObjectArrayOfValues();
  59 
  60         testReflectArray();
  61         testUtilArrays();
  62     }
  63 
  64     void testClassForName() {
  65         String arrayClsName = "[Lruntime.valhalla.valuetypes.Point;";
  66         String qarrayClsName = "[Qruntime.valhalla.valuetypes.Point;";
  67         try {
  68             // L-type..
  69             Class<?> arrayCls = Class.forName(arrayClsName);
  70             assertTrue(arrayCls.isArray(), "Expected an array class");
  71 
  72             assertTrue(arrayCls.getComponentType() == Point.class.asBoxType(),
  73                        "Expected component type of Point.class got: " + arrayCls.getComponentType());
  74 
  75             arrayClsName = "[" + arrayClsName;
  76             Class<?> mulArrayCls = Class.forName(arrayClsName);
  77             assertTrue(mulArrayCls.isArray());
  78             assertTrue(mulArrayCls.getComponentType() == arrayCls);
  79 
  80             // Q-type...
  81             arrayCls = Class.forName(qarrayClsName);
  82             assertTrue(arrayCls.isArray(), "Expected an array class");
  83 
  84             assertTrue(arrayCls.getComponentType() == Point.class.asValueType(),
  85                        arrayCls +
  86                        " Expected component type of Point.class got: " + arrayCls.getComponentType());
  87 
  88             qarrayClsName = "[" + qarrayClsName;
  89             mulArrayCls = Class.forName(qarrayClsName);
  90             assertTrue(mulArrayCls.isArray());
  91             assertTrue(mulArrayCls.getComponentType() == arrayCls);
  92         }
  93         catch (ClassNotFoundException cnfe) {
  94             fail("Class.forName(" + arrayClsName + ") failed", cnfe);
  95         }
  96     }
  97 
  98     void testSimplePointArray() {
  99         Point[] defaultPoint = new Point[1];
 100         Point p = defaultPoint[0];
 101         assertEquals(p.x, 0, "invalid default loaded from array");
 102         assertEquals(p.y, 0, "invalid default loaded from array");
 103         boolean gotNpe = false;
 104         try {
 105             defaultPoint[0] = (Point) getNull();
 106         } catch (NullPointerException npe) {
 107             gotNpe = true;
 108         }
 109         assertTrue(gotNpe, "Expected NullPointerException");
 110 
 111         Point[] points = createSimplePointArray();
 112         checkSimplePointArray(points);
 113         System.gc(); // check that VTs survive GC
 114 
 115         assertTrue(points instanceof Point[], "Instance of");
 116 
 117         Point[] pointsCopy = new Point[points.length];
 118         System.arraycopy(points, 0, pointsCopy, 0, points.length);
 119         checkSimplePointArray(pointsCopy);
 120     }
 121 
 122     static Point[] createSimplePointArray() {
 123         Point[] ps = new Point[2];
 124         assertEquals(ps.length, 2, "Length");
 125         ps.toString();
 126         ps[0] = Point.createPoint(1, 2);
 127         ps[1] = Point.createPoint(3, 4);
 128         boolean sawOob = false;
 129         try {
 130             ps[2] = Point.createPoint(0, 0);
 131         } catch (ArrayIndexOutOfBoundsException aioobe) { sawOob = true; }
 132         assertTrue(sawOob, "Didn't see AIOOBE");
 133         System.gc(); // check that VTs survive GC
 134         return ps;
 135     }
 136 
 137     static void checkSimplePointArray(Point[] points) {
 138         assertEquals(points[0].x, 1, "invalid 0 point x value");
 139         assertEquals(points[0].y, 2, "invalid 0 point y value");
 140         assertEquals(points[1].x, 3, "invalid 1 point x value");
 141         assertEquals(points[1].y, 4, "invalid 1 point y value");
 142     }
 143 
 144     void testLong8Array() {
 145         Long8Value[] values = new Long8Value[3];
 146         assertEquals(values.length, 3, "length");
 147         values.toString();
 148         Long8Value value = values[1];
 149         long zl = 0;
 150         Long8Value.check(value, zl, zl, zl, zl, zl, zl, zl, zl);
 151         values[1] = Long8Value.create(1, 2, 3, 4, 5, 6, 7, 8);
 152         value = values[1];
 153         Long8Value.check(value, 1, 2, 3, 4, 5, 6, 7, 8);
 154 
 155         Long8Value[] copy = new Long8Value[values.length];
 156         System.arraycopy(values, 0, copy, 0, values.length);
 157         value = copy[1];
 158         Long8Value.check(value, 1, 2, 3, 4, 5, 6, 7, 8);
 159     }
 160 
 161     void testMixedPersonArray() {
 162         Person[] people = new Person[3];
 163 
 164         people[0] = Person.create(1, "First", "Last");
 165         assertEquals(people[0].getId(), 1, "Invalid Id person");
 166         assertEquals(people[0].getFirstName(), "First", "Invalid First Name");
 167         assertEquals(people[0].getLastName(), "Last", "Invalid Last Name");
 168 
 169         people[1] = Person.create(2, "Jane", "Wayne");
 170         people[2] = Person.create(3, "Bob", "Dobalina");
 171 
 172         Person[] peopleCopy = new Person[people.length];
 173         System.arraycopy(people, 0, peopleCopy, 0, people.length);
 174         assertEquals(peopleCopy[2].getId(), 3, "Invalid Id");
 175         assertEquals(peopleCopy[2].getFirstName(), "Bob", "Invalid First Name");
 176         assertEquals(peopleCopy[2].getLastName(), "Dobalina", "Invalid Last Name");
 177     }
 178 
 179     void testMultiDimPointArray() {
 180         Point[][][] multiPoints = new Point[2][3][4];
 181         assertEquals(multiPoints.length, 2, "1st dim length");
 182         assertEquals(multiPoints[0].length, 3, "2st dim length");
 183         assertEquals(multiPoints[0][0].length, 4, "3rd dim length");
 184 
 185         Point defaultPoint = multiPoints[1][2][3];
 186         assertEquals(defaultPoint.x, 0, "invalid point x value");
 187         assertEquals(defaultPoint.y, 0, "invalid point x value");
 188     }
 189 
 190     void testReflectArray() {
 191         // Check the java.lang.reflect.Array.newInstance methods...
 192         Class<?> cls = (Class<?>) Point[].class;
 193         Point[][] array = (Point[][]) Array.newInstance(cls, 1);
 194         assertEquals(array.length, 1, "Incorrect length");
 195         assertTrue(array[0] == null, "Expected NULL");
 196 
 197         Point[][][] array3 = (Point[][][]) Array.newInstance(cls, 1, 2);
 198         assertEquals(array3.length, 1, "Incorrect length");
 199         assertEquals(array3[0].length, 2, "Incorrect length");
 200         assertTrue(array3[0][0] == null, "Expected NULL");
 201 
 202         // Now create ObjArrays of ValueArray...
 203         cls = (Class<?>) Point.class.asBoxType();
 204         Point.box[][] barray = (Point.box[][]) Array.newInstance(cls, 1, 2);
 205         assertEquals(barray.length, 1, "Incorrect length");
 206         assertEquals(barray[0].length, 2, "Incorrect length");
 207         barray[0][1] = Point.createPoint(1, 2);
 208         Point.box pb = barray[0][1];
 209         int x = pb.getX();
 210         assertEquals(x, 1, "Bad Point Value");
 211     }
 212 
 213     static final value class MyInt implements Comparable<MyInt> {
 214         final int value;
 215 
 216         private MyInt() { value = 0; }
 217         public int getValue() { return value; }
 218         public String toString() { return "MyInt: " + getValue(); }
 219         public int compareTo(MyInt that) { return Integer.compare(this.getValue(), that.getValue()); }
 220         public boolean equals(Object o) {
 221             if (o instanceof MyInt) {
 222                 return this.getValue() == ((MyInt) o).getValue();
 223             }
 224             return false;
 225         }
 226 
 227         public static MyInt create(int v) {
 228             MyInt mi = MyInt.default;
 229             mi = __WithField(mi.value, v);
 230             return mi;
 231         }
 232 
 233         public static final MyInt.box MIN = MyInt.create(Integer.MIN_VALUE);
 234         public static final MyInt.box ZERO = MyInt.create(0);
 235         public static final MyInt.box MAX = MyInt.create(Integer.MAX_VALUE);
 236     }
 237 
 238     static MyInt staticMyInt = MyInt.create(-1);
 239     static MyInt[] staticMyIntArray = new MyInt[] { staticMyInt };
 240     static MyInt[][] staticMyIntArrayArray = new MyInt[][] { staticMyIntArray, staticMyIntArray };
 241 
 242     static interface SomeSecondaryType {
 243         default String hi() { return "Hi"; }
 244     }
 245 
 246     static final value class MyOtherInt implements SomeSecondaryType {
 247         final int value;
 248         private MyOtherInt() { value = 0; }
 249     }
 250 
 251     void testSanityCheckcasts() {
 252         MyInt[] myInts = new MyInt[1];
 253         assertTrue(myInts instanceof Object[]);
 254         assertTrue(myInts instanceof Comparable[]);
 255 
 256         Class<?> cls = MyInt.class.asValueType();
 257         assertTrue(cls.isValue());
 258         Object arrObj = Array.newInstance(cls, 1);
 259         assertTrue(arrObj instanceof Object[], "Not Object array");
 260         assertTrue(arrObj instanceof Comparable[], "Not Comparable array");
 261         assertTrue(arrObj instanceof MyInt[], "Not MyInt array");
 262 
 263         Object[] arr = (Object[]) arrObj;
 264         assertTrue(arr instanceof Comparable[], "Not Comparable array");
 265         assertTrue(arr instanceof MyInt[], "Not MyInt array");
 266         Comparable[] comparables = (Comparable[])arr;
 267         MyInt[] myIntArr = (MyInt[]) arr;
 268 
 269         // multi-dim, check secondary array types are setup...
 270         MyOtherInt[][] matrix = new MyOtherInt[1][1];
 271         assertTrue(matrix[0] instanceof MyOtherInt[]);
 272         assertTrue(matrix[0] instanceof SomeSecondaryType[]);
 273 
 274         // Box types vs Value...
 275         MyInt.box[] myValueRefs = new MyInt.box[1];
 276         assertTrue(myValueRefs instanceof MyInt.box[]);
 277         assertTrue(myValueRefs instanceof Object[]);
 278         assertTrue(myValueRefs instanceof Comparable[]);
 279     }
 280 
 281 
 282     void testUtilArrays() {
 283         // Sanity check j.u.Arrays
 284         MyInt[] myInts = new MyInt[] { MyInt.MAX, MyInt.MIN };
 285         // Sanity sort another copy
 286         MyInt[] copyMyInts = Arrays.copyOf(myInts, myInts.length + 1);
 287         checkArrayElementsEqual(copyMyInts, new MyInt[] { myInts[0], myInts[1], MyInt.ZERO});
 288 
 289         Arrays.sort(copyMyInts);
 290         checkArrayElementsEqual(copyMyInts, new MyInt[] { MyInt.MIN, MyInt.ZERO, MyInt.MAX });
 291 
 292         List myIntList = Arrays.asList(copyMyInts);
 293         checkArrayElementsEqual(copyMyInts, myIntList.toArray(new MyInt[copyMyInts.length]));
 294         // This next line needs testMixedLayoutArrays to work
 295         checkArrayElementsEqual(copyMyInts, myIntList.toArray());
 296 
 297         // Sanity check j.u.ArrayList
 298         ArrayList<MyInt> aList = new ArrayList<MyInt>(Arrays.asList(copyMyInts));
 299         assertTrue(aList.indexOf(MyInt.MIN) == 0, "Bad Index");
 300         assertTrue(aList.indexOf(MyInt.ZERO) == 1, "Bad Index");
 301         assertTrue(aList.indexOf(MyInt.MAX) == 2, "Bad Index");
 302 
 303         aList.remove(2);
 304         aList.add(MyInt.create(5));
 305     }
 306 
 307 
 308     void testObjectArrayOfValues() {
 309         testSanityObjectArrays();
 310         testMixedLayoutArrays();
 311     }
 312 
 313     void testSanityObjectArrays() {
 314         Object[] objects = new Object[2];
 315         assertTrue(objects[0] == null && objects[1] == null, "Not null ?");
 316 
 317         objects[0] = MyInt.create(1);
 318         objects[1] = Integer.valueOf(2);
 319         assertTrue(objects[0].equals(MyInt.create(1)), "Bad Value");
 320         assertTrue(objects[1].equals(Integer.valueOf(2)), "Bad Object");
 321 
 322         Comparable[] copyComparables = new Comparable[objects.length];
 323         System.arraycopy(objects, 0, copyComparables, 0, objects.length);
 324         checkArrayElementsEqual(objects, copyComparables);
 325 
 326         objects[0] = null;
 327         objects[1] = null;
 328         assertTrue(objects[0] == null && objects[1] == null, "Not null ?");
 329 
 330         Comparable[] comparables = new Comparable[2];
 331         assertTrue(comparables[0] == null && comparables[1] == null, "Not null ?");
 332         comparables[0] = MyInt.create(3);
 333         comparables[1] = Integer.valueOf(4);
 334         assertTrue(comparables[0].equals(MyInt.create(3)), "Bad Value");
 335         assertTrue(comparables[1].equals(Integer.valueOf(4)), "Bad Object");
 336 
 337         Object[] copyObjects = new Object[2];
 338         System.arraycopy(comparables, 0, copyObjects, 0, comparables.length);
 339         checkArrayElementsEqual(comparables, copyObjects);
 340 
 341         comparables[0] = null;
 342         comparables[1] = null;
 343         assertTrue(comparables[0] == null && comparables[1] == null, "Not null ?");
 344 
 345         MyInt.box[] myIntRefArray = new MyInt.box[1];
 346         assertTrue(myIntRefArray[0] == null, "Got: " + myIntRefArray[0]);
 347         myIntRefArray[0] = null;
 348     }
 349 
 350     void testMixedLayoutArrays() {
 351         Object[] objArray = new Object[3];
 352         Comparable[] compArray = new Comparable[3];
 353         MyInt[] valArray = new MyInt[] { MyInt.MIN, MyInt.ZERO, MyInt.MAX };
 354 
 355         arrayCopy(valArray, 0, objArray, 0, 3);
 356         checkArrayElementsEqual(valArray, objArray);
 357         arrayCopy(valArray, 0, objArray, 0, 3);
 358 
 359         objArray = new Object[3];
 360         System.arraycopy(valArray, 0, objArray, 0, 3);
 361         checkArrayElementsEqual(valArray, objArray);
 362 
 363         System.arraycopy(valArray, 0, compArray, 0, 3);
 364         checkArrayElementsEqual(valArray, compArray);
 365 
 366         valArray = new MyInt[] { MyInt.ZERO, MyInt.ZERO, MyInt.ZERO };
 367         System.arraycopy(compArray, 0, valArray, 0, 3);
 368         checkArrayElementsEqual(valArray, compArray);
 369 
 370         valArray = new MyInt[] { MyInt.ZERO, MyInt.ZERO, MyInt.ZERO };
 371         System.arraycopy(objArray, 0, valArray, 0, 3);
 372         checkArrayElementsEqual(valArray, objArray);
 373 
 374         // Sanity check dst == src
 375         System.arraycopy(valArray, 0, valArray, 0, 3);
 376         checkArrayElementsEqual(valArray, objArray);
 377 
 378         objArray[0] = "Not a value object";
 379         try {
 380             System.arraycopy(objArray, 0, valArray, 0, 3);
 381             throw new RuntimeException("Expected ArrayStoreException");
 382         } catch (ArrayStoreException ase) {}
 383 
 384         MyInt.box[] myIntRefArray = new MyInt.box[3];
 385         System.arraycopy(valArray, 0, myIntRefArray, 0, 3);
 386         checkArrayElementsEqual(valArray, myIntRefArray);
 387 
 388         myIntRefArray[0] = null;
 389         try {
 390             System.arraycopy(myIntRefArray, 0, valArray, 0, 3);
 391             throw new RuntimeException("Expected NullPointerException");
 392         } catch (NullPointerException npe) {}
 393     }
 394 
 395     static final value class MyPoint {
 396         final               MyInt.val x;
 397         final               MyInt y;
 398 
 399         private MyPoint() {
 400             x = MyInt.ZERO;
 401             y = x;
 402         }
 403         public boolean equals(Object that) {
 404             if (that instanceof MyPoint) {
 405                 MyPoint thatPoint = (MyPoint) that;
 406                 return x.equals(thatPoint.x) && java.util.Objects.equals(y, thatPoint.y);
 407             }
 408             return false;
 409         }
 410         static MyPoint create(int x) {
 411             MyPoint mp = MyPoint.default;
 412             mp = __WithField(mp.x, MyInt.create(x));
 413             return mp;
 414         }
 415         static MyPoint create(int x, int y) {
 416             MyPoint mp = MyPoint.default;
 417             mp = __WithField(mp.x, MyInt.create(x));
 418             mp = __WithField(mp.y, MyInt.create(y));
 419             return mp;
 420         }
 421         static final MyPoint.box ORIGIN = create(0);
 422     }
 423 
 424     void testComposition() {
 425         // Test array operations with compostion of values, check element payload is correct...
 426         MyPoint a = MyPoint.create(1, 2);
 427         MyPoint b = MyPoint.create(7, 21);
 428         MyPoint c = MyPoint.create(Integer.MAX_VALUE, Integer.MIN_VALUE);
 429 
 430         MyPoint[] pts = new MyPoint[3];
 431         if (!pts[0].equals(MyPoint.ORIGIN)) {
 432             throw new RuntimeException("Equals failed: " + pts[0] + " vs " + MyPoint.ORIGIN);
 433         }
 434         pts = new MyPoint[] { a, b, c };
 435         checkArrayElementsEqual(pts, new Object[] { a, b, c});
 436         Object[] oarr = new Object[3];
 437 
 438         arrayCopy(pts, 0, oarr, 0, 3);
 439         checkArrayElementsEqual(pts, oarr);
 440 
 441         oarr = new Object[3];
 442         System.arraycopy(pts, 0, oarr, 0, 3);
 443         checkArrayElementsEqual(pts, oarr);
 444 
 445         System.arraycopy(oarr, 0, pts, 0, 3);
 446         checkArrayElementsEqual(pts, oarr);
 447 
 448         oarr = new Object[3];
 449         try {
 450             System.arraycopy(oarr, 0, pts, 0, 3);
 451             throw new RuntimeException("Expected NPE");
 452         }
 453         catch (NullPointerException npe) {}
 454 
 455         oarr = new Object[3];
 456         oarr[0] = new Object();
 457         try {
 458             System.arraycopy(oarr, 0, pts, 0, 3);
 459             throw new RuntimeException("Expected ASE");
 460         }
 461         catch (ArrayStoreException ase) {}
 462     }
 463 
 464     void checkArrayElementsEqual(MyInt[] arr1, Object[] arr2) {
 465         assertTrue(arr1.length == arr2.length, "Bad length");
 466         for (int i = 0; i < arr1.length; i++) {
 467             assertTrue(java.util.Objects.equals(arr1[i], arr2[i]), "Element " + i + " not equal");
 468         }
 469     }
 470 
 471     void checkArrayElementsEqual(MyPoint[] arr1, Object[] arr2) {
 472         assertTrue(arr1.length == arr2.length, "Bad length");
 473         for (int i = 0; i < arr1.length; i++) {
 474             assertTrue(java.util.Objects.equals(arr1[i], arr2[i]), "Element " + i + " not equal");
 475         }
 476     }
 477 
 478     void checkArrayElementsEqual(Object[] arr1, Object[] arr2) {
 479         assertTrue(arr1.length == arr2.length, "Bad length");
 480         for (int i = 0; i < arr1.length; i++) {
 481             assertTrue(java.util.Objects.equals(arr1[i], arr2[i]), "Element " + i + " not equal");
 482         }
 483     }
 484 
 485     void arrayCopy(MyInt[] src, int srcPos, Object[] dst, int dstPos, int length) {
 486         for (int i = 0; i < length ; i++) {
 487             dst[dstPos++] = src[srcPos++];
 488         }
 489     }
 490     void arrayCopy(MyPoint[] src, int srcPos, Object[] dst, int dstPos, int length) {
 491         for (int i = 0; i < length ; i++) {
 492             dst[dstPos++] = src[srcPos++];
 493         }
 494     }
 495 
 496     Object getNull() { return null; }
 497 
 498 }