/* * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package runtime.valhalla.valuetypes; import java.util.Arrays; import static jdk.test.lib.Asserts.*; /* * @test ValueTypeArray * @summary Plain array test for Value Types * @library /testlibrary / * @run main/othervm -noverify -Xint -XX:+ValueArrayFlatten runtime.valhalla.valuetypes.ValueTypeArray * @run main/othervm -noverify -Xint -XX:-ValueArrayFlatten runtime.valhalla.valuetypes.ValueTypeArray * @run main/othervm -noverify -Xcomp -XX:+ValueArrayFlatten runtime.valhalla.valuetypes.ValueTypeArray * @run main/othervm -noverify -Xcomp -XX:-ValueArrayFlatten runtime.valhalla.valuetypes.ValueTypeArray */ public class ValueTypeArray { public static void main(String[] args) { ValueTypeArray valueTypeArray = new ValueTypeArray(); valueTypeArray.run(); } public void run() { testClassForName(); testSimplePointArray(); testLong8Array(); // embedded oops not yet supported //testMixedPersonArray(); testMultiDimPointArray(); // Some design issues, ignore for now //testAtomicArray(); //testArrayCopy(); testReflectArray(); } void testClassForName() { String arrayClsName = "[Qruntime.valhalla.valuetypes.Point;"; try { Class arrayCls = Class.forName(arrayClsName); assertTrue(arrayCls.isArray(), "Expected an array class"); assertTrue(arrayCls.getComponentType() == Point.class, "Expected component type of Point.class"); arrayClsName = "[" + arrayClsName; Class mulArrayCls = Class.forName(arrayClsName); assertTrue(mulArrayCls.isArray()); assertTrue(mulArrayCls.getComponentType() == arrayCls); } catch (ClassNotFoundException cnfe) { fail("Class.forName(" + arrayClsName + ") failed", cnfe); } } void testSimplePointArray() { Point[] points = createSimplePointArray(); checkSimplePointArray(points); System.gc(); // check that VTs survive GC assertTrue(points instanceof Point[], "Instance of"); Point[] pointsCopy = new Point[points.length]; System.arraycopy(points, 0, pointsCopy, 0, points.length); checkSimplePointArray(pointsCopy); } static Point[] createSimplePointArray() { Point[] ps = new Point[2]; assertEquals(ps.length, 2, "Length"); System.out.println(ps); ps[0] = Point.createPoint(1, 2); ps[1] = Point.createPoint(3, 4); System.gc(); // check that VTs survive GC return ps; } static void checkSimplePointArray(Point[] points) { assertEquals(points[0].x, 1, "invalid 0 point x value"); assertEquals(points[0].y, 2, "invalid 0 point y value"); assertEquals(points[1].x, 3, "invalid 1 point x value"); assertEquals(points[1].y, 4, "invalid 1 point y value"); } void testLong8Array() { Long8Value[] values = new Long8Value[3]; assertEquals(values.length, 3, "length"); System.out.println(values); Long8Value value = values[1]; long zl = 0; Long8Value.check(value, zl, zl, zl, zl, zl, zl, zl, zl); values[1] = Long8Value.create(1, 2, 3, 4, 5, 6, 7, 8); value = values[1]; Long8Value.check(value, 1, 2, 3, 4, 5, 6, 7, 8); Long8Value[] copy = new Long8Value[values.length]; System.arraycopy(values, 0, copy, 0, values.length); value = copy[1]; Long8Value.check(value, 1, 2, 3, 4, 5, 6, 7, 8); } void testMixedPersonArray() { Person[] people = new Person[3]; people[0] = Person.create(1, "First", "Last"); assertEquals(people[0].getId(), 1L, "Invalid Id"); assertEquals(people[0].getFirstName(), "First", "Invalid First Name"); assertEquals(people[0].getLastName(), "Last", "Invalid Last Name"); people[1] = Person.create(2, "Jane", "Wayne"); people[2] = Person.create(3, "Bob", "Dobalina"); Person[] peopleCopy = new Person[people.length]; System.arraycopy(people, 0, peopleCopy, 0, people.length); assertEquals(peopleCopy[2].getId(), 3L, "Invalid Id"); assertEquals(peopleCopy[2].getFirstName(), "Bob", "Invalid First Name"); assertEquals(peopleCopy[2].getLastName(), "Dobalina", "Invalid Last Name"); } void testMultiDimPointArray() { Point[][][] multiPoints = new Point[2][3][4]; assertEquals(multiPoints.length, 2, "1st dim length"); assertEquals(multiPoints[0].length, 3, "2st dim length"); assertEquals(multiPoints[0][0].length, 4, "3rd dim length"); Point defaultPoint = multiPoints[1][2][3]; assertEquals(defaultPoint.x, 0, "invalid point x value"); assertEquals(defaultPoint.y, 0, "invalid point x value"); } @SuppressWarnings("unchecked") void testAtomicArray() { // not working just now ? IntValue[] intValues = new IntValue[3]; assertTrue(Arrays.isAccessAtomic(intValues), "Not Atomic !"); assertTrue(Arrays.ensureAtomicAccess(intValues) == intValues); Long8Value long8Value = Long8Value.create(1, 2, 3, 4, 5, 6, 7, 8); Long8Value.check(long8Value, 1, 2, 3, 4, 5, 6, 7, 8); Long8Value[] long8Values = new Long8Value[3]; long8Values[1] = Long8Value.create(1, 2, 3, 4, 5, 6, 7, 11); assertFalse(Arrays.isAccessAtomic(long8Values), "Not Relaxed !"); long8Values = (Long8Value[]) Arrays.ensureAtomicAccess(long8Values); assertTrue(Arrays.isAccessAtomic(long8Values), "Not Atomic !"); Long8Value.check(long8Values[1], 1, 2, 3, 4, 5, 6, 7, 11); Long8Value[] oldLong8Values = long8Values; long8Values = (Long8Value[]) Arrays.ensureAtomicAccess(long8Values); assertTrue(oldLong8Values == long8Values, "Not the same !"); long8Values[0] = Long8Value.create(1, 2, 3, 4, 5, 6, 7, 8); assertEquals(long8Values[0].getLongField1(), 1L, "Field 1 incorrect"); assertEquals(long8Values[0].getLongField8(), 8L, "Field 8 incorrect"); Long8Value.check(long8Values[0], 1, 2, 3, 4, 5, 6, 7, 8); long8Values[1] = long8Values[0]; assertEquals(long8Values[1].getLongField1(), 1L, "Field 1 incorrect"); assertEquals(long8Values[1].getLongField8(), 8L, "Field 8 incorrect"); Long8Value.check(long8Values[1], 1, 2, 3, 4, 5, 6, 7, 8); long8Values[2] = long8Value; assertEquals(long8Values[2].getLongField1(), 1L, "Field 1 incorrect"); assertEquals(long8Values[2].getLongField8(), 8L, "Field 8 incorrect"); Long8Value.check(long8Values[2], 1, 2, 3, 4, 5, 6, 7, 8); } @SuppressWarnings("unchecked") void testArrayCopy() { // Test copy atomic vs relax combos... int testLength = 3; Long8Value long8Value = Long8Value.create(1, 2, 3, 4, 5, 6, 7, 8); Long8Value long8ValueZero = Long8Value.create(0, 0, 0, 0, 0, 0, 0, 0); Long8Value[] relaxedValues = new Long8Value[testLength]; for (int i = 0; i < testLength; i++) { relaxedValues[i] = long8Value; } // relaxed -> relaxed Long8Value[] relaxedValues2 = new Long8Value[testLength]; System.arraycopy(relaxedValues, 0, relaxedValues2, 0, testLength); Long8Value.check(relaxedValues2[testLength-1], 1, 2, 3, 4, 5, 6, 7, 8); // relaxed -> atomic Long8Value[] atomicValues = (Long8Value[]) Arrays.ensureAtomicAccess(relaxedValues); for (int i = 0; i < testLength; i++) { atomicValues[i] = long8ValueZero; } System.arraycopy(relaxedValues, 0, atomicValues, 0, testLength); Long8Value.check(relaxedValues2[testLength-1], 1, 2, 3, 4, 5, 6, 7, 8); // atomic -> relaxed for (int i = 0; i < testLength; i++) { relaxedValues[i] = long8ValueZero; } System.arraycopy(atomicValues, 0, relaxedValues, 0, testLength); Long8Value.check(relaxedValues[0], 1, 2, 3, 4, 5, 6, 7, 8); // atomic -> atomic Long8Value[] atomicValues2 = (Long8Value[]) Arrays.ensureAtomicAccess(relaxedValues); for (int i = 0; i < testLength; i++) { atomicValues2[i] = long8ValueZero; } System.arraycopy(atomicValues, 0, atomicValues2, 0, testLength); Long8Value.check(atomicValues2[0], 1, 2, 3, 4, 5, 6, 7, 8); } void testReflectArray() { // Check the java.lang.reflect.Array.newInstance methods... Class cls = (Class) Point[].class; Point[][] array = (Point[][]) java.lang.reflect.Array.newInstance(cls, 1); assertEquals(array.length, 1, "Incorrect length"); assertTrue(array[0] == null, "Expected NULL"); Point[][][] array3 = (Point[][][]) java.lang.reflect.Array.newInstance(cls, 1, 2); assertEquals(array3.length, 1, "Incorrect length"); assertEquals(array3[0].length, 2, "Incorrect length"); assertTrue(array3[0][0] == null, "Expected NULL"); // Now create ObjArrays of ValueArray... cls = (Class) Point.class; array = (Point[][]) java.lang.reflect.Array.newInstance(cls, 1, 2); assertEquals(array.length, 1, "Incorrect length"); assertEquals(array[0].length, 2, "Incorrect length"); Point p = array[0][1]; int x = p.x; assertEquals(x, 0, "Bad Point Value"); } /* TODO: Current repo state doesn't pick up the common test/lib Asserts remove this implementation when fixed */ public static void fail(String msg, Throwable thr) { // Missing Asserts.fail(String, Throwable) throw new RuntimeException(msg, thr); } }