Skip to content

Commit 758449d

Browse files
committed
Quality: add unit tests for method calls with "var args"
1 parent 25afd1d commit 758449d

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2017 java2script.org and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Udo Borkowski - initial implementation
10+
*******************************************************************************/
11+
12+
package net.sf.j2s.test.junit.basic;
13+
14+
import junit.framework.TestCase;
15+
import net.sf.j2s.test.junit.sample.C;
16+
import net.sf.j2s.test.junit.sample.C2;
17+
import net.sf.j2s.test.junit.sample.CF;
18+
import net.sf.j2s.test.junit.sample.CF1;
19+
import net.sf.j2s.test.junit.sample.CVarArgs;
20+
import net.sf.j2s.test.junit.util.Output;
21+
22+
public class VarArgsTest extends TestCase {
23+
24+
/**
25+
* Call a varArgs method with not arguments.
26+
*/
27+
public void testVarArgsEmpty() {
28+
String s = new CVarArgs().fVarArgsInt();
29+
assertEquals("ints: ", s);
30+
}
31+
32+
/**
33+
* Call a varArgs method with some arguments.
34+
*/
35+
public void testVarArgsNotEmpty() {
36+
String s = new CVarArgs().fVarArgsInt(4,7,9);
37+
assertEquals("ints: 4 7 9 ", s);
38+
}
39+
40+
/**
41+
* Call a varArgs method with some arguments (via array).
42+
*/
43+
public void testVarArgsNotEmptyViaArray() {
44+
int[] args = new int[]{4,7,9};
45+
String s = new CVarArgs().fVarArgsInt(args);
46+
assertEquals("ints: 4 7 9 ", s);
47+
}
48+
49+
/**
50+
* Call a varArgs method with a mandatory arguments and no optional arguments.
51+
*/
52+
public void testStringPlusVarArgsEmpty() {
53+
String s = new CVarArgs().fStringVarArgsInt("demo ");
54+
assertEquals("demo ", s);
55+
}
56+
57+
/**
58+
* Call a varArgs method with a mandatory arguments and no optional arguments.
59+
*/
60+
public void testStringPlusVarArgsNotEmpty() {
61+
String s = new CVarArgs().fStringVarArgsInt("demo ", 4, 7);
62+
assertEquals("demo 4 7 ", s);
63+
}
64+
65+
/**
66+
* Call a varArgs method of array type (here int[]) with not arguments.
67+
*/
68+
public void testIntArrVarArgsEmpty() {
69+
String s = new CVarArgs().fIntArr();
70+
assertEquals("intss ", s);
71+
}
72+
73+
/**
74+
* Call a varArgs method of array type (here int[]) with some arguments.
75+
*/
76+
public void testIntArrVarArgsNotEmpty() {
77+
String s = new CVarArgs().fIntArr(new int[]{3,5,7}, new int[]{2,4});
78+
assertEquals("intss [3 5 7 ] [2 4 ] ", s);
79+
}
80+
81+
/**
82+
* Call a varArgs method of array type (here int[]) with some arguments (via array).
83+
*/
84+
public void testIntArrVarArgsNotEmptyViaArray() {
85+
int[][] args = new int[][]{new int[]{3,5,7}, new int[]{2,4}};
86+
String s = new CVarArgs().fIntArr(args );
87+
assertEquals("intss [3 5 7 ] [2 4 ] ", s);
88+
}
89+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package net.sf.j2s.test.junit.sample;
2+
3+
public class CVarArgs {
4+
public String fVarArgsInt(int... ns) {
5+
String result = "ints: ";
6+
for (int i = 0; i < ns.length; i++) {
7+
result += ns[i] + " ";
8+
}
9+
return result;
10+
}
11+
12+
public String fStringVarArgsInt(String s, int... ns) {
13+
String result = s;
14+
for (int i = 0; i < ns.length; i++) {
15+
result += ns[i] + " ";
16+
}
17+
return result;
18+
}
19+
20+
public String fIntArr(int[]... nss) {
21+
String result = "intss ";
22+
for (int j = 0; j < nss.length; j++) {
23+
int[] ns = nss[j];
24+
result += "[";
25+
for (int i = 0; i < ns.length; i++) {
26+
result += ns[i] + " ";
27+
}
28+
result += "] ";
29+
}
30+
return result;
31+
}
32+
}

0 commit comments

Comments
 (0)