130 lines
4.2 KiB
Java
130 lines
4.2 KiB
Java
package uebung06.test.sudoku;
|
|
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
|
|
import sudoku.*;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.stream.IntStream;
|
|
|
|
import static org.junit.Assert.assertFalse;
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
public class SudokuAppTest {
|
|
private static final int MAX = 81;
|
|
private static final int MIN = 0;
|
|
private static final int SUDOKU_SIZE = 9;
|
|
private static Sudoku board;
|
|
private static int[] init;
|
|
private static int[] wrongSol;
|
|
|
|
@Before
|
|
public void init() {
|
|
board = new Sudoku();
|
|
init = IntStream.of(4, 5, 0, 0, 0, 0, 2, 0, 0,
|
|
6, 0, 0, 0, 2, 4, 8, 0, 0,
|
|
8, 0, 0, 0, 6, 1, 3, 0, 0,
|
|
0, 9, 0, 4, 0, 0, 0, 5, 0,
|
|
0, 1, 0, 2, 0, 8, 0, 7, 0,
|
|
0, 3, 0, 0, 0, 9, 0, 8, 0,
|
|
0, 0, 7, 1, 4, 0, 0, 0, 8,
|
|
0, 0, 2, 7, 9, 0, 0, 0, 6,
|
|
0, 0, 5, 0, 0, 0, 0, 2, 1).toArray();
|
|
wrongSol = IntStream.of(4, 5, 1, 8, 7, 3, 2, 6, 9,
|
|
6, 7, 3, 9, 2, 4, 8, 1, 5,
|
|
8, 2, 9, 5, 6, 1, 3, 4, 7,
|
|
2, 9, 8, 4, 1, 7, 6, 5, 3,
|
|
5, 1, 6, 2, 3, 8, 9, 7, 4,
|
|
7, 3, 4, 6, 5, 9, 1, 8, 2,
|
|
3, 6, 7, 1, 4, 2, 5, 9, 8,
|
|
1, 8, 2, 7, 9, 5, 4, 3, 6,
|
|
9, 5, 4, 3, 8, 6, 7, 2, 1).toArray();
|
|
}
|
|
|
|
//Testing first If-condition in Sudoku.init
|
|
@Test(expected = IllegalArgumentException.class)
|
|
public void illegalLowerBoundInitSudokuAppTest() {
|
|
board.initialize(IntStream.range(MIN, MIN+1).toArray());
|
|
}
|
|
|
|
//Testing first If-condition in Sudoku.init
|
|
@Test(expected = IllegalArgumentException.class)
|
|
public void illegalUpperBoundInitSudokuAppTest() {
|
|
board.initialize(IntStream.range(MIN, MAX+1).toArray());
|
|
}
|
|
|
|
//Testing ArrayIndex of Value
|
|
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
|
public void illegalArrayIndexOutOfBoundInitSudokuAppTest() {
|
|
board.initialize(IntStream.range(MIN, MAX).toArray());
|
|
}
|
|
|
|
//Testing Sudoku.solve()
|
|
@Test
|
|
public void correctSolutionSudokuAppTest() {
|
|
board.initialize(init);
|
|
board.solve();
|
|
|
|
assertTrue(verifySudokuSol());
|
|
}
|
|
|
|
//Just testing verifySudokuSol-Method
|
|
@Test
|
|
public void wrongSolutionSudokuAppTest() {
|
|
try {
|
|
board.initialize(wrongSol);
|
|
} catch (final IllegalArgumentException e) {
|
|
//do nothing, just handling throwing exceptions from Sudoku-class
|
|
}
|
|
|
|
assertFalse(verifySudokuSol());
|
|
}
|
|
|
|
public Boolean verifySudokuSol() {
|
|
HashSet<Value> rows = new HashSet<>();
|
|
HashSet<Value> cols = new HashSet<>();
|
|
|
|
HashMap<Integer, HashSet<Value>> blocks = new HashMap<>();
|
|
IntStream.range(0, SUDOKU_SIZE)
|
|
.forEach(block -> blocks.put(block, new HashSet<>())); //init Hashsets for every block
|
|
|
|
for (int y = 0; y < SUDOKU_SIZE; y++) {
|
|
for (int x = 0; x < SUDOKU_SIZE; x++) {
|
|
//check column
|
|
Value val = board.field(x, y).getValue();
|
|
if (!cols.add(val))
|
|
return false;
|
|
if (cols.size() == SUDOKU_SIZE)
|
|
cols.clear();
|
|
|
|
//check row
|
|
val = board.field(y, x).getValue();
|
|
if (!rows.add(val))
|
|
return false;
|
|
if (rows.size() == SUDOKU_SIZE)
|
|
rows.clear();
|
|
|
|
//check 3x3 block
|
|
val = board.field(x, y).getValue();
|
|
if (!blocks.get(getBlockIndex(x, y)).add(val))
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public int getBlockIndex(int x, int y) {
|
|
return getColIndex(x) + getRowIndex(y);
|
|
}
|
|
|
|
public int getColIndex(int x) {
|
|
return x / 3;
|
|
}
|
|
|
|
public int getRowIndex(int y) {
|
|
return (y/3)*3;
|
|
}
|
|
}
|