1/* 2 * Copyright (c) 2011, 2013, 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 * basic test for lambda conversion 29 * @author Brian Goetz 30 * @author Maurizio Cimadamore 31 * @run main LambdaConv01 32 */ 33 34public class LambdaConv01 { 35 36 static int assertionCount = 0; 37 38 static void assertTrue(boolean cond) { 39 assertionCount++; 40 if (!cond) 41 throw new AssertionError(); 42 } 43 44 interface IntToInt { 45 public int foo(int x); 46 } 47 48 interface IntToVoid { 49 public void foo(int x); 50 } 51 52 interface VoidToInt { 53 public int foo(); 54 } 55 56 interface TU<T, U> { 57 public T foo(U u); 58 } 59 60 public static <T, U> T exec(TU<T, U> lambda, U x) { 61 return lambda.foo(x); 62 } 63 64 static { 65 //Assignment conversion: 66 VoidToInt f1 = ()-> 3; 67 assertTrue(3 == f1.foo()); 68 //Covariant returns: 69 TU<Number, Integer> f2 = (Integer x) -> x; 70 assertTrue(3 == f2.foo(3).intValue()); 71 //Method resolution with boxing: 72 int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3); 73 assertTrue(3 == res); 74 //Runtime exception transparency: 75 try { 76 LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null); 77 } 78 catch (RuntimeException e) { 79 assertTrue(true); 80 } 81 } 82 83 { 84 //Assignment conversion: 85 VoidToInt f1 = ()-> 3; 86 assertTrue(3 == f1.foo()); 87 //Covariant returns: 88 TU<Number, Integer> f2 = (Integer x) -> x; 89 assertTrue(3 == f2.foo(3).intValue()); 90 //Method resolution with boxing: 91 int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3); 92 assertTrue(3 == res); 93 //Runtime exception transparency: 94 try { 95 LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null); 96 } 97 catch (RuntimeException e) { 98 assertTrue(true); 99 } 100 } 101 102 public static void test1() { 103 //Assignment conversion: 104 VoidToInt f1 = ()-> 3; 105 assertTrue(3 == f1.foo()); 106 //Covariant returns: 107 TU<Number, Integer> f2 = (Integer x) -> x; 108 assertTrue(3 == f2.foo(3).intValue()); 109 //Method resolution with boxing: 110 int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3); 111 assertTrue(3 == res); 112 //Runtime exception transparency: 113 try { 114 LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null); 115 } 116 catch (RuntimeException e) { 117 assertTrue(true); 118 } 119 } 120 121 public void test2() { 122 //Assignment conversion: 123 VoidToInt f1 = ()-> 3; 124 assertTrue(3 == f1.foo()); 125 //Covariant returns: 126 TU<Number, Integer> f2 = (Integer x) -> x; 127 assertTrue(3 == f2.foo(3).intValue()); 128 //Method resolution with boxing: 129 int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3); 130 assertTrue(3 == res); 131 //Runtime exception transparency: 132 try { 133 LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null); 134 } 135 catch (RuntimeException e) { 136 assertTrue(true); 137 } 138 } 139 140 public static void main(String[] args) { 141 test1(); 142 new LambdaConv01().test2(); 143 assertTrue(assertionCount == 16); 144 } 145} 146