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