Skip to content

Commit c0dc63b

Browse files
committed
Add unit tests for ToH
1 parent ca10371 commit c0dc63b

File tree

2 files changed

+66
-22
lines changed

2 files changed

+66
-22
lines changed

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

+6-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.thealgorithms.others;
22

3-
import java.util.Scanner;
3+
import java.util.List;
44

55
/**
66
* The {@code TowerOfHanoi} class provides functionality to solve the Tower of
@@ -32,32 +32,16 @@ private TowerOfHanoi() {
3232
* intermediate pole to the end pole.
3333
* </p>
3434
*/
35-
public static void shift(int n, String startPole, String intermediatePole, String endPole) {
36-
// If n becomes zero, the program returns, ending the recursion.
35+
public static void shift(int n, String startPole, String intermediatePole, String endPole, List<String> result) {
3736
if (n != 0) {
3837
// Recursively move n-1 discs from startPole to intermediatePole
39-
shift(n - 1, startPole, endPole, intermediatePole);
38+
shift(n - 1, startPole, endPole, intermediatePole, result);
4039

41-
// Print the move of the nth disc from startPole to endPole
42-
System.out.format("Move %d from %s to %s%n", n, startPole, endPole);
40+
// Add the move of the nth disc from startPole to endPole
41+
result.add(String.format("Move %d from %s to %s", n, startPole, endPole));
4342

4443
// Recursively move the n-1 discs from intermediatePole to endPole
45-
shift(n - 1, intermediatePole, startPole, endPole);
44+
shift(n - 1, intermediatePole, startPole, endPole, result);
4645
}
4746
}
48-
49-
/**
50-
* The main method that starts the Tower of Hanoi puzzle solution.
51-
* It prompts the user for the number of discs and invokes the {@code shift}
52-
* function to begin solving the puzzle.
53-
*
54-
* @param args Command-line arguments (not used in this case).
55-
*/
56-
public static void main(String[] args) {
57-
System.out.print("Enter number of discs on Pole 1: ");
58-
Scanner scanner = new Scanner(System.in);
59-
int numberOfDiscs = scanner.nextInt(); // input of number of discs on pole 1
60-
shift(numberOfDiscs, "Pole1", "Pole2", "Pole3"); // Shift function called
61-
scanner.close();
62-
}
6347
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.thealgorithms.others;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class TowerOfHanoiTest {
9+
10+
@Test
11+
public void testHanoiWithOneDisc() {
12+
List<String> result = new ArrayList<>();
13+
TowerOfHanoi.shift(1, "Pole1", "Pole2", "Pole3", result);
14+
15+
// Expected output for 1 disc
16+
List<String> expected = List.of("Move 1 from Pole1 to Pole3");
17+
assertEquals(expected, result);
18+
}
19+
20+
@Test
21+
public void testHanoiWithTwoDiscs() {
22+
List<String> result = new ArrayList<>();
23+
TowerOfHanoi.shift(2, "Pole1", "Pole2", "Pole3", result);
24+
25+
// Expected output for 2 discs
26+
List<String> expected = List.of(
27+
"Move 1 from Pole1 to Pole2",
28+
"Move 2 from Pole1 to Pole3",
29+
"Move 1 from Pole2 to Pole3"
30+
);
31+
assertEquals(expected, result);
32+
}
33+
34+
@Test
35+
public void testHanoiWithThreeDiscs() {
36+
List<String> result = new ArrayList<>();
37+
TowerOfHanoi.shift(3, "Pole1", "Pole2", "Pole3", result);
38+
39+
// Expected output for 3 discs
40+
List<String> expected = List.of(
41+
"Move 1 from Pole1 to Pole3",
42+
"Move 2 from Pole1 to Pole2",
43+
"Move 1 from Pole3 to Pole2",
44+
"Move 3 from Pole1 to Pole3",
45+
"Move 1 from Pole2 to Pole1",
46+
"Move 2 from Pole2 to Pole3",
47+
"Move 1 from Pole1 to Pole3"
48+
);
49+
assertEquals(expected, result);
50+
}
51+
52+
@Test
53+
public void testHanoiWithZeroDiscs() {
54+
List<String> result = new ArrayList<>();
55+
TowerOfHanoi.shift(0, "Pole1", "Pole2", "Pole3", result);
56+
57+
// There should be no moves if there are 0 discs
58+
assertTrue(result.isEmpty());
59+
}
60+
}

0 commit comments

Comments
 (0)