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);
|
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);
|