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