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.dialect.core.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     static class Context {
141         final int x;
142 
143         Context(int x) {
144             this.x = x;
145         }
146 
147         Quotable quotable() {
148             return (Quotable & IntUnaryOperator) y -> x + y;
149         }
150     }
151 
152     @Test(dataProvider = "ints")
153     public void testCaptureIntField(int x) {
154         Context context = new Context(x);
155         Quotable quotable = context.quotable();
156         Quoted quoted = Op.ofQuotable(quotable).get();
157         assertEquals(quoted.capturedValues().size(), 1);
158         assertEquals(quoted.capturedValues().values().iterator().next(), context);
159         List<Object> arguments = new ArrayList<>();
160         arguments.add(1);
161         arguments.addAll(quoted.capturedValues().values());
162         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
163                 arguments);
164         assertEquals(res, x + 1);
165     }
166 
167     @DataProvider(name = "ints")
168     public Object[][] ints() {
169         return IntStream.range(0, 50)
170                 .mapToObj(i -> new Object[] { i })
171                 .toArray(Object[][]::new);
172     }
173 
174     @Test(dataProvider = "ints")
175     public void testCaptureReferenceReceiver(int i) {
176         int prevCount = Box.count;
177         Quotable quotable = (Quotable & IntUnaryOperator)new Box(i)::add;
178         Quoted quoted = Op.ofQuotable(quotable).get();
179         assertEquals(Box.count, prevCount + 1); // no duplicate receiver computation!
180         assertEquals(quoted.capturedValues().size(), 1);
181         assertEquals(((Box)((Var)quoted.capturedValues().values().iterator().next()).value()).i, i);
182         List<Object> arguments = new ArrayList<>();
183         arguments.add(1);
184         arguments.addAll(quoted.capturedValues().values());
185         int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(),
186                 arguments);
187         assertEquals(res, i + 1);
188     }
189 
190     record Box(int i) {
191 
192         static int count = 0;
193 
194         Box {
195            count++; // keep track of side-effects
196         }
197 
198         int add(int i) {
199             return i + this.i;
200         }
201     }
202 }