1 import jdk.incubator.code.Op;
2 import jdk.incubator.code.Quotable;
3 import jdk.incubator.code.Quoted;
4 import org.junit.jupiter.api.Assertions;
5 import org.junit.jupiter.api.Test;
6
7 import java.util.List;
8 import java.util.function.IntUnaryOperator;
9 import java.util.stream.Stream;
10
11 /*
12 * @test
13 * @modules jdk.incubator.code
14 * @run junit LambdaModelUniquenessTest
15 */
16 public class LambdaModelUniquenessTest {
17
18 Quotable f() {
19 return (Runnable & Quotable) () -> {
20 System.out.println("Running...");
21 };
22 }
23
24 @Test
25 public void testWithLambdaThatDoNotCapture() {
26 Quotable q1 = f();
27 Quotable q2 = f();
28 Quoted quoted1 = Op.ofQuotable(q1).orElseThrow();
29 Quoted quoted2 = Op.ofQuotable(q2).orElseThrow();
30 Assertions.assertSame(quoted1.op(), quoted2.op());
31 }
32
33 @Test
34 public void testWithLambdaThatDoNotCapture2() {
35 Quotable q1 = f();
36 Quotable q2 = f();
37 List<Op> ops = Stream.of(q1, q2).parallel().map(q -> Op.ofQuotable(q).orElseThrow().op()).toList();
38 Assertions.assertSame(ops.getFirst(), ops.getLast());
39 }
40
41 static Quotable g(int i) {
42 return (IntUnaryOperator & Quotable) j -> j + i;
43 }
44
45 @Test
46 public void testWithLambdaThatCapture() {
47 Quotable q1 = g(1);
48 Quotable q2 = g(2);
49 Quoted quoted1 = Op.ofQuotable(q1).orElseThrow();
50 Quoted quoted2 = Op.ofQuotable(q2).orElseThrow();
51 Assertions.assertSame(quoted1.op(), quoted2.op());
52 }
53
54 @Test
55 public void testWithLambdaThatCapture2() {
56 Quotable q1 = g(1);
57 Quotable q2 = g(2);
58 List<Op> ops = Stream.of(q1, q2).parallel().map(q -> Op.ofQuotable(q).orElseThrow().op()).toList();
59 Assertions.assertSame(ops.getFirst(), ops.getLast());
60 }
61 }