11package com .thealgorithms .recursion ;
22
3-
43import static org .junit .jupiter .api .Assertions .assertEquals ;
54import static org .junit .jupiter .api .Assertions .assertThrows ;
65
@@ -12,16 +11,15 @@ public class TowerOfHanoiTest {
1211
1312 /**
1413 * Test Case 1: Base Case (n = 1)
15- * Requirement: Explicitly requested.
1614 * Logic: With only 1 disk, it should move directly from Source (A) to Destination (C).
1715 */
1816 @ Test
1917 public void testBaseCase () {
2018 List <String > result = TowerOfHanoi .solveTowerOfHanoi (1 , 'A' , 'C' , 'B' );
21-
19+
2220 // Assertion 1: Check size (2^1 - 1 = 1 move)
2321 assertEquals (1 , result .size (), "Should have exactly 1 move for 1 disk" );
24-
22+
2523 // Assertion 2: Verify the exact string
2624 assertEquals ("Move disk 1 from rod A to rod C" , result .get (0 ));
2725 }
@@ -33,16 +31,16 @@ public void testBaseCase() {
3331 @ Test
3432 public void testSmallRecursion () {
3533 List <String > result = TowerOfHanoi .solveTowerOfHanoi (2 , 'A' , 'C' , 'B' );
36-
34+
3735 // Assertion 1: Check size (2^2 - 1 = 3 moves)
3836 assertEquals (3 , result .size ());
39-
37+
4038 // Assertion 2: Verify the exact sequence of moves
4139 List <String > expected = Arrays .asList ("Move disk 1 from rod A to rod B" , // Small disk to Aux
4240 "Move disk 2 from rod A to rod C" , // Big disk to Dest
4341 "Move disk 1 from rod B to rod C" // Small disk to Dest
4442 );
45-
43+
4644 assertEquals (expected , result , "Sequence of moves for 2 disks is incorrect" );
4745 }
4846
@@ -53,22 +51,21 @@ public void testSmallRecursion() {
5351 @ Test
5452 public void testStandardCase () {
5553 List <String > result = TowerOfHanoi .solveTowerOfHanoi (3 , 'A' , 'C' , 'B' );
56-
54+
5755 // Assertion 1: Check size (2^3 - 1 = 7 moves)
5856 assertEquals (7 , result .size ());
59-
57+
6058 // Assertion 2: Verify start and end moves specifically
6159 assertEquals ("Move disk 1 from rod A to rod C" , result .get (0 )); // First move
6260 assertEquals ("Move disk 1 from rod A to rod C" , result .get (6 )); // Last move
6361 }
6462
6563 /**
66- * Extra Test Case: Negative Input
67- * Logic: Ensures your "Defensive Programming" check works .
64+ * Illegal Input Case: Negative Input
65+ * Logic: Ensures the exception is thrown when n is negative .
6866 */
6967 @ Test
7068 public void testNegativeInput () {
71- assertThrows (IllegalArgumentException .class , () -> { TowerOfHanoi .solveTowerOfHanoi (-5 , 'A' , 'C' , 'B' ); },
72- "Should throw exception for negative disks" );
69+ assertThrows (IllegalArgumentException .class , () -> { TowerOfHanoi .solveTowerOfHanoi (-5 , 'A' , 'C' , 'B' ); }, "Should throw exception for negative disks" );
7370 }
7471}
0 commit comments