1 /*
  2  * Copyright (c) 2019, 2022, 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  * @modules java.base/jdk.internal.misc
 28  * @run testng TestValueLayouts
 29  */
 30 
 31 import org.testng.annotations.*;
 32 
 33 import java.lang.foreign.*;
 34 import java.nio.ByteOrder;
 35 import jdk.internal.misc.Unsafe;
 36 
 37 import static java.lang.foreign.ValueLayout.*;
 38 import static org.testng.Assert.*;
 39 
 40 public class TestValueLayouts {
 41 
 42     @Test
 43     public void testByte() {
 44         testAligned(JAVA_BYTE, byte.class, Byte.BYTES);
 45     }
 46 
 47     @Test
 48     public void testBoolean() {
 49         testAligned(JAVA_BOOLEAN, boolean.class, Byte.BYTES);
 50     }
 51 
 52     @Test
 53     public void testShort() {
 54         testAligned(JAVA_SHORT, short.class, Short.BYTES);
 55     }
 56 
 57     @Test
 58     public void testShortUnaligned() {
 59         testUnaligned(JAVA_SHORT_UNALIGNED, short.class, Short.BYTES);
 60     }
 61 
 62     @Test
 63     public void testInt() {
 64         testAligned(JAVA_INT, int.class, Integer.BYTES);
 65     }
 66 
 67     @Test
 68     public void testIntUnaligned() {
 69         testUnaligned(JAVA_INT_UNALIGNED, int.class, Integer.BYTES);
 70     }
 71 
 72     @Test
 73     public void testLong() {
 74         testAligned(JAVA_LONG, long.class, Long.BYTES, ADDRESS.byteSize());
 75     }
 76 
 77     @Test
 78     public void testLongUnaligned() {
 79         testUnaligned(JAVA_LONG_UNALIGNED, long.class, Long.BYTES);
 80     }
 81 
 82     @Test
 83     public void testFloat() {
 84         testAligned(JAVA_FLOAT, float.class, Float.BYTES);
 85     }
 86 
 87     @Test
 88     public void testFloatUnaligned() {
 89         testUnaligned(JAVA_FLOAT_UNALIGNED, float.class, Float.BYTES);
 90     }
 91 
 92     @Test
 93     public void testDouble() {
 94         testAligned(JAVA_DOUBLE, double.class, Double.BYTES, ADDRESS.byteSize());
 95     }
 96 
 97     @Test
 98     public void testDoubleUnaligned() {
 99         testUnaligned(JAVA_DOUBLE_UNALIGNED, double.class, Double.BYTES);
100     }
101 
102     @Test
103     public void testChar() {
104         testAligned(JAVA_CHAR, char.class, Character.BYTES);
105     }
106 
107     @Test
108     public void testCharUnaligned() {
109         testUnaligned(JAVA_CHAR_UNALIGNED, char.class, Character.BYTES);
110     }
111 
112     @Test
113     public void testAddress() {
114         testAligned(ADDRESS, MemorySegment.class, Unsafe.ADDRESS_SIZE);
115     }
116 
117     @Test
118     public void testAddressUnaligned() {
119         testUnaligned(ADDRESS_UNALIGNED, MemorySegment.class, Unsafe.ADDRESS_SIZE);
120     }
121 
122     void testAligned(ValueLayout layout,
123                      Class<?> carrier,
124                      long byteSize) {
125         test(layout, carrier, byteSize, byteSize);
126     }
127 
128     void testAligned(ValueLayout layout,
129                      Class<?> carrier,
130                      long byteSize,
131                      long byteAlignment) {
132         test(layout, carrier, byteSize, byteAlignment);
133     }
134 
135     void testUnaligned(ValueLayout layout,
136                        Class<?> carrier,
137                        long byteSize) {
138         test(layout, carrier, byteSize, Byte.BYTES);
139     }
140 
141     void test(ValueLayout layout,
142               Class<?> carrier,
143               long byteSize,
144               long byteAlignment) {
145         assertEquals(layout.carrier(), carrier);
146         assertEquals(layout.byteSize(), byteSize);
147         assertEquals(layout.order(), ByteOrder.nativeOrder());
148         assertEquals(layout.byteAlignment(), byteAlignment);
149         assertTrue(layout.name().isEmpty());
150 
151     }
152 
153 }