1/*
2 * Copyright (c) 2011, 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 * @bug 8003280
27 * @summary Add lambda tests
28 *   Test lambda expressions for different method signatures (parameter and return type)
29 * @compile LambdaTest2.java
30 * @run main LambdaTest2
31 */
32
33public class LambdaTest2 {
34
35    private static int count = 0;
36
37    private static void assertTrue(boolean b) {
38        if(!b)
39            throw new AssertionError();
40    }
41
42    public static void main(String[] args) {
43        LambdaTest2 test = new LambdaTest2();
44
45        test.method2((int n) -> { ; });
46        test.method2(n -> { }); // "int" is optional here
47        test.method2((int n) -> { }); // ";" is optional here
48        test.method2((int n) -> { return; }); // ";" is mandatory here
49        test.method2((int n) -> { count += n; });
50        assertTrue(count == 10);
51
52        VoidInt vi = (int i) -> {
53                            switch (i) {
54                                case 0:
55                                    System.out.println("normal");
56                                    break;
57                                default:
58                                    System.out.println("invalid");
59                            }
60                       };
61
62        test.method3(()-> { count++; });
63        test.method3(() -> {});
64        assertTrue(count == 11);
65
66        VoidVoid vv = ()-> { while(true)
67                            if(false)
68                                break;
69                            else
70                                continue;
71                       };
72
73        IntVoid iv1 = () -> 42;
74        IntVoid iv2 = () -> { return 43; };//";" is mandatory here
75        assertTrue(iv1.ivMethod() == 42);
76        assertTrue(iv2.ivMethod() == 43);
77
78        IntInt ii1 = (int n) -> n+1;
79        IntInt ii2 = n -> 42;
80        IntInt ii3 = n -> { return 43; };
81        IntInt ii4 =
82            (int n) -> {
83                if(n > 0)
84                    return n+1;
85                else
86                    return n-1;
87            };
88        assertTrue(ii1.iiMethod(1) == 2);
89        assertTrue(ii2.iiMethod(1) == 42);
90        assertTrue(ii3.iiMethod(1) == 43);
91        assertTrue(ii4.iiMethod(-1) == -2);
92    }
93
94    void method2(VoidInt a) {
95        a.viMethod(10);
96    }
97
98    void method3(VoidVoid a) {
99        a.vvMethod();
100    }
101
102    //SAM type interfaces
103    interface VoidInt {
104        void viMethod(int n);
105    }
106
107    interface VoidVoid {
108        void vvMethod();
109    }
110
111    interface IntVoid {
112        int ivMethod();
113    }
114
115    interface IntInt {
116        int iiMethod(int n);
117    }
118}
119