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 java.util.stream.DoubleStream; 25 import java.util.stream.IntStream; 26 import jdk.incubator.foreign.MemorySegment; 27 import jdk.incubator.foreign.ResourceScope; 28 import jdk.incubator.foreign.SegmentAllocator; 29 import org.testng.annotations.Test; 30 import test.jextract.test8241925.*; 31 import static org.testng.Assert.assertEquals; 32 import static test.jextract.test8241925.test8241925_h.*; 33 import static jdk.incubator.foreign.CLinker.*; 34 35 /* 36 * @test 37 * @library .. 38 * @modules jdk.incubator.jextract 39 * @bug 8241925 40 * @summary jextract should generate simple allocation, access API for C primitive types 41 * @run driver JtregJextract -l Test8241925 -t test.jextract.test8241925 -- test8241925.h 42 * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED LibTest8241925Test 43 */ 44 public class LibTest8241925Test { 45 @Test 46 public void test() { 47 try (var scope = ResourceScope.newConfinedScope()) { 48 var allocator = SegmentAllocator.newNativeArena(scope); 49 var addr = allocator.allocate(C_INT, 12); 50 assertEquals(addr.get(C_INT, 0), 12); 51 square(addr); 52 assertEquals(addr.get(C_INT, 0), 144); 53 54 addr = allocator.allocate(C_DOUBLE, 12.0); 55 assertEquals(addr.get(C_DOUBLE, 0), 12.0, 0.1); 56 square_fp(addr); 57 assertEquals(addr.get(C_DOUBLE, 0), 144.0, 0.1); 58 59 int[] intArray = { 34, 67, 78, 8 }; 60 addr = allocator.allocateArray(C_INT, intArray); 61 int sum = sum(addr, intArray.length); 62 assertEquals(sum, IntStream.of(intArray).sum()); 63 int[] convertedArray = addr.toArray(C_INT); 64 assertEquals(convertedArray, intArray); 65 66 double[] dblArray = { 34.5, 67.56, 78.2, 8.45 }; 67 addr = allocator.allocateArray(C_DOUBLE, dblArray); 68 double sumd = sum_fp(addr, dblArray.length); 69 assertEquals(sumd, DoubleStream.of(dblArray).sum(), 0.1); 70 double[] convertedDblArray = addr.toArray(C_DOUBLE); 71 for (int i = 0; i < dblArray.length; i++) { 72 assertEquals(dblArray[i], convertedDblArray[i], 0.1); 73 } 74 75 assertEquals(name().getUtf8String(0), "java"); 76 77 var dest = allocator.allocateArray(C_CHAR, 12); 78 dest.copyFrom(allocator.allocateUtf8String("hello ")); 79 var src = allocator.allocateUtf8String("world"); 80 assertEquals(concatenate(dest, src).getUtf8String(0), "hello world"); 81 } 82 } 83 }