forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTowerOfHanoiTest.java
More file actions
82 lines (66 loc) · 3.03 KB
/
TowerOfHanoiTest.java
File metadata and controls
82 lines (66 loc) · 3.03 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.thealgorithms.puzzlesandgames;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public class TowerOfHanoiTest {
@ParameterizedTest
@MethodSource("diskCountAndMoveCount")
void testMoveCountMatchesFormula(int disks, int expectedMoves) {
List<String> result = new ArrayList<>();
TowerOfHanoi.shift(disks, "A", "B", "C", result);
assertEquals(expectedMoves, result.size());
}
private static Stream<Arguments> diskCountAndMoveCount() {
return Stream.of(Arguments.of(1, 1), Arguments.of(2, 3), Arguments.of(3, 7), Arguments.of(4, 15), Arguments.of(5, 31), Arguments.of(10, 1023));
}
@Test
public void testHanoiWithOneDisc() {
List<String> result = new ArrayList<>();
TowerOfHanoi.shift(1, "Pole1", "Pole2", "Pole3", result);
// Expected output for 1 disc
List<String> expected = List.of("Move 1 from Pole1 to Pole3");
assertEquals(expected, result);
}
@Test
public void testHanoiWithTwoDiscs() {
List<String> result = new ArrayList<>();
TowerOfHanoi.shift(2, "Pole1", "Pole2", "Pole3", result);
// Expected output for 2 discs
List<String> expected = List.of("Move 1 from Pole1 to Pole2", "Move 2 from Pole1 to Pole3", "Move 1 from Pole2 to Pole3");
assertEquals(expected, result);
}
@Test
public void testHanoiWithThreeDiscs() {
List<String> result = new ArrayList<>();
TowerOfHanoi.shift(3, "Pole1", "Pole2", "Pole3", result);
// Expected output for 3 discs
List<String> expected = List.of("Move 1 from Pole1 to Pole3", "Move 2 from Pole1 to Pole2", "Move 1 from Pole3 to Pole2", "Move 3 from Pole1 to Pole3", "Move 1 from Pole2 to Pole1", "Move 2 from Pole2 to Pole3", "Move 1 from Pole1 to Pole3");
assertEquals(expected, result);
}
@Test
public void testHanoiWithDifferentPoles() {
List<String> result = new ArrayList<>();
TowerOfHanoi.shift(2, "X", "Y", "Z", result);
List<String> expected = List.of("Move 1 from X to Y", "Move 2 from X to Z", "Move 1 from Y to Z");
assertEquals(expected, result);
}
@Test
public void testHanoiWithZeroDiscs() {
List<String> result = new ArrayList<>();
TowerOfHanoi.shift(0, "Pole1", "Pole2", "Pole3", result);
// There should be no moves if there are 0 discs
assertTrue(result.isEmpty());
}
@Test
public void testHanoiWithNegativeDiscsThrows() {
List<String> result = new ArrayList<>();
assertThrows(IllegalArgumentException.class, () -> TowerOfHanoi.shift(-1, "Pole1", "Pole2", "Pole3", result));
}
}