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 Inline 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 runtime.valhalla.valuetypes.ValueTypeArray
39 * @run main/othervm -Xint -XX:ValueArrayElemMaxFlatSize=0 runtime.valhalla.valuetypes.ValueTypeArray
40 * @run main/othervm -Xcomp -XX:ValueArrayElemMaxFlatSize=-1 runtime.valhalla.valuetypes.ValueTypeArray
41 * @run main/othervm -Xcomp -XX:ValueArrayElemMaxFlatSize=0 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();
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() {
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.asIndirectType();
204 Point?[][] barray = (Point?[][]) 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? pb = barray[0][1];
209 int x = pb.getX();
210 assertEquals(x, 1, "Bad Point Value");
211 }
212
213 static final inline 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 // Null-able fields here are a temp hack to avoid ClassCircularityError
234 public static final MyInt? MIN = MyInt.create(Integer.MIN_VALUE);
235 public static final MyInt? ZERO = MyInt.create(0);
236 public static final MyInt? MAX = MyInt.create(Integer.MAX_VALUE);
237 }
238
239 static MyInt staticMyInt = MyInt.create(-1);
240 static MyInt[] staticMyIntArray = new MyInt[] { staticMyInt };
241 static MyInt[][] staticMyIntArrayArray = new MyInt[][] { staticMyIntArray, staticMyIntArray };
242
243 static interface SomeSecondaryType {
244 default String hi() { return "Hi"; }
245 }
246
247 static final inline class MyOtherInt implements SomeSecondaryType {
248 final int value;
249 private MyOtherInt() { value = 0; }
250 }
409 try {
410 System.arraycopy(objArray, 0, valArray, 0, 3);
411 throw new RuntimeException("Expected ArrayStoreException");
412 } catch (ArrayStoreException ase) {}
413
414 MyInt?[] myIntRefArray = new MyInt?[3];
415 System.arraycopy(valArray, 0, myIntRefArray, 0, 3);
416 checkArrayElementsEqual(valArray, myIntRefArray);
417
418 myIntRefArray[0] = null;
419 try {
420 System.arraycopy(myIntRefArray, 0, valArray, 0, 3);
421 throw new RuntimeException("Expected NullPointerException");
422 } catch (NullPointerException npe) {}
423 }
424
425 static final inline class MyPoint {
426 final MyInt x;
427 final MyInt y;
428
429 private MyPoint() {
430 x = (MyInt) MyInt.ZERO;
431 y = x;
432 }
433 public boolean equals(Object that) {
434 if (that instanceof MyPoint) {
435 MyPoint thatPoint = (MyPoint) that;
436 return x.equals(thatPoint.x) && java.util.Objects.equals(y, thatPoint.y);
437 }
438 return false;
439 }
440 static MyPoint create(int x) {
441 MyPoint mp = MyPoint.default;
442 mp = __WithField(mp.x, MyInt.create(x));
443 return mp;
444 }
445 static MyPoint create(int x, int y) {
446 MyPoint mp = MyPoint.default;
447 mp = __WithField(mp.x, MyInt.create(x));
448 mp = __WithField(mp.y, MyInt.create(y));
449 return mp;
450 }
451 static final MyPoint? ORIGIN = create(0);
452 }
453
454 void testComposition() {
455 // Test array operations with compostion of inline types, check element payload is correct...
456 MyPoint a = MyPoint.create(1, 2);
457 MyPoint b = MyPoint.create(7, 21);
458 MyPoint c = MyPoint.create(Integer.MAX_VALUE, Integer.MIN_VALUE);
459
460 MyPoint[] pts = new MyPoint[3];
461 if (!pts[0].equals(MyPoint.ORIGIN)) {
462 throw new RuntimeException("Equals failed: " + pts[0] + " vs " + MyPoint.ORIGIN);
463 }
464 pts = new MyPoint[] { a, b, c };
465 checkArrayElementsEqual(pts, new Object[] { a, b, c});
466 Object[] oarr = new Object[3];
467
468 arrayCopy(pts, 0, oarr, 0, 3);
469 checkArrayElementsEqual(pts, oarr);
|
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 Inline Types
36 * @library /test/lib
37 * @compile -XDallowGenericsOverValues ValueTypeArray.java Point.java Long8Value.java Person.java
38 * @run main/othervm -Xint -XX:ValueArrayElemMaxFlatSize=-1 runtime.valhalla.valuetypes.ValueTypeArray
39 * @run main/othervm -Xint -XX:ValueArrayElemMaxFlatSize=0 runtime.valhalla.valuetypes.ValueTypeArray
40 * @run main/othervm -Xcomp -XX:ValueArrayElemMaxFlatSize=-1 runtime.valhalla.valuetypes.ValueTypeArray
41 * @run main/othervm -Xcomp -XX:ValueArrayElemMaxFlatSize=0 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();
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 System.gc(); // check that VTs survive GC
113 checkSimplePointArray(points);
114
115 assertTrue(points instanceof Point[], "Instance of");
116
117 testSimplePointArrayCopy();
118 }
119
120 void testSimplePointArrayCopy() {
121 Point[] points = createSimplePointArray();
122 Point[] pointsCopy = new Point[points.length];
123 System.arraycopy(points, 0, pointsCopy, 0, points.length);
124 checkSimplePointArray(pointsCopy);
125
126 // Conjoint, overlap...left
127 System.arraycopy(points, 0, points, 1, 2);
128 checkArrayElementsEqual(points, new Point[] { pointsCopy[0], pointsCopy[0], pointsCopy[1], pointsCopy[3] });
129
130 // Conjoint, overlap...right
131 points = createSimplePointArray();
132 System.arraycopy(points, 2, points, 1, 2);
133 checkArrayElementsEqual(points, new Point[] { pointsCopy[0], pointsCopy[2], pointsCopy[3], pointsCopy[3] });
134 }
135
136 static Point[] createSimplePointArray() {
137 Point[] ps = new Point[4];
138 assertEquals(ps.length, 4, "Length");
139 ps.toString();
140 ps[0] = Point.createPoint(1, 2);
141 ps[1] = Point.createPoint(3, 4);
142 ps[2] = Point.createPoint(5, 6);
143 ps[3] = Point.createPoint(7, 8);
144 boolean sawOob = false;
145 try {
146 ps[ps.length] = Point.createPoint(0, 0);
147 } catch (ArrayIndexOutOfBoundsException aioobe) { sawOob = true; }
148 assertTrue(sawOob, "Didn't see AIOOBE");
149 return ps;
150 }
151
152 static void checkSimplePointArray(Point[] points) {
153 assertEquals(points[0].x, 1, "invalid 0 point x value");
154 assertEquals(points[0].y, 2, "invalid 0 point y value");
155 assertEquals(points[1].x, 3, "invalid 1 point x value");
156 assertEquals(points[1].y, 4, "invalid 1 point y value");
157 assertEquals(points[2].x, 5, "invalid 2 point x value");
158 assertEquals(points[2].y, 6, "invalid 2 point y value");
159 assertEquals(points[3].x, 7, "invalid 3 point x value");
160 assertEquals(points[3].y, 8, "invalid 3 point y value");
161 }
162
163 void testLong8Array() {
164 Long8Value[] values = new Long8Value[3];
165 assertEquals(values.length, 3, "length");
166 values.toString();
167 Long8Value value = values[1];
168 long zl = 0;
169 Long8Value.check(value, zl, zl, zl, zl, zl, zl, zl, zl);
170 values[1] = Long8Value.create(1, 2, 3, 4, 5, 6, 7, 8);
171 value = values[1];
172 Long8Value.check(value, 1, 2, 3, 4, 5, 6, 7, 8);
173
174 Long8Value[] copy = new Long8Value[values.length];
175 System.arraycopy(values, 0, copy, 0, values.length);
176 value = copy[1];
177 Long8Value.check(value, 1, 2, 3, 4, 5, 6, 7, 8);
178 }
179
180 void testMixedPersonArray() {
215
216 Point[][][] array3 = (Point[][][]) Array.newInstance(cls, 1, 2);
217 assertEquals(array3.length, 1, "Incorrect length");
218 assertEquals(array3[0].length, 2, "Incorrect length");
219 assertTrue(array3[0][0] == null, "Expected NULL");
220
221 // Now create ObjArrays of ValueArray...
222 cls = (Class<?>) Point.class.asIndirectType();
223 Point?[][] barray = (Point?[][]) Array.newInstance(cls, 1, 2);
224 assertEquals(barray.length, 1, "Incorrect length");
225 assertEquals(barray[0].length, 2, "Incorrect length");
226 barray[0][1] = Point.createPoint(1, 2);
227 Point? pb = barray[0][1];
228 int x = pb.getX();
229 assertEquals(x, 1, "Bad Point Value");
230 }
231
232 static final inline class MyInt implements Comparable<MyInt?> {
233 final int value;
234
235 private MyInt() { this(0); }
236 private MyInt(int v) { value = v; }
237 public int getValue() { return value; }
238 public String toString() { return "MyInt: " + getValue(); }
239 public int compareTo(MyInt? that) { return Integer.compare(this.getValue(), that.getValue()); }
240 public boolean equals(Object o) {
241 if (o instanceof MyInt) {
242 return this.getValue() == ((MyInt) o).getValue();
243 }
244 return false;
245 }
246
247 public static MyInt create(int v) {
248 return new MyInt(v);
249 }
250
251 // Null-able fields here are a temp hack to avoid ClassCircularityError
252 public static final MyInt? MIN = MyInt.create(Integer.MIN_VALUE);
253 public static final MyInt? ZERO = MyInt.create(0);
254 public static final MyInt? MAX = MyInt.create(Integer.MAX_VALUE);
255 }
256
257 static MyInt staticMyInt = MyInt.create(-1);
258 static MyInt[] staticMyIntArray = new MyInt[] { staticMyInt };
259 static MyInt[][] staticMyIntArrayArray = new MyInt[][] { staticMyIntArray, staticMyIntArray };
260
261 static interface SomeSecondaryType {
262 default String hi() { return "Hi"; }
263 }
264
265 static final inline class MyOtherInt implements SomeSecondaryType {
266 final int value;
267 private MyOtherInt() { value = 0; }
268 }
427 try {
428 System.arraycopy(objArray, 0, valArray, 0, 3);
429 throw new RuntimeException("Expected ArrayStoreException");
430 } catch (ArrayStoreException ase) {}
431
432 MyInt?[] myIntRefArray = new MyInt?[3];
433 System.arraycopy(valArray, 0, myIntRefArray, 0, 3);
434 checkArrayElementsEqual(valArray, myIntRefArray);
435
436 myIntRefArray[0] = null;
437 try {
438 System.arraycopy(myIntRefArray, 0, valArray, 0, 3);
439 throw new RuntimeException("Expected NullPointerException");
440 } catch (NullPointerException npe) {}
441 }
442
443 static final inline class MyPoint {
444 final MyInt x;
445 final MyInt y;
446
447 private MyPoint() { this(0, 0); }
448 private MyPoint(int x, int y) {
449 this.x = new MyInt(x);
450 this.y = new MyInt(y);
451 }
452 public boolean equals(Object that) {
453 if (that instanceof MyPoint) {
454 MyPoint thatPoint = (MyPoint) that;
455 return x.equals(thatPoint.x) && java.util.Objects.equals(y, thatPoint.y);
456 }
457 return false;
458 }
459 static MyPoint create(int x) {
460 return new MyPoint(x, x);
461 }
462 static MyPoint create(int x, int y) {
463 return new MyPoint(x, y);
464 }
465 static final MyPoint? ORIGIN = create(0);
466 }
467
468 void testComposition() {
469 // Test array operations with compostion of inline types, check element payload is correct...
470 MyPoint a = MyPoint.create(1, 2);
471 MyPoint b = MyPoint.create(7, 21);
472 MyPoint c = MyPoint.create(Integer.MAX_VALUE, Integer.MIN_VALUE);
473
474 MyPoint[] pts = new MyPoint[3];
475 if (!pts[0].equals(MyPoint.ORIGIN)) {
476 throw new RuntimeException("Equals failed: " + pts[0] + " vs " + MyPoint.ORIGIN);
477 }
478 pts = new MyPoint[] { a, b, c };
479 checkArrayElementsEqual(pts, new Object[] { a, b, c});
480 Object[] oarr = new Object[3];
481
482 arrayCopy(pts, 0, oarr, 0, 3);
483 checkArrayElementsEqual(pts, oarr);
|