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 -XDenablePrimitiveClasses Point.java JumboInline.java FlattenableSemanticTest.java
 33  * @run main/othervm -XX:+EnableValhalla -XX:+EnablePrimitiveClasses -XX:InlineFieldMaxFlatSize=64 runtime.valhalla.inlinetypes.FlattenableSemanticTest
 34  * @run main/othervm -XX:+EnableValhalla -XX:+EnablePrimitiveClasses -XX:+UnlockDiagnosticVMOptions -XX:ForceNonTearable=* runtime.valhalla.inlinetypes.FlattenableSemanticTest
 35  * // debug: -XX:+PrintInlineLayout -XX:-ShowMessageBoxOnError
 36  */
 37 public class FlattenableSemanticTest {
 38 
 39     static Point.ref nfsp;
 40     static Point fsp;
 41 
 42     Point.ref nfip;
 43     Point fip;
 44 
 45     static JumboInline.ref nfsj;
 46     static JumboInline fsj;
 47 
 48     JumboInline.ref nfij;
 49     JumboInline fij;
 50 
 51     static Object getNull() {
 52         return null;
 53     }
 54 
 55     FlattenableSemanticTest() { }
 56 
 57     public static void main(String[] args) {
 58         FlattenableSemanticTest test = new FlattenableSemanticTest();
 59 
 60         // Uninitialized inline fields must be null for non flattenable fields
 61         Asserts.assertNull(nfsp, "Invalid non null value for unitialized non flattenable field");
 62         Asserts.assertNull(nfsj, "Invalid non null value for unitialized non flattenable field");
 63         Asserts.assertNull(test.nfip, "Invalid non null value for unitialized non flattenable field");
 64         Asserts.assertNull(test.nfij, "Invalid non null value for unitialized non flattenable field");
 65 
 66         // fsp.equals(null);
 67 
 68         // Uninitialized inline fields must be non null for flattenable fields
 69         Asserts.assertNotNull(fsp, "Invalid null value for unitialized flattenable field");
 70         Asserts.assertNotNull(fsj, "Invalid null value for unitialized flattenable field");
 71         Asserts.assertNotNull(test.fip, "Invalid null value for unitialized flattenable field");
 72         Asserts.assertNotNull(test.fij, "Invalid null value for unitialized flattenable field");
 73 
 74         // Assigning null must be allowed for non flattenable inline fields
 75         boolean exception = true;
 76         try {
 77             nfsp = (Point.ref)getNull();
 78             nfsp = null;
 79             exception = false;
 80         } catch (NullPointerException e) {
 81             exception = true;
 82         }
 83         Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field");
 84 
 85         try {
 86             nfsj = (JumboInline.ref)getNull();
 87             nfsj = null;
 88             exception = false;
 89         } catch (NullPointerException e) {
 90             exception = true;
 91         }
 92         Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field");
 93 
 94         try {
 95             test.nfip = (Point.ref)getNull();
 96             test.nfip = null;
 97             exception = false;
 98         } catch (NullPointerException e) {
 99             exception = true;
100         }
101         Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field");
102 
103         try {
104             test.nfij = (JumboInline.ref)getNull();
105             test.nfij = null;
106             exception = false;
107         } catch (NullPointerException e) {
108             exception = true;
109         }
110         Asserts.assertFalse(exception, "Invalid NPE when assigning null to a non flattenable field");
111 
112         // Assigning null to a flattenable inline field must trigger a NPE
113         exception = false;
114         try {
115             fsp = (Point)getNull();
116         } catch(NullPointerException e) {
117             exception = true;
118         }
119         Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field");
120         exception = false;
121         try {
122             fsj = (JumboInline)getNull();
123         } catch(NullPointerException e) {
124             exception = true;
125         }
126         Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field");
127         exception = false;
128         try {
129             test.fip = (Point)getNull();
130         } catch(NullPointerException e) {
131             exception = true;
132         }
133         Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field");
134         exception = false;
135         try {
136             test.fij = (JumboInline)getNull();
137         } catch(NullPointerException e) {
138             exception = true;
139         }
140         Asserts.assertTrue(exception, "NPE not thrown when assigning null to a flattenable field");
141         exception = false;
142     }
143 
144 }