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