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 MethodReferenceTestVarArgs
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
40@Test
41public class MethodReferenceTestVarArgs {
42
43    interface SII {
44
45        String m(Integer a, Integer b);
46    }
47
48    interface Siii {
49
50        String m(int a, int b, int c);
51    }
52
53    interface Si {
54
55        String m(int a);
56    }
57
58    interface SaO {
59
60        String m(Object[] a);
61    }
62
63    interface Sai {
64
65        String m(int[] a);
66    }
67
68    interface Svi {
69
70        String m(int... va);
71    }
72
73    // These should be processed as var args
74
75    static String xvI(Integer... vi) {
76        StringBuilder sb = new StringBuilder("xvI:");
77        for (Integer i : vi) {
78            sb.append(i);
79            sb.append("-");
80        }
81        return sb.toString();
82    }
83
84    static String xIvI(Integer f, Integer... vi) {
85        StringBuilder sb = new StringBuilder("xIvI:");
86        sb.append(f);
87        for (Integer i : vi) {
88            sb.append(i);
89            sb.append("-");
90        }
91        return sb.toString();
92    }
93
94    static String xvi(int... vi) {
95        int sum = 0;
96        for (int i : vi) {
97            sum += i;
98        }
99        return "xvi:" + sum;
100    }
101
102    static String xIvi(Integer f, int... vi) {
103        int sum = 0;
104        for (int i : vi) {
105            sum += i;
106        }
107        return "xIvi:(" + f + ")" + sum;
108    }
109
110    static String xvO(Object... vi) {
111        StringBuilder sb = new StringBuilder("xvO:");
112        for (Object i : vi) {
113            if (i.getClass().isArray()) {
114                sb.append("[");
115                int len = Array.getLength(i);
116                for (int x = 0; x < len; ++x)  {
117                    sb.append(Array.get(i, x));
118                    sb.append(",");
119                }
120                sb.append("]");
121
122            } else {
123                sb.append(i);
124            }
125            sb.append("*");
126        }
127        return sb.toString();
128    }
129
130    public void testVarArgsSuperclass() {
131        SII q;
132
133        q = MethodReferenceTestVarArgs::xvO;
134        assertEquals(q.m(55,66), "xvO:55*66*");
135    }
136
137    public void testVarArgsArray() {
138        Sai q;
139
140        q = MethodReferenceTestVarArgs::xvO;
141        assertEquals(q.m(new int[] { 55,66 } ), "xvO:[55,66,]*");
142    }
143
144    public void testVarArgsII() {
145        SII q;
146
147        q = MethodReferenceTestVarArgs::xvI;
148        assertEquals(q.m(33,7), "xvI:33-7-");
149
150        q = MethodReferenceTestVarArgs::xIvI;
151        assertEquals(q.m(50,40), "xIvI:5040-");
152
153        q = MethodReferenceTestVarArgs::xvi;
154        assertEquals(q.m(100,23), "xvi:123");
155
156        q = MethodReferenceTestVarArgs::xIvi;
157        assertEquals(q.m(9,21), "xIvi:(9)21");
158    }
159
160    public void testVarArgsiii() {
161        Siii q;
162
163        q = MethodReferenceTestVarArgs::xvI;
164        assertEquals(q.m(3, 2, 1), "xvI:3-2-1-");
165
166        q = MethodReferenceTestVarArgs::xIvI;
167        assertEquals(q.m(888, 99, 2), "xIvI:88899-2-");
168
169        q = MethodReferenceTestVarArgs::xvi;
170        assertEquals(q.m(900,80,7), "xvi:987");
171
172        q = MethodReferenceTestVarArgs::xIvi;
173        assertEquals(q.m(333,27, 72), "xIvi:(333)99");
174    }
175
176    public void testVarArgsi() {
177        Si q;
178
179        q = MethodReferenceTestVarArgs::xvI;
180        assertEquals(q.m(3), "xvI:3-");
181
182        q = MethodReferenceTestVarArgs::xIvI;
183        assertEquals(q.m(888), "xIvI:888");
184
185        q = MethodReferenceTestVarArgs::xvi;
186        assertEquals(q.m(900), "xvi:900");
187
188        q = MethodReferenceTestVarArgs::xIvi;
189        assertEquals(q.m(333), "xIvi:(333)0");
190    }
191
192    // These should NOT be processed as var args
193
194    public void testVarArgsaO() {
195        SaO q;
196
197        q = MethodReferenceTestVarArgs::xvO;
198        assertEquals(q.m(new String[] { "yo", "there", "dude" }), "xvO:yo*there*dude*");
199    }
200
201
202}
203