1 /*
  2  * Copyright (c) 2020, 2023, 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  * @enablePreview
 27  * @run testng/othervm MemoryLayoutPrincipalTotalityTest
 28  */
 29 
 30 import org.testng.annotations.*;
 31 
 32 import java.lang.foreign.*;
 33 
 34 import static java.lang.foreign.ValueLayout.*;
 35 import static org.testng.Assert.*;
 36 
 37 public class MemoryLayoutPrincipalTotalityTest {
 38 
 39     // The tests in this class is mostly there to ensure compile-time pattern matching totality.
 40 
 41     @Test
 42     public void testBasicTotality() {
 43         MemoryLayout memoryLayout = javaIntMemoryLayout();
 44         int v0 = switch (memoryLayout) {
 45             case MemoryLayout ml -> 1;
 46         };
 47         assertEquals(v0, 1);
 48     }
 49 
 50     @Test
 51     public void testMLRemovedTotality() {
 52         MemoryLayout memoryLayout = javaIntMemoryLayout();
 53         var v1 = switch (memoryLayout) {
 54             case GroupLayout gl -> 0;
 55             case PaddingLayout pl -> 0; // leaf
 56             case SequenceLayout sl -> 0; // leaf
 57             case ValueLayout vl -> 1;
 58         };
 59         assertEquals(v1, 1);
 60     }
 61 
 62     @Test
 63     public void testMLGLRemovedTotality() {
 64         MemoryLayout memoryLayout = javaIntMemoryLayout();
 65         var v2 = switch (memoryLayout) {
 66             case PaddingLayout pl -> 0; // leaf
 67             case SequenceLayout sl -> 0; // leaf
 68             case ValueLayout vl -> 1;
 69             case StructLayout sl -> 0; // leaf
 70             case UnionLayout ul -> 0; // leaf
 71         };
 72         assertEquals(v2, 1);
 73     }
 74 
 75     @Test
 76     public void testMLGLVLRemovedTotality() {
 77         MemoryLayout memoryLayout = javaIntMemoryLayout();
 78         var v3 = switch (memoryLayout) {
 79             case PaddingLayout pl -> 0; // leaf
 80             case SequenceLayout sl -> 0; // leaf
 81             case StructLayout sl -> 0; // leaf
 82             case UnionLayout ul -> 0; // leaf
 83             case AddressLayout oa -> 0; // leaf
 84             case OfBoolean ob -> 0; // leaf
 85             case OfByte ob -> 0; // leaf
 86             case OfChar oc -> 0; // leaf
 87             case OfDouble od -> 0; // leaf
 88             case OfFloat of -> 0; // leaf
 89             case OfInt oi -> 1; // leaf
 90             case OfLong ol -> 0; // leaf
 91             case OfShort os -> 0; // leaf
 92         };
 93         assertEquals(v3, 1);
 94     }
 95 
 96     @Test
 97     public void testMLVLRemovedTotality() {
 98         MemoryLayout memoryLayout = javaIntMemoryLayout();
 99         var v4 = switch (memoryLayout) {
100             case GroupLayout gl -> 0;
101             case PaddingLayout pl -> 0; // leaf
102             case SequenceLayout sl -> 0; // leaf
103             case AddressLayout oa -> 0; // leaf
104             case OfBoolean ob -> 0; // leaf
105             case OfByte ob -> 0; // leaf
106             case OfChar oc -> 0; // leaf
107             case OfDouble od -> 0; // leaf
108             case OfFloat of -> 0; // leaf
109             case OfInt oi -> 1; // leaf
110             case OfLong ol -> 0; // leaf
111             case OfShort os -> 0; // leaf
112         };
113         assertEquals(v4, 1);
114     }
115 
116     private static MemoryLayout javaIntMemoryLayout() {
117         return JAVA_INT;
118     }
119 
120 }