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 = Op.ofQuotable(quotable).get();
 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 = Op.ofQuotable(quotable).get();
 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 testCaptureThisInInvocationArg() {
 86         Quotable quotable = (Quotable & ToIntFunction<Number>)y -> y.intValue() + Integer.valueOf(hashCode());
 87         Quoted quoted = Op.ofQuotable(quotable).get();
 88         assertEquals(quoted.capturedValues().size(), 1);
 89         Iterator<Object> it = quoted.capturedValues().values().iterator();
 90         assertEquals(it.next(), this);
 91         List<Object> arguments = new ArrayList<>();
 92         arguments.add(1);
 93         arguments.addAll(quoted.capturedValues().values());
 94         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
 95                 arguments);
 96         assertEquals(res, 1 + hashCode());
 97     }
 98 
 99     record R(int i) {}
100 
101     @Test
102     public void testCaptureThisInNewArg() {
103         Quotable quotable = (Quotable & ToIntFunction<Number>)y -> y.intValue() + new R(hashCode()).i;
104         Quoted quoted = Op.ofQuotable(quotable).get();
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     @Test
117     public void testCaptureMany() {
118         int[] ia = new int[8];
119         int i1 = ia[0] = 0;
120         int i2 = ia[1] = 1;
121         int i3 = ia[2] = 2;
122         int i4 = ia[3] = 3;
123         int i5 = ia[4] = 4;
124         int i6 = ia[5] = 5;
125         int i7 = ia[6] = 6;
126         int i8 = ia[7] = 7;
127 
128         Quotable quotable = (Quotable & IntSupplier) () -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8;
129         Quoted quoted = Op.ofQuotable(quotable).get();
130         assertEquals(quoted.capturedValues().size(), ia.length);
131         assertEquals(quoted.op().capturedValues(), new ArrayList<>(quoted.capturedValues().keySet()));
132         Iterator<Object> it = quoted.capturedValues().values().iterator();
133         int i = 0;
134         while (it.hasNext()) {
135             int actual = (int) ((Var)it.next()).value();
136             assertEquals(actual, ia[i++]);
137         }
138     }
139 
140     @Test(dataProvider = "ints")
141     public void testCaptureIntField(int x) {
142         class Context {
143             final int x;
144 
145             Context(int x) {
146                 this.x = x;
147             }
148 
149             Quotable quotable() {
150                 return (Quotable & IntUnaryOperator) y -> x + y;
151             }
152         }
153         Context context = new Context(x);
154         Quotable quotable = context.quotable();
155         Quoted quoted = Op.ofQuotable(quotable).get();
156         assertEquals(quoted.capturedValues().size(), 1);
157         assertEquals(quoted.capturedValues().values().iterator().next(), context);
158         List<Object> arguments = new ArrayList<>();
159         arguments.add(1);
160         arguments.addAll(quoted.capturedValues().values());
161         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
162                 arguments);
163         assertEquals(res, x + 1);
164     }
165 
166     @DataProvider(name = "ints")
167     public Object[][] ints() {
168         return IntStream.range(0, 50)
169                 .mapToObj(i -> new Object[] { i })
170                 .toArray(Object[][]::new);
171     }
172 
173     @Test(dataProvider = "ints")
174     public void testCaptureReferenceReceiver(int i) {
175         int prevCount = Box.count;
176         Quotable quotable = (Quotable & IntUnaryOperator)new Box(i)::add;
177         Quoted quoted = Op.ofQuotable(quotable).get();
178         assertEquals(Box.count, prevCount + 1); // no duplicate receiver computation!
179         assertEquals(quoted.capturedValues().size(), 1);
180         assertEquals(((Box)((Var)quoted.capturedValues().values().iterator().next()).value()).i, i);
181         List<Object> arguments = new ArrayList<>();
182         arguments.add(1);
183         arguments.addAll(quoted.capturedValues().values());
184         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
185                 arguments);
186         assertEquals(res, i + 1);
187     }
188 
189     record Box(int i) {
190 
191         static int count = 0;
192 
193         Box {
194            count++; // keep track of side-effects
195         }
196 
197         int add(int i) {
198             return i + this.i;
199         }
200     }
201 }