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

changeset 22
06170a0be62a
child 25
569220009c54
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/test/java/de/uapcore/sudoku/FieldTest.java	Tue Jul 28 14:27:14 2020 +0200
     1.3 @@ -0,0 +1,154 @@
     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 FieldTest {
    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 Field createTestField() {
    1.26 +        final var field = new Field();
    1.27 +        final var testdata = createTestdata();
    1.28 +        for (int x = 0 ; x < 9 ; x++) {
    1.29 +            for (int y = 0 ; y < 9 ; y++) {
    1.30 +                field.setCellValue(x, y, testdata[x+9*y]);
    1.31 +            }
    1.32 +        }
    1.33 +        return field;
    1.34 +    }
    1.35 +
    1.36 +    @Test
    1.37 +    void getAndSetValues() {
    1.38 +        final var f = new Field();
    1.39 +        assertEquals(0, f.getCellValue(3, 4));
    1.40 +        assertTrue(f.isCellEmpty(3, 4));
    1.41 +        f.setCellValue(3, 4, 6);
    1.42 +        assertEquals(6, 3, 4);
    1.43 +        assertFalse(f.isCellEmpty(3, 4));
    1.44 +    }
    1.45 +
    1.46 +    @Test
    1.47 +    void setAllCellsModified() {
    1.48 +        final var f = new Field();
    1.49 +
    1.50 +        assertFalse(f.isAnyCellModified());
    1.51 +        f.setAllCellsModified(true);
    1.52 +        for (int x = 0 ; x < 9 ; x++) {
    1.53 +            for (int y = 0 ; y < 9 ; y++) {
    1.54 +                assertTrue(f.isCellModified(x, y));
    1.55 +            }
    1.56 +        }
    1.57 +
    1.58 +        f.setAllCellsModified(false);
    1.59 +        assertFalse(f.isAnyCellModified());
    1.60 +    }
    1.61 +
    1.62 +    @Test
    1.63 +    void isAnyCellModified() {
    1.64 +        final var f = new Field();
    1.65 +
    1.66 +        assertFalse(f.isAnyCellModified());
    1.67 +        f.setCellValue(3, 4, 6);
    1.68 +        assertFalse(f.isAnyCellModified());
    1.69 +        f.setCellModified(3, 4, true);
    1.70 +        assertTrue(f.isAnyCellModified());
    1.71 +    }
    1.72 +
    1.73 +    @Test
    1.74 +    void clear() {
    1.75 +        final var f = new Field();
    1.76 +        f.setCellValue(3, 4, 6);
    1.77 +        f.setCellValue(2, 5, 9);
    1.78 +        f.clear();
    1.79 +        for (int x = 0 ; x < 9 ; x++) {
    1.80 +            for (int y = 0 ; y < 9 ; y++) {
    1.81 +                assertTrue(f.isCellEmpty(x, y));
    1.82 +            }
    1.83 +        }
    1.84 +    }
    1.85 +
    1.86 +    @Test
    1.87 +    void getSquare() {
    1.88 +        final var field = createTestField();
    1.89 +
    1.90 +        final var square01 = new int[][]{
    1.91 +                new int[]{0,0,8},
    1.92 +                new int[]{9,0,0},
    1.93 +                new int[]{0,0,0}};
    1.94 +
    1.95 +        final var square01actual = field.getSquare(0, 1);
    1.96 +        assertEquals(3, square01actual.length);
    1.97 +        for (int i = 0 ; i < 3 ; i++) {
    1.98 +            assertArrayEquals(square01[i], square01actual[i]);
    1.99 +        }
   1.100 +
   1.101 +        final var square20 = new int[][]{
   1.102 +                new int[]{8,0,7},
   1.103 +                new int[]{0,0,0},
   1.104 +                new int[]{0,4,0}};
   1.105 +
   1.106 +        final var square20actual = field.getSquare(2, 0);
   1.107 +        assertEquals(3, square20actual.length);
   1.108 +        for (int i = 0 ; i < 3 ; i++) {
   1.109 +            assertArrayEquals(square20[i], square20actual[i]);
   1.110 +        }
   1.111 +    }
   1.112 +
   1.113 +    @Test
   1.114 +    void getRow() {
   1.115 +        final var field = createTestField();
   1.116 +
   1.117 +        final var row3 = new int[]{0,9,0,0,6,0,0,0,8};
   1.118 +        final var row4 = new int[]{0,0,0,2,0,7,0,0,0};
   1.119 +
   1.120 +        assertArrayEquals(row3, field.getRow(3));
   1.121 +        assertArrayEquals(row4, field.getRow(4));
   1.122 +    }
   1.123 +
   1.124 +    @Test
   1.125 +    void getColumn() {
   1.126 +        final var field = createTestField();
   1.127 +
   1.128 +        final var col2 = new int[]{0,0,5,0,0,0,2,0,6};
   1.129 +        final var col6 = new int[]{8,0,7,0,0,0,5,0,0};
   1.130 +
   1.131 +        assertArrayEquals(col2, field.getColumn(2));
   1.132 +        assertArrayEquals(col6, field.getColumn(6));
   1.133 +    }
   1.134 +
   1.135 +    @Test
   1.136 +    void getSquareIAE() {
   1.137 +        final var field = createTestField();
   1.138 +        assertThrows(IllegalArgumentException.class, () -> field.getSquare(3, 2));
   1.139 +        assertThrows(IllegalArgumentException.class, () -> field.getSquare(2, 3));
   1.140 +        assertThrows(IllegalArgumentException.class, () -> field.getSquare(2, -1));
   1.141 +        assertThrows(IllegalArgumentException.class, () -> field.getSquare(-1, 2));
   1.142 +    }
   1.143 +
   1.144 +    @Test
   1.145 +    void getRowIAE() {
   1.146 +        final var field = createTestField();
   1.147 +        assertThrows(IllegalArgumentException.class, () -> field.getRow(9));
   1.148 +        assertThrows(IllegalArgumentException.class, () -> field.getRow(-1));
   1.149 +    }
   1.150 +
   1.151 +    @Test
   1.152 +    void getColumnIAE() {
   1.153 +        final var field = createTestField();
   1.154 +        assertThrows(IllegalArgumentException.class, () -> field.getColumn(9));
   1.155 +        assertThrows(IllegalArgumentException.class, () -> field.getColumn(-1));
   1.156 +    }
   1.157 +}

mercurial