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.NativeAllocationScope;
27 import org.testng.annotations.Test;
28 import test.jextract.test8241925.*;
29 import static org.testng.Assert.assertEquals;
30 import static test.jextract.test8241925.test8241925_h.*;
31 
32 /*
33  * @test
34  * @library ..
35  * @modules jdk.incubator.jextract
36  * @bug 8241925
37  * @summary jextract should generate simple allocation, access API for C primitive types
38  * @run driver JtregJextract -l Test8241925 -t test.jextract.test8241925 -- test8241925.h
39  * @run testng/othervm -Dforeign.restricted=permit LibTest8241925Test
40  */
41 public class LibTest8241925Test {
42     @Test
43     public void test() {
44         try (var scope = NativeAllocationScope.unboundedScope()) {
45             var addr = Cint.allocate(12, scope);
46             assertEquals(Cint.get(addr), 12);
47             square(addr);
48             assertEquals(Cint.get(addr), 144);
49 
50             addr = Cdouble.allocate(12.0, scope);
51             assertEquals(Cdouble.get(addr), 12.0, 0.1);
52             square_fp(addr);
53             assertEquals(Cdouble.get(addr), 144.0, 0.1);
54 
55             int[] intArray = { 34, 67, 78, 8 };
56             addr = Cint.allocateArray(intArray, scope);
57             int sum = sum(addr, intArray.length);
58             assertEquals(sum, IntStream.of(intArray).sum());
59             int[] convertedArray = Cint.toJavaArray(addr.segment());
60             assertEquals(convertedArray, intArray);
61 
62             double[] dblArray = { 34.5, 67.56, 78.2, 8.45 };
63             addr = Cdouble.allocateArray(dblArray, scope);
64             double sumd = sum_fp(addr, dblArray.length);
65             assertEquals(sumd, DoubleStream.of(dblArray).sum(), 0.1);
66             double[] convertedDblArray = Cdouble.toJavaArray(addr.segment());
67             for (int i = 0; i < dblArray.length; i++) {
68                 assertEquals(dblArray[i], convertedDblArray[i], 0.1);
69             }
70 
71             assertEquals(Cstring.toJavaString(name()), "java");
72 
73             var dest = Cchar.allocateArray(12, scope);
74             Cstring.copy(dest, "hello ");
75             var src = Cstring.toCString("world", scope);
76             assertEquals(Cstring.toJavaString(concatenate(dest, src)), "hello world");
77         }
78     }
79 }