src/test/java/de/uapcore/sudoku/SolverTest.java

changeset 23
07b9adaed78e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/test/java/de/uapcore/sudoku/SolverTest.java	Tue Jul 28 14:44:48 2020 +0200
     1.3 @@ -0,0 +1,108 @@
     1.4 +package de.uapcore.sudoku;
     1.5 +
     1.6 +import org.junit.jupiter.api.Test;
     1.7 +
     1.8 +import static org.junit.jupiter.api.Assertions.*;
     1.9 +
    1.10 +class SolverTest {
    1.11 +
    1.12 +    private int[] createTestdata() {
    1.13 +        return new int[]{
    1.14 +                0,1,0,9,0,0,8,0,0,
    1.15 +                0,0,0,0,0,8,0,0,4,
    1.16 +                6,0,5,0,0,0,7,0,0,
    1.17 +                0,9,0,0,6,0,0,0,8,
    1.18 +                0,0,0,2,0,7,0,0,0,
    1.19 +                8,0,0,0,3,0,0,6,0,
    1.20 +                0,0,2,0,0,0,5,0,3,
    1.21 +                1,0,0,4,0,0,0,0,0,
    1.22 +                0,0,6,0,0,2,0,1,0};
    1.23 +    }
    1.24 +
    1.25 +    private int[] createTestdataSolved() {
    1.26 +        return new int[]{
    1.27 +                4,1,7,9,2,5,8,3,6,
    1.28 +                3,2,9,6,7,8,1,5,4,
    1.29 +                6,8,5,3,4,1,7,9,2,
    1.30 +                2,9,1,5,6,4,3,7,8,
    1.31 +                5,6,3,2,8,7,9,4,1,
    1.32 +                8,7,4,1,3,9,2,6,5,
    1.33 +                9,4,2,7,1,6,5,8,3,
    1.34 +                1,5,8,4,9,3,6,2,7,
    1.35 +                7,3,6,8,5,2,4,1,9};
    1.36 +    }
    1.37 +
    1.38 +    private Field createTestField(int[] testdata) {
    1.39 +        final var field = new Field();
    1.40 +        for (int x = 0 ; x < 9 ; x++) {
    1.41 +            for (int y = 0 ; y < 9 ; y++) {
    1.42 +                field.setCellValue(x, y, testdata[x+9*y]);
    1.43 +            }
    1.44 +        }
    1.45 +        return field;
    1.46 +    }
    1.47 +
    1.48 +
    1.49 +    @Test
    1.50 +    void solveSuccess() {
    1.51 +        final var field = createTestField(createTestdata());
    1.52 +        final var solvedField = createTestField(createTestdataSolved());
    1.53 +        assertTrue(new Solver().solve(field));
    1.54 +        for (int x = 0 ; x < 9 ; x++) {
    1.55 +            for (int y = 0 ; y < 9 ; y++) {
    1.56 +                assertEquals(solvedField.getCellValue(x, y), field.getCellValue(x, y));
    1.57 +            }
    1.58 +        }
    1.59 +    }
    1.60 +
    1.61 +    @Test
    1.62 +    void solvingSetsModifiedStates() {
    1.63 +        final var originalField = createTestField(createTestdata());
    1.64 +        final var field = createTestField(createTestdata());
    1.65 +        assertTrue(new Solver().solve(field));
    1.66 +        for (int x = 0 ; x < 9 ; x++) {
    1.67 +            for (int y = 0 ; y < 9 ; y++) {
    1.68 +                if (originalField.isCellEmpty(x, y)) {
    1.69 +                    assertTrue(field.isCellModified(x, y));
    1.70 +                } else {
    1.71 +                    assertFalse(field.isCellModified(x, y));
    1.72 +                }
    1.73 +            }
    1.74 +        }
    1.75 +    }
    1.76 +
    1.77 +    @Test
    1.78 +    void checkSuccess() {
    1.79 +        final var field = createTestField(createTestdata());
    1.80 +        assertTrue(new Solver().check(field));
    1.81 +        assertFalse(field.isAnyCellModified());
    1.82 +    }
    1.83 +
    1.84 +    @Test
    1.85 +    void solveFail() {
    1.86 +        final var originalField = createTestField(createTestdata());
    1.87 +        final var field = createTestField(createTestdata());
    1.88 +        originalField.setCellValue(0, 3, 5);
    1.89 +        field.setCellValue(0, 3, 5);
    1.90 +        assertTrue(new Solver().check(field));
    1.91 +        assertFalse(new Solver().solve(field));
    1.92 +        for (int x = 0 ; x < 9 ; x++) {
    1.93 +            for (int y = 0 ; y < 9 ; y++) {
    1.94 +                if (originalField.isCellEmpty(x, y)) {
    1.95 +                    assertTrue(field.isCellEmpty(x, y));
    1.96 +                } else {
    1.97 +                    assertFalse(field.isCellEmpty(x, y));
    1.98 +                    assertFalse(field.isCellModified(x, y));
    1.99 +                }
   1.100 +            }
   1.101 +        }
   1.102 +    }
   1.103 +
   1.104 +    @Test
   1.105 +    void checkFail() {
   1.106 +        final var field = createTestField(createTestdata());
   1.107 +        field.setCellValue(4, 6, 4);
   1.108 +        assertFalse(new Solver().check(field));
   1.109 +        assertFalse(field.isAnyCellModified());
   1.110 +    }
   1.111 +}

mercurial