Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
230 additions
and
22 deletions.
There are no files selected for viewing
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
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
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
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
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
27 changes: 23 additions & 4 deletions
27
src/main/java/com/creationline/sudoku/validator/HorizontalGroup.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,39 @@ | ||
package com.creationline.sudoku.validator; | ||
|
||
import java.util.Objects; | ||
import java.util.stream.IntStream; | ||
|
||
/** | ||
* @author Kohsuke Kawaguchi | ||
*/ | ||
class HorizontalGroup extends Group { | ||
final int x; | ||
final int y; | ||
|
||
public HorizontalGroup(Board board, int x) { | ||
public HorizontalGroup(Board board, int y) { | ||
super(board); | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
@Override | ||
public IntStream cells() { | ||
return IntStream.range(0,9).map(y -> board.get(x, y)); | ||
return IntStream.range(0,9).map(x -> board.get(x,y)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName()+"@"+y; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
HorizontalGroup that = (HorizontalGroup) o; | ||
return y == that.y; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(y); | ||
} | ||
} |
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
27 changes: 23 additions & 4 deletions
27
src/main/java/com/creationline/sudoku/validator/VerticalGroup.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,39 @@ | ||
package com.creationline.sudoku.validator; | ||
|
||
import java.util.Objects; | ||
import java.util.stream.IntStream; | ||
|
||
/** | ||
* @author Kohsuke Kawaguchi | ||
*/ | ||
class VerticalGroup extends Group { | ||
final int y; | ||
final int x; | ||
|
||
public VerticalGroup(Board board, int y) { | ||
public VerticalGroup(Board board, int x) { | ||
super(board); | ||
this.y = y; | ||
this.x = x; | ||
} | ||
|
||
@Override | ||
public IntStream cells() { | ||
return IntStream.range(0,9).map(x -> board.get(x,y)); | ||
return IntStream.range(0,9).map(y -> board.get(x, y)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName()+"@"+x; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
VerticalGroup that = (VerticalGroup) o; | ||
return x == that.x; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(x); | ||
} | ||
} |
88 changes: 83 additions & 5 deletions
88
src/test/java/com/creationline/sudoku/validator/AppTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,98 @@ | ||
package com.creationline.sudoku.validator; | ||
|
||
import static java.util.Collections.*; | ||
import static java.util.stream.Collectors.*; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Unit test for simple App. | ||
* What needs to be covered in these tests? | ||
* | ||
* | ||
*/ | ||
public class AppTest | ||
{ | ||
/** | ||
* Rigorous Test :-) | ||
* Simplest form of the test that should pass. | ||
*/ | ||
@Test | ||
public void testSanity() { | ||
var b = Board.read(new String[]{ | ||
"53..7....", | ||
"6..195...", | ||
".98....6.", | ||
"8...6...3", | ||
"4..8.3..1", | ||
"7...2...6", | ||
".6....28.", | ||
"...419..5", | ||
"....8..79" | ||
}); | ||
|
||
Assert.assertEquals(b.findInconsistencies().collect(toList()), emptyList()); | ||
} | ||
|
||
/** | ||
* Intent: make sure inconsistency in every row is separately checked. | ||
*/ | ||
@Test | ||
public void rowCheck() { | ||
for (int y=0; y<9; y++) { | ||
var b = board( | ||
repeat(y,"........."), | ||
".1. ... .1.", | ||
repeat(9-y-1,".........")); | ||
|
||
assertInconsistenciesOf(b, | ||
new Inconsistency(new HorizontalGroup(b, y), 1)); | ||
} | ||
} | ||
|
||
/** | ||
* Intent: make sure inconsistency in every column is separately checked. | ||
*/ | ||
@Test | ||
public void shouldAnswerWithTrue() | ||
{ | ||
assertTrue( true ); | ||
public void columnCheck() { | ||
for (int y=0; y<9; y++) { | ||
var b = board( | ||
repeat(y,"........."), | ||
".1. ... .1.", | ||
repeat(9-y-1,".........")); | ||
b = b.flipDiagonal(); | ||
|
||
assertInconsistenciesOf(b, | ||
new Inconsistency(new VerticalGroup(b, y), 1)); | ||
} | ||
} | ||
|
||
void assertInconsistenciesOf(Board b, Inconsistency... expected) { | ||
Assert.assertEquals( | ||
List.of(expected), | ||
b.findInconsistencies().collect(toList()) | ||
); | ||
} | ||
|
||
Board board(Object... rows) { | ||
var lines = new ArrayList<String>(); | ||
for (var row : rows) { | ||
if (row instanceof String) | ||
lines.add((String) row); | ||
if (row instanceof String[]) | ||
lines.addAll(Arrays.asList((String[])row)); | ||
} | ||
return Board.read(lines.toArray(new String[0])); | ||
} | ||
|
||
String[] repeat(int n, String line) { | ||
var lines = new String[n]; | ||
for (int i=0; i<n; i++) | ||
lines[i] = line; | ||
return lines; | ||
} | ||
} |