1 import jdk.incubator.code.Op; 2 import jdk.incubator.code.Quotable; 3 import jdk.incubator.code.Quoted; 4 import org.testng.Assert; 5 import org.testng.annotations.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 testng 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 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 Assert.assertSame(quoted1.op(), quoted2.op()); 31 } 32 33 @Test 34 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 Assert.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 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 Assert.assertSame(quoted1.op(), quoted2.op()); 52 } 53 54 @Test 55 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 Assert.assertSame(ops.getFirst(), ops.getLast()); 60 } 61 }