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