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 MethodReferenceTestVarArgsThis
29 */
30
31import org.testng.annotations.Test;
32import java.lang.reflect.Array;
33
34import static org.testng.Assert.assertEquals;
35
36/**
37 * @author Robert Field
38 */
39
40interface NsII { String m(Integer a, Integer b); }
41
42interface Nsiii { String m(int a, int b, int c); }
43
44interface Nsi { String m(int a); }
45
46interface NsaO { String m(Object[] a); }
47
48interface Nsai { String m(int[] a); }
49
50interface Nsvi { String m(int... va); }
51
52@Test
53public class MethodReferenceTestVarArgsThis {
54
55    // These should be processed as var args
56
57    String xvI(Integer... vi) {
58        StringBuilder sb = new StringBuilder("xvI:");
59        for (Integer i : vi) {
60            sb.append(i);
61            sb.append("-");
62        }
63        return sb.toString();
64    }
65
66    String xIvI(Integer f, Integer... vi) {
67        StringBuilder sb = new StringBuilder("xIvI:");
68        sb.append(f);
69        for (Integer i : vi) {
70            sb.append(i);
71            sb.append("-");
72        }
73        return sb.toString();
74    }
75
76    String xvi(int... vi) {
77        int sum = 0;
78        for (int i : vi) {
79            sum += i;
80        }
81        return "xvi:" + sum;
82    }
83
84    String xIvi(Integer f, int... vi) {
85        int sum = 0;
86        for (int i : vi) {
87            sum += i;
88        }
89        return "xIvi:(" + f + ")" + sum;
90    }
91
92    String xvO(Object... vi) {
93        StringBuilder sb = new StringBuilder("xvO:");
94        for (Object i : vi) {
95            if (i.getClass().isArray()) {
96                sb.append("[");
97                int len = Array.getLength(i);
98                for (int x = 0; x < len; ++x)  {
99                    sb.append(Array.get(i, x));
100                    sb.append(",");
101                }
102                sb.append("]");
103
104            } else {
105                sb.append(i);
106            }
107            sb.append("*");
108        }
109        return sb.toString();
110    }
111
112    public void testVarArgsNsSuperclass() {
113        NsII q;
114
115        q = this::xvO;
116        assertEquals(q.m(55,66), "xvO:55*66*");
117    }
118
119    public void testVarArgsNsArray() {
120        Nsai q;
121
122        q = this::xvO;
123        assertEquals(q.m(new int[] { 55,66 } ), "xvO:[55,66,]*");
124    }
125
126    public void testVarArgsNsII() {
127        NsII q;
128
129        q = this::xvI;
130        assertEquals(q.m(33,7), "xvI:33-7-");
131
132        q = this::xIvI;
133        assertEquals(q.m(50,40), "xIvI:5040-");
134
135        q = this::xvi;
136        assertEquals(q.m(100,23), "xvi:123");
137
138        q = this::xIvi;
139        assertEquals(q.m(9,21), "xIvi:(9)21");
140    }
141
142    public void testVarArgsNsiii() {
143        Nsiii q;
144
145        q = this::xvI;
146        assertEquals(q.m(3, 2, 1), "xvI:3-2-1-");
147
148        q = this::xIvI;
149        assertEquals(q.m(888, 99, 2), "xIvI:88899-2-");
150
151        q = this::xvi;
152        assertEquals(q.m(900,80,7), "xvi:987");
153
154        q = this::xIvi;
155        assertEquals(q.m(333,27, 72), "xIvi:(333)99");
156    }
157
158    public void testVarArgsNsi() {
159        Nsi q;
160
161        q = this::xvI;
162        assertEquals(q.m(3), "xvI:3-");
163
164        q = this::xIvI;
165        assertEquals(q.m(888), "xIvI:888");
166
167        q = this::xvi;
168        assertEquals(q.m(900), "xvi:900");
169
170        q = this::xIvi;
171        assertEquals(q.m(333), "xIvi:(333)0");
172    }
173
174    // These should NOT be processed as var args
175
176    public void testVarArgsNsaO() {
177        NsaO q;
178
179        q = this::xvO;
180        assertEquals(q.m(new String[] { "yo", "there", "dude" }), "xvO:yo*there*dude*");
181    }
182
183
184}
185