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