1 /*
 2  *  Copyright (c) 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 /*
26  * @test
27  * @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
28  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestFunctionDescriptor
29  */
30 
31 import jdk.incubator.foreign.FunctionDescriptor;
32 import jdk.incubator.foreign.MemoryLayout;
33 import org.testng.annotations.Test;
34 
35 import java.lang.constant.Constable;
36 import java.util.List;
37 import java.util.Optional;
38 import java.util.stream.Collectors;
39 
40 import static org.testng.Assert.assertEquals;
41 import static org.testng.Assert.assertFalse;
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.withAppendedArgumentLayouts(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.withReturnLayout(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.withVoidReturnLayout();
93 
94         assertEquals(fd.argumentLayouts(), List.of(C_DOUBLE, C_LONG_LONG));
95         Optional<MemoryLayout> returnLayoutOp = fd.returnLayout();
96         assertFalse(returnLayoutOp.isPresent());
97     }
98 }