|
| 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