1 /* 2 * Copyright (c) 2015, 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 /testlibrary / 34 * @run main/othervm -noverify -XX:+ValueArrayFlatten runtime.valhalla.valuetypes.ValueTypeArray 35 * @run main/othervm -noverify -XX:-ValueArrayFlatten runtime.valhalla.valuetypes.ValueTypeArray 36 */ 37 public class ValueTypeArray { 38 public static void main(String[] args) { 39 ValueTypeArray valueTypeArray = new ValueTypeArray(); 40 valueTypeArray.run(); 41 } 42 43 public void run() { 44 testClassForName(); 45 testSimplePointArray(); 46 testLong8Array(); 47 // embedded oops not yet supported 48 //testMixedPersonArray(); 49 testMultiDimPointArray(); 50 // Some design issues, ignore for now 51 //testAtomicArray(); 52 //testArrayCopy(); 53 testReflectArray(); 54 } 55 56 void testClassForName() { 57 String arrayClsName = "[Qruntime.valhalla.valuetypes.Point;"; 58 try { 59 Class<?> arrayCls = Class.forName(arrayClsName); 60 assertTrue(arrayCls.isArray(), "Expected an array class"); 61 assertTrue(arrayCls.getComponentType() == Point.class, 62 "Expected component type of Point.class"); 63 64 arrayClsName = "[" + arrayClsName; 65 Class<?> mulArrayCls = Class.forName(arrayClsName); 66 assertTrue(mulArrayCls.isArray()); 67 assertTrue(mulArrayCls.getComponentType() == arrayCls); 68 } 69 catch (ClassNotFoundException cnfe) { 70 fail("Class.forName(" + arrayClsName + ") failed", cnfe); 71 } 72 } 73 74 void testSimplePointArray() { 75 Point[] points = createSimplePointArray(); 76 checkSimplePointArray(points); 77 System.gc(); // check that VTs survive GC 78 79 assertTrue(points instanceof Point[], "Instance of"); 80 81 Point[] pointsCopy = new Point[points.length]; 82 System.arraycopy(points, 0, pointsCopy, 0, points.length); 83 checkSimplePointArray(pointsCopy); 84 } 85 86 static Point[] createSimplePointArray() { 87 Point[] ps = new Point[2]; 88 assertEquals(ps.length, 2, "Length"); 89 System.out.println(ps); 90 ps[0] = Point.createPoint(1, 2); 91 ps[1] = Point.createPoint(3, 4); 92 System.gc(); // check that VTs survive GC 93 return ps; 94 } 95 96 static void checkSimplePointArray(Point[] points) { 97 assertEquals(points[0].x, 1, "invalid 0 point x value"); 98 assertEquals(points[0].y, 2, "invalid 0 point y value"); 99 assertEquals(points[1].x, 3, "invalid 1 point x value"); 100 assertEquals(points[1].y, 4, "invalid 1 point y value"); 101 } 102 103 void testLong8Array() { 104 Long8Value[] values = new Long8Value[3]; 105 assertEquals(values.length, 3, "length"); 106 System.out.println(values); 107 Long8Value value = values[1]; 108 long zl = 0; 109 Long8Value.check(value, zl, zl, zl, zl, zl, zl, zl, zl); 110 values[1] = Long8Value.create(1, 2, 3, 4, 5, 6, 7, 8); 111 value = values[1]; 112 Long8Value.check(value, 1, 2, 3, 4, 5, 6, 7, 8); 113 114 Long8Value[] copy = new Long8Value[values.length]; 115 System.arraycopy(values, 0, copy, 0, values.length); 116 value = copy[1]; 117 Long8Value.check(value, 1, 2, 3, 4, 5, 6, 7, 8); 118 } 119 120 void testMixedPersonArray() { 121 Person[] people = new Person[3]; 122 123 people[0] = Person.create(1, "First", "Last"); 124 assertEquals(people[0].getId(), 1L, "Invalid Id"); 125 assertEquals(people[0].getFirstName(), "First", "Invalid First Name"); 126 assertEquals(people[0].getLastName(), "Last", "Invalid Last Name"); 127 128 people[1] = Person.create(2, "Jane", "Wayne"); 129 people[2] = Person.create(3, "Bob", "Dobalina"); 130 131 Person[] peopleCopy = new Person[people.length]; 132 System.arraycopy(people, 0, peopleCopy, 0, people.length); 133 assertEquals(peopleCopy[2].getId(), 3L, "Invalid Id"); 134 assertEquals(peopleCopy[2].getFirstName(), "Bob", "Invalid First Name"); 135 assertEquals(peopleCopy[2].getLastName(), "Dobalina", "Invalid Last Name"); 136 } 137 138 void testMultiDimPointArray() { 139 Point[][][] multiPoints = new Point[2][3][4]; 140 assertEquals(multiPoints.length, 2, "1st dim length"); 141 assertEquals(multiPoints[0].length, 3, "2st dim length"); 142 assertEquals(multiPoints[0][0].length, 4, "3rd dim length"); 143 144 Point defaultPoint = multiPoints[1][2][3]; 145 assertEquals(defaultPoint.x, 0, "invalid point x value"); 146 assertEquals(defaultPoint.y, 0, "invalid point x value"); 147 } 148 149 @SuppressWarnings("unchecked") 150 void testAtomicArray() { 151 // <any T> not working just now ? 152 IntValue[] intValues = new IntValue[3]; 153 assertTrue(Arrays.isAccessAtomic(intValues), "Not Atomic !"); 154 assertTrue(Arrays.ensureAtomicAccess(intValues) == intValues); 155 156 Long8Value long8Value = Long8Value.create(1, 2, 3, 4, 5, 6, 7, 8); 157 Long8Value.check(long8Value, 1, 2, 3, 4, 5, 6, 7, 8); 158 159 Long8Value[] long8Values = new Long8Value[3]; 160 long8Values[1] = Long8Value.create(1, 2, 3, 4, 5, 6, 7, 11); 161 assertFalse(Arrays.isAccessAtomic(long8Values), "Not Relaxed !"); 162 long8Values = (Long8Value[]) Arrays.ensureAtomicAccess(long8Values); 163 assertTrue(Arrays.isAccessAtomic(long8Values), "Not Atomic !"); 164 Long8Value.check(long8Values[1], 1, 2, 3, 4, 5, 6, 7, 11); 165 166 Long8Value[] oldLong8Values = long8Values; 167 long8Values = (Long8Value[]) Arrays.ensureAtomicAccess(long8Values); 168 assertTrue(oldLong8Values == long8Values, "Not the same !"); 169 170 long8Values[0] = Long8Value.create(1, 2, 3, 4, 5, 6, 7, 8); 171 assertEquals(long8Values[0].getLongField1(), 1L, "Field 1 incorrect"); 172 assertEquals(long8Values[0].getLongField8(), 8L, "Field 8 incorrect"); 173 Long8Value.check(long8Values[0], 1, 2, 3, 4, 5, 6, 7, 8); 174 175 long8Values[1] = long8Values[0]; 176 assertEquals(long8Values[1].getLongField1(), 1L, "Field 1 incorrect"); 177 assertEquals(long8Values[1].getLongField8(), 8L, "Field 8 incorrect"); 178 Long8Value.check(long8Values[1], 1, 2, 3, 4, 5, 6, 7, 8); 179 180 long8Values[2] = long8Value; 181 assertEquals(long8Values[2].getLongField1(), 1L, "Field 1 incorrect"); 182 assertEquals(long8Values[2].getLongField8(), 8L, "Field 8 incorrect"); 183 Long8Value.check(long8Values[2], 1, 2, 3, 4, 5, 6, 7, 8); 184 185 } 186 187 @SuppressWarnings("unchecked") 188 void testArrayCopy() { 189 // Test copy atomic vs relax combos... 190 int testLength = 3; 191 Long8Value long8Value = Long8Value.create(1, 2, 3, 4, 5, 6, 7, 8); 192 Long8Value long8ValueZero = Long8Value.create(0, 0, 0, 0, 0, 0, 0, 0); 193 Long8Value[] relaxedValues = new Long8Value[testLength]; 194 for (int i = 0; i < testLength; i++) { 195 relaxedValues[i] = long8Value; 196 } 197 198 // relaxed -> relaxed 199 Long8Value[] relaxedValues2 = new Long8Value[testLength]; 200 System.arraycopy(relaxedValues, 0, relaxedValues2, 0, testLength); 201 Long8Value.check(relaxedValues2[testLength-1], 1, 2, 3, 4, 5, 6, 7, 8); 202 203 // relaxed -> atomic 204 Long8Value[] atomicValues = (Long8Value[]) Arrays.ensureAtomicAccess(relaxedValues); 205 for (int i = 0; i < testLength; i++) { 206 atomicValues[i] = long8ValueZero; 207 } 208 System.arraycopy(relaxedValues, 0, atomicValues, 0, testLength); 209 Long8Value.check(relaxedValues2[testLength-1], 1, 2, 3, 4, 5, 6, 7, 8); 210 211 // atomic -> relaxed 212 for (int i = 0; i < testLength; i++) { 213 relaxedValues[i] = long8ValueZero; 214 } 215 System.arraycopy(atomicValues, 0, relaxedValues, 0, testLength); 216 Long8Value.check(relaxedValues[0], 1, 2, 3, 4, 5, 6, 7, 8); 217 218 // atomic -> atomic 219 Long8Value[] atomicValues2 = (Long8Value[]) Arrays.ensureAtomicAccess(relaxedValues); 220 for (int i = 0; i < testLength; i++) { 221 atomicValues2[i] = long8ValueZero; 222 } 223 System.arraycopy(atomicValues, 0, atomicValues2, 0, testLength); 224 Long8Value.check(atomicValues2[0], 1, 2, 3, 4, 5, 6, 7, 8); 225 } 226 227 void testReflectArray() { 228 // Check the java.lang.reflect.Array.newInstance methods... 229 Class<?> cls = (Class<?>) Point[].class; 230 Point[][] array = (Point[][]) java.lang.reflect.Array.newInstance(cls, 1); 231 assertEquals(array.length, 1, "Incorrect length"); 232 assertTrue(array[0] == null, "Expected NULL"); 233 234 Point[][][] array3 = (Point[][][]) java.lang.reflect.Array.newInstance(cls, 1, 2); 235 assertEquals(array3.length, 1, "Incorrect length"); 236 assertEquals(array3[0].length, 2, "Incorrect length"); 237 assertTrue(array3[0][0] == null, "Expected NULL"); 238 239 // Now create ObjArrays of ValueArray... 240 cls = (Class<?>) Point.class; 241 array = (Point[][]) java.lang.reflect.Array.newInstance(cls, 1, 2); 242 assertEquals(array.length, 1, "Incorrect length"); 243 assertEquals(array[0].length, 2, "Incorrect length"); 244 Point p = array[0][1]; 245 int x = p.x; 246 assertEquals(x, 0, "Bad Point Value"); 247 } 248 249 /* 250 TODO: Current repo state doesn't pick up the common test/lib Asserts 251 remove this implementation when fixed 252 */ 253 public static void fail(String msg, Throwable thr) { // Missing Asserts.fail(String, Throwable) 254 throw new RuntimeException(msg, thr); 255 } 256 }