1 /*
  2  * Copyright (c) 2021, 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.MemorySegment;
 25 import jdk.incubator.foreign.ResourceScope;
 26 import org.testng.annotations.Test;
 27 
 28 import java.util.concurrent.atomic.AtomicInteger;
 29 
 30 import static org.testng.Assert.*;
 31 
 32 import static test.jextract.funcpointers.func_h.*;
 33 import test.jextract.funcpointers.*;
 34 
 35 /*
 36  * @test id=classes
 37  * @library ..
 38  * @modules jdk.incubator.jextract
 39  * @run driver JtregJextract -l Func -t test.jextract.funcpointers -- func.h
 40  * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED TestFuncPointerInvokers
 41  */
 42 /*
 43  * @test id=sources
 44  * @library ..
 45  * @modules jdk.incubator.jextract
 46  * @run driver JtregJextractSources -l Func -t test.jextract.funcpointers -- func.h
 47  * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED TestFuncPointerInvokers
 48  */
 49 public class TestFuncPointerInvokers {
 50     @Test
 51     public void testStructFieldTypedef() {
 52         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 53             AtomicInteger val = new AtomicInteger(-1);
 54             MemorySegment bar = Bar.allocate(scope);
 55             Bar.foo$set(bar, Foo.allocate((i) -> val.set(i), scope).address());
 56             Bar.foo(bar, scope).apply(42);
 57             assertEquals(val.get(), 42);
 58         }
 59     }
 60 
 61     @Test
 62     public void testStructFieldFITypedef() {
 63         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 64             AtomicInteger val = new AtomicInteger(-1);
 65             MemorySegment bar = Bar.allocate(scope);
 66             Bar.foo$set(bar, Foo.allocate((i) -> val.set(i), scope).address());
 67             Foo.ofAddress(Bar.foo$get(bar), scope).apply(42);
 68             assertEquals(val.get(), 42);
 69         }
 70     }
 71 
 72     @Test
 73     public void testGlobalTypedef() {
 74         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 75             AtomicInteger val = new AtomicInteger(-1);
 76             f$set(Foo.allocate((i) -> val.set(i), scope).address());
 77             f().apply(42);
 78             assertEquals(val.get(), 42);
 79         }
 80     }
 81 
 82     @Test
 83     public void testGlobalFITypedef() {
 84         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 85             AtomicInteger val = new AtomicInteger(-1);
 86             f$set(Foo.allocate((i) -> val.set(i), scope).address());
 87             Foo.ofAddress(f$get(), scope).apply(42);
 88             assertEquals(val.get(), 42);
 89         }
 90     }
 91 
 92     @Test
 93     public void testStructFieldFunctionPointer() {
 94         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 95             AtomicInteger val = new AtomicInteger(-1);
 96             MemorySegment baz = Baz.allocate(scope);
 97             Baz.fp$set(baz, Baz.fp.allocate((i) -> val.set(i), scope).address());
 98             Baz.fp(baz, scope).apply(42);
 99             assertEquals(val.get(), 42);
100         }
101     }
102 
103     @Test
104     public void testStructFieldFIFunctionPointer() {
105         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
106             AtomicInteger val = new AtomicInteger(-1);
107             MemorySegment baz = Baz.allocate(scope);
108             Baz.fp$set(baz, Baz.fp.allocate((i) -> val.set(i), scope).address());
109             Baz.fp.ofAddress(Baz.fp$get(baz), scope).apply(42);
110             assertEquals(val.get(), 42);
111         }
112     }
113 
114     @Test
115     public void testGlobalFunctionPointer() {
116         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
117             AtomicInteger val = new AtomicInteger(-1);
118             fp$set(fp.allocate((i) -> val.set(i), scope).address());
119             fp().apply(42);
120             assertEquals(val.get(), 42);
121         }
122     }
123 
124     @Test
125     public void testGlobalFIFunctionPointer() {
126         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
127             AtomicInteger val = new AtomicInteger(-1);
128             fp$set(fp.allocate((i) -> val.set(i), scope).address());
129             fp.ofAddress(fp$get(), scope).apply(42);
130             assertEquals(val.get(), 42);
131         }
132     }
133 }