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.lang.reflect.Method;
 25 import jdk.incubator.foreign.GroupLayout;
 26 import jdk.incubator.foreign.MemoryLayout;
 27 import jdk.incubator.foreign.MemoryLayout.PathElement;
 28 import jdk.incubator.foreign.MemorySegment;
 29 import jdk.incubator.foreign.ResourceScope;
 30 import org.testng.annotations.Test;
 31 
 32 import test.jextract.unsupported.unsupported_h;
 33 import static org.testng.Assert.assertEquals;
 34 import static org.testng.Assert.assertNull;
 35 import static test.jextract.unsupported.unsupported_h.*;
 36 import test.jextract.unsupported.*;
 37 
 38 /*
 39  * @test id=classes
 40  * @library ..
 41  * @modules jdk.incubator.jextract
 42  * @run driver JtregJextract -l Unsupported -t test.jextract.unsupported -- unsupported.h
 43  * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED LibUnsupportedTest
 44  */
 45 
 46 /*
 47  * @test id=sources
 48  * @library ..
 49  * @modules jdk.incubator.jextract
 50  *
 51  * @run driver JtregJextractSources -l Unsupported -t test.jextract.unsupported -- unsupported.h
 52  * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED LibUnsupportedTest
 53  */
 54 
 55 public class LibUnsupportedTest {
 56     @Test
 57     public void testAllocateFoo() {
 58         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 59             var seg = Foo.allocate(scope);
 60             Foo.i$set(seg, 32);
 61             Foo.c$set(seg, (byte)'z');
 62             assertEquals(Foo.i$get(seg), 32);
 63             assertEquals(Foo.c$get(seg), (byte)'z');
 64         }
 65     }
 66 
 67     @Test
 68     public void testGetFoo() {
 69         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 70             var seg = MemorySegment.ofAddressNative(getFoo(), Foo.sizeof(), scope);
 71             Foo.i$set(seg, 42);
 72             Foo.c$set(seg, (byte)'j');
 73             assertEquals(Foo.i$get(seg), 42);
 74             assertEquals(Foo.c$get(seg), (byte)'j');
 75         }
 76     }
 77 
 78     private static void checkField(GroupLayout group, String fieldName, MemoryLayout expected) {
 79         assertEquals(group.select(PathElement.groupElement(fieldName)), expected.withName(fieldName));
 80     }
 81 
 82     @Test
 83     public void testFieldTypes() {
 84         GroupLayout g = (GroupLayout)Foo.$LAYOUT();
 85         checkField(g, "i", C_INT);
 86         checkField(g, "c", C_CHAR);
 87     }
 88 
 89     @Test
 90     public void testIgnoredMethods() {
 91         assertNull(findMethod(unsupported_h.class, "func"));
 92         assertNull(findMethod(unsupported_h.class, "func2"));
 93         assertNull(findMethod(unsupported_h.class, "func3"));
 94         assertNull(findMethod(unsupported_h.class, "func4"));
 95         assertNull(findMethod(unsupported_h.class, "makeFoo"));
 96         assertNull(findMethod(unsupported_h.class, "copyFoo"));
 97     }
 98 
 99     private Method findMethod(Class<?> cls, String name) {
100         for (Method m : cls.getMethods()) {
101             if (m.getName().equals(name)) {
102                 return m;
103             }
104         }
105         return null;
106     }
107 }