-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Expand file tree
/
Copy pathTowerOfHanoiTest.java
More file actions
39 lines (32 loc) · 1.47 KB
/
TowerOfHanoiTest.java
File metadata and controls
39 lines (32 loc) · 1.47 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
package com.thealgorithms.recursion;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
public class TowerOfHanoiTest {
@Test
public void testBaseCase() {
List<String> result = TowerOfHanoi.solveTowerOfHanoi(1, 'A', 'C', 'B');
assertEquals(1, result.size(), "Should have exactly 1 move for 1 disk");
assertEquals("Move disk 1 from rod A to rod C", result.get(0));
}
@Test
public void testSmallRecursion() {
List<String> result = TowerOfHanoi.solveTowerOfHanoi(2, 'A', 'C', 'B');
assertEquals(3, result.size());
List<String> expected = Arrays.asList("Move disk 1 from rod A to rod B", "Move disk 2 from rod A to rod C", "Move disk 1 from rod B to rod C");
assertEquals(expected, result, "Sequence of moves for 2 disks is incorrect");
}
@Test
public void testStandardCase() {
List<String> result = TowerOfHanoi.solveTowerOfHanoi(3, 'A', 'C', 'B');
assertEquals(7, result.size());
assertEquals("Move disk 1 from rod A to rod C", result.get(0));
assertEquals("Move disk 1 from rod A to rod C", result.get(6));
}
@Test
public void testNegativeInput() {
assertThrows(IllegalArgumentException.class, () -> { TowerOfHanoi.solveTowerOfHanoi(-5, 'A', 'C', 'B'); }, "Should throw exception for negative disks");
}
}