ue6
This commit is contained in:
65
uebung06/test/sudoku/FieldTest.java
Normal file
65
uebung06/test/sudoku/FieldTest.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package uebung06.test.sudoku;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import sudoku.*;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class FieldTest {
|
||||
private static final int X = 8;
|
||||
private static final int Y = 5;
|
||||
private static final int NUL = -1;
|
||||
private static final Value VAL = Value.EIGHT;
|
||||
private static Sudoku sudoku;
|
||||
private static Field field;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
sudoku = new Sudoku();
|
||||
field = new Field(sudoku, X, Y);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void illegalXLowerBoundFieldTest() {
|
||||
field = new Field(sudoku, NUL, Y);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void illegalXUpperBoundFieldTest() {
|
||||
field = new Field(sudoku, Sudoku.SIZE, Y);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void illegalYLowerBoundFieldTest() {
|
||||
field = new Field(sudoku, X, NUL);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void illegalYUpperBoundFieldTest() {
|
||||
field = new Field(sudoku, X, Sudoku.SIZE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void valueFieldTest() {
|
||||
field.setValue(VAL);
|
||||
assertEquals(field.getValue(), VAL);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearValueFieldTest() {
|
||||
field.setValue(VAL);
|
||||
field.clearValue();
|
||||
assertNull(field.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isValueEmptyFieldTest() {
|
||||
field.setValue(VAL);
|
||||
field.clearValue();
|
||||
assertTrue(field.isEmpty());
|
||||
}
|
||||
}
|
129
uebung06/test/sudoku/SudokuAppTest.java
Normal file
129
uebung06/test/sudoku/SudokuAppTest.java
Normal file
@@ -0,0 +1,129 @@
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user