1 /*
 2  * Copyright (c) 2022, 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 Test lambdas with parameter types or return type of value class
27  * @enablePreview
28  * @run junit/othervm LambdaTest
29  */
30 
31 import java.util.function.Function;
32 import java.util.function.IntFunction;
33 
34 import jdk.internal.vm.annotation.ImplicitlyConstructible;
35 import jdk.internal.vm.annotation.NullRestricted;
36 import org.junit.jupiter.api.Test;
37 import static org.junit.jupiter.api.Assertions.*;
38 
39 public class LambdaTest {
40     @ImplicitlyConstructible
41     static value class V {
42         int x;
43         V(int x) {
44             this.x = x;
45         }
46 
47         static V get(int x) {
48             return new V(x);
49         }
50     }
51 
52     @ImplicitlyConstructible
53     static value class Value {
54         @NullRestricted
55         V v;
56         Value(V v) {
57             this.v = v;
58         }
59         static Value get(int x) {
60             return new Value(new V(x));
61         }
62     }
63 
64     static int getV(V v) {
65         return v.x;
66     }
67 
68     static int getValue(Value v) {
69         return v.v.x;
70     }
71 
72     @Test
73     public void testValueParameterType() {
74         Function<Value, Integer> func1 = LambdaTest::getValue;
75         assertTrue(func1.apply(new Value(new V(100))) == 100);
76 
77         Function<V, Integer> func2 = LambdaTest::getV;
78         assertTrue(func2.apply(new V(200)) == 200);
79     }
80 
81     @Test
82     public void testValueReturnType() {
83         IntFunction<Value> func1 = Value::get;
84         assertEquals(func1.apply(10), new Value(new V(10)));
85 
86         IntFunction<V> func2 = V::get;
87         assertEquals(func2.apply(20), new V(20));
88     }
89 }