1 /*
  2  *  Copyright (c) 2019, 2020, 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 TestLayoutConstants
 27  */
 28 
 29 import jdk.incubator.foreign.FunctionDescriptor;
 30 import jdk.incubator.foreign.MemoryLayout;
 31 
 32 import java.lang.invoke.MethodHandles;
 33 import java.nio.ByteOrder;
 34 
 35 import jdk.incubator.foreign.ValueLayout;
 36 import org.testng.annotations.*;
 37 import static org.testng.Assert.*;
 38 
 39 public class TestLayoutConstants {
 40 
 41     @Test(dataProvider = "layouts")
 42     public void testDescribeResolve(MemoryLayout expected) {
 43         try {
 44             MemoryLayout actual = expected.describeConstable().get()
 45                     .resolveConstantDesc(MethodHandles.lookup());
 46             assertEquals(actual, expected);
 47         } catch (ReflectiveOperationException ex) {
 48             throw new AssertionError(ex);
 49         }
 50     }
 51 
 52     @Test(dataProvider = "functions")
 53     public void testDescribeResolveFunction(MemoryLayout layout, boolean isVoid) {
 54         FunctionDescriptor expected = isVoid ?
 55                 FunctionDescriptor.ofVoid(layout) :
 56                 FunctionDescriptor.of(layout, layout);
 57         try {
 58             FunctionDescriptor actual = expected.describeConstable().get()
 59                     .resolveConstantDesc(MethodHandles.lookup());
 60             assertEquals(actual, expected);
 61         } catch (ReflectiveOperationException ex) {
 62             throw new AssertionError(ex);
 63         }
 64     }
 65 
 66     @DataProvider(name = "layouts")
 67     public Object[][] createLayouts() {
 68         return new Object[][] {
 69                 //padding
 70                 {MemoryLayout.paddingLayout(32)},
 71                 { MemoryLayout.sequenceLayout(MemoryLayout.paddingLayout(32)) },
 72                 { MemoryLayout.sequenceLayout(5, MemoryLayout.paddingLayout(32)) },
 73                 { MemoryLayout.structLayout(MemoryLayout.paddingLayout(32), MemoryLayout.paddingLayout(32)) },
 74                 { MemoryLayout.unionLayout(MemoryLayout.paddingLayout(32), MemoryLayout.paddingLayout(32)) },
 75                 //values, big endian
 76                 { ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN) },
 77                 { MemoryLayout.structLayout(
 78                         ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN),
 79                         ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN)) },
 80                 { MemoryLayout.unionLayout(
 81                         ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN),
 82                         ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN)) },
 83                 //values, little endian
 84                 { ValueLayout.JAVA_INT.withOrder(ByteOrder.LITTLE_ENDIAN) },
 85                 { MemoryLayout.structLayout(
 86                         ValueLayout.JAVA_INT.withOrder(ByteOrder.LITTLE_ENDIAN),
 87                         ValueLayout.JAVA_INT.withOrder(ByteOrder.LITTLE_ENDIAN)) },
 88                 { MemoryLayout.unionLayout(
 89                         ValueLayout.JAVA_INT.withOrder(ByteOrder.LITTLE_ENDIAN),
 90                         ValueLayout.JAVA_INT.withOrder(ByteOrder.LITTLE_ENDIAN)) },
 91                 //deeply nested
 92                 { MemoryLayout.structLayout(
 93                         MemoryLayout.paddingLayout(16),
 94                         MemoryLayout.structLayout(
 95                                 MemoryLayout.paddingLayout(8),
 96                                 ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN))) },
 97                 { MemoryLayout.unionLayout(
 98                         MemoryLayout.paddingLayout(16),
 99                         MemoryLayout.structLayout(
100                                 MemoryLayout.paddingLayout(8),
101                                 ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN))) },
102                 { MemoryLayout.sequenceLayout(
103                         MemoryLayout.structLayout(
104                                 MemoryLayout.paddingLayout(8),
105                                 ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN))) },
106                 { MemoryLayout.sequenceLayout(5,
107                         MemoryLayout.structLayout(
108                                 MemoryLayout.paddingLayout(8),
109                                 ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN))) },
110                 { ValueLayout.JAVA_INT.withOrder(ByteOrder.LITTLE_ENDIAN).withName("myInt") },
111                 { ValueLayout.JAVA_INT.withOrder(ByteOrder.LITTLE_ENDIAN).withBitAlignment(8) },
112         };
113     }
114 
115     @DataProvider(name = "functions")
116     public Object[][] createFunctions() {
117         Object[][] layouts = createLayouts();
118         Object[][] functions = new Object[layouts.length * 2][];
119         boolean[] values = new boolean[] { true, false };
120         for (int i = 0 ; i < layouts.length ; i++) {
121             for (boolean isVoid : values) {
122                 int offset = 0;
123                 if (isVoid) {
124                     offset += 1;
125                 }
126                 functions[i * 2 + offset] = new Object[] { layouts[i][0], isVoid };
127             }
128         }
129         return functions;
130     }
131 }