|
4 | 4 | import static org.junit.jupiter.api.Assertions.assertThrows; |
5 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue; |
6 | 6 |
|
| 7 | +import java.io.ByteArrayOutputStream; |
| 8 | +import java.io.PrintStream; |
7 | 9 | import java.util.List; |
8 | 10 | import org.junit.jupiter.api.Test; |
9 | 11 |
|
@@ -124,4 +126,98 @@ void testAllCombinationsValid() { |
124 | 126 | assertEquals(5, sum, "Combination " + combination + " does not sum to 5"); |
125 | 127 | } |
126 | 128 | } |
| 129 | + |
| 130 | + @Test |
| 131 | + void testPrintDiceCombinations() { |
| 132 | + // Capture System.out |
| 133 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 134 | + PrintStream originalOut = System.out; |
| 135 | + System.setOut(new PrintStream(outputStream)); |
| 136 | + |
| 137 | + try { |
| 138 | + // Test printing combinations for target 3 |
| 139 | + DiceThrower.printDiceCombinations(3); |
| 140 | + String output = outputStream.toString(); |
| 141 | + |
| 142 | + // Verify all expected combinations are printed |
| 143 | + assertTrue(output.contains("111")); |
| 144 | + assertTrue(output.contains("12")); |
| 145 | + assertTrue(output.contains("21")); |
| 146 | + assertTrue(output.contains("3")); |
| 147 | + |
| 148 | + // Count number of lines (combinations) |
| 149 | + String[] lines = output.trim().split("\n"); |
| 150 | + assertEquals(4, lines.length); |
| 151 | + } finally { |
| 152 | + // Restore System.out |
| 153 | + System.setOut(originalOut); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void testPrintDiceCombinationsZero() { |
| 159 | + // Capture System.out |
| 160 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 161 | + PrintStream originalOut = System.out; |
| 162 | + System.setOut(new PrintStream(outputStream)); |
| 163 | + |
| 164 | + try { |
| 165 | + DiceThrower.printDiceCombinations(0); |
| 166 | + String output = outputStream.toString(); |
| 167 | + |
| 168 | + // Should print empty string (one line) |
| 169 | + assertEquals("", output.trim()); |
| 170 | + } finally { |
| 171 | + System.setOut(originalOut); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + void testMainMethod() { |
| 177 | + // Capture System.out |
| 178 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 179 | + PrintStream originalOut = System.out; |
| 180 | + System.setOut(new PrintStream(outputStream)); |
| 181 | + |
| 182 | + try { |
| 183 | + // Test main method |
| 184 | + DiceThrower.main(new String[]{}); |
| 185 | + String output = outputStream.toString(); |
| 186 | + |
| 187 | + // Verify expected output contains header and combinations |
| 188 | + assertTrue(output.contains("All dice combinations that sum to 4:")); |
| 189 | + assertTrue(output.contains("Total combinations: 8")); |
| 190 | + assertTrue(output.contains("1111")); |
| 191 | + assertTrue(output.contains("22")); |
| 192 | + assertTrue(output.contains("4")); |
| 193 | + } finally { |
| 194 | + System.setOut(originalOut); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + void testEdgeCaseTargetFive() { |
| 200 | + List<String> result = DiceThrower.getDiceCombinations(5); |
| 201 | + assertEquals(16, result.size()); |
| 202 | + |
| 203 | + // Test specific combinations exist |
| 204 | + assertTrue(result.contains("11111")); |
| 205 | + assertTrue(result.contains("1112")); |
| 206 | + assertTrue(result.contains("122")); |
| 207 | + assertTrue(result.contains("14")); |
| 208 | + assertTrue(result.contains("23")); |
| 209 | + assertTrue(result.contains("5")); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + void testTargetGreaterThanSix() { |
| 214 | + List<String> result = DiceThrower.getDiceCombinations(8); |
| 215 | + assertTrue(result.size() > 0); |
| 216 | + |
| 217 | + // Verify some expected combinations |
| 218 | + assertTrue(result.contains("62")); |
| 219 | + assertTrue(result.contains("53")); |
| 220 | + assertTrue(result.contains("44")); |
| 221 | + assertTrue(result.contains("2222")); |
| 222 | + } |
127 | 223 | } |
0 commit comments