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