public
Created

Sudoku Basic unit tests

  • Download Gist
Board.java
Java
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
package sudoku;
 
import static org.junit.Assert.*;
 
import java.util.Arrays;
 
import org.junit.Before;
import org.junit.Test;
 
public class BoardTest {
 
private int[][] sudoku = {
{8,6,0,0,2,0,0,0,0},
{0,0,0,7,0,0,0,5,9},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,6,0,8,0,0},
{0,4,0,0,0,0,0,0,0},
{0,0,5,3,0,0,0,0,7},
{0,0,0,0,0,0,0,0,0},
{0,2,0,0,0,0,6,0,0},
{0,0,7,5,0,9,0,0,0}
};
private Board board;
@Before
public void initBoard() {
board = new Board(sudoku);
}
 
@Test
public void testGetRow() {
int[] row5 = {0,0,5,3,0,0,0,0,7};
assertTrue(Arrays.equals(row5, board.getRow(5)));
}
 
@Test
public void testGetColumn() {
int[] col5 = {0,0,0,0,0,0,0,0,9};
assertTrue(Arrays.equals(col5, board.getColumn(5)));
}
 
@Test
public void testGetRegion() {
int[] region3 = {0,0,0,0,5,9,0,0,0};
assertTrue(Arrays.equals(region3, board.getRegion(2, 8)));
}
 
@Test
public void testGetCell() {
assertEquals(4,board.getCell(4, 1));
}
 
@Test
public void testSetCell() {
board.setCell(4, 1, 2);
assertEquals(2,board.getCell(4, 1));
}
 
@Test
public void testToString() {
final String expectedOutput =
" -------------------------\n" +
" | 8 6 _ | _ 2 _ | _ _ _ |\n" +
" | _ _ _ | 7 _ _ | _ 5 9 |\n" +
" | _ _ _ | _ _ _ | _ _ _ |\n" +
" -------------------------\n" +
" | _ _ _ | _ 6 _ | 8 _ _ |\n" +
" | _ 4 _ | _ _ _ | _ _ _ |\n" +
" | _ _ 5 | 3 _ _ | _ _ 7 |\n" +
" -------------------------\n" +
" | _ _ _ | _ _ _ | _ _ _ |\n" +
" | _ 2 _ | _ _ _ | 6 _ _ |\n" +
" | _ _ 7 | 5 _ 9 | _ _ _ |\n" +
" -------------------------\n";
assertEquals(expectedOutput, board.toString());
}
}
Solver.java
Java
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
package sudoku;
 
import static org.junit.Assert.*;
 
import java.util.Arrays;
 
import org.junit.Before;
import org.junit.Test;
 
public class SolverTest {
 
private int[][] sudoku = {
{8,6,0,0,2,0,0,0,0},
{0,0,0,7,0,0,0,5,9},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,6,0,8,0,0},
{0,4,0,0,0,0,0,0,0},
{0,0,5,3,0,0,0,0,7},
{0,0,0,0,0,0,0,0,0},
{0,2,0,0,0,0,6,0,0},
{0,0,7,5,0,9,0,0,0}
};
private int[] sortedLine = {1,2,3,4,5,6,7,8,9};
private Solver solver;
 
@Before
public void initSolver() {
solver = new Solver(sudoku);
}
@Test
public void testSolve() {
solver.solve();
verifySolved(sudoku);
}
@Test
public void testGetBoard() {
Board b = solver.getBoard();
for (int i=0;i < Board.GRID_SIZE; i++) {
assertArrayEquals(sudoku[i], b.getRow(i));
}
}
private void verifySolved(int[][] toVerify) {
Board b = new Board(toVerify);
// Check all lines and columns
for (int i=0;i < Board.GRID_SIZE; i++) {
assertTrue(isArrayOk(b.getRow(i)));
assertTrue(isArrayOk(b.getColumn(i)));
}
// Check all regions
for (int x=0; x < Board.REGION_SIZE; x++)
for (int y=0; y < Board.REGION_SIZE; y++) {
int row = x*Board.REGION_SIZE;
int col = y*Board.REGION_SIZE;
assertTrue(isArrayOk(b.getRegion(row, col)));
}
}
private boolean isArrayOk(int[] toVerify) {
int[] copy = Arrays.copyOf(toVerify, Board.GRID_SIZE);
Arrays.sort(copy);
return Arrays.equals(sortedLine, copy);
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.