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.dialect.core.CoreOp.Var;
 32 import jdk.incubator.code.Op;
 33 import jdk.incubator.code.Quoted;
 34 import jdk.incubator.code.interpreter.Interpreter;
 35 import java.lang.invoke.MethodHandles;
 36 import java.util.ArrayList;
 37 import java.util.Iterator;
 38 import java.util.List;
 39 import java.util.stream.IntStream;
 40 
 41 import org.testng.annotations.*;
 42 import static org.testng.Assert.*;
 43 
 44 public class TestCaptureQuoted {
 45 
 46     @Test(dataProvider = "ints")
 47     public void testCaptureIntParam(int x) {
 48         Quoted quoted = (int y) -> x + y;
 49         assertEquals(quoted.capturedValues().size(), 1);
 50         assertEquals(((Var)quoted.capturedValues().values().iterator().next()).value(), x);
 51         List<Object> arguments = new ArrayList<>();
 52         arguments.add(1);
 53         arguments.addAll(quoted.capturedValues().values());
 54         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
 55                 arguments);
 56         assertEquals(res, x + 1);
 57     }
 58 
 59     @Test(dataProvider = "ints")
 60     public void testCaptureIntField(int x) {
 61         class Context {
 62             final int x;
 63 
 64             Context(int x) {
 65                 this.x = x;
 66             }
 67 
 68             Quoted quoted() {
 69                 return (int y) -> x + y;
 70             }
 71         }
 72         Context context = new Context(x);
 73         Quoted quoted = context.quoted();
 74         assertEquals(quoted.capturedValues().size(), 1);
 75         assertEquals(quoted.capturedValues().values().iterator().next(), context);
 76         List<Object> arguments = new ArrayList<>();
 77         arguments.add(1);
 78         arguments.addAll(quoted.capturedValues().values());
 79         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
 80                 arguments);
 81         assertEquals(res, x + 1);
 82     }
 83 
 84     @Test
 85     public void testCaptureThisRefAndIntConstant() {
 86         final int x = 100;
 87         String hello = "hello";
 88         Quoted quoted = (Integer y) -> y.intValue() + hashCode() + hello.length() + x;
 89         assertEquals(quoted.capturedValues().size(), 3);
 90         Iterator<Object> it = quoted.capturedValues().values().iterator();
 91         assertEquals(it.next(), this);
 92         assertEquals(((Var)it.next()).value(), hello);
 93         assertEquals(((Var)it.next()).value(), x);
 94         List<Object> arguments = new ArrayList<>();
 95         arguments.add(1);
 96         arguments.addAll(quoted.capturedValues().values());
 97         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
 98                 arguments);
 99         assertEquals(res, x + 1 + hashCode() + hello.length());
100     }
101 
102     @Test
103     public void testCaptureThisInInvocationArg() {
104         Quoted quoted = (Number y) -> y.intValue() + Integer.valueOf(hashCode());
105         assertEquals(quoted.capturedValues().size(), 1);
106         Iterator<Object> it = quoted.capturedValues().values().iterator();
107         assertEquals(it.next(), this);
108         List<Object> arguments = new ArrayList<>();
109         arguments.add(1);
110         arguments.addAll(quoted.capturedValues().values());
111         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
112                 arguments);
113         assertEquals(res, 1 + hashCode());
114     }
115 
116     record R(int i) {}
117 
118     @Test
119     public void testCaptureThisInNewArg() {
120         Quoted quoted = (Number y) -> y.intValue() + new R(hashCode()).i;
121         assertEquals(quoted.capturedValues().size(), 1);
122         Iterator<Object> it = quoted.capturedValues().values().iterator();
123         assertEquals(it.next(), this);
124         List<Object> arguments = new ArrayList<>();
125         arguments.add(1);
126         arguments.addAll(quoted.capturedValues().values());
127         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
128                 arguments);
129         assertEquals(res, 1 + hashCode());
130     }
131 
132 
133     @DataProvider(name = "ints")
134     public Object[][] ints() {
135         return IntStream.range(0, 50)
136                 .mapToObj(i -> new Object[] { i })
137                 .toArray(Object[][]::new);
138     }
139 }