mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
Create package prime, matrix and games (#6139)
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SudokuTest {
|
||||
|
||||
@Test
|
||||
void testIsSafe2() {
|
||||
int[][] board = {{3, 0, 6, 5, 0, 8, 4, 0, 0}, {5, 2, 0, 0, 0, 0, 0, 0, 0}, {0, 8, 7, 0, 0, 0, 0, 3, 1}, {0, 0, 3, 0, 1, 0, 0, 8, 0}, {9, 0, 0, 8, 6, 3, 0, 0, 5}, {0, 5, 0, 0, 9, 0, 6, 0, 0}, {1, 3, 0, 0, 0, 0, 2, 5, 0}, {0, 0, 0, 0, 0, 0, 0, 7, 4}, {0, 0, 5, 2, 0, 6, 3, 0, 0}};
|
||||
|
||||
assertFalse(Sudoku.isSafe(board, 0, 1, 3));
|
||||
assertTrue(Sudoku.isSafe(board, 1, 2, 1));
|
||||
assertThrows(ArrayIndexOutOfBoundsException.class, () -> { Sudoku.isSafe(board, 10, 10, 5); });
|
||||
assertThrows(ArrayIndexOutOfBoundsException.class, () -> { Sudoku.isSafe(board, -1, 0, 5); });
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSolveSudoku() {
|
||||
int[][] board = {{3, 0, 6, 5, 0, 8, 4, 0, 0}, {5, 2, 0, 0, 0, 0, 0, 0, 0}, {0, 8, 7, 0, 0, 0, 0, 3, 1}, {0, 0, 3, 0, 1, 0, 0, 8, 0}, {9, 0, 0, 8, 6, 3, 0, 0, 5}, {0, 5, 0, 0, 9, 0, 6, 0, 0}, {1, 3, 0, 0, 0, 0, 2, 5, 0}, {0, 0, 0, 0, 0, 0, 0, 7, 4}, {0, 0, 5, 2, 0, 6, 3, 0, 0}};
|
||||
|
||||
assertTrue(Sudoku.solveSudoku(board, board.length));
|
||||
assertEquals(1, board[0][1]);
|
||||
assertThrows(ArrayIndexOutOfBoundsException.class, () -> { Sudoku.solveSudoku(board, 10); });
|
||||
assertTrue(Sudoku.solveSudoku(board, -1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUnsolvableSudoku() {
|
||||
int[][] unsolvableBoard = {{5, 1, 6, 8, 4, 9, 7, 3, 2}, {3, 0, 7, 6, 0, 5, 0, 0, 0}, {8, 0, 9, 7, 0, 0, 0, 6, 5}, {1, 3, 5, 0, 6, 0, 9, 0, 7}, {4, 7, 2, 5, 9, 1, 0, 0, 6}, {9, 6, 8, 3, 7, 0, 0, 5, 0}, {2, 5, 3, 1, 8, 6, 0, 7, 4}, {6, 8, 4, 2, 5, 7, 3, 9, 0}, {7, 9, 1, 4, 3, 0, 5, 0, 0}};
|
||||
|
||||
assertFalse(Sudoku.solveSudoku(unsolvableBoard, unsolvableBoard.length));
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TowerOfHanoiTest {
|
||||
|
||||
@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 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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user