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 quotable lambdas.
 27  * @modules jdk.incubator.code
 28  * @run testng TestCaptureQuotable
 29  */
 30 
 31 import org.testng.annotations.*;
 32 
 33 import jdk.incubator.code.op.CoreOp.Var;
 34 import jdk.incubator.code.Op;
 35 import jdk.incubator.code.Quotable;
 36 import jdk.incubator.code.Quoted;
 37 import jdk.incubator.code.interpreter.Interpreter;
 38 import java.lang.invoke.MethodHandles;
 39 import java.util.ArrayList;
 40 import java.util.Iterator;
 41 import java.util.List;
 42 import java.util.function.IntSupplier;
 43 import java.util.function.IntUnaryOperator;
 44 import java.util.function.ToIntFunction;
 45 import java.util.stream.IntStream;
 46 
 47 import static org.testng.Assert.*;
 48 
 49 public class TestCaptureQuotable {
 50 
 51     @Test(dataProvider = "ints")
 52     public void testCaptureIntParam(int x) {
 53         Quotable quotable = (Quotable & IntUnaryOperator)y -> x + y;
 54         Quoted quoted = quotable.quoted();
 55         assertEquals(quoted.capturedValues().size(), 1);
 56         assertEquals(((Var)quoted.capturedValues().values().iterator().next()).value(), x);
 57         List<Object> arguments = new ArrayList<>();
 58         arguments.add(1);
 59         arguments.addAll(quoted.capturedValues().values());
 60         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
 61                 arguments);
 62         assertEquals(res, x + 1);
 63     }
 64 
 65     @Test
 66     public void testCaptureThisRefAndIntConstant() {
 67         final int x = 100;
 68         String hello = "hello";
 69         Quotable quotable = (Quotable & ToIntFunction<Number>)y -> y.intValue() + hashCode() + hello.length() + x;
 70         Quoted quoted = quotable.quoted();
 71         assertEquals(quoted.capturedValues().size(), 3);
 72         Iterator<Object> it = quoted.capturedValues().values().iterator();
 73         assertEquals(it.next(), this);
 74         assertEquals(((Var)it.next()).value(), hello);
 75         assertEquals(((Var)it.next()).value(), x);
 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 + hashCode() + hello.length());
 82     }
 83 
 84     @Test
 85     public void testCaptureMany() {
 86         int[] ia = new int[8];
 87         int i1 = ia[0] = 0;
 88         int i2 = ia[1] = 1;
 89         int i3 = ia[2] = 2;
 90         int i4 = ia[3] = 3;
 91         int i5 = ia[4] = 4;
 92         int i6 = ia[5] = 5;
 93         int i7 = ia[6] = 6;
 94         int i8 = ia[7] = 7;
 95 
 96         Quotable quotable = (Quotable & IntSupplier) () -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8;
 97         Quoted quoted = quotable.quoted();
 98         assertEquals(quoted.capturedValues().size(), ia.length);
 99         assertEquals(quoted.op().capturedValues(), new ArrayList<>(quoted.capturedValues().keySet()));
100         Iterator<Object> it = quoted.capturedValues().values().iterator();
101         int i = 0;
102         while (it.hasNext()) {
103             int actual = (int) ((Var)it.next()).value();
104             assertEquals(actual, ia[i++]);
105         }
106     }
107 
108     @Test(dataProvider = "ints")
109     public void testCaptureIntField(int x) {
110         class Context {
111             final int x;
112 
113             Context(int x) {
114                 this.x = x;
115             }
116 
117             Quotable quotable() {
118                 return (Quotable & IntUnaryOperator) y -> x + y;
119             }
120         }
121         Context context = new Context(x);
122         Quotable quotable = context.quotable();
123         Quoted quoted = quotable.quoted();
124         assertEquals(quoted.capturedValues().size(), 1);
125         assertEquals(quoted.capturedValues().values().iterator().next(), context);
126         List<Object> arguments = new ArrayList<>();
127         arguments.add(1);
128         arguments.addAll(quoted.capturedValues().values());
129         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
130                 arguments);
131         assertEquals(res, x + 1);
132     }
133 
134     @DataProvider(name = "ints")
135     public Object[][] ints() {
136         return IntStream.range(0, 50)
137                 .mapToObj(i -> new Object[] { i })
138                 .toArray(Object[][]::new);
139     }
140 
141     @Test(dataProvider = "ints")
142     public void testCaptureReferenceReceiver(int i) {
143         int prevCount = Box.count;
144         Quotable quotable = (Quotable & IntUnaryOperator)new Box(i)::add;
145         Quoted quoted = quotable.quoted();
146         assertEquals(Box.count, prevCount + 1); // no duplicate receiver computation!
147         assertEquals(quoted.capturedValues().size(), 1);
148         assertEquals(((Box)((Var)quoted.capturedValues().values().iterator().next()).value()).i, i);
149         List<Object> arguments = new ArrayList<>();
150         arguments.add(1);
151         arguments.addAll(quoted.capturedValues().values());
152         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
153                 arguments);
154         assertEquals(res, i + 1);
155     }
156 
157     record Box(int i) {
158 
159         static int count = 0;
160 
161         Box {
162            count++; // keep track of side-effects
163         }
164 
165         int add(int i) {
166             return i + this.i;
167         }
168     }
169 }