1 /* 2 * Copyright (c) 2020, 2023, 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 * @requires jdk.foreign.linker != "UNSUPPORTED" 28 * @run testng/othervm --enable-native-access=ALL-UNNAMED TestFunctionDescriptor 29 */ 30 31 import java.lang.foreign.FunctionDescriptor; 32 import java.lang.foreign.MemoryLayout; 33 import java.lang.foreign.MemorySegment; 34 import java.lang.invoke.MethodType; 35 import java.util.List; 36 import java.util.Optional; 37 import org.testng.annotations.Test; 38 39 import static org.testng.Assert.assertEquals; 40 import static org.testng.Assert.assertFalse; 41 import static org.testng.Assert.assertNotEquals; 42 import static org.testng.Assert.assertTrue; 43 44 public class TestFunctionDescriptor extends NativeTestHelper { 45 46 static final String DUMMY_ATTR = "dummy"; 47 48 @Test 49 public void testOf() { 50 FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_DOUBLE, C_LONG_LONG); 51 52 assertEquals(fd.argumentLayouts(), List.of(C_DOUBLE, C_LONG_LONG)); 53 Optional<MemoryLayout> returnLayoutOp = fd.returnLayout(); 54 assertTrue(returnLayoutOp.isPresent()); 55 assertEquals(returnLayoutOp.get(), C_INT); 56 } 57 58 @Test 59 public void testOfVoid() { 60 FunctionDescriptor fd = FunctionDescriptor.ofVoid(C_DOUBLE, C_LONG_LONG); 61 62 assertEquals(fd.argumentLayouts(), List.of(C_DOUBLE, C_LONG_LONG)); 63 Optional<MemoryLayout> returnLayoutOp = fd.returnLayout(); 64 assertFalse(returnLayoutOp.isPresent()); 65 } 66 67 @Test 68 public void testAppendArgumentLayouts() { 69 FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_DOUBLE, C_LONG_LONG); 70 fd = fd.appendArgumentLayouts(C_POINTER); 71 72 assertEquals(fd.argumentLayouts(), List.of(C_DOUBLE, C_LONG_LONG, C_POINTER)); 73 Optional<MemoryLayout> returnLayoutOp = fd.returnLayout(); 74 assertTrue(returnLayoutOp.isPresent()); 75 assertEquals(returnLayoutOp.get(), C_INT); 76 } 77 78 @Test 79 public void testChangeReturnLayout() { 80 FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_DOUBLE, C_LONG_LONG); 81 fd = fd.changeReturnLayout(C_INT); 82 83 assertEquals(fd.argumentLayouts(), List.of(C_DOUBLE, C_LONG_LONG)); 84 Optional<MemoryLayout> returnLayoutOp = fd.returnLayout(); 85 assertTrue(returnLayoutOp.isPresent()); 86 assertEquals(returnLayoutOp.get(), C_INT); 87 } 88 89 @Test 90 public void testDropReturnLayout() { 91 FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_DOUBLE, C_LONG_LONG); 92 fd = fd.dropReturnLayout(); 93 94 assertEquals(fd.argumentLayouts(), List.of(C_DOUBLE, C_LONG_LONG)); 95 Optional<MemoryLayout> returnLayoutOp = fd.returnLayout(); 96 assertFalse(returnLayoutOp.isPresent()); 97 } 98 99 @Test 100 public void testEquals() { 101 FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT); 102 assertEquals(fd, fd); 103 FunctionDescriptor fdReturnSomethingElse = FunctionDescriptor.of(C_LONG_LONG, C_INT, C_INT); 104 FunctionDescriptor fdOtherArguments = FunctionDescriptor.of(C_INT, C_INT); 105 assertFalse(fd.equals(fdReturnSomethingElse)); 106 assertFalse(fd.equals(fdOtherArguments)); 107 assertFalse(fd.equals(null)); 108 assertFalse(fd.equals("A")); 109 } 110 111 @Test 112 public void testCarrierMethodType() { 113 FunctionDescriptor fd = FunctionDescriptor.of(C_INT, 114 C_INT, 115 MemoryLayout.structLayout(C_INT, C_INT), 116 MemoryLayout.sequenceLayout(3, C_INT)); 117 MethodType cmt = fd.toMethodType(); 118 assertEquals(cmt, MethodType.methodType(int.class, int.class, MemorySegment.class, MemorySegment.class)); 119 } 120 121 @Test(expectedExceptions = IllegalArgumentException.class) 122 public void testBadCarrierMethodType() { 123 FunctionDescriptor fd = FunctionDescriptor.of(C_INT, 124 C_INT, 125 MemoryLayout.structLayout(C_INT, C_INT), 126 MemoryLayout.sequenceLayout(3, C_INT), 127 MemoryLayout.paddingLayout(4)); 128 fd.toMethodType(); // should throw 129 } 130 131 @Test(expectedExceptions = IllegalArgumentException.class) 132 public void testIllegalInsertArgNegIndex() { 133 FunctionDescriptor fd = FunctionDescriptor.of(C_INT); 134 fd.insertArgumentLayouts(-1, C_INT); 135 } 136 137 @Test(expectedExceptions = IllegalArgumentException.class) 138 public void testIllegalInsertArgOutOfBounds() { 139 FunctionDescriptor fd = FunctionDescriptor.of(C_INT); 140 fd.insertArgumentLayouts(2, C_INT); 141 } 142 143 @Test(expectedExceptions = IllegalArgumentException.class) 144 public void testBadPaddingInVoidFunction() { 145 FunctionDescriptor.ofVoid(MemoryLayout.paddingLayout(1)); 146 } 147 148 @Test(expectedExceptions = IllegalArgumentException.class) 149 public void testBadPaddingInNonVoidFunction() { 150 FunctionDescriptor.of(MemoryLayout.paddingLayout(1)); 151 } 152 153 @Test(expectedExceptions = IllegalArgumentException.class) 154 public void testBadPaddingInAppendArgLayouts() { 155 FunctionDescriptor.ofVoid().appendArgumentLayouts(MemoryLayout.paddingLayout(1)); 156 } 157 158 @Test(expectedExceptions = IllegalArgumentException.class) 159 public void testBadPaddingInInsertArgLayouts() { 160 FunctionDescriptor.ofVoid().insertArgumentLayouts(0, MemoryLayout.paddingLayout(1)); 161 } 162 163 @Test(expectedExceptions = IllegalArgumentException.class) 164 public void testBadPaddingInChangeRetLayout() { 165 FunctionDescriptor.ofVoid().changeReturnLayout(MemoryLayout.paddingLayout(1)); 166 } 167 168 }