1 /* 2 * Copyright (c) 2018, 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.foreign.Libraries; 25 import java.foreign.Library; 26 import java.foreign.NativeTypes; 27 import java.foreign.Scope; 28 import java.foreign.memory.LayoutType; 29 import java.foreign.memory.Pointer; 30 import java.lang.invoke.MethodHandles; 31 import org.testng.annotations.Test; 32 import test.jextract.struct.struct_h; 33 34 import static org.testng.Assert.assertEquals; 35 import static org.testng.Assert.fail; 36 import static test.jextract.struct.struct_h.*; 37 38 /* 39 * @test 40 * @library .. 41 * @run driver JtregJextract -t test.jextract.struct -- struct.h 42 * @run testng LibStructTest 43 */ 44 public class LibStructTest { 45 static final struct_h libStruct; 46 47 static { 48 Library lib = Libraries.loadLibrary(MethodHandles.lookup(), "Struct"); 49 libStruct = Libraries.bind(struct_h.class, lib); 50 } 51 52 @Test 53 public void testDerefUndefined() { 54 Pointer<UndefinedStruct> ptr = libStruct.allocateUndefinedStruct(); 55 try { 56 UndefinedStruct x = ptr.get(); 57 fail("Should not be able to dereference a Pointer to undefined struct"); 58 } catch (IllegalStateException ex) { 59 // ignore expected 60 } 61 62 try (Scope scope = Scope.globalScope().fork()) { 63 UndefinedStruct x = scope.allocateStruct(UndefinedStruct.class); 64 fail("Should not be able to allocate an undefined struct"); 65 } catch (UnsupportedOperationException ex) { 66 // ignore expected 67 } 68 } 69 70 @Test 71 public void testVoidArguments() { 72 libStruct.voidArguments(); 73 assertEquals(Pointer.toString(libStruct.LastCalledMethod$get()), "voidArguments"); 74 } 75 76 @Test 77 public void testEmptyArguments() { 78 libStruct.emptyArguments(1); 79 assertEquals(Pointer.toString(libStruct.LastCalledMethod$get()), "emptyArguments"); 80 } 81 82 @Test 83 public void testCastAndAccess() { 84 Pointer<UndefinedStruct> ptr = libStruct.allocateUndefinedStruct(); 85 Plain pt = libStruct.fromUndefinedStruct(ptr); 86 assertEquals(pt.x$get(), 0x1234); 87 assertEquals(pt.y$get(), 0x3412); 88 89 try { 90 Plain tmp = ptr.cast(LayoutType.ofStruct(Plain.class)).get(); 91 fail("Should not be able to cast incompatible layout directly"); 92 } catch (ClassCastException ex) { 93 // ignore expected 94 } 95 96 // This is working now as an escape, but should it really? 97 Plain tmp = ptr.cast(NativeTypes.VOID).cast(LayoutType.ofStruct(Plain.class)).get(); 98 assertEquals(tmp.x$get(), 0x1234); 99 assertEquals(tmp.y$get(), 0x3412); 100 } 101 }