1 /*
  2  * Copyright (c) 2025, 2026, 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.code.Reflect;
 25 import jdk.incubator.code.Op;
 26 import jdk.incubator.code.Quoted;
 27 import jdk.incubator.code.dialect.java.JavaOp;
 28 import org.junit.jupiter.api.Assertions;
 29 import org.junit.jupiter.api.Test;
 30 
 31 import java.util.List;
 32 import java.util.function.IntUnaryOperator;
 33 import java.util.stream.IntStream;
 34 import java.util.stream.Stream;
 35 
 36 /*
 37  * @test
 38  * @modules jdk.incubator.code
 39  * @run junit TestOpOfLambda
 40  */
 41 public class TestOpOfLambda {
 42 
 43     Runnable f() {
 44         return (@Reflect Runnable) () -> {
 45             System.out.println("Running...");
 46         };
 47     }
 48 
 49     @Test
 50     public void testWithLambdaThatDoNotCaptureInSequence() {
 51         Runnable q1 = f();
 52         Runnable q2 = f();
 53         Quoted<?> quoted1 = Op.ofLambda(q1).orElseThrow();
 54         Quoted<?> quoted2 = Op.ofLambda(q2).orElseThrow();
 55         Assertions.assertSame(quoted1.op(), quoted2.op());
 56     }
 57 
 58     @Test
 59     public void testWithLambdaThatDoNotCaptureInParallel() { // parallel
 60         Runnable q1 = f();
 61         Runnable q2 = f();
 62         List<JavaOp.LambdaOp> ops = Stream.of(q1, q2).parallel().map(q -> Op.ofLambda(q).orElseThrow().op()).toList();
 63         Assertions.assertSame(ops.getFirst(), ops.getLast());
 64     }
 65 
 66     static IntUnaryOperator g(int i) {
 67         return (@Reflect IntUnaryOperator) j -> j + i;
 68     }
 69 
 70     @Test
 71     public void testWithLambdaThatCaptureInSequence() {
 72         IntUnaryOperator q1 = g(1);
 73         IntUnaryOperator q2 = g(2);
 74         Quoted<?> quoted1 = Op.ofLambda(q1).orElseThrow();
 75         Quoted<?> quoted2 = Op.ofLambda(q2).orElseThrow();
 76         Assertions.assertSame(quoted1.op(), quoted2.op());
 77     }
 78 
 79     @Test
 80     public void testWithLambdaThatCaptureInParallel() {
 81         IntUnaryOperator q1 = g(1);
 82         IntUnaryOperator q2 = g(2);
 83         List<JavaOp.LambdaOp> ops = Stream.of(q1, q2).parallel().map(q -> Op.ofLambda(q).orElseThrow().op()).toList();
 84         Assertions.assertSame(ops.getFirst(), ops.getLast());
 85     }
 86 
 87     @Test
 88     public void testQuotedIsSameInSequence() {
 89         int j = 8;
 90         IntUnaryOperator q = (@Reflect IntUnaryOperator) i -> i * 2 + j;
 91         Quoted<?> q1 = Op.ofLambda(q).get();
 92         Quoted<?> q2 = Op.ofLambda(q).get();
 93         Assertions.assertSame(q1, q2);
 94     }
 95 
 96     @Test
 97     public void testQuotedIsSameInParallel() {
 98         int j = 8;
 99         IntUnaryOperator q = (@Reflect IntUnaryOperator) i -> i * 2 + j;
100         List<Quoted<JavaOp.LambdaOp>> quotedObjects = IntStream.range(1, 3).parallel().mapToObj(_ -> Op.ofLambda(q).get()).toList();
101         Assertions.assertSame(quotedObjects.getFirst(), quotedObjects.getLast());
102     }
103 }