1 /*
 2  * Copyright (c) 2022, 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  * @run testng/othervm LambdaTest
28  */
29 
30 import java.util.function.Function;
31 import java.util.function.IntFunction;
32 
33 import org.testng.annotations.DataProvider;
34 import org.testng.annotations.Test;
35 import static org.testng.Assert.*;
36 
37 public class LambdaTest {
38     static value class V {
39         int v;
40         V(int v) {
41             this.v = v;
42         }
43 
44         static V get(int v) {
45             return new V(v);
46         }
47     }
48 
49     static primitive class P {
50         int p;
51         P(int p) {
52             this.p = p;
53         }
54 
55         static P get(int p) {
56             return new P(p);
57         }
58     }
59 
60     static int getV(V v) {
61         return v.v;
62     }
63 
64     static int getP(P p) {
65         return p.p;
66     }
67 
68     @Test
69     public void testValueParameterType() {
70         Function<P.ref, Integer> func1 = LambdaTest::getP;
71         assertTrue(func1.apply(new P(100)) == 100);
72 
73         Function<V, Integer> func2 = LambdaTest::getV;
74         assertTrue(func2.apply(new V(200)) == 200);
75     }
76 
77     @Test
78     public void testValueReturnType() {
79         IntFunction<P.ref> func1 = P::get;
80         assertEquals(func1.apply(10), new P(10));
81 
82         IntFunction<V> func2 = V::get;
83         assertEquals(func2.apply(20), new V(20));
84     }
85 }