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