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