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 import jdk.incubator.foreign.MemoryAddress;
25 import org.testng.annotations.Test;
26 import test.jextract.test8246341.*;
27 import static org.testng.Assert.assertEquals;
28 import static org.testng.Assert.assertTrue;
29 import static test.jextract.test8246341.test8246341_h.*;
30 
31 /*
32  * @test
33  * @library ..
34  * @modules jdk.incubator.jextract
35  * @bug 8246341
36  * @summary jextract should generate Cpointer utilities class
37  * @run driver JtregJextract -l Test8246341 -t test.jextract.test8246341 -- test8246341.h
38  * @run testng/othervm -Dforeign.restricted=permit LibTest8246341Test
39  */
40 public class LibTest8246341Test {
41     @Test
42     public void testPointerArray() {
43         boolean[] callbackCalled = new boolean[1];
44         try (var callback = func$callback.allocate((argc, argv) -> {
45             callbackCalled[0] = true;
46             var addr = Cpointer.asArray(argv, argc);
47             assertEquals(argc, 4);
48             assertEquals(Cstring.toJavaString(Cpointer.get(addr, 0)), "java");
49             assertEquals(Cstring.toJavaString(Cpointer.get(addr, 1)), "python");
50             assertEquals(Cstring.toJavaString(Cpointer.get(addr, 2)), "javascript");
51             assertEquals(Cstring.toJavaString(Cpointer.get(addr, 3)), "c++");
52         })) {
53             func(callback.baseAddress());
54         }
55         assertTrue(callbackCalled[0]);
56     }
57 
58     @Test
59     public void testPointerAllocate() {
60         try (var scope = new CScope(Cpointer.sizeof())) {
61             var addr = Cpointer.allocate(MemoryAddress.NULL, scope);
62             fillin(addr);
63             assertEquals(Cstring.toJavaString(Cpointer.get(addr)), "hello world");
64         }
65 
66         try (var seg = Cpointer.allocate(MemoryAddress.NULL)) {
67             var addr = seg.baseAddress();
68             fillin(addr);
69             assertEquals(Cstring.toJavaString(Cpointer.get(addr)), "hello world");
70         }
71     }
72 }