Skip to content

Commit 73c938d

Browse files
Fixed minor issues in test file
1 parent 3bf37fd commit 73c938d

File tree

2 files changed

+8
-36
lines changed

2 files changed

+8
-36
lines changed

src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ private static void moveDisks(int n, char source, char destination, char auxilia
6161
moves.add("Move disk " + n + " from rod " + source + " to rod " + destination);
6262
moveDisks(n - 1, auxiliary, destination, source, moves);
6363
}
64-
}
64+
}

src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,63 +9,35 @@
99

1010
public 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

Comments
 (0)