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 org.testng.annotations.Test;
25 import jdk.incubator.foreign.MemorySegment;
26 import test.jextract.test8245003.*;
27 import static org.testng.Assert.assertEquals;
28 import static org.testng.Assert.assertFalse;
29 import static test.jextract.test8245003.test8245003_h.*;
30 import static jdk.incubator.foreign.CLinker.*;
31 
32 /*
33  * @test id=classes
34  * @bug 8245003
35  * @summary jextract does not generate accessor for MemorySegement typed values
36  * @library ..
37  * @modules jdk.incubator.jextract
38  * @run driver JtregJextract -l Test8245003 -t test.jextract.test8245003 -- test8245003.h
39  * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED Test8245003
40  */
41 /*
42  * @test id=sources
43  * @bug 8245003
44  * @summary jextract does not generate accessor for MemorySegement typed values
45  * @library ..
46  * @modules jdk.incubator.jextract
47  * @run driver JtregJextractSources -l Test8245003 -t test.jextract.test8245003 -- test8245003.h
48  * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED Test8245003
49  */
50 public class Test8245003 {
51     @Test
52     public void testStructAccessor() {
53         var seg = special_pt$SEGMENT();
54         assertEquals(seg.byteSize(), Point.sizeof());
55         assertEquals(Point.x$get(seg), 56);
56         assertEquals(Point.y$get(seg), 75);
57 
58         seg = special_pt3d$SEGMENT();
59         assertEquals(seg.byteSize(), Point3D.sizeof());
60         assertEquals(Point3D.z$get(seg), 35);
61         var pointSeg = Point3D.p$slice(seg);
62         assertEquals(pointSeg.byteSize(), Point.sizeof());
63         assertEquals(Point.x$get(pointSeg), 43);
64         assertEquals(Point.y$get(pointSeg), 45);
65     }
66 
67     @Test
68     public void testArrayAccessor() {
69         var seg = iarr$SEGMENT();
70         assertEquals(seg.byteSize(), C_INT.byteSize()*5);
71         int[] arr = seg.toArray(C_INT);
72         assertEquals(arr.length, 5);
73         assertEquals(arr[0], 2);
74         assertEquals(arr[1], -2);
75         assertEquals(arr[2], 42);
76         assertEquals(arr[3], -42);
77         assertEquals(arr[4], 345);
78 
79         seg = foo$SEGMENT();
80         assertEquals(seg.byteSize(), Foo.sizeof());
81         assertEquals(Foo.count$get(seg), 37);
82         var greeting = Foo.greeting$slice(seg);
83         byte[] barr = greeting.toArray(C_CHAR);
84         assertEquals(new String(barr), "hello");
85     }
86 }