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