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.MemorySegment; 26 import jdk.incubator.foreign.ResourceScope; 27 import jdk.incubator.foreign.SegmentAllocator; 28 import org.testng.annotations.Test; 29 import test.jextract.test8246341.*; 30 import static org.testng.Assert.assertEquals; 31 import static org.testng.Assert.assertTrue; 32 import static test.jextract.test8246341.test8246341_h.*; 33 import static jdk.incubator.foreign.CLinker.*; 34 35 /* 36 * @test id=classes 37 * @bug 8246341 38 * @summary jextract should generate Cpointer utilities class 39 * @library .. 40 * @modules jdk.incubator.jextract 41 * @run driver JtregJextract -l Test8246341 -t test.jextract.test8246341 -- test8246341.h 42 * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED LibTest8246341Test 43 */ 44 /* 45 * @test id=sources 46 * @bug 8246341 47 * @summary jextract should generate Cpointer utilities class 48 * @library .. 49 * @modules jdk.incubator.jextract 50 * @run driver JtregJextractSources -l Test8246341 -t test.jextract.test8246341 -- test8246341.h 51 * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED LibTest8246341Test 52 */ 53 public class LibTest8246341Test { 54 @Test 55 public void testPointerArray() { 56 boolean[] callbackCalled = new boolean[1]; 57 try (ResourceScope scope = ResourceScope.newConfinedScope()) { 58 var callback = func$callback.allocate((argc, argv) -> { 59 callbackCalled[0] = true; 60 var addr = MemorySegment.ofAddress(argv, C_POINTER.byteSize() * argc, scope); 61 assertEquals(argc, 4); 62 assertEquals(addr.get(C_POINTER, 0).getUtf8String(0), "java"); 63 assertEquals(addr.get(C_POINTER, C_POINTER.byteSize() * 1).getUtf8String(0), "python"); 64 assertEquals(addr.get(C_POINTER, C_POINTER.byteSize() * 2).getUtf8String(0), "javascript"); 65 assertEquals(addr.get(C_POINTER, C_POINTER.byteSize() * 3).getUtf8String(0), "c++"); 66 }, scope); 67 func(callback); 68 } 69 assertTrue(callbackCalled[0]); 70 } 71 72 @Test 73 public void testPointerAllocate() { 74 try (var scope = ResourceScope.newConfinedScope()) { 75 var allocator = SegmentAllocator.newNativeArena(C_POINTER.byteSize(), scope); 76 var addr = allocator.allocate(C_POINTER); 77 addr.set(C_POINTER, 0, MemoryAddress.NULL); 78 fillin(addr); 79 assertEquals(addr.get(C_POINTER, 0).getUtf8String(0), "hello world"); 80 } 81 } 82 }