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.CLinker;
 25 import jdk.incubator.foreign.GroupLayout;
 26 import jdk.incubator.foreign.MemoryLayout;
 27 import jdk.incubator.foreign.MemoryLayout.PathElement;
 28 import jdk.incubator.foreign.ResourceScope;
 29 import org.testng.annotations.Test;
 30 
 31 import static org.testng.Assert.assertEquals;
 32 import static test.jextract.struct.struct_h.*;
 33 import test.jextract.struct.*;
 34 
 35 /*
 36  * @test id=classes
 37  * @library ..
 38  * @modules jdk.incubator.jextract
 39  * @run driver JtregJextract -l Struct -t test.jextract.struct -- struct.h
 40  * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED LibStructTest
 41  */
 42 
 43 /*
 44  * @test id=sources
 45  * @library ..
 46  * @modules jdk.incubator.jextract
 47  *
 48  * @run driver JtregJextractSources -l Struct -t test.jextract.struct -- struct.h
 49  * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED LibStructTest
 50  */
 51 
 52 public class LibStructTest {
 53     @Test
 54     public void testMakePoint() {
 55         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 56             var seg = makePoint(scope, 42, -39);
 57             assertEquals(Point.x$get(seg), 42);
 58             assertEquals(Point.y$get(seg), -39);
 59         }
 60     }
 61 
 62     @Test
 63     public void testAllocate() {
 64         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 65             var seg = Point.allocate(scope);
 66             Point.x$set(seg, 56);
 67             Point.y$set(seg, 65);
 68             assertEquals(Point.x$get(seg), 56);
 69             assertEquals(Point.y$get(seg), 65);
 70         }
 71     }
 72 
 73     @Test
 74     public void testAllocateArray() {
 75         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 76             var seg = Point.allocateArray(3, scope);
 77             for (int i = 0; i < 3; i++) {
 78                 Point.x$set(seg, i, 56 + i);
 79                 Point.y$set(seg, i, 65 + i);
 80             }
 81             for (int i = 0; i < 3; i++) {
 82                 assertEquals(Point.x$get(seg, i), 56 + i);
 83                 assertEquals(Point.y$get(seg, i), 65 + i);
 84             }
 85         }
 86     }
 87 
 88     private static void checkField(GroupLayout group, String fieldName, MemoryLayout expected) {
 89         assertEquals(group.select(PathElement.groupElement(fieldName)), expected.withName(fieldName));
 90     }
 91 
 92     @Test
 93     public void testFieldTypes() {
 94         GroupLayout g = (GroupLayout)AllTypes.$LAYOUT();
 95         checkField(g, "sc", C_CHAR);
 96         checkField(g, "uc", C_CHAR);
 97         checkField(g, "s",  C_SHORT);
 98         checkField(g, "us", C_SHORT);
 99         checkField(g, "i",  C_INT);
100         checkField(g, "ui", C_INT);
101         checkField(g, "l",  C_LONG);
102         checkField(g, "ul", C_LONG);
103         checkField(g, "ll", C_LONG_LONG);
104         checkField(g, "ull",C_LONG_LONG);
105         checkField(g, "f",  C_FLOAT);
106         checkField(g, "d",  C_DOUBLE);
107     }
108 }