-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoardTests.java
176 lines (154 loc) · 4.51 KB
/
BoardTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import junit.framework.TestCase;
import static org.junit.Assert.assertNotEquals;
public class BoardTests extends TestCase {
public void testEquals() {
int[][] blocks = new int[][]{
{0, 1},
{2, 3},
};
int[][] blocks2 = new int[][]{
{5, 3},
{4, 8},
};
int[][] blocksEmpty = new int[0][0];
Board a = new Board(blocks);
Board b = new Board(blocks);
Board c = new Board(blocks2);
Board empty1 = new Board(blocksEmpty);
Board empty2 = new Board(blocksEmpty);
assertEquals(a, b);
assertEquals(a, a);
assertEquals(empty1, empty2);
assertNotEquals(a, empty1);
assertNotEquals(a, c);
assertNotEquals(a, null);
}
public void testIsGoal() {
int[][] blocks1 = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};
int[][] blocks2 = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 0, 8}
};
Board a = new Board(blocks1);
Board b = new Board(blocks2);
assertTrue(a.isGoal());
assertFalse(b.isGoal());
}
public void testTwin() {
int[][] blocks1 = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};
Board board = new Board(blocks1);
for (int i = 0; i < 20; i++) {
assertNotEquals(board, board.twin());
}
int[][] blocks2 = new int[][] {
{0, 1},
{2, 3},
};
Board board2 = new Board(blocks2);
for (int i = 0; i < 20; i++) {
assertNotEquals(board2, board2.twin());
}
}
public void testNeighbours() {
int[][] blocks1 = new int[][]{
{1, 2, 3},
{4, 0, 6},
{7, 8, 5}
};
Board board1 = new Board(blocks1);
int count = 0;
for (Board b : board1.neighbors()) count += 1;
assertEquals(4, count);
int[][] blocks2 = new int[][]{
{0, 2, 3},
{4, 1, 6},
{7, 8, 5}
};
Board board2 = new Board(blocks2);
count = 0;
for (Board b : board2.neighbors()) count += 1;
assertEquals(2, count);
int[][] blocks3 = new int[][]{
{3, 2, 0},
{4, 1, 6},
{7, 8, 5}
};
Board board3 = new Board(blocks3);
count = 0;
for (Board b : board3.neighbors()) count += 1;
assertEquals(2, count);
int[][] blocks4 = new int[][]{
{3, 2, 5},
{4, 1, 6},
{7, 8, 0}
};
Board board4 = new Board(blocks4);
count = 0;
for (Board b : board4.neighbors()) count += 1;
assertEquals(2, count);
int[][] blocks5 = new int[][]{
{3, 2, 5},
{4, 1, 6},
{0, 8, 7}
};
Board board5 = new Board(blocks5);
count = 0;
for (Board b : board5.neighbors()) count += 1;
assertEquals(2, count);
}
public void testHamming() {
int[][] blocks1 = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};
Board board1 = new Board(blocks1);
assertEquals(0, board1.hamming());
int[][] blocks2 = new int[][] {
{1, 2, 3},
{4, 5, 6},
{7, 0, 8}
};
Board board2 = new Board(blocks2);
assertEquals(1, board2.hamming());
int[][] blocks3 = new int[][] {
{8, 1, 3},
{4, 0, 2},
{7, 6, 5}
};
Board board3 = new Board(blocks3);
assertEquals(5, board3.hamming());
}
public void testManhattan() {
int[][] blocks1 = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};
Board board1 = new Board(blocks1);
assertEquals(0, board1.manhattan());
int[][] blocks2 = new int[][] {
{1, 2, 3},
{4, 5, 6},
{7, 0, 8}
};
Board board2 = new Board(blocks2);
assertEquals(1, board2.manhattan());
int[][] blocks3 = new int[][] {
{8, 1, 3},
{4, 0, 2},
{7, 6, 5}
};
Board board3 = new Board(blocks3);
assertEquals(10, board3.manhattan());
}
}