Skip to content

Commit 93bf9d8

Browse files
committed
Add tests for iteration and control statements (#28)
1 parent c4c0264 commit 93bf9d8

File tree

3 files changed

+171
-2
lines changed

3 files changed

+171
-2
lines changed

basics/src/test/java/pl/mperor/lab/java/data/type/ArraysTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void testMultiDimensionalArray() {
2323

2424
@Test
2525
public void testSquareMatrix() {
26-
int[][] multi = new int[2][2];
27-
Assertions.assertArrayEquals(new int[][]{{0, 0}, {0, 0}}, multi);
26+
int[][] matrix = new int[2][2];
27+
Assertions.assertArrayEquals(new int[][]{{0, 0}, {0, 0}}, matrix);
2828
}
2929
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package pl.mperor.lab.java.statement;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class ControlStatementTest {
7+
8+
@Test
9+
public void testIfElseStatement() {
10+
Assertions.assertEquals(1, defaultIfNull(null, 1));
11+
Assertions.assertEquals(-1, defaultIfNull(-1, 1));
12+
}
13+
14+
public static <T> T defaultIfNull(T nullable, T ifNull) {
15+
if (nullable != null) {
16+
return nullable;
17+
}
18+
19+
return ifNull;
20+
}
21+
22+
@Test
23+
public void testSwitchBlock() {
24+
Assertions.assertEquals(2, calculateByOperator("+", 1, 1));
25+
Assertions.assertEquals(0, calculateByOperator("-", 1, 1));
26+
Assertions.assertEquals(4, calculateByOperator("*", 2, 2));
27+
Assertions.assertEquals(2, calculateByOperator("/", 4, 2));
28+
Assertions.assertThrows(IllegalStateException.class, () -> calculateByOperator("?", 1, 1));
29+
}
30+
31+
public static int calculateByOperator(String operator, int a, int b) {
32+
switch (operator) {
33+
case "+":
34+
return a + b;
35+
case "-":
36+
return a - b;
37+
case "*":
38+
return a * b;
39+
case "/":
40+
return b != 0 ? a / b : 0;
41+
default:
42+
throw new IllegalStateException("Invalid operator: " + operator);
43+
}
44+
}
45+
46+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package pl.mperor.lab.java.statement;
2+
3+
import org.junit.jupiter.api.AfterEach;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.OutputStream;
10+
import java.io.PrintStream;
11+
import java.util.List;
12+
13+
public class IterationStatementTest {
14+
15+
private PrintStream original = System.out;
16+
private OutputStream out;
17+
18+
@BeforeEach
19+
void setUp() {
20+
out = new ByteArrayOutputStream();
21+
System.setOut(new PrintStream(out));
22+
}
23+
24+
@AfterEach
25+
void tearDown() {
26+
System.setOut(original);
27+
}
28+
29+
@Test
30+
public void testWhileLoop() {
31+
var integers = List.of(1, 2, 3);
32+
var iterator = integers.iterator();
33+
while (iterator.hasNext()) {
34+
System.out.print(iterator.next());
35+
}
36+
Assertions.assertEquals("123", out.toString());
37+
}
38+
39+
@Test
40+
public void testDoWhileLoop() {
41+
var connector = new FaultyConnector();
42+
do {
43+
connector.reset();
44+
} while (!connector.connect());
45+
Assertions.assertEquals(2, connector.resetCounter);
46+
}
47+
48+
static class FaultyConnector {
49+
int doubleResetNeeded = 2;
50+
int resetCounter = 0;
51+
52+
boolean connect() {
53+
return resetCounter >= doubleResetNeeded;
54+
}
55+
56+
void reset() {
57+
resetCounter++;
58+
}
59+
}
60+
61+
@Test
62+
public void testForLoop() {
63+
int[] integers = {1, 2, 3};
64+
for (int i = 0; i < integers.length; i++) {
65+
System.out.printf("(%d->%d)", i, integers[i]);
66+
}
67+
Assertions.assertEquals("(0->1)(1->2)(2->3)", out.toString());
68+
}
69+
70+
@Test
71+
public void testEnhancedForLoopAkaForEachLoop() {
72+
var integers = List.of(1, 2, 3);
73+
for (Integer integer : integers) {
74+
System.out.print(integer);
75+
}
76+
Assertions.assertEquals("123", out.toString());
77+
}
78+
79+
@Test
80+
public void testJumpStatements() {
81+
breakFromInfiniteLoop();
82+
printEvenNumbers(1, 2, 3, 4);
83+
Assertions.assertEquals("24", out.toString());
84+
}
85+
86+
private void breakFromInfiniteLoop() {
87+
for (; ; ) {
88+
break;
89+
}
90+
}
91+
92+
private void printEvenNumbers(int... numbers) {
93+
for (int number : numbers) {
94+
if (number % 2 != 0) {
95+
continue;
96+
}
97+
System.out.print(number);
98+
}
99+
}
100+
101+
@Test
102+
public void testNestedLoop() {
103+
record Point(int x, int y) {
104+
@Override
105+
public String toString() {
106+
return "" + x + y;
107+
}
108+
}
109+
Point[][] matrix = {
110+
{new Point(0, 0), new Point(0, 1)},
111+
{new Point(1, 0), new Point(1, 1)}
112+
};
113+
114+
for (int row = 0; row < matrix.length; row++) {
115+
for (int col = 0; col < matrix[row].length; col++) {
116+
System.out.printf("(%s)", matrix[row][col]);
117+
}
118+
}
119+
120+
Assertions.assertEquals("(00)(01)(10)(11)", out.toString());
121+
}
122+
123+
}

0 commit comments

Comments
 (0)