1 /*
   2  * Copyright (c) 2018, 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 package runtime.valhalla.valuetypes;
  24 
  25 import java.lang.invoke.*;
  26 
  27 import jdk.experimental.value.MethodHandleBuilder;
  28 
  29 import jdk.test.lib.Asserts;
  30 
  31 /*
  32  * @test
  33  * @summary Flattenable field semantic test
  34  * @modules java.base/jdk.experimental.bytecode
  35  *          java.base/jdk.experimental.value
  36  * @library /test/lib
  37  * @compile -XDemitQtypes -XDenableValueTypes -XDallowWithFieldOperator Point.java JumboValue.java
  38  * @compile -XDemitQtypes -XDenableValueTypes -XDallowWithFieldOperator FlattenableSemanticTest.java
  39  * @run main/othervm -Xint -XX:ValueFieldMaxFlatSize=64 runtime.valhalla.valuetypes.FlattenableSemanticTest
  40  * @run main/othervm -Xint -XX:ForceNonTearable=* runtime.valhalla.valuetypes.FlattenableSemanticTest
  41  * @run main/othervm -Xcomp -XX:ValueFieldMaxFlatSize=64 runtime.valhalla.valuetypes.FlattenableSemanticTest
  42  * @run main/othervm -Xcomp -XX:ForceNonTearable=* runtime.valhalla.valuetypes.FlattenableSemanticTest
  43  * // debug: -XX:+PrintValueLayout -XX:-ShowMessageBoxOnError
  44  */
  45 public class FlattenableSemanticTest {
  46 
  47     static Point? nfsp;
  48     static Point fsp;
  49 
  50     Point? nfip;
  51     Point fip;
  52 
  53     static JumboValue? nfsj;
  54     static JumboValue fsj;
  55 
  56     JumboValue? nfij;
  57     JumboValue fij;
  58 
  59     static Object getNull() {
  60         return null;
  61     }
  62 
  63     FlattenableSemanticTest() { }
  64 
  65     public static void main(String[] args) {
  66         FlattenableSemanticTest test = new FlattenableSemanticTest();
  67 
  68         // Uninitialized inline fields must be null for non flattenable fields
  69         Asserts.assertNull(nfsp, "Invalid non null value for unitialized non flattenable field");
  70         Asserts.assertNull(nfsj, "Invalid non null value for unitialized non flattenable field");
  71         Asserts.assertNull(test.nfip, "Invalid non null value for unitialized non flattenable field");
  72         Asserts.assertNull(test.nfij, "Invalid non null value for unitialized non flattenable field");
  73 
  74         // fsp.equals(null);
  75 
  76         // Uninitialized inline fields must be non null for flattenable fields
  77         Asserts.assertNotNull(fsp, "Invalid null value for unitialized flattenable field");
  78         Asserts.assertNotNull(fsj, "Invalid null value for unitialized flattenable field");
  79         Asserts.assertNotNull(test.fip, "Invalid null value for unitialized flattenable field");
  80         Asserts.assertNotNull(test.fij, "Invalid null value for unitialized flattenable field");
  81 
  82         // Assigning null must be allowed for non flattenable inline fields
  83         boolean exception = true;
  84         try {
  85             nfsp = (Point?)getNull();
  86             nfsp = null;
  87             exception = false;
  88         } catch (NullPointerException e) {
  89             exception = true;
  90         }
  91         Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field");
  92 
  93         try {
  94             nfsj = (JumboValue?)getNull();
  95             nfsj = null;
  96             exception = false;
  97         } catch (NullPointerException e) {
  98             exception = true;
  99         }
 100         Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field");
 101 
 102         try {
 103             test.nfip = (Point?)getNull();
 104             test.nfip = null;
 105             exception = false;
 106         } catch (NullPointerException e) {
 107             exception = true;
 108         }
 109         Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field");
 110 
 111         try {
 112             test.nfij = (JumboValue?)getNull();
 113             test.nfij = null;
 114             exception = false;
 115         } catch (NullPointerException e) {
 116             exception = true;
 117         }
 118         Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field");
 119 
 120         // Assigning null to a flattenable inline field must trigger a NPE
 121         exception = false;
 122         try {
 123             fsp = (Point)getNull();
 124         } catch(NullPointerException e) {
 125             exception = true;
 126         }
 127         Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field");
 128         exception = false;
 129         try {
 130             fsj = (JumboValue)getNull();
 131         } catch(NullPointerException e) {
 132             exception = true;
 133         }
 134         Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field");
 135         exception = false;
 136         try {
 137             test.fip = (Point)getNull();
 138         } catch(NullPointerException e) {
 139             exception = true;
 140         }
 141         Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field");
 142         exception = false;
 143         try {
 144             test.fij = (JumboValue)getNull();
 145         } catch(NullPointerException e) {
 146             exception = true;
 147         }
 148         Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field");
 149         exception = false;
 150     }
 151 
 152 }