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