1 /*
  2  * Copyright (c) 2024, 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 /*
 25  * @test
 26  * @summary Smoke test for captured values in quoted lambdas.
 27  * @modules jdk.incubator.code
 28  * @run testng TestCaptureQuoted
 29  */
 30 
 31 import jdk.incubator.code.Quotable;
 32 import jdk.incubator.code.op.CoreOp.Var;
 33 import jdk.incubator.code.Op;
 34 import jdk.incubator.code.Quoted;
 35 import jdk.incubator.code.interpreter.Interpreter;
 36 import java.lang.invoke.MethodHandles;
 37 import java.util.ArrayList;
 38 import java.util.Iterator;
 39 import java.util.List;
 40 import java.util.function.ToIntFunction;
 41 import java.util.stream.IntStream;
 42 
 43 import org.testng.annotations.*;
 44 import static org.testng.Assert.*;
 45 
 46 public class TestCaptureQuoted {
 47 
 48     @Test(dataProvider = "ints")
 49     public void testCaptureIntParam(int x) {
 50         Quoted quoted = (int y) -> x + y;
 51         assertEquals(quoted.capturedValues().size(), 1);
 52         assertEquals(((Var)quoted.capturedValues().values().iterator().next()).value(), x);
 53         List<Object> arguments = new ArrayList<>();
 54         arguments.add(1);
 55         arguments.addAll(quoted.capturedValues().values());
 56         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
 57                 arguments);
 58         assertEquals(res, x + 1);
 59     }
 60 
 61     @Test(dataProvider = "ints")
 62     public void testCaptureIntField(int x) {
 63         class Context {
 64             final int x;
 65 
 66             Context(int x) {
 67                 this.x = x;
 68             }
 69 
 70             Quoted quoted() {
 71                 return (int y) -> x + y;
 72             }
 73         }
 74         Context context = new Context(x);
 75         Quoted quoted = context.quoted();
 76         assertEquals(quoted.capturedValues().size(), 1);
 77         assertEquals(quoted.capturedValues().values().iterator().next(), context);
 78         List<Object> arguments = new ArrayList<>();
 79         arguments.add(1);
 80         arguments.addAll(quoted.capturedValues().values());
 81         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
 82                 arguments);
 83         assertEquals(res, x + 1);
 84     }
 85 
 86     @Test
 87     public void testCaptureThisRefAndIntConstant() {
 88         final int x = 100;
 89         String hello = "hello";
 90         Quoted quoted = (Integer y) -> y.intValue() + hashCode() + hello.length() + x;
 91         assertEquals(quoted.capturedValues().size(), 3);
 92         Iterator<Object> it = quoted.capturedValues().values().iterator();
 93         assertEquals(it.next(), this);
 94         assertEquals(((Var)it.next()).value(), hello);
 95         assertEquals(((Var)it.next()).value(), x);
 96         List<Object> arguments = new ArrayList<>();
 97         arguments.add(1);
 98         arguments.addAll(quoted.capturedValues().values());
 99         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
100                 arguments);
101         assertEquals(res, x + 1 + hashCode() + hello.length());
102     }
103 
104     @DataProvider(name = "ints")
105     public Object[][] ints() {
106         return IntStream.range(0, 50)
107                 .mapToObj(i -> new Object[] { i })
108                 .toArray(Object[][]::new);
109     }
110 }