mcimadamore@1415: /* mcimadamore@1415: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. mcimadamore@1415: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@1415: * mcimadamore@1415: * This code is free software; you can redistribute it and/or modify it mcimadamore@1415: * under the terms of the GNU General Public License version 2 only, as mcimadamore@1415: * published by the Free Software Foundation. mcimadamore@1415: * mcimadamore@1415: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@1415: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@1415: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@1415: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@1415: * accompanied this code). mcimadamore@1415: * mcimadamore@1415: * You should have received a copy of the GNU General Public License version mcimadamore@1415: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@1415: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@1415: * mcimadamore@1415: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mcimadamore@1415: * or visit www.oracle.com if you need additional information or have any mcimadamore@1415: * questions. mcimadamore@1415: */ mcimadamore@1415: mcimadamore@1415: /* mcimadamore@1415: * @test mcimadamore@1415: * @bug 8003280 mcimadamore@1415: * @summary Add lambda tests mcimadamore@1415: * basic test for lambda conversion mcimadamore@1415: * @author Brian Goetz mcimadamore@1415: * @author Maurizio Cimadamore mcimadamore@1415: * @run main LambdaConv01 mcimadamore@1415: */ mcimadamore@1415: mcimadamore@1415: public class LambdaConv01 { mcimadamore@1415: mcimadamore@1415: static int assertionCount = 0; mcimadamore@1415: mcimadamore@1415: static void assertTrue(boolean cond) { mcimadamore@1415: assertionCount++; mcimadamore@1415: if (!cond) mcimadamore@1415: throw new AssertionError(); mcimadamore@1415: } mcimadamore@1415: mcimadamore@1415: interface IntToInt { mcimadamore@1415: public int foo(int x); mcimadamore@1415: } mcimadamore@1415: mcimadamore@1415: interface IntToVoid { mcimadamore@1415: public void foo(int x); mcimadamore@1415: } mcimadamore@1415: mcimadamore@1415: interface VoidToInt { mcimadamore@1415: public int foo(); mcimadamore@1415: } mcimadamore@1415: mcimadamore@1415: interface TU { mcimadamore@1415: public T foo(U u); mcimadamore@1415: } mcimadamore@1415: mcimadamore@1415: public static T exec(TU lambda, U x) { mcimadamore@1415: return lambda.foo(x); mcimadamore@1415: } mcimadamore@1415: mcimadamore@1415: static { mcimadamore@1415: //Assignment conversion: mcimadamore@1415: VoidToInt f1 = ()-> 3; mcimadamore@1415: assertTrue(3 == f1.foo()); mcimadamore@1415: //Covariant returns: mcimadamore@1415: TU f2 = (Integer x) -> x; mcimadamore@1415: assertTrue(3 == f2.foo(3)); mcimadamore@1415: //Method resolution with boxing: mcimadamore@1415: int res = LambdaConv01.exec((Integer x) -> x, 3); mcimadamore@1415: assertTrue(3 == res); mcimadamore@1415: //Runtime exception transparency: mcimadamore@1415: try { mcimadamore@1415: LambdaConv01.exec((Object x) -> x.hashCode(), null); mcimadamore@1415: } mcimadamore@1415: catch (RuntimeException e) { mcimadamore@1415: assertTrue(true); mcimadamore@1415: } mcimadamore@1415: } mcimadamore@1415: mcimadamore@1415: { mcimadamore@1415: //Assignment conversion: mcimadamore@1415: VoidToInt f1 = ()-> 3; mcimadamore@1415: assertTrue(3 == f1.foo()); mcimadamore@1415: //Covariant returns: mcimadamore@1415: TU f2 = (Integer x) -> x; mcimadamore@1415: assertTrue(3 == f2.foo(3)); mcimadamore@1415: //Method resolution with boxing: mcimadamore@1415: int res = LambdaConv01.exec((Integer x) -> x, 3); mcimadamore@1415: assertTrue(3 == res); mcimadamore@1415: //Runtime exception transparency: mcimadamore@1415: try { mcimadamore@1415: LambdaConv01.exec((Object x) -> x.hashCode(), null); mcimadamore@1415: } mcimadamore@1415: catch (RuntimeException e) { mcimadamore@1415: assertTrue(true); mcimadamore@1415: } mcimadamore@1415: } mcimadamore@1415: mcimadamore@1415: public static void test1() { mcimadamore@1415: //Assignment conversion: mcimadamore@1415: VoidToInt f1 = ()-> 3; mcimadamore@1415: assertTrue(3 == f1.foo()); mcimadamore@1415: //Covariant returns: mcimadamore@1415: TU f2 = (Integer x) -> x; mcimadamore@1415: assertTrue(3 == f2.foo(3)); mcimadamore@1415: //Method resolution with boxing: mcimadamore@1415: int res = LambdaConv01.exec((Integer x) -> x, 3); mcimadamore@1415: assertTrue(3 == res); mcimadamore@1415: //Runtime exception transparency: mcimadamore@1415: try { mcimadamore@1415: LambdaConv01.exec((Object x) -> x.hashCode(), null); mcimadamore@1415: } mcimadamore@1415: catch (RuntimeException e) { mcimadamore@1415: assertTrue(true); mcimadamore@1415: } mcimadamore@1415: } mcimadamore@1415: mcimadamore@1415: public void test2() { mcimadamore@1415: //Assignment conversion: mcimadamore@1415: VoidToInt f1 = ()-> 3; mcimadamore@1415: assertTrue(3 == f1.foo()); mcimadamore@1415: //Covariant returns: mcimadamore@1415: TU f2 = (Integer x) -> x; mcimadamore@1415: assertTrue(3 == f2.foo(3)); mcimadamore@1415: //Method resolution with boxing: mcimadamore@1415: int res = LambdaConv01.exec((Integer x) -> x, 3); mcimadamore@1415: assertTrue(3 == res); mcimadamore@1415: //Runtime exception transparency: mcimadamore@1415: try { mcimadamore@1415: LambdaConv01.exec((Object x) -> x.hashCode(), null); mcimadamore@1415: } mcimadamore@1415: catch (RuntimeException e) { mcimadamore@1415: assertTrue(true); mcimadamore@1415: } mcimadamore@1415: } mcimadamore@1415: mcimadamore@1415: public static void main(String[] args) { mcimadamore@1415: test1(); mcimadamore@1415: new LambdaConv01().test2(); mcimadamore@1415: assertTrue(assertionCount == 16); mcimadamore@1415: } mcimadamore@1415: }