Skip to content

Commit 51eda70

Browse files
committed
Add test for method mechanisms
1 parent 26e3b6f commit 51eda70

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package pl.mperor.lab.java.lang.element;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.Arrays;
7+
8+
public class MethodTest {
9+
10+
@Test
11+
public void testOverloading() {
12+
byte b = 1;
13+
Assertions.assertEquals(1, Overloading.countArgs(b), "Type Promotion");
14+
Assertions.assertEquals(1, Overloading.countArgs(1.0), "Different Parameter Types");
15+
Assertions.assertEquals(2, Overloading.countArgs(1, 'a'), "Different Number of Parameters");
16+
Assertions.assertEquals(3, Overloading.countArgs("one", "two", "three"), "Varargs");
17+
Assertions.assertEquals(0, Overloading.countArgs(), "Varargs");
18+
Assertions.assertEquals(1, new Overloading().countArgs('c'), "Not only static!");
19+
}
20+
21+
static class Overloading {
22+
static int countArgs(long l) {
23+
return 1;
24+
}
25+
26+
static int countArgs(double d) {
27+
return 1;
28+
}
29+
30+
static <T> int countArgs(long l, T t) {
31+
return 2;
32+
}
33+
34+
static <T> int countArgs(T... t) {
35+
return t != null ? t.length : 0;
36+
}
37+
38+
int countArgs(char c) {
39+
return 1;
40+
}
41+
}
42+
43+
@Test
44+
public void testParameterPassingMechanism() {
45+
int primitive = 0;
46+
ParameterPassing.primitiveByValue(primitive);
47+
Assertions.assertEquals(0, primitive);
48+
49+
String string = "original";
50+
ParameterPassing.immutableObjectByValue(string);
51+
Assertions.assertEquals("original", string);
52+
53+
int[] array = {1, 2, 3};
54+
ParameterPassing.mutableObjectByValue(array);
55+
Assertions.assertArrayEquals(new int[]{0, 0, 0}, array);
56+
}
57+
58+
static class ParameterPassing {
59+
static void primitiveByValue(int primitive) {
60+
primitive = 1;
61+
}
62+
63+
static void immutableObjectByValue(String immutable) {
64+
immutable = "";
65+
}
66+
67+
static void mutableObjectByValue(int[] array) {
68+
Arrays.fill(array, 0);
69+
}
70+
}
71+
72+
}

0 commit comments

Comments
 (0)