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 jdk.incubator.foreign.GroupLayout;
25 import jdk.incubator.foreign.MemorySegment;
26 import jdk.incubator.foreign.ResourceScope;
27 import org.testng.annotations.Test;
28 import static org.testng.Assert.assertEquals;
29 import static test.jextract.test8254983.test8254983_h.*;
30 import test.jextract.test8254983.*;
31 
32 /*
33  * @test id=classes
34  * @library ..
35  * @modules jdk.incubator.jextract
36  * @bug 8254983
37  * @summary jextract fails to hande layout paths nested structs/union
38  * @run driver JtregJextract -t test.jextract.test8254983 -- test8254983.h
39  * @run testng/othervm -Dforeign.restricted=permit LibTest8254983Test
40  */
41 /*
42  * @test id=sources
43  * @library ..
44  * @modules jdk.incubator.jextract
45  * @bug 8254983
46  * @summary jextract fails to hande layout paths nested structs/union
47  * @run driver JtregJextractSources -t test.jextract.test8254983 -- test8254983.h
48  * @run testng/othervm -Dforeign.restricted=permit LibTest8254983Test
49  */
50 public class LibTest8254983Test {
51     @Test
52     public void testOuterStruct() {
53         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
54             assertEquals(((GroupLayout)Foo._struct.$LAYOUT()).memberLayouts().size(), 1);
55             MemorySegment str = Foo._struct.allocate(scope);
56             Foo._struct.x$set(str, 42);
57             assertEquals(Foo._struct.x$get(str), 42);
58         }
59     }
60 
61     @Test
62     public void testInnerStruct() {
63         assertEquals(((GroupLayout)Foo._union._struct.$LAYOUT()).memberLayouts().size(), 2);
64         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
65             MemorySegment str = Foo._union._struct.allocate(scope);
66             Foo._union._struct.x$set(str, 42);
67             assertEquals(Foo._union._struct.x$get(str), 42);
68         }
69     }
70 }