Path: blob/master/test/jdk/java/foreign/TestLayoutConstants.java
41145 views
/*1* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @run testng TestLayoutConstants26*/2728import jdk.incubator.foreign.FunctionDescriptor;29import jdk.incubator.foreign.MemoryLayouts;30import jdk.incubator.foreign.MemoryLayout;3132import java.lang.invoke.MethodHandles;3334import org.testng.annotations.*;35import static org.testng.Assert.*;3637public class TestLayoutConstants {3839@Test(dataProvider = "layouts")40public void testDescribeResolve(MemoryLayout expected) {41try {42MemoryLayout actual = expected.describeConstable().get()43.resolveConstantDesc(MethodHandles.lookup());44assertEquals(actual, expected);45} catch (ReflectiveOperationException ex) {46throw new AssertionError(ex);47}48}4950@Test(dataProvider = "functions")51public void testDescribeResolveFunction(MemoryLayout layout, boolean isVoid) {52FunctionDescriptor expected = isVoid ?53FunctionDescriptor.ofVoid(layout) :54FunctionDescriptor.of(layout, layout);55try {56FunctionDescriptor actual = expected.describeConstable().get()57.resolveConstantDesc(MethodHandles.lookup());58assertEquals(actual, expected);59} catch (ReflectiveOperationException ex) {60throw new AssertionError(ex);61}62}6364@DataProvider(name = "layouts")65public Object[][] createLayouts() {66return new Object[][] {67//padding68{ MemoryLayouts.PAD_32 },69{ MemoryLayout.sequenceLayout(MemoryLayouts.PAD_32) },70{ MemoryLayout.sequenceLayout(5, MemoryLayouts.PAD_32) },71{ MemoryLayout.structLayout(MemoryLayouts.PAD_32, MemoryLayouts.PAD_32) },72{ MemoryLayout.unionLayout(MemoryLayouts.PAD_32, MemoryLayouts.PAD_32) },73//values, big endian74{ MemoryLayouts.BITS_32_BE },75{ MemoryLayout.structLayout(76MemoryLayouts.BITS_32_BE,77MemoryLayouts.BITS_32_BE) },78{ MemoryLayout.unionLayout(79MemoryLayouts.BITS_32_BE,80MemoryLayouts.BITS_32_BE) },81//values, little endian82{ MemoryLayouts.BITS_32_LE },83{ MemoryLayout.structLayout(84MemoryLayouts.BITS_32_LE,85MemoryLayouts.BITS_32_LE) },86{ MemoryLayout.unionLayout(87MemoryLayouts.BITS_32_LE,88MemoryLayouts.BITS_32_LE) },89//deeply nested90{ MemoryLayout.structLayout(91MemoryLayouts.PAD_16,92MemoryLayout.structLayout(93MemoryLayouts.PAD_8,94MemoryLayouts.BITS_32_BE)) },95{ MemoryLayout.unionLayout(96MemoryLayouts.PAD_16,97MemoryLayout.structLayout(98MemoryLayouts.PAD_8,99MemoryLayouts.BITS_32_BE)) },100{ MemoryLayout.sequenceLayout(101MemoryLayout.structLayout(102MemoryLayouts.PAD_8,103MemoryLayouts.BITS_32_BE)) },104{ MemoryLayout.sequenceLayout(5,105MemoryLayout.structLayout(106MemoryLayouts.PAD_8,107MemoryLayouts.BITS_32_BE)) },108{ MemoryLayouts.BITS_32_LE.withName("myInt") },109{ MemoryLayouts.BITS_32_LE.withBitAlignment(8) },110{ MemoryLayouts.BITS_32_LE.withAttribute("xyz", "abc") },111};112}113114@DataProvider(name = "functions")115public Object[][] createFunctions() {116Object[][] layouts = createLayouts();117Object[][] functions = new Object[layouts.length * 2][];118boolean[] values = new boolean[] { true, false };119for (int i = 0 ; i < layouts.length ; i++) {120for (boolean isVoid : values) {121int offset = 0;122if (isVoid) {123offset += 1;124}125functions[i * 2 + offset] = new Object[] { layouts[i][0], isVoid };126}127}128return functions;129}130}131132133