1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8003639
* @summary convert lambda testng tests to jtreg and add them
* @run testng LambdaTranslationTest2
*/
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
/**
* LambdaTranslationTest2 -- end-to-end smoke tests for lambda evaluation
*/
@Test
public class LambdaTranslationTest2 {
final String dummy = "dummy";
public void testLambdas() {
TPredicate<String> isEmpty = s -> s.isEmpty();
assertTrue(isEmpty.test(""));
assertTrue(!isEmpty.test("foo"));
TPredicate<Object> oIsEmpty = s -> ((String) s).isEmpty();
assertTrue(oIsEmpty.test(""));
assertTrue(!oIsEmpty.test("foo"));
TPredicate<Object> alwaysTrue = o -> true;
assertTrue(alwaysTrue.test(""));
assertTrue(alwaysTrue.test(null));
TPredicate<Object> alwaysFalse = o -> false;
assertTrue(!alwaysFalse.test(""));
assertTrue(!alwaysFalse.test(null));
// tests local capture
String foo = "foo";
TPredicate<String> equalsFoo = s -> s.equals(foo);
assertTrue(!equalsFoo.test(""));
assertTrue(equalsFoo.test("foo"));
// tests instance capture
TPredicate<String> equalsDummy = s -> s.equals(dummy);
assertTrue(!equalsDummy.test(""));
assertTrue(equalsDummy.test("dummy"));
TMapper<Object, Object> ident = s -> s;
assertEquals("blarf", ident.map("blarf"));
assertEquals("wooga", ident.map("wooga"));
assertTrue("wooga" == ident.map("wooga"));
// constant capture
TMapper<Object, Object> prefixer = s -> "p" + s;
assertEquals("pblarf", prefixer.map("blarf"));
assertEquals("pwooga", prefixer.map("wooga"));
// instance capture
TMapper<Object, Object> prefixer2 = s -> dummy + s;
assertEquals("dummyblarf", prefixer2.map("blarf"));
assertEquals("dummywooga", prefixer2.map("wooga"));
}
interface Factory<T> {
T make();
}
interface StringFactory extends Factory<String> { }
interface StringFactory2 extends Factory<String> {
String make();
}
public void testBridges() {
Factory<String> of = () -> "y";
Factory<?> ef = () -> "z";
assertEquals("y", of.make());
assertEquals("y", ((Factory<?>) of).make());
assertEquals("y", ((Factory) of).make());
assertEquals("z", ef.make());
assertEquals("z", ((Factory) ef).make());
}
public void testBridgesImplicitSpecialization() {
StringFactory sf = () -> "x";
assertEquals("x", sf.make());
assertEquals("x", ((Factory<String>) sf).make());
assertEquals("x", ((Factory<?>) sf).make());
assertEquals("x", ((Factory) sf).make());
}
public void testBridgesExplicitSpecialization() {
StringFactory2 sf = () -> "x";
assertEquals("x", sf.make());
assertEquals("x", ((Factory<String>) sf).make());
assertEquals("x", ((Factory<?>) sf).make());
assertEquals("x", ((Factory) sf).make());
}
public void testSuperCapture() {
class A {
String make() { return "x"; }
}
class B extends A {
void testSuperCapture() {
StringFactory sf = () -> super.make();
assertEquals("x", sf.make());
}
}
new B().testSuperCapture();
}
interface WidenD {
public String m(float a0, double a1);
}
interface WidenS {
public String m(byte a0, short a1);
}
interface WidenI {
public String m(byte a0, short a1, char a2, int a3);
}
interface WidenL {
public String m(byte a0, short a1, char a2, int a3, long a4);
}
interface Box {
public String m(byte a0, short a1, char a2, int a3, long a4, boolean a5, float a6, double a7);
}
static String pb(Byte a0, Short a1, Character a2, Integer a3, Long a4, Boolean a5, Float a6, Double a7) {
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);
}
static String pwI1(int a0, int a1, int a2, int a3) {
return String.format("b%d s%d c%d i%d", a0, a1, a2, a3);
}
static String pwI2(Integer a0, Integer a1, Integer a2, Integer a3) {
return String.format("b%d s%d c%d i%d", a0, a1, a2, a3);
}
static String pwL1(long a0, long a1, long a2, long a3, long a4) {
return String.format("b%d s%d c%d i%d j%d", a0, a1, a2, a3, a4);
}
static String pwL2(Long a0, Long a1, Long a2, Long a3, Long a4) {
return String.format("b%d s%d c%d i%d j%d", a0, a1, a2, a3, a4);
}
static String pwS1(short a0, short a1) {
return String.format("b%d s%d", a0, a1);
}
static String pwS2(Short a0, Short a1) {
return String.format("b%d s%d", a0, a1);
}
static String pwD1(double a0, double a1) {
return String.format("f%f d%f", a0, a1);
}
static String pwD2(Double a0, Double a1) {
return String.format("f%f d%f", a0, a1);
}
public void testPrimitiveWidening() {
WidenS ws1 = LambdaTranslationTest2::pwS1;
assertEquals("b1 s2", ws1.m((byte) 1, (short) 2));
WidenD wd1 = LambdaTranslationTest2::pwD1;
assertEquals("f1.000000 d2.000000", wd1.m(1.0f, 2.0));
WidenI wi1 = LambdaTranslationTest2::pwI1;
assertEquals("b1 s2 c3 i4", wi1.m((byte) 1, (short) 2, (char) 3, 4));
WidenL wl1 = LambdaTranslationTest2::pwL1;
assertEquals("b1 s2 c3 i4 j5", wl1.m((byte) 1, (short) 2, (char) 3, 4, 5L));
// @@@ TODO: clarify spec on widen+box conversion
}
interface Unbox {
public String m(Byte a0, Short a1, Character a2, Integer a3, Long a4, Boolean a5, Float a6, Double a7);
}
static String pu(byte a0, short a1, char a2, int a3, long a4, boolean a5, float a6, double a7) {
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);
}
public void testUnboxing() {
Unbox u = LambdaTranslationTest2::pu;
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));
}
public void testBoxing() {
Box b = LambdaTranslationTest2::pb;
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));
}
static boolean cc(Object o) {
return ((String) o).equals("foo");
}
public void testArgCastingAdaptation() {
TPredicate<String> p = LambdaTranslationTest2::cc;
assertTrue(p.test("foo"));
assertTrue(!p.test("bar"));
}
interface SonOfPredicate<T> extends TPredicate<T> { }
public void testExtendsSAM() {
SonOfPredicate<String> p = s -> s.isEmpty();
assertTrue(p.test(""));
assertTrue(!p.test("foo"));
}
public void testConstructorRef() {
Factory<List<String>> lf = ArrayList<String>::new;
List<String> list = lf.make();
assertTrue(list instanceof ArrayList);
assertTrue(list != lf.make());
list.add("a");
assertEquals("[a]", list.toString());
}
private static String privateMethod() {
return "private";
}
public void testPrivateMethodRef() {
Factory<String> sf = LambdaTranslationTest2::privateMethod;
assertEquals("private", sf.make());
}
private interface PrivateIntf {
String make();
}
public void testPrivateIntf() {
PrivateIntf p = () -> "foo";
assertEquals("foo", p.make());
}
interface Op<T> {
public T op(T a, T b);
}
public void testBoxToObject() {
Op<Integer> maxer = Math::max;
for (int i=-100000; i < 100000; i += 100)
for (int j=-100000; j < 100000; j += 99) {
assertEquals((int) maxer.op(i,j), Math.max(i,j));
}
}
protected static String protectedMethod() {
return "protected";
}
public void testProtectedMethodRef() {
Factory<String> sf = LambdaTranslationTest2::protectedMethod;
assertEquals("protected", sf.make());
}
class Inner1 {
String m1() {
return "Inner1.m1()";
}
class Inner2 {
public String m1() {
return "Inner1.Inner2.m1()";
}
protected String m2() {
return "Inner1.Inner2.m2()";
}
String m3() {
return "Inner1.Inner2.m3()";
}
class Inner3<T> {
T t = null;
Inner3(T t) {
this.t = t;
}
T m1() {
return t;
}
}
}
}
public void testInnerClassMethodRef() {
Factory<String> fs = new Inner1()::m1;
assertEquals("Inner1.m1()", fs.make());
fs = new Inner1().new Inner2()::m1;
assertEquals("Inner1.Inner2.m1()", fs.make());
fs = new Inner1().new Inner2()::m2;
assertEquals("Inner1.Inner2.m2()", fs.make());
fs = new Inner1().new Inner2()::m3;
assertEquals("Inner1.Inner2.m3()", fs.make());
fs = new Inner1().new Inner2().new Inner3<String>("Inner1.Inner2.Inner3")::m1;
assertEquals("Inner1.Inner2.Inner3", fs.make());
Factory<Integer> fsi = new Inner1().new Inner2().new Inner3<Integer>(100)::m1;
assertEquals(100, (int)fsi.make());
}
}
|