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