1/* 2 * Copyright (c) 2012, 2016, 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 24import org.testng.annotations.Test; 25 26import java.util.ArrayList; 27import java.util.List; 28import java.util.function.Function; 29import java.util.function.Predicate; 30 31import static org.testng.Assert.assertEquals; 32import static org.testng.Assert.assertTrue; 33 34/** 35 * LambdaTranslationTest2 -- end-to-end smoke tests for lambda evaluation 36 */ 37@Test 38public class LambdaTranslationTest2 { 39 40 final String dummy = "dummy"; 41 42 public void testLambdas() { 43 Predicate<String> isEmpty = s -> s.isEmpty(); 44 assertTrue(isEmpty.test("")); 45 assertTrue(!isEmpty.test("foo")); 46 47 Predicate<Object> oIsEmpty = s -> ((String) s).isEmpty(); 48 assertTrue(oIsEmpty.test("")); 49 assertTrue(!oIsEmpty.test("foo")); 50 51 Predicate<Object> alwaysTrue = o -> true; 52 assertTrue(alwaysTrue.test("")); 53 assertTrue(alwaysTrue.test(null)); 54 55 Predicate<Object> alwaysFalse = o -> false; 56 assertTrue(!alwaysFalse.test("")); 57 assertTrue(!alwaysFalse.test(null)); 58 59 // tests local capture 60 String foo = "foo"; 61 Predicate<String> equalsFoo = s -> s.equals(foo); 62 assertTrue(!equalsFoo.test("")); 63 assertTrue(equalsFoo.test("foo")); 64 65 // tests instance capture 66 Predicate<String> equalsDummy = s -> s.equals(dummy); 67 assertTrue(!equalsDummy.test("")); 68 assertTrue(equalsDummy.test("dummy")); 69 70 Function<Object, Object> ident = s -> s; 71 72 assertEquals("blarf", ident.apply("blarf")); 73 assertEquals("wooga", ident.apply("wooga")); 74 assertTrue("wooga" == ident.apply("wooga")); 75 76 // constant capture 77 Function<Object, Object> prefixer = s -> "p" + s; 78 assertEquals("pblarf", prefixer.apply("blarf")); 79 assertEquals("pwooga", prefixer.apply("wooga")); 80 81 // instance capture 82 Function<Object, Object> prefixer2 = s -> dummy + s; 83 assertEquals("dummyblarf", prefixer2.apply("blarf")); 84 assertEquals("dummywooga", prefixer2.apply("wooga")); 85 } 86 87 interface Supplier<T> { 88 T make(); 89 } 90 91 interface StringFactory extends Supplier<String> { } 92 93 interface StringFactory2 extends Supplier<String> { 94 String make(); 95 } 96 97 public void testBridges() { 98 Supplier<String> of = () -> "y"; 99 Supplier<?> ef = () -> "z"; 100 101 assertEquals("y", of.make()); 102 assertEquals("y", ((Supplier<?>) of).make()); 103 assertEquals("y", ((Supplier) of).make()); 104 105 assertEquals("z", ef.make()); 106 assertEquals("z", ((Supplier) ef).make()); 107 } 108 109 public void testBridgesImplicitSpecialization() { 110 StringFactory sf = () -> "x"; 111 112 assertEquals("x", sf.make()); 113 assertEquals("x", ((Supplier<String>) sf).make()); 114 assertEquals("x", ((Supplier<?>) sf).make()); 115 assertEquals("x", ((Supplier) sf).make()); 116 } 117 118 public void testBridgesExplicitSpecialization() { 119 StringFactory2 sf = () -> "x"; 120 121 assertEquals("x", sf.make()); 122 assertEquals("x", ((Supplier<String>) sf).make()); 123 assertEquals("x", ((Supplier<?>) sf).make()); 124 assertEquals("x", ((Supplier) sf).make()); 125 } 126 127 public void testSuperCapture() { 128 class A { 129 String make() { return "x"; } 130 } 131 132 class B extends A { 133 void testSuperCapture() { 134 StringFactory sf = () -> super.make(); 135 assertEquals("x", sf.make()); 136 } 137 } 138 139 new B().testSuperCapture(); 140 } 141 142 interface WidenD { 143 public String m(float a0, double a1); 144 } 145 146 interface WidenS { 147 public String m(byte a0, short a1); 148 } 149 150 interface WidenI { 151 public String m(byte a0, short a1, char a2, int a3); 152 } 153 154 interface WidenL { 155 public String m(byte a0, short a1, char a2, int a3, long a4); 156 } 157 158 interface Box { 159 public String m(byte a0, short a1, char a2, int a3, long a4, boolean a5, float a6, double a7); 160 } 161 162 static String pb(Byte a0, Short a1, Character a2, Integer a3, Long a4, Boolean a5, Float a6, Double a7) { 163 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); 164 } 165 166 static String pwI1(int a0, int a1, int a2, int a3) { 167 return String.format("b%d s%d c%d i%d", a0, a1, a2, a3); 168 } 169 170 static String pwI2(Integer a0, Integer a1, Integer a2, Integer a3) { 171 return String.format("b%d s%d c%d i%d", a0, a1, a2, a3); 172 } 173 174 static String pwL1(long a0, long a1, long a2, long a3, long a4) { 175 return String.format("b%d s%d c%d i%d j%d", a0, a1, a2, a3, a4); 176 } 177 178 static String pwL2(Long a0, Long a1, Long a2, Long a3, Long a4) { 179 return String.format("b%d s%d c%d i%d j%d", a0, a1, a2, a3, a4); 180 } 181 182 static String pwS1(short a0, short a1) { 183 return String.format("b%d s%d", a0, a1); 184 } 185 186 static String pwS2(Short a0, Short a1) { 187 return String.format("b%d s%d", a0, a1); 188 } 189 190 static String pwD1(double a0, double a1) { 191 return String.format("f%f d%f", a0, a1); 192 } 193 194 static String pwD2(Double a0, Double a1) { 195 return String.format("f%f d%f", a0, a1); 196 } 197 198 public void testPrimitiveWidening() { 199 WidenS ws1 = LambdaTranslationTest2::pwS1; 200 assertEquals("b1 s2", ws1.m((byte) 1, (short) 2)); 201 202 WidenD wd1 = LambdaTranslationTest2::pwD1; 203 assertEquals(String.format("f%f d%f", 1.0f, 2.0), wd1.m(1.0f, 2.0)); 204 205 WidenI wi1 = LambdaTranslationTest2::pwI1; 206 assertEquals("b1 s2 c3 i4", wi1.m((byte) 1, (short) 2, (char) 3, 4)); 207 208 WidenL wl1 = LambdaTranslationTest2::pwL1; 209 assertEquals("b1 s2 c3 i4 j5", wl1.m((byte) 1, (short) 2, (char) 3, 4, 5L)); 210 211 // @@@ TODO: clarify spec on widen+box conversion 212 } 213 214 interface Unbox { 215 public String m(Byte a0, Short a1, Character a2, Integer a3, Long a4, Boolean a5, Float a6, Double a7); 216 } 217 218 static String pu(byte a0, short a1, char a2, int a3, long a4, boolean a5, float a6, double a7) { 219 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); 220 } 221 222 public void testUnboxing() { 223 Unbox u = LambdaTranslationTest2::pu; 224 String expected = String.format("b%d s%d c%c i%d j%d z%b f%f d%f", 225 (byte)1, (short) 2, 'A', 4, 5L, true, 6.0f, 7.0); 226 assertEquals(expected, u.m((byte)1, (short) 2, 'A', 4, 5L, true, 6.0f, 7.0)); 227 } 228 229 public void testBoxing() { 230 Box b = LambdaTranslationTest2::pb; 231 String expected = String.format("b%d s%d c%c i%d j%d z%b f%f d%f", 232 (byte) 1, (short) 2, 'A', 4, 5L, true, 6.0f, 7.0); 233 assertEquals(expected, b.m((byte) 1, (short) 2, 'A', 4, 5L, true, 6.0f, 7.0)); 234 } 235 236 static boolean cc(Object o) { 237 return ((String) o).equals("foo"); 238 } 239 240 public void testArgCastingAdaptation() { 241 Predicate<String> p = LambdaTranslationTest2::cc; 242 assertTrue(p.test("foo")); 243 assertTrue(!p.test("bar")); 244 } 245 246 interface SonOfPredicate<T> extends Predicate<T> { } 247 248 public void testExtendsSAM() { 249 SonOfPredicate<String> p = s -> s.isEmpty(); 250 assertTrue(p.test("")); 251 assertTrue(!p.test("foo")); 252 } 253 254 public void testConstructorRef() { 255 Supplier<List<String>> lf = ArrayList<String>::new; 256 List<String> list = lf.make(); 257 assertTrue(list instanceof ArrayList); 258 assertTrue(list != lf.make()); 259 list.add("a"); 260 assertEquals("[a]", list.toString()); 261 } 262 263 private static String privateMethod() { 264 return "private"; 265 } 266 267 public void testPrivateMethodRef() { 268 Supplier<String> sf = LambdaTranslationTest2::privateMethod; 269 assertEquals("private", sf.make()); 270 } 271 272 private interface PrivateIntf { 273 String make(); 274 } 275 276 public void testPrivateIntf() { 277 PrivateIntf p = () -> "foo"; 278 assertEquals("foo", p.make()); 279 } 280 281 interface Op<T> { 282 public T op(T a, T b); 283 } 284 285 public void testBoxToObject() { 286 Op<Integer> maxer = Math::max; 287 for (int i=-100000; i < 100000; i += 100) 288 for (int j=-100000; j < 100000; j += 99) { 289 assertEquals((int) maxer.op(i,j), Math.max(i,j)); 290 } 291 } 292 293 protected static String protectedMethod() { 294 return "protected"; 295 } 296 297 public void testProtectedMethodRef() { 298 Supplier<String> sf = LambdaTranslationTest2::protectedMethod; 299 assertEquals("protected", sf.make()); 300 } 301 302 class Inner1 { 303 String m1() { 304 return "Inner1.m1()"; 305 } 306 307 class Inner2 { 308 public String m1() { 309 return "Inner1.Inner2.m1()"; 310 } 311 312 protected String m2() { 313 return "Inner1.Inner2.m2()"; 314 } 315 316 String m3() { 317 return "Inner1.Inner2.m3()"; 318 } 319 320 class Inner3<T> { 321 T t = null; 322 Inner3(T t) { 323 this.t = t; 324 } 325 T m1() { 326 return t; 327 } 328 } 329 } 330 } 331 332 public void testInnerClassMethodRef() { 333 Supplier<String> fs = new Inner1()::m1; 334 assertEquals("Inner1.m1()", fs.make()); 335 336 fs = new Inner1().new Inner2()::m1; 337 assertEquals("Inner1.Inner2.m1()", fs.make()); 338 339 fs = new Inner1().new Inner2()::m2; 340 assertEquals("Inner1.Inner2.m2()", fs.make()); 341 342 fs = new Inner1().new Inner2()::m3; 343 assertEquals("Inner1.Inner2.m3()", fs.make()); 344 345 fs = new Inner1().new Inner2().new Inner3<String>("Inner1.Inner2.Inner3")::m1; 346 assertEquals("Inner1.Inner2.Inner3", fs.make()); 347 348 Supplier<Integer> fsi = new Inner1().new Inner2().new Inner3<Integer>(100)::m1; 349 assertEquals(100, (int)fsi.make()); 350 } 351} 352