1 /*
  2  * Copyright (c) 2019, 2025, 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 /**
 25  * @test
 26  * @summary Test accessing value class arrays that exceed the flattening threshold.
 27  * @library /test/lib
 28  * @enablePreview
 29  * @modules java.base/jdk.internal.value
 30  *          java.base/jdk.internal.vm.annotation
 31  * @run main/othervm -Xbatch
 32  *                   TestFlatArrayThreshold
 33  * @run main/othervm -XX:FlatArrayElementMaxOops=1 -Xbatch
 34  *                   TestFlatArrayThreshold
 35  * @run main/othervm -XX:+UseArrayFlattening -Xbatch
 36  *                   TestFlatArrayThreshold
 37 
 38  */
 39 
 40 import jdk.test.lib.Asserts;
 41 
 42 import jdk.internal.value.ValueClass;
 43 import jdk.internal.vm.annotation.LooselyConsistentValue;
 44 
 45 @LooselyConsistentValue
 46 value class MyValue1 {
 47     Object o1;
 48     Object o2;
 49 
 50     public MyValue1(Object o1, Object o2) {
 51         this.o1 = o1;
 52         this.o2 = o2;
 53     }
 54 }
 55 
 56 public class TestFlatArrayThreshold {
 57 
 58     public static MyValue1 test1(MyValue1[] va, MyValue1 vt) {
 59         va[0] = vt;
 60         return va[1];
 61     }
 62 
 63     public static MyValue1 test2(MyValue1[] va, MyValue1 vt) {
 64         va[0] = vt;
 65         return va[1];
 66     }
 67 
 68     public static Object test3(Object[] va, MyValue1 vt) {
 69         va[0] = vt;
 70         return va[1];
 71     }
 72 
 73     public static Object test4(Object[] va, MyValue1 vt) {
 74         va[0] = vt;
 75         return va[1];
 76     }
 77 
 78     public static MyValue1 test5(MyValue1[] va, Object vt) {
 79         va[0] = (MyValue1)vt;
 80         return va[1];
 81     }
 82 
 83     public static MyValue1 test6(MyValue1[] va, Object vt) {
 84         va[0] = (MyValue1)vt;
 85         return va[1];
 86     }
 87 
 88     public static Object test7(Object[] va, Object vt) {
 89         va[0] = vt;
 90         return va[1];
 91     }
 92 
 93     static public void main(String[] args) {
 94         MyValue1 vt = new MyValue1(new Integer(42), new Integer(43));
 95         MyValue1[] va = (MyValue1[])ValueClass.newNullRestrictedNonAtomicArray(MyValue1.class, 2, new MyValue1(null, null));
 96         MyValue1[] vaB = new MyValue1[2];
 97         va[1] = vt;
 98         for (int i = 0; i < 10_000; ++i) {
 99             MyValue1 result1 = test1(va, vt);
100             Asserts.assertEQ(result1.o1, 42);
101             Asserts.assertEQ(result1.o2, 43);
102 
103             MyValue1 result2 = test2(va, vt);
104             Asserts.assertEQ(result2.o1, 42);
105             Asserts.assertEQ(result2.o2, 43);
106             result2 = test2(vaB, null);
107             Asserts.assertEQ(result2, null);
108 
109             MyValue1 result3 = (MyValue1)test3(va, vt);
110             Asserts.assertEQ(result3.o1, 42);
111             Asserts.assertEQ(result3.o2, 43);
112             result3 = (MyValue1)test3(vaB, vt);
113             Asserts.assertEQ(result3, null);
114 
115             MyValue1 result4 = (MyValue1)test4(va, vt);
116             Asserts.assertEQ(result4.o1, 42);
117             Asserts.assertEQ(result4.o2, 43);
118             result4 = (MyValue1)test4(vaB, null);
119             Asserts.assertEQ(result4, null);
120 
121             MyValue1 result5 = test5(va, vt);
122             Asserts.assertEQ(result5.o1, 42);
123             Asserts.assertEQ(result5.o2, 43);
124 
125             MyValue1 result6 = test6(va, vt);
126             Asserts.assertEQ(result6.o1, 42);
127             Asserts.assertEQ(result6.o2, 43);
128             result6 = test6(vaB, null);
129             Asserts.assertEQ(result6, null);
130 
131             MyValue1 result7 = (MyValue1)test7(va, vt);
132             Asserts.assertEQ(result7.o1, 42);
133             Asserts.assertEQ(result7.o2, 43);
134             result7 = (MyValue1)test7(vaB, null);
135             Asserts.assertEQ(result7, null);
136         }
137         try {
138             test2(va, null);
139             throw new RuntimeException("NullPointerException expected");
140         } catch (NullPointerException npe) {
141             // Expected
142         }
143         try {
144             test4(va, null);
145             throw new RuntimeException("NullPointerException expected");
146         } catch (NullPointerException npe) {
147             // Expected
148         }
149         try {
150             test5(va, null);
151             throw new RuntimeException("NullPointerException expected");
152         } catch (NullPointerException npe) {
153             // Expected
154         }
155         try {
156             test6(va, null);
157             throw new RuntimeException("NullPointerException expected");
158         } catch (NullPointerException npe) {
159             // Expected
160         }
161         try {
162             test7(va, null);
163             throw new RuntimeException("NullPointerException expected");
164         } catch (NullPointerException npe) {
165             // Expected
166         }
167     }
168 }