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

Tue, 28 Jul 2020 14:45:06 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Jul 2020 14:45:06 +0200
changeset 24
c042490f3aef
parent 23
07b9adaed78e
permissions
-rw-r--r--

adds target/ to .hgignore

package de.uapcore.sudoku;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class SolverTest {

    private int[] createTestdata() {
        return new int[]{
                0,1,0,9,0,0,8,0,0,
                0,0,0,0,0,8,0,0,4,
                6,0,5,0,0,0,7,0,0,
                0,9,0,0,6,0,0,0,8,
                0,0,0,2,0,7,0,0,0,
                8,0,0,0,3,0,0,6,0,
                0,0,2,0,0,0,5,0,3,
                1,0,0,4,0,0,0,0,0,
                0,0,6,0,0,2,0,1,0};
    }

    private int[] createTestdataSolved() {
        return new int[]{
                4,1,7,9,2,5,8,3,6,
                3,2,9,6,7,8,1,5,4,
                6,8,5,3,4,1,7,9,2,
                2,9,1,5,6,4,3,7,8,
                5,6,3,2,8,7,9,4,1,
                8,7,4,1,3,9,2,6,5,
                9,4,2,7,1,6,5,8,3,
                1,5,8,4,9,3,6,2,7,
                7,3,6,8,5,2,4,1,9};
    }

    private Field createTestField(int[] testdata) {
        final var field = new Field();
        for (int x = 0 ; x < 9 ; x++) {
            for (int y = 0 ; y < 9 ; y++) {
                field.setCellValue(x, y, testdata[x+9*y]);
            }
        }
        return field;
    }


    @Test
    void solveSuccess() {
        final var field = createTestField(createTestdata());
        final var solvedField = createTestField(createTestdataSolved());
        assertTrue(new Solver().solve(field));
        for (int x = 0 ; x < 9 ; x++) {
            for (int y = 0 ; y < 9 ; y++) {
                assertEquals(solvedField.getCellValue(x, y), field.getCellValue(x, y));
            }
        }
    }

    @Test
    void solvingSetsModifiedStates() {
        final var originalField = createTestField(createTestdata());
        final var field = createTestField(createTestdata());
        assertTrue(new Solver().solve(field));
        for (int x = 0 ; x < 9 ; x++) {
            for (int y = 0 ; y < 9 ; y++) {
                if (originalField.isCellEmpty(x, y)) {
                    assertTrue(field.isCellModified(x, y));
                } else {
                    assertFalse(field.isCellModified(x, y));
                }
            }
        }
    }

    @Test
    void checkSuccess() {
        final var field = createTestField(createTestdata());
        assertTrue(new Solver().check(field));
        assertFalse(field.isAnyCellModified());
    }

    @Test
    void solveFail() {
        final var originalField = createTestField(createTestdata());
        final var field = createTestField(createTestdata());
        originalField.setCellValue(0, 3, 5);
        field.setCellValue(0, 3, 5);
        assertTrue(new Solver().check(field));
        assertFalse(new Solver().solve(field));
        for (int x = 0 ; x < 9 ; x++) {
            for (int y = 0 ; y < 9 ; y++) {
                if (originalField.isCellEmpty(x, y)) {
                    assertTrue(field.isCellEmpty(x, y));
                } else {
                    assertFalse(field.isCellEmpty(x, y));
                    assertFalse(field.isCellModified(x, y));
                }
            }
        }
    }

    @Test
    void checkFail() {
        final var field = createTestField(createTestdata());
        field.setCellValue(4, 6, 4);
        assertFalse(new Solver().check(field));
        assertFalse(field.isAnyCellModified());
    }
}

mercurial