Created
October 20, 2011 23:58
-
-
Save vincentg/1302745 to your computer and use it in GitHub Desktop.
Sudoku Basic unit tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment