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

changeset 22
06170a0be62a
child 25
569220009c54
equal deleted inserted replaced
21:11cb2d9413d4 22:06170a0be62a
1 package de.uapcore.sudoku;
2
3 import org.junit.jupiter.api.Test;
4
5 import static org.junit.jupiter.api.Assertions.*;
6
7 class FieldTest {
8
9 private int[] createTestdata() {
10 return new int[]{
11 0,1,0,9,0,0,8,0,0,
12 0,0,0,0,0,8,0,0,4,
13 6,0,5,0,0,0,7,0,0,
14 0,9,0,0,6,0,0,0,8,
15 0,0,0,2,0,7,0,0,0,
16 8,0,0,0,3,0,0,6,0,
17 0,0,2,0,0,0,5,0,3,
18 1,0,0,4,0,0,0,0,0,
19 0,0,6,0,0,2,0,1,0};
20 }
21
22 private Field createTestField() {
23 final var field = new Field();
24 final var testdata = createTestdata();
25 for (int x = 0 ; x < 9 ; x++) {
26 for (int y = 0 ; y < 9 ; y++) {
27 field.setCellValue(x, y, testdata[x+9*y]);
28 }
29 }
30 return field;
31 }
32
33 @Test
34 void getAndSetValues() {
35 final var f = new Field();
36 assertEquals(0, f.getCellValue(3, 4));
37 assertTrue(f.isCellEmpty(3, 4));
38 f.setCellValue(3, 4, 6);
39 assertEquals(6, 3, 4);
40 assertFalse(f.isCellEmpty(3, 4));
41 }
42
43 @Test
44 void setAllCellsModified() {
45 final var f = new Field();
46
47 assertFalse(f.isAnyCellModified());
48 f.setAllCellsModified(true);
49 for (int x = 0 ; x < 9 ; x++) {
50 for (int y = 0 ; y < 9 ; y++) {
51 assertTrue(f.isCellModified(x, y));
52 }
53 }
54
55 f.setAllCellsModified(false);
56 assertFalse(f.isAnyCellModified());
57 }
58
59 @Test
60 void isAnyCellModified() {
61 final var f = new Field();
62
63 assertFalse(f.isAnyCellModified());
64 f.setCellValue(3, 4, 6);
65 assertFalse(f.isAnyCellModified());
66 f.setCellModified(3, 4, true);
67 assertTrue(f.isAnyCellModified());
68 }
69
70 @Test
71 void clear() {
72 final var f = new Field();
73 f.setCellValue(3, 4, 6);
74 f.setCellValue(2, 5, 9);
75 f.clear();
76 for (int x = 0 ; x < 9 ; x++) {
77 for (int y = 0 ; y < 9 ; y++) {
78 assertTrue(f.isCellEmpty(x, y));
79 }
80 }
81 }
82
83 @Test
84 void getSquare() {
85 final var field = createTestField();
86
87 final var square01 = new int[][]{
88 new int[]{0,0,8},
89 new int[]{9,0,0},
90 new int[]{0,0,0}};
91
92 final var square01actual = field.getSquare(0, 1);
93 assertEquals(3, square01actual.length);
94 for (int i = 0 ; i < 3 ; i++) {
95 assertArrayEquals(square01[i], square01actual[i]);
96 }
97
98 final var square20 = new int[][]{
99 new int[]{8,0,7},
100 new int[]{0,0,0},
101 new int[]{0,4,0}};
102
103 final var square20actual = field.getSquare(2, 0);
104 assertEquals(3, square20actual.length);
105 for (int i = 0 ; i < 3 ; i++) {
106 assertArrayEquals(square20[i], square20actual[i]);
107 }
108 }
109
110 @Test
111 void getRow() {
112 final var field = createTestField();
113
114 final var row3 = new int[]{0,9,0,0,6,0,0,0,8};
115 final var row4 = new int[]{0,0,0,2,0,7,0,0,0};
116
117 assertArrayEquals(row3, field.getRow(3));
118 assertArrayEquals(row4, field.getRow(4));
119 }
120
121 @Test
122 void getColumn() {
123 final var field = createTestField();
124
125 final var col2 = new int[]{0,0,5,0,0,0,2,0,6};
126 final var col6 = new int[]{8,0,7,0,0,0,5,0,0};
127
128 assertArrayEquals(col2, field.getColumn(2));
129 assertArrayEquals(col6, field.getColumn(6));
130 }
131
132 @Test
133 void getSquareIAE() {
134 final var field = createTestField();
135 assertThrows(IllegalArgumentException.class, () -> field.getSquare(3, 2));
136 assertThrows(IllegalArgumentException.class, () -> field.getSquare(2, 3));
137 assertThrows(IllegalArgumentException.class, () -> field.getSquare(2, -1));
138 assertThrows(IllegalArgumentException.class, () -> field.getSquare(-1, 2));
139 }
140
141 @Test
142 void getRowIAE() {
143 final var field = createTestField();
144 assertThrows(IllegalArgumentException.class, () -> field.getRow(9));
145 assertThrows(IllegalArgumentException.class, () -> field.getRow(-1));
146 }
147
148 @Test
149 void getColumnIAE() {
150 final var field = createTestField();
151 assertThrows(IllegalArgumentException.class, () -> field.getColumn(9));
152 assertThrows(IllegalArgumentException.class, () -> field.getColumn(-1));
153 }
154 }

mercurial