1 import jdk.incubator.code.Op;
2 import jdk.incubator.code.Reflect;
3 import jdk.incubator.code.dialect.core.CoreOp;
4 import org.junit.jupiter.api.Assertions;
5 import org.junit.jupiter.api.Test;
6
7 import java.lang.reflect.Method;
8 import java.util.List;
9 import java.util.Optional;
10 import java.util.stream.IntStream;
11
12 /*
13 * @test
14 * @modules jdk.incubator.code
15 * @run junit TestOpOfMethod
16 */
17 public class TestOpOfMethod {
18
19 @Reflect
20 static void f() {
21 }
22 @Reflect
23 static void g() {
24 }
25
26 @Test
27 public void testInstancesReflectSameMethodHaveSameModel() throws NoSuchMethodException {
28 Method f = this.getClass().getDeclaredMethod("f");
29 Method f2 = this.getClass().getDeclaredMethod("f");
30 CoreOp.FuncOp fm = Op.ofMethod(f).orElseThrow();
31 CoreOp.FuncOp fm2 = Op.ofMethod(f2).orElseThrow();
32 Assertions.assertSame(fm, fm2);
33 }
34
35 @Test
36 public void testInstancesReflectDiffMethodsHaveDiffModels() throws NoSuchMethodException {
37 Method f = this.getClass().getDeclaredMethod("f");
38 CoreOp.FuncOp fm = Op.ofMethod(f).orElseThrow();
39
40 Method g = this.getClass().getDeclaredMethod("g");
41 CoreOp.FuncOp gm = Op.ofMethod(g).orElseThrow();
42
43 Assertions.assertNotSame(gm, fm);
44 }
45
46 @Test
47 public void testOpOfMethodIsThreadSafe() throws NoSuchMethodException {
48 Method f = this.getClass().getDeclaredMethod("f");
49 List<Optional<CoreOp.FuncOp>> fops = IntStream.range(1, 3).parallel().mapToObj(_ -> Op.ofMethod(f)).toList();
50 Assertions.assertSame(fops.getFirst(), fops.getLast());
51 }
52 }