99
1010public class TowerOfHanoiTest {
1111
12- /**
13- * Test Case 1: Base Case (n = 1)
14- * Logic: With only 1 disk, it should move directly from Source (A) to Destination (C).
15- */
1612 @ Test
1713 public void testBaseCase () {
1814 List <String > result = TowerOfHanoi .solveTowerOfHanoi (1 , 'A' , 'C' , 'B' );
19-
20- // Assertion 1: Check size (2^1 - 1 = 1 move)
2115 assertEquals (1 , result .size (), "Should have exactly 1 move for 1 disk" );
22-
23- // Assertion 2: Verify the exact string
2416 assertEquals ("Move disk 1 from rod A to rod C" , result .get (0 ));
2517 }
2618
27- /**
28- * Test Case 2: Small Recursion (n = 2)
29- * Logic: Tests the basic recursive step (Move 1 to Aux, Move 2 to Dest, Move 1 to Dest).
30- */
3119 @ Test
3220 public void testSmallRecursion () {
3321 List <String > result = TowerOfHanoi .solveTowerOfHanoi (2 , 'A' , 'C' , 'B' );
34-
35- // Assertion 1: Check size (2^2 - 1 = 3 moves)
3622 assertEquals (3 , result .size ());
37-
38- // Assertion 2: Verify the exact sequence of moves
39- List <String > expected = Arrays .asList ("Move disk 1 from rod A to rod B" , // Small disk to Aux
40- "Move disk 2 from rod A to rod C" , // Big disk to Dest
41- "Move disk 1 from rod B to rod C" // Small disk to Dest
23+ List <String > expected = Arrays .asList (
24+ "Move disk 1 from rod A to rod B" ,
25+ "Move disk 2 from rod A to rod C" ,
26+ "Move disk 1 from rod B to rod C"
4227 );
43-
4428 assertEquals (expected , result , "Sequence of moves for 2 disks is incorrect" );
4529 }
4630
47- /**
48- * Test Case 3: Standard Case (n = 3)
49- * Logic: Verifies a slightly larger recursion to ensure the stack depth works correctly.
50- */
5131 @ Test
5232 public void testStandardCase () {
5333 List <String > result = TowerOfHanoi .solveTowerOfHanoi (3 , 'A' , 'C' , 'B' );
54-
55- // Assertion 1: Check size (2^3 - 1 = 7 moves)
5634 assertEquals (7 , result .size ());
57-
58- // Assertion 2: Verify start and end moves specifically
59- assertEquals ("Move disk 1 from rod A to rod C" , result .get (0 )); // First move
60- assertEquals ("Move disk 1 from rod A to rod C" , result .get (6 )); // Last move
35+ assertEquals ("Move disk 1 from rod A to rod C" , result .get (0 ));
36+ assertEquals ("Move disk 1 from rod A to rod C" , result .get (6 ));
6137 }
6238
63- /**
64- * Illegal Input Case: Negative Input
65- * Logic: Ensures the exception is thrown when n is negative.
66- */
6739 @ Test
6840 public void testNegativeInput () {
69- assertThrows (IllegalArgumentException .class , () -> { TowerOfHanoi .solveTowerOfHanoi (-5 , 'A' , 'C' , 'B' ); } , "Should throw exception for negative disks" );
41+ assertThrows (IllegalArgumentException .class , () -> TowerOfHanoi .solveTowerOfHanoi (-5 , 'A' , 'C' , 'B' ), "Should throw exception for negative disks" );
7042 }
7143}
0 commit comments