test/tools/javac/lambda/typeInference/InferenceTest2.java

Mon, 21 Jan 2013 20:13:56 +0000

author
mcimadamore
date
Mon, 21 Jan 2013 20:13:56 +0000
changeset 1510
7873d37f5b37
parent 0
959103a6100f
permissions
-rw-r--r--

8005244: Implement overload resolution as per latest spec EDR
Summary: Add support for stuck expressions and provisional applicability
Reviewed-by: jjg

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 import java.util.List;
aoqi@0 25 import java.util.ArrayList;
aoqi@0 26 import java.util.Arrays;
aoqi@0 27 import java.io.Serializable;
aoqi@0 28 import java.io.File;
aoqi@0 29
aoqi@0 30 /**
aoqi@0 31 * @test
aoqi@0 32 * @bug 8003280
aoqi@0 33 * @summary Add lambda tests
aoqi@0 34 * Parameter types inferred from target type in generics without wildcard
aoqi@0 35 * @compile InferenceTest2.java
aoqi@0 36 * @run main InferenceTest2
aoqi@0 37 */
aoqi@0 38
aoqi@0 39 public class InferenceTest2 {
aoqi@0 40
aoqi@0 41 private static void assertTrue(boolean cond) {
aoqi@0 42 if (!cond)
aoqi@0 43 throw new AssertionError();
aoqi@0 44 }
aoqi@0 45
aoqi@0 46 public static void main(String[] args) {
aoqi@0 47
aoqi@0 48 InferenceTest2 test = new InferenceTest2();
aoqi@0 49
aoqi@0 50 //test SAM1<T>
aoqi@0 51 SAM1<String> sam1 = para -> { String result = "";
aoqi@0 52 for(String s : para)
aoqi@0 53 if(s.compareTo(result) > 0)
aoqi@0 54 result = s;
aoqi@0 55 return result; };
aoqi@0 56 List<String> list = Arrays.asList("a", "b", "c");
aoqi@0 57 assertTrue(sam1.m1(list).equals("c"));
aoqi@0 58
aoqi@0 59 test.method1(para -> para.get(0));
aoqi@0 60
aoqi@0 61 //test SAM2<T>
aoqi@0 62 SAM2<String> sam2 = para -> {para = para.substring(0);};
aoqi@0 63 SAM2<Double> sam2_2 = para -> {};
aoqi@0 64 SAM2<File> sam2_3 = para -> { if(para.isDirectory())
aoqi@0 65 System.out.println("directory");
aoqi@0 66 };
aoqi@0 67
aoqi@0 68 //test SAM3<T>
aoqi@0 69 SAM3<String> sam3 = para -> para[0].substring(0, para[0].length()-1);
aoqi@0 70 assertTrue(sam3.m3("hello+").equals("hello"));
aoqi@0 71
aoqi@0 72 SAM3<Integer> sam3_2 = para -> para[0] - para[1];
aoqi@0 73 assertTrue(sam3_2.m3(1, -1) == 2);
aoqi@0 74
aoqi@0 75 SAM3<Double> sam3_3 = para -> para[0] + para[1] + para[2] + para[3];
aoqi@0 76 assertTrue(sam3_3.m3(1.0, 2.0, 3.0, 4.0) == 10.0);
aoqi@0 77
aoqi@0 78 test.method3(para -> para[0] + 1);
aoqi@0 79
aoqi@0 80 //test SAM6<T>
aoqi@0 81 SAM6<String> sam6 = (para1, para2) -> para1.concat(para2);
aoqi@0 82 assertTrue(sam6.m6("hello", "world").equals("helloworld"));
aoqi@0 83
aoqi@0 84 test.method6((para1, para2) -> para1 >= para2? para1 : para2);
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 void method1(SAM1<Integer> sam1) {
aoqi@0 88 List<Integer> list = Arrays.asList(3,2,1);
aoqi@0 89 assertTrue(sam1.m1(list) == 3);
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 void method3(SAM3<Double> sam3) {
aoqi@0 93 assertTrue(sam3.m3(2.5) == 3.5);
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 void method6(SAM6<Long> sam6) {
aoqi@0 97 assertTrue(sam6.m6(5L, -5L) == 5);
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 interface SAM1<T> {
aoqi@0 101 T m1(List<T> x);
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 interface SAM2<T extends Serializable> {
aoqi@0 105 void m2(T x);
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 interface SAM3<T> {
aoqi@0 109 T m3(T... x);
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 interface SAM6<T> {
aoqi@0 113 T m6(T a, T b);
aoqi@0 114 }
aoqi@0 115 }

mercurial