|
| 1 | +package com.thealgorithms.recursion; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.PrintStream; |
| 5 | + |
| 6 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +/** |
| 12 | + * Test class for Josephus problem implementation |
| 13 | + * |
| 14 | + * @author ritesh-3822 |
| 15 | + * @see Josephus |
| 16 | + */ |
| 17 | +class JosephusTest { |
| 18 | + |
| 19 | + @Test |
| 20 | + void testNOneAnyK() { |
| 21 | + // single person -> survivor is 1 |
| 22 | + assertEquals(1, Josephus.getJosephus(1, 1)); |
| 23 | + assertEquals(1, Josephus.getJosephus(1, 5)); |
| 24 | + assertEquals(1, Josephus.getJosephus(1, 100)); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + void testSmallCases() { |
| 29 | + // Known small results |
| 30 | + assertEquals(3, Josephus.getJosephus(5, 2)); // classic: n=5,k=2 -> 3 |
| 31 | + assertEquals(4, Josephus.getJosephus(7, 3)); // classic: n=7,k=3 -> 4 |
| 32 | + assertEquals(5, Josephus.getJosephus(10, 2)); // n=10,k=2 -> 5 |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void testLargerKnown() { |
| 37 | + // Known classic example |
| 38 | + assertEquals(28, Josephus.getJosephus(40, 3)); // classic result |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void testVariousKValues() { |
| 43 | + assertEquals(1, Josephus.getJosephus(2, 2)); // persons 1..2, k=2 -> survivor 1 |
| 44 | + assertEquals(2, Josephus.getJosephus(2, 1)); // k=1 eliminates in order -> last is 2 |
| 45 | + assertEquals(4, Josephus.getJosephus(8, 3)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void testLargeNPerformance() { |
| 50 | + // sanity for large n: should complete quickly (recursive depth = n) |
| 51 | + int survivor = Josephus.getJosephus(1000, 7); |
| 52 | + assertTrue(survivor >= 1 && survivor <= 1000); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testInvalidInputs() { |
| 57 | + assertThrows(IllegalArgumentException.class, () -> Josephus.getJosephus(0, 3)); |
| 58 | + assertThrows(IllegalArgumentException.class, () -> Josephus.getJosephus(-5, 2)); |
| 59 | + assertThrows(IllegalArgumentException.class, () -> Josephus.getJosephus(5, 0)); |
| 60 | + assertThrows(IllegalArgumentException.class, () -> Josephus.getJosephus(5, -1)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testPrintJosephus() { |
| 65 | + // Capture System.out |
| 66 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 67 | + PrintStream originalOut = System.out; |
| 68 | + System.setOut(new PrintStream(outputStream)); |
| 69 | + |
| 70 | + try { |
| 71 | + Josephus.printJosephus(7, 3); // known survivor 4 |
| 72 | + String output = outputStream.toString().trim(); |
| 73 | + // output should contain just the survivor number (since printJosephus prints only the number) |
| 74 | + assertEquals("4", output); |
| 75 | + } finally { |
| 76 | + System.setOut(originalOut); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void testMainMethodContainsDemo() { |
| 82 | + // Capture System.out |
| 83 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 84 | + PrintStream originalOut = System.out; |
| 85 | + System.setOut(new PrintStream(outputStream)); |
| 86 | + |
| 87 | + try { |
| 88 | + Josephus.main(new String[] {}); |
| 89 | + String output = outputStream.toString(); |
| 90 | + assertTrue(output.contains("Josephus problem demo:")); |
| 91 | + assertTrue(output.contains("Survivor (1-based position):")); |
| 92 | + } finally { |
| 93 | + System.setOut(originalOut); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments