|
35 | 35 | import java.lang.foreign.Linker;
|
36 | 36 | import java.lang.invoke.MethodHandle;
|
37 | 37 |
|
| 38 | +import static java.lang.foreign.MemoryLayout.*; |
| 39 | +import static java.lang.foreign.ValueLayout.JAVA_CHAR; |
| 40 | +import static java.lang.foreign.ValueLayout.JAVA_SHORT; |
| 41 | +import static org.testng.Assert.assertSame; |
38 | 42 | import static org.testng.Assert.assertNotSame;
|
39 | 43 |
|
40 | 44 | public class TestLinker extends NativeTestHelper {
|
41 | 45 |
|
42 |
| - @Test |
43 |
| - public void testLinkerOptionsCache() { |
| 46 | + record LinkRequest(FunctionDescriptor descriptor, Linker.Option... options) {} |
| 47 | + |
| 48 | + @Test(dataProvider = "notSameCases") |
| 49 | + public void testLinkerOptionsCache(LinkRequest l1, LinkRequest l2) { |
44 | 50 | Linker linker = Linker.nativeLinker();
|
45 |
| - FunctionDescriptor descriptor = FunctionDescriptor.ofVoid(C_INT, C_INT); |
46 |
| - MethodHandle mh1 = linker.downcallHandle(descriptor); |
47 |
| - MethodHandle mh2 = linker.downcallHandle(descriptor, Linker.Option.firstVariadicArg(1)); |
| 51 | + MethodHandle mh1 = linker.downcallHandle(l1.descriptor(), l1.options()); |
| 52 | + MethodHandle mh2 = linker.downcallHandle(l2.descriptor(), l2.options()); |
48 | 53 | // assert that these are 2 distinct link request. No caching allowed
|
49 | 54 | assertNotSame(mh1, mh2);
|
50 | 55 | }
|
51 | 56 |
|
| 57 | + @DataProvider |
| 58 | + public static Object[][] notSameCases() { |
| 59 | + FunctionDescriptor fd_II_V = FunctionDescriptor.ofVoid(C_INT, C_INT); |
| 60 | + return new Object[][]{ |
| 61 | + {new LinkRequest(fd_II_V), new LinkRequest(fd_II_V, Linker.Option.firstVariadicArg(1))}, |
| 62 | + {new LinkRequest(FunctionDescriptor.ofVoid(JAVA_SHORT)), new LinkRequest(FunctionDescriptor.ofVoid(JAVA_CHAR))}, |
| 63 | + {new LinkRequest(FunctionDescriptor.ofVoid(JAVA_SHORT)), new LinkRequest(FunctionDescriptor.ofVoid(JAVA_CHAR))}, |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + @Test(dataProvider = "namedDescriptors") |
| 68 | + public void testNamedLinkerCache(FunctionDescriptor f1, FunctionDescriptor f2) { |
| 69 | + Linker linker = Linker.nativeLinker(); |
| 70 | + MethodHandle mh1 = linker.downcallHandle(f1); |
| 71 | + MethodHandle mh2 = linker.downcallHandle(f2); |
| 72 | + // assert that these are the same link request, even though layout names differ |
| 73 | + assertSame(mh1, mh2); |
| 74 | + } |
| 75 | + |
| 76 | + @DataProvider |
| 77 | + public static Object[][] namedDescriptors() { |
| 78 | + return new Object[][]{ |
| 79 | + { FunctionDescriptor.ofVoid(C_INT), |
| 80 | + FunctionDescriptor.ofVoid(C_INT.withName("x")) }, |
| 81 | + { FunctionDescriptor.ofVoid(structLayout(C_INT)), |
| 82 | + FunctionDescriptor.ofVoid(structLayout(C_INT).withName("x")) }, |
| 83 | + { FunctionDescriptor.ofVoid(structLayout(C_INT)), |
| 84 | + FunctionDescriptor.ofVoid(structLayout(C_INT.withName("x"))) }, |
| 85 | + { FunctionDescriptor.ofVoid(structLayout(C_INT, paddingLayout(32), C_LONG_LONG)), |
| 86 | + FunctionDescriptor.ofVoid(structLayout(C_INT, paddingLayout(32), C_LONG_LONG.withName("x"))) }, |
| 87 | + { FunctionDescriptor.ofVoid(structLayout(C_INT, paddingLayout(32), C_LONG_LONG)), |
| 88 | + FunctionDescriptor.ofVoid(structLayout(C_INT, paddingLayout(32).withName("x"), C_LONG_LONG)) }, |
| 89 | + { FunctionDescriptor.ofVoid(structLayout(sequenceLayout(1, C_INT))), |
| 90 | + FunctionDescriptor.ofVoid(structLayout(sequenceLayout(1, C_INT).withName("x"))) }, |
| 91 | + { FunctionDescriptor.ofVoid(structLayout(sequenceLayout(1, C_INT))), |
| 92 | + FunctionDescriptor.ofVoid(structLayout(sequenceLayout(1, C_INT.withName("x")))) }, |
| 93 | + { FunctionDescriptor.ofVoid(unionLayout(C_INT)), |
| 94 | + FunctionDescriptor.ofVoid(unionLayout(C_INT).withName("x")) }, |
| 95 | + { FunctionDescriptor.ofVoid(unionLayout(C_INT)), |
| 96 | + FunctionDescriptor.ofVoid(unionLayout(C_INT.withName("x"))) }, |
| 97 | + { FunctionDescriptor.ofVoid(C_POINTER), |
| 98 | + FunctionDescriptor.ofVoid(C_POINTER.withName("x")) }, |
| 99 | + { FunctionDescriptor.ofVoid(C_POINTER.withTargetLayout(C_INT)), |
| 100 | + FunctionDescriptor.ofVoid(C_POINTER.withTargetLayout(C_INT.withName("x"))) }, |
| 101 | + { FunctionDescriptor.ofVoid(C_POINTER.withTargetLayout(C_INT)), |
| 102 | + FunctionDescriptor.ofVoid(C_POINTER.withName("x").withTargetLayout(C_INT.withName("x"))) }, |
| 103 | + }; |
| 104 | + } |
| 105 | + |
52 | 106 | @DataProvider
|
53 | 107 | public static Object[][] invalidIndexCases() {
|
54 | 108 | return new Object[][]{
|
|
0 commit comments