1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/lambda/lambdaExecution/LambdaTranslationTest2.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,355 @@ 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 LambdaTranslationTest2 1.34 + */ 1.35 + 1.36 +import org.testng.annotations.Test; 1.37 + 1.38 +import java.util.ArrayList; 1.39 +import java.util.List; 1.40 + 1.41 +import static org.testng.Assert.assertEquals; 1.42 +import static org.testng.Assert.assertTrue; 1.43 + 1.44 +/** 1.45 + * LambdaTranslationTest2 -- end-to-end smoke tests for lambda evaluation 1.46 + */ 1.47 + 1.48 +@Test 1.49 +public class LambdaTranslationTest2 { 1.50 + 1.51 + final String dummy = "dummy"; 1.52 + 1.53 + public void testLambdas() { 1.54 + TPredicate<String> isEmpty = s -> s.isEmpty(); 1.55 + assertTrue(isEmpty.test("")); 1.56 + assertTrue(!isEmpty.test("foo")); 1.57 + 1.58 + TPredicate<Object> oIsEmpty = s -> ((String) s).isEmpty(); 1.59 + assertTrue(oIsEmpty.test("")); 1.60 + assertTrue(!oIsEmpty.test("foo")); 1.61 + 1.62 + TPredicate<Object> alwaysTrue = o -> true; 1.63 + assertTrue(alwaysTrue.test("")); 1.64 + assertTrue(alwaysTrue.test(null)); 1.65 + 1.66 + TPredicate<Object> alwaysFalse = o -> false; 1.67 + assertTrue(!alwaysFalse.test("")); 1.68 + assertTrue(!alwaysFalse.test(null)); 1.69 + 1.70 + // tests local capture 1.71 + String foo = "foo"; 1.72 + TPredicate<String> equalsFoo = s -> s.equals(foo); 1.73 + assertTrue(!equalsFoo.test("")); 1.74 + assertTrue(equalsFoo.test("foo")); 1.75 + 1.76 + // tests instance capture 1.77 + TPredicate<String> equalsDummy = s -> s.equals(dummy); 1.78 + assertTrue(!equalsDummy.test("")); 1.79 + assertTrue(equalsDummy.test("dummy")); 1.80 + 1.81 + TMapper<Object, Object> ident = s -> s; 1.82 + 1.83 + assertEquals("blarf", ident.map("blarf")); 1.84 + assertEquals("wooga", ident.map("wooga")); 1.85 + assertTrue("wooga" == ident.map("wooga")); 1.86 + 1.87 + // constant capture 1.88 + TMapper<Object, Object> prefixer = s -> "p" + s; 1.89 + assertEquals("pblarf", prefixer.map("blarf")); 1.90 + assertEquals("pwooga", prefixer.map("wooga")); 1.91 + 1.92 + // instance capture 1.93 + TMapper<Object, Object> prefixer2 = s -> dummy + s; 1.94 + assertEquals("dummyblarf", prefixer2.map("blarf")); 1.95 + assertEquals("dummywooga", prefixer2.map("wooga")); 1.96 + } 1.97 + 1.98 + interface Factory<T> { 1.99 + T make(); 1.100 + } 1.101 + 1.102 + interface StringFactory extends Factory<String> { } 1.103 + 1.104 + interface StringFactory2 extends Factory<String> { 1.105 + String make(); 1.106 + } 1.107 + 1.108 + public void testBridges() { 1.109 + Factory<String> of = () -> "y"; 1.110 + Factory<?> ef = () -> "z"; 1.111 + 1.112 + assertEquals("y", of.make()); 1.113 + assertEquals("y", ((Factory<?>) of).make()); 1.114 + assertEquals("y", ((Factory) of).make()); 1.115 + 1.116 + assertEquals("z", ef.make()); 1.117 + assertEquals("z", ((Factory) ef).make()); 1.118 + } 1.119 + 1.120 + public void testBridgesImplicitSpecialization() { 1.121 + StringFactory sf = () -> "x"; 1.122 + 1.123 + assertEquals("x", sf.make()); 1.124 + assertEquals("x", ((Factory<String>) sf).make()); 1.125 + assertEquals("x", ((Factory<?>) sf).make()); 1.126 + assertEquals("x", ((Factory) sf).make()); 1.127 + } 1.128 + 1.129 + public void testBridgesExplicitSpecialization() { 1.130 + StringFactory2 sf = () -> "x"; 1.131 + 1.132 + assertEquals("x", sf.make()); 1.133 + assertEquals("x", ((Factory<String>) sf).make()); 1.134 + assertEquals("x", ((Factory<?>) sf).make()); 1.135 + assertEquals("x", ((Factory) sf).make()); 1.136 + } 1.137 + 1.138 + public void testSuperCapture() { 1.139 + class A { 1.140 + String make() { return "x"; } 1.141 + } 1.142 + 1.143 + class B extends A { 1.144 + void testSuperCapture() { 1.145 + StringFactory sf = () -> super.make(); 1.146 + assertEquals("x", sf.make()); 1.147 + } 1.148 + } 1.149 + 1.150 + new B().testSuperCapture(); 1.151 + } 1.152 + 1.153 + interface WidenD { 1.154 + public String m(float a0, double a1); 1.155 + } 1.156 + 1.157 + interface WidenS { 1.158 + public String m(byte a0, short a1); 1.159 + } 1.160 + 1.161 + interface WidenI { 1.162 + public String m(byte a0, short a1, char a2, int a3); 1.163 + } 1.164 + 1.165 + interface WidenL { 1.166 + public String m(byte a0, short a1, char a2, int a3, long a4); 1.167 + } 1.168 + 1.169 + interface Box { 1.170 + public String m(byte a0, short a1, char a2, int a3, long a4, boolean a5, float a6, double a7); 1.171 + } 1.172 + 1.173 + static String pb(Byte a0, Short a1, Character a2, Integer a3, Long a4, Boolean a5, Float a6, Double a7) { 1.174 + return String.format("b%d s%d c%c i%d j%d z%b f%f d%f", a0, a1, a2, a3, a4, a5, a6, a7); 1.175 + } 1.176 + 1.177 + static String pwI1(int a0, int a1, int a2, int a3) { 1.178 + return String.format("b%d s%d c%d i%d", a0, a1, a2, a3); 1.179 + } 1.180 + 1.181 + static String pwI2(Integer a0, Integer a1, Integer a2, Integer a3) { 1.182 + return String.format("b%d s%d c%d i%d", a0, a1, a2, a3); 1.183 + } 1.184 + 1.185 + static String pwL1(long a0, long a1, long a2, long a3, long a4) { 1.186 + return String.format("b%d s%d c%d i%d j%d", a0, a1, a2, a3, a4); 1.187 + } 1.188 + 1.189 + static String pwL2(Long a0, Long a1, Long a2, Long a3, Long a4) { 1.190 + return String.format("b%d s%d c%d i%d j%d", a0, a1, a2, a3, a4); 1.191 + } 1.192 + 1.193 + static String pwS1(short a0, short a1) { 1.194 + return String.format("b%d s%d", a0, a1); 1.195 + } 1.196 + 1.197 + static String pwS2(Short a0, Short a1) { 1.198 + return String.format("b%d s%d", a0, a1); 1.199 + } 1.200 + 1.201 + static String pwD1(double a0, double a1) { 1.202 + return String.format("f%f d%f", a0, a1); 1.203 + } 1.204 + 1.205 + static String pwD2(Double a0, Double a1) { 1.206 + return String.format("f%f d%f", a0, a1); 1.207 + } 1.208 + 1.209 + public void testPrimitiveWidening() { 1.210 + WidenS ws1 = LambdaTranslationTest2::pwS1; 1.211 + assertEquals("b1 s2", ws1.m((byte) 1, (short) 2)); 1.212 + 1.213 + WidenD wd1 = LambdaTranslationTest2::pwD1; 1.214 + assertEquals("f1.000000 d2.000000", wd1.m(1.0f, 2.0)); 1.215 + 1.216 + WidenI wi1 = LambdaTranslationTest2::pwI1; 1.217 + assertEquals("b1 s2 c3 i4", wi1.m((byte) 1, (short) 2, (char) 3, 4)); 1.218 + 1.219 + WidenL wl1 = LambdaTranslationTest2::pwL1; 1.220 + assertEquals("b1 s2 c3 i4 j5", wl1.m((byte) 1, (short) 2, (char) 3, 4, 5L)); 1.221 + 1.222 + // @@@ TODO: clarify spec on widen+box conversion 1.223 + } 1.224 + 1.225 + interface Unbox { 1.226 + public String m(Byte a0, Short a1, Character a2, Integer a3, Long a4, Boolean a5, Float a6, Double a7); 1.227 + } 1.228 + 1.229 + static String pu(byte a0, short a1, char a2, int a3, long a4, boolean a5, float a6, double a7) { 1.230 + return String.format("b%d s%d c%c i%d j%d z%b f%f d%f", a0, a1, a2, a3, a4, a5, a6, a7); 1.231 + } 1.232 + 1.233 + public void testUnboxing() { 1.234 + Unbox u = LambdaTranslationTest2::pu; 1.235 + assertEquals("b1 s2 cA i4 j5 ztrue f6.000000 d7.000000", u.m((byte)1, (short) 2, 'A', 4, 5L, true, 6.0f, 7.0)); 1.236 + } 1.237 + 1.238 + public void testBoxing() { 1.239 + Box b = LambdaTranslationTest2::pb; 1.240 + assertEquals("b1 s2 cA i4 j5 ztrue f6.000000 d7.000000", b.m((byte) 1, (short) 2, 'A', 4, 5L, true, 6.0f, 7.0)); 1.241 + } 1.242 + 1.243 + static boolean cc(Object o) { 1.244 + return ((String) o).equals("foo"); 1.245 + } 1.246 + 1.247 + public void testArgCastingAdaptation() { 1.248 + TPredicate<String> p = LambdaTranslationTest2::cc; 1.249 + assertTrue(p.test("foo")); 1.250 + assertTrue(!p.test("bar")); 1.251 + } 1.252 + 1.253 + interface SonOfPredicate<T> extends TPredicate<T> { } 1.254 + 1.255 + public void testExtendsSAM() { 1.256 + SonOfPredicate<String> p = s -> s.isEmpty(); 1.257 + assertTrue(p.test("")); 1.258 + assertTrue(!p.test("foo")); 1.259 + } 1.260 + 1.261 + public void testConstructorRef() { 1.262 + Factory<List<String>> lf = ArrayList<String>::new; 1.263 + List<String> list = lf.make(); 1.264 + assertTrue(list instanceof ArrayList); 1.265 + assertTrue(list != lf.make()); 1.266 + list.add("a"); 1.267 + assertEquals("[a]", list.toString()); 1.268 + } 1.269 + 1.270 + private static String privateMethod() { 1.271 + return "private"; 1.272 + } 1.273 + 1.274 + public void testPrivateMethodRef() { 1.275 + Factory<String> sf = LambdaTranslationTest2::privateMethod; 1.276 + assertEquals("private", sf.make()); 1.277 + } 1.278 + 1.279 + private interface PrivateIntf { 1.280 + String make(); 1.281 + } 1.282 + 1.283 + public void testPrivateIntf() { 1.284 + PrivateIntf p = () -> "foo"; 1.285 + assertEquals("foo", p.make()); 1.286 + } 1.287 + 1.288 + interface Op<T> { 1.289 + public T op(T a, T b); 1.290 + } 1.291 + 1.292 + public void testBoxToObject() { 1.293 + Op<Integer> maxer = Math::max; 1.294 + for (int i=-100000; i < 100000; i += 100) 1.295 + for (int j=-100000; j < 100000; j += 99) { 1.296 + assertEquals((int) maxer.op(i,j), Math.max(i,j)); 1.297 + } 1.298 + } 1.299 + 1.300 + protected static String protectedMethod() { 1.301 + return "protected"; 1.302 + } 1.303 + 1.304 + public void testProtectedMethodRef() { 1.305 + Factory<String> sf = LambdaTranslationTest2::protectedMethod; 1.306 + assertEquals("protected", sf.make()); 1.307 + } 1.308 + 1.309 + class Inner1 { 1.310 + String m1() { 1.311 + return "Inner1.m1()"; 1.312 + } 1.313 + 1.314 + class Inner2 { 1.315 + public String m1() { 1.316 + return "Inner1.Inner2.m1()"; 1.317 + } 1.318 + 1.319 + protected String m2() { 1.320 + return "Inner1.Inner2.m2()"; 1.321 + } 1.322 + 1.323 + String m3() { 1.324 + return "Inner1.Inner2.m3()"; 1.325 + } 1.326 + 1.327 + class Inner3<T> { 1.328 + T t = null; 1.329 + Inner3(T t) { 1.330 + this.t = t; 1.331 + } 1.332 + T m1() { 1.333 + return t; 1.334 + } 1.335 + } 1.336 + } 1.337 + } 1.338 + 1.339 + public void testInnerClassMethodRef() { 1.340 + Factory<String> fs = new Inner1()::m1; 1.341 + assertEquals("Inner1.m1()", fs.make()); 1.342 + 1.343 + fs = new Inner1().new Inner2()::m1; 1.344 + assertEquals("Inner1.Inner2.m1()", fs.make()); 1.345 + 1.346 + fs = new Inner1().new Inner2()::m2; 1.347 + assertEquals("Inner1.Inner2.m2()", fs.make()); 1.348 + 1.349 + fs = new Inner1().new Inner2()::m3; 1.350 + assertEquals("Inner1.Inner2.m3()", fs.make()); 1.351 + 1.352 + fs = new Inner1().new Inner2().new Inner3<String>("Inner1.Inner2.Inner3")::m1; 1.353 + assertEquals("Inner1.Inner2.Inner3", fs.make()); 1.354 + 1.355 + Factory<Integer> fsi = new Inner1().new Inner2().new Inner3<Integer>(100)::m1; 1.356 + assertEquals(100, (int)fsi.make()); 1.357 + } 1.358 +}