1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/lambda/lambdaExecution/LambdaTranslationTest1.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,234 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +/** 1.30 + * @test 1.31 + * @bug 8003639 1.32 + * @summary convert lambda testng tests to jtreg and add them 1.33 + * @run testng LambdaTranslationTest1 1.34 + */ 1.35 + 1.36 +import org.testng.annotations.Test; 1.37 + 1.38 +import static org.testng.Assert.assertEquals; 1.39 + 1.40 +@Test 1.41 +public class LambdaTranslationTest1 extends LT1Sub { 1.42 + 1.43 + String cntxt = "blah"; 1.44 + 1.45 + private static final ThreadLocal<Object> result = new ThreadLocal<>(); 1.46 + 1.47 + private static void setResult(Object s) { result.set(s); } 1.48 + private static void appendResult(Object s) { result.set(result.get().toString() + s); } 1.49 + 1.50 + private static void assertResult(String expected) { 1.51 + assertEquals(result.get().toString(), expected); 1.52 + } 1.53 + 1.54 + static Integer count(String s) { 1.55 + return s.length(); 1.56 + } 1.57 + 1.58 + static int icount(String s) { 1.59 + return s.length(); 1.60 + } 1.61 + 1.62 + static void eye(Integer i) { 1.63 + setResult(String.format("I:%d", i)); 1.64 + } 1.65 + 1.66 + static void ieye(int i) { 1.67 + setResult(String.format("i:%d", i)); 1.68 + } 1.69 + 1.70 + static void deye(double d) { 1.71 + setResult(String.format("d:%f", d)); 1.72 + } 1.73 + 1.74 + public void testLambdas() { 1.75 + TBlock<Object> b = t -> {setResult("Sink0::" + t);}; 1.76 + b.apply("Howdy"); 1.77 + assertResult("Sink0::Howdy"); 1.78 + 1.79 + TBlock<String> b1 = t -> {setResult("Sink1::" + t);}; 1.80 + b1.apply("Rowdy"); 1.81 + assertResult("Sink1::Rowdy"); 1.82 + 1.83 + for (int i = 5; i < 10; ++i) { 1.84 + TBlock<Integer> b2 = t -> {setResult("Sink2::" + t);}; 1.85 + b2.apply(i); 1.86 + assertResult("Sink2::" + i); 1.87 + } 1.88 + 1.89 + TBlock<Integer> b3 = t -> {setResult("Sink3::" + t);}; 1.90 + for (int i = 900; i > 0; i -= 100) { 1.91 + b3.apply(i); 1.92 + assertResult("Sink3::" + i); 1.93 + } 1.94 + 1.95 + cntxt = "blah"; 1.96 + TBlock<String> b4 = t -> {setResult(String.format("b4: %s .. %s", cntxt, t));}; 1.97 + b4.apply("Yor"); 1.98 + assertResult("b4: blah .. Yor"); 1.99 + 1.100 + String flaw = "flaw"; 1.101 + TBlock<String> b5 = t -> {setResult(String.format("b5: %s .. %s", flaw, t));}; 1.102 + b5.apply("BB"); 1.103 + assertResult("b5: flaw .. BB"); 1.104 + 1.105 + cntxt = "flew"; 1.106 + TBlock<String> b6 = t -> {setResult(String.format("b6: %s .. %s .. %s", t, cntxt, flaw));}; 1.107 + b6.apply("flee"); 1.108 + assertResult("b6: flee .. flew .. flaw"); 1.109 + 1.110 + TBlock<String> b7 = t -> {setResult(String.format("b7: %s %s", t, this.protectedSuperclassMethod()));}; 1.111 + b7.apply("this:"); 1.112 + assertResult("b7: this: instance:flew"); 1.113 + 1.114 + TBlock<String> b8 = t -> {setResult(String.format("b8: %s %s", t, super.protectedSuperclassMethod()));}; 1.115 + b8.apply("super:"); 1.116 + assertResult("b8: super: I'm the sub"); 1.117 + 1.118 + TBlock<String> b7b = t -> {setResult(String.format("b9: %s %s", t, protectedSuperclassMethod()));}; 1.119 + b7b.apply("implicit this:"); 1.120 + assertResult("b9: implicit this: instance:flew"); 1.121 + 1.122 + TBlock<Object> b10 = t -> {setResult(String.format("b10: new LT1Thing: %s", (new LT1Thing(t)).str));}; 1.123 + b10.apply("thing"); 1.124 + assertResult("b10: new LT1Thing: thing"); 1.125 + 1.126 + TBlock<Object> b11 = t -> {setResult(String.format("b11: %s", (new LT1Thing(t) { 1.127 + String get() { 1.128 + return "*" + str.toString() + "*"; 1.129 + } 1.130 + }).get()));}; 1.131 + b11.apply(999); 1.132 + assertResult("b11: *999*"); 1.133 + } 1.134 + 1.135 + public void testMethodRefs() { 1.136 + LT1IA ia = LambdaTranslationTest1::eye; 1.137 + ia.doit(1234); 1.138 + assertResult("I:1234"); 1.139 + 1.140 + LT1IIA iia = LambdaTranslationTest1::ieye; 1.141 + iia.doit(1234); 1.142 + assertResult("i:1234"); 1.143 + 1.144 + LT1IA da = LambdaTranslationTest1::deye; 1.145 + da.doit(1234); 1.146 + assertResult("d:1234.000000"); 1.147 + 1.148 + LT1SA a = LambdaTranslationTest1::count; 1.149 + assertEquals((Integer) 5, a.doit("howdy")); 1.150 + 1.151 + a = LambdaTranslationTest1::icount; 1.152 + assertEquals((Integer) 6, a.doit("shower")); 1.153 + } 1.154 + 1.155 + public void testInner() throws Exception { 1.156 + (new In()).doInner(); 1.157 + } 1.158 + 1.159 + protected String protectedSuperclassMethod() { 1.160 + return "instance:" + cntxt; 1.161 + } 1.162 + 1.163 + private class In { 1.164 + 1.165 + private int that = 1234; 1.166 + 1.167 + void doInner() { 1.168 + TBlock<String> i4 = t -> {setResult(String.format("i4: %d .. %s", that, t));}; 1.169 + i4.apply("=1234"); 1.170 + assertResult("i4: 1234 .. =1234"); 1.171 + 1.172 + TBlock<String> i5 = t -> {setResult(""); appendResult(t); appendResult(t);}; 1.173 + i5.apply("fruit"); 1.174 + assertResult("fruitfruit"); 1.175 + 1.176 + cntxt = "human"; 1.177 + TBlock<String> b4 = t -> {setResult(String.format("b4: %s .. %s", cntxt, t));}; 1.178 + b4.apply("bin"); 1.179 + assertResult("b4: human .. bin"); 1.180 + 1.181 + final String flaw = "flaw"; 1.182 + 1.183 +/** 1.184 + Callable<String> c5 = () -> "["+flaw+"]" ; 1.185 + System.out.printf("c5: %s\n", c5.call() ); 1.186 + **/ 1.187 + 1.188 + TBlock<String> b5 = t -> {setResult(String.format("b5: %s .. %s", flaw, t));}; 1.189 + b5.apply("BB"); 1.190 + assertResult("b5: flaw .. BB"); 1.191 + 1.192 + cntxt = "borg"; 1.193 + TBlock<String> b6 = t -> {setResult(String.format("b6: %s .. %s .. %s", t, cntxt, flaw));}; 1.194 + b6.apply("flee"); 1.195 + assertResult("b6: flee .. borg .. flaw"); 1.196 + 1.197 + TBlock<String> b7b = t -> {setResult(String.format("b7b: %s %s", t, protectedSuperclassMethod()));}; 1.198 + b7b.apply("implicit outer this"); 1.199 + assertResult("b7b: implicit outer this instance:borg"); 1.200 + 1.201 + /** 1.202 + TBlock<Object> b9 = t -> { System.out.printf("New: %s\n", (new LT1Thing(t)).str); }; 1.203 + b9.apply("thing"); 1.204 + 1.205 + TBlock<Object> ba = t -> { System.out.printf("Def: %s\n", (new LT1Thing(t) { String get() { return "*" + str.toString() +"*";}}).get() ); }; 1.206 + ba.apply(999); 1.207 + 1.208 + */ 1.209 + } 1.210 + } 1.211 +} 1.212 + 1.213 +class LT1Sub { 1.214 + protected String protectedSuperclassMethod() { 1.215 + return "I'm the sub"; 1.216 + } 1.217 +} 1.218 + 1.219 +class LT1Thing { 1.220 + final Object str; 1.221 + 1.222 + LT1Thing(Object s) { 1.223 + str = s; 1.224 + } 1.225 +} 1.226 + 1.227 +interface LT1SA { 1.228 + Integer doit(String s); 1.229 +} 1.230 + 1.231 +interface LT1IA { 1.232 + void doit(int i); 1.233 +} 1.234 + 1.235 +interface LT1IIA { 1.236 + void doit(Integer i); 1.237 +}