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