1 /*
   2  * Copyright (c) 2017, 2018, Red Hat, Inc. 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  * @bug 8182997 8214898
  27  * @library /test/lib
  28  * @summary Test the handling of Arrays of unloaded value classes.
  29  * @compile -XDemitQtypes -XDenableValueTypes -XDallowFlattenabilityModifiers -XDallowWithFieldOperator TestUnloadedValueTypeArray.java
  30  * @run main/othervm -XX:+EnableValhalla -Xcomp
  31  *        -XX:CompileCommand=compileonly,TestUnloadedValueTypeArray::test1
  32  *        -XX:CompileCommand=compileonly,TestUnloadedValueTypeArray::test2
  33  *        -XX:CompileCommand=compileonly,TestUnloadedValueTypeArray::test3
  34  *      TestUnloadedValueTypeArray
  35  */
  36 
  37 import jdk.test.lib.Asserts;
  38 
  39 value final class MyValue {
  40     final int foo;
  41 
  42     private MyValue() {
  43         foo = 0x42;
  44     }
  45 }
  46 
  47 value final class MyValue2 {
  48     final int foo;
  49 
  50     private MyValue2() {
  51         foo = 0x42;
  52     }
  53     static MyValue2 make(int n) {
  54         return __WithField(MyValue2.default.foo, n);
  55     }
  56 }
  57 
  58 value final class MyValue3 {
  59     final int foo;
  60 
  61     private MyValue3() {
  62         foo = 0x42;
  63     }
  64     static MyValue3 make(int n) {
  65         return __WithField(MyValue3.default.foo, n);
  66     }
  67 }
  68 
  69 
  70 
  71 public class TestUnloadedValueTypeArray {
  72 
  73     static MyValue[] target() {
  74         return new MyValue[10];
  75     }
  76 
  77     static void test1() {
  78         target();
  79     }
  80 
  81     static int test2(MyValue2[] arr) {
  82         if (arr != null) {
  83             return arr[1].foo;
  84         } else {
  85             return 1234;
  86         }
  87     }
  88 
  89     static void test2_verifier() {
  90         int n = 50000;
  91 
  92         int m = 9999;
  93         for (int i=0; i<n; i++) {
  94             m = test2(null);
  95         }
  96         Asserts.assertEQ(m, 1234);
  97 
  98         MyValue2[] arr = new MyValue2[2];
  99         arr[1] = MyValue2.make(5678);
 100         m = 9999;
 101         for (int i=0; i<n; i++) {
 102             m = test2(arr);
 103         }
 104         Asserts.assertEQ(m, 5678);
 105     }
 106 
 107     static void test3(MyValue3[] arr) {
 108         if (arr != null) {
 109             arr[1] = MyValue3.make(2345);
 110         }
 111     }
 112 
 113     static void test3_verifier() {
 114         int n = 50000;
 115 
 116         for (int i=0; i<n; i++) {
 117             test3(null);
 118         }
 119 
 120         MyValue3[] arr = new MyValue3[2];
 121         for (int i=0; i<n; i++) {
 122             test3(arr);
 123         }
 124         Asserts.assertEQ(arr[1].foo, 2345);
 125     }
 126 
 127     static public void main(String[] args) {
 128         test1();
 129         test2_verifier();
 130         test3_verifier();
 131     }
 132 }