< prev index next >

test/jdk/java/foreign/TestLayoutConstants.java

Print this page

 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.MemoryLayouts;
 31 import jdk.incubator.foreign.MemoryLayout;
 32 
 33 import java.lang.invoke.MethodHandles;

 34 

 35 import org.testng.annotations.*;
 36 import static org.testng.Assert.*;
 37 
 38 public class TestLayoutConstants {
 39 
 40     @Test(dataProvider = "layouts")
 41     public void testDescribeResolve(MemoryLayout expected) {
 42         try {
 43             MemoryLayout actual = expected.describeConstable().get()
 44                     .resolveConstantDesc(MethodHandles.lookup());
 45             assertEquals(actual, expected);
 46         } catch (ReflectiveOperationException ex) {
 47             throw new AssertionError(ex);
 48         }
 49     }
 50 
 51     @Test(dataProvider = "functions")
 52     public void testDescribeResolveFunction(MemoryLayout layout, boolean isVoid) {
 53         FunctionDescriptor expected = isVoid ?
 54                 FunctionDescriptor.ofVoid(layout) :
 55                 FunctionDescriptor.of(layout, layout);
 56         try {
 57             FunctionDescriptor actual = expected.describeConstable().get()
 58                     .resolveConstantDesc(MethodHandles.lookup());
 59             assertEquals(actual, expected);
 60         } catch (ReflectiveOperationException ex) {
 61             throw new AssertionError(ex);
 62         }
 63     }
 64 
 65     @DataProvider(name = "layouts")
 66     public Object[][] createLayouts() {
 67         return new Object[][] {
 68                 //padding
 69                 { MemoryLayouts.PAD_32 },
 70                 { MemoryLayout.sequenceLayout(MemoryLayouts.PAD_32) },
 71                 { MemoryLayout.sequenceLayout(5, MemoryLayouts.PAD_32) },
 72                 { MemoryLayout.structLayout(MemoryLayouts.PAD_32, MemoryLayouts.PAD_32) },
 73                 { MemoryLayout.unionLayout(MemoryLayouts.PAD_32, MemoryLayouts.PAD_32) },
 74                 //values, big endian
 75                 { MemoryLayouts.BITS_32_BE },
 76                 { MemoryLayout.structLayout(
 77                         MemoryLayouts.BITS_32_BE,
 78                         MemoryLayouts.BITS_32_BE) },
 79                 { MemoryLayout.unionLayout(
 80                         MemoryLayouts.BITS_32_BE,
 81                         MemoryLayouts.BITS_32_BE) },
 82                 //values, little endian
 83                 { MemoryLayouts.BITS_32_LE },
 84                 { MemoryLayout.structLayout(
 85                         MemoryLayouts.BITS_32_LE,
 86                         MemoryLayouts.BITS_32_LE) },
 87                 { MemoryLayout.unionLayout(
 88                         MemoryLayouts.BITS_32_LE,
 89                         MemoryLayouts.BITS_32_LE) },
 90                 //deeply nested
 91                 { MemoryLayout.structLayout(
 92                         MemoryLayouts.PAD_16,
 93                         MemoryLayout.structLayout(
 94                                 MemoryLayouts.PAD_8,
 95                                 MemoryLayouts.BITS_32_BE)) },
 96                 { MemoryLayout.unionLayout(
 97                         MemoryLayouts.PAD_16,
 98                         MemoryLayout.structLayout(
 99                                 MemoryLayouts.PAD_8,
100                                 MemoryLayouts.BITS_32_BE)) },
101                 { MemoryLayout.sequenceLayout(
102                         MemoryLayout.structLayout(
103                                 MemoryLayouts.PAD_8,
104                                 MemoryLayouts.BITS_32_BE)) },
105                 { MemoryLayout.sequenceLayout(5,
106                         MemoryLayout.structLayout(
107                                 MemoryLayouts.PAD_8,
108                                 MemoryLayouts.BITS_32_BE)) },
109                 { MemoryLayouts.BITS_32_LE.withName("myInt") },
110                 { MemoryLayouts.BITS_32_LE.withBitAlignment(8) },
111                 { MemoryLayouts.BITS_32_LE.withAttribute("xyz", "abc") },
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 }

 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 }
< prev index next >