|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+import org.junit.Ignore;
|
|
|
2
|
+import org.junit.Test;
|
|
|
3
|
+
|
|
|
4
|
+import java.util.List;
|
|
|
5
|
+import java.util.ArrayList;
|
|
|
6
|
+import java.util.Arrays;
|
|
|
7
|
+import java.util.Collections;
|
|
|
8
|
+
|
|
|
9
|
+import static org.junit.Assert.assertEquals;
|
|
|
10
|
+import static org.junit.Assert.assertNull;
|
|
|
11
|
+
|
|
|
12
|
+import org.junit.Rule;
|
|
|
13
|
+import org.junit.rules.ExpectedException;
|
|
|
14
|
+
|
|
|
15
|
+public class DominoesTest {
|
|
|
16
|
+
|
|
|
17
|
+ @Rule
|
|
|
18
|
+ public ExpectedException expectedException = ExpectedException.none();
|
|
|
19
|
+
|
|
|
20
|
+ @Test
|
|
|
21
|
+ public void emtpyInputEmptyOutputTest() throws ChainNotFoundException {
|
|
|
22
|
+ Dominoes dominoes = new Dominoes();
|
|
|
23
|
+
|
|
|
24
|
+ List<Domino> dominoesList = new ArrayList<>();
|
|
|
25
|
+
|
|
|
26
|
+ List<Domino> chain = dominoes.formChain(dominoesList);
|
|
|
27
|
+
|
|
|
28
|
+ assertEquals("The output list should be empty.", 0, chain.size());
|
|
|
29
|
+ }
|
|
|
30
|
+
|
|
|
31
|
+ @Ignore("Remove to run test")
|
|
|
32
|
+ @Test
|
|
|
33
|
+ public void singletonInputSingletonOutput() throws ChainNotFoundException {
|
|
|
34
|
+ Dominoes dominoes = new Dominoes();
|
|
|
35
|
+
|
|
|
36
|
+ Domino[] dominoesArray = {new Domino(1, 1)};
|
|
|
37
|
+ List<Domino> dominoesList = Arrays.asList(dominoesArray);
|
|
|
38
|
+
|
|
|
39
|
+ List<Domino> chain = dominoes.formChain(dominoesList);
|
|
|
40
|
+
|
|
|
41
|
+ assertValidChain(dominoesList, chain);
|
|
|
42
|
+ }
|
|
|
43
|
+
|
|
|
44
|
+ @Ignore("Remove to run test")
|
|
|
45
|
+ @Test
|
|
|
46
|
+ public void singletonCantBeChainedTest() throws ChainNotFoundException {
|
|
|
47
|
+ Dominoes dominoes = new Dominoes();
|
|
|
48
|
+
|
|
|
49
|
+ Domino[] dominoesArray = {new Domino(1, 2)};
|
|
|
50
|
+ List<Domino> dominoesList = Arrays.asList(dominoesArray);
|
|
|
51
|
+
|
|
|
52
|
+ expectedException.expect(ChainNotFoundException.class);
|
|
|
53
|
+ expectedException.expectMessage("No domino chain found.");
|
|
|
54
|
+
|
|
|
55
|
+ dominoes.formChain(dominoesList);
|
|
|
56
|
+ }
|
|
|
57
|
+
|
|
|
58
|
+ @Ignore("Remove to run test")
|
|
|
59
|
+ @Test
|
|
|
60
|
+ public void threeElementsTest() throws ChainNotFoundException {
|
|
|
61
|
+ Dominoes dominoes = new Dominoes();
|
|
|
62
|
+
|
|
|
63
|
+ Domino[] dominoesArray = {new Domino(1, 2), new Domino(3, 1), new Domino(2, 3)};
|
|
|
64
|
+ List<Domino> dominoesList = Arrays.asList(dominoesArray);
|
|
|
65
|
+
|
|
|
66
|
+ List<Domino> chain = dominoes.formChain(dominoesList);
|
|
|
67
|
+
|
|
|
68
|
+ assertValidChain(dominoesList, chain);
|
|
|
69
|
+ }
|
|
|
70
|
+
|
|
|
71
|
+ @Ignore("Remove to run test")
|
|
|
72
|
+ @Test
|
|
|
73
|
+ public void canReverseDominoesTest() throws ChainNotFoundException {
|
|
|
74
|
+ Dominoes dominoes = new Dominoes();
|
|
|
75
|
+
|
|
|
76
|
+ Domino[] dominoesArray = {new Domino(1, 2), new Domino(1, 3), new Domino(2, 3)};
|
|
|
77
|
+ List<Domino> dominoesList = Arrays.asList(dominoesArray);
|
|
|
78
|
+
|
|
|
79
|
+ List<Domino> chain = dominoes.formChain(dominoesList);
|
|
|
80
|
+
|
|
|
81
|
+ assertValidChain(dominoesList, chain);
|
|
|
82
|
+ }
|
|
|
83
|
+
|
|
|
84
|
+ @Ignore("Remove to run test")
|
|
|
85
|
+ @Test
|
|
|
86
|
+ public void cantBeChainedTest() throws ChainNotFoundException {
|
|
|
87
|
+ Dominoes dominoes = new Dominoes();
|
|
|
88
|
+
|
|
|
89
|
+ Domino[] dominoesArray = {new Domino(1, 2), new Domino(4, 1), new Domino(2, 3)};
|
|
|
90
|
+ List<Domino> dominoesList = Arrays.asList(dominoesArray);
|
|
|
91
|
+
|
|
|
92
|
+ expectedException.expect(ChainNotFoundException.class);
|
|
|
93
|
+ expectedException.expectMessage("No domino chain found.");
|
|
|
94
|
+
|
|
|
95
|
+ dominoes.formChain(dominoesList);
|
|
|
96
|
+ }
|
|
|
97
|
+
|
|
|
98
|
+ @Ignore("Remove to run test")
|
|
|
99
|
+ @Test
|
|
|
100
|
+ public void disconnectedSimpleTest() throws ChainNotFoundException {
|
|
|
101
|
+ Dominoes dominoes = new Dominoes();
|
|
|
102
|
+
|
|
|
103
|
+ Domino[] dominoesArray = {new Domino(1, 1), new Domino(2, 2)};
|
|
|
104
|
+ List<Domino> dominoesList = Arrays.asList(dominoesArray);
|
|
|
105
|
+
|
|
|
106
|
+ expectedException.expect(ChainNotFoundException.class);
|
|
|
107
|
+ expectedException.expectMessage("No domino chain found.");
|
|
|
108
|
+
|
|
|
109
|
+ dominoes.formChain(dominoesList);
|
|
|
110
|
+ }
|
|
|
111
|
+
|
|
|
112
|
+ @Ignore("Remove to run test")
|
|
|
113
|
+ @Test
|
|
|
114
|
+ public void disconnectedDoubleLoopTest() throws ChainNotFoundException {
|
|
|
115
|
+ Dominoes dominoes = new Dominoes();
|
|
|
116
|
+
|
|
|
117
|
+ Domino[] dominoesArray = {new Domino(1, 2), new Domino(2, 1), new Domino(3, 4), new Domino(4, 3)};
|
|
|
118
|
+ List<Domino> dominoesList = Arrays.asList(dominoesArray);
|
|
|
119
|
+
|
|
|
120
|
+ expectedException.expect(ChainNotFoundException.class);
|
|
|
121
|
+ expectedException.expectMessage("No domino chain found.");
|
|
|
122
|
+
|
|
|
123
|
+ dominoes.formChain(dominoesList);
|
|
|
124
|
+ }
|
|
|
125
|
+
|
|
|
126
|
+ @Ignore("Remove to run test")
|
|
|
127
|
+ @Test
|
|
|
128
|
+ public void disconnectedSingleIsolatedTest() throws ChainNotFoundException {
|
|
|
129
|
+ Dominoes dominoes = new Dominoes();
|
|
|
130
|
+
|
|
|
131
|
+ Domino[] dominoesArray = {new Domino(1, 2), new Domino(2, 3), new Domino(3, 1), new Domino(4, 4)};
|
|
|
132
|
+ List<Domino> dominoesList = Arrays.asList(dominoesArray);
|
|
|
133
|
+
|
|
|
134
|
+ expectedException.expect(ChainNotFoundException.class);
|
|
|
135
|
+ expectedException.expectMessage("No domino chain found.");
|
|
|
136
|
+
|
|
|
137
|
+ dominoes.formChain(dominoesList);
|
|
|
138
|
+ }
|
|
|
139
|
+
|
|
|
140
|
+ @Ignore("Remove to run test")
|
|
|
141
|
+ @Test
|
|
|
142
|
+ public void needBacktrackTest() throws ChainNotFoundException {
|
|
|
143
|
+ Dominoes dominoes = new Dominoes();
|
|
|
144
|
+
|
|
|
145
|
+ Domino[] dominoesArray = {new Domino(1, 2), new Domino(2, 3), new Domino(3, 1), new Domino(2, 4), new Domino(4, 2)};
|
|
|
146
|
+ List<Domino> dominoesList = Arrays.asList(dominoesArray);
|
|
|
147
|
+
|
|
|
148
|
+ List<Domino> chain = dominoes.formChain(dominoesList);
|
|
|
149
|
+
|
|
|
150
|
+ assertValidChain(dominoesList, chain);
|
|
|
151
|
+ }
|
|
|
152
|
+
|
|
|
153
|
+ @Ignore("Remove to run test")
|
|
|
154
|
+ @Test
|
|
|
155
|
+ public void separateLoopsTest() throws ChainNotFoundException {
|
|
|
156
|
+ Dominoes dominoes = new Dominoes();
|
|
|
157
|
+
|
|
|
158
|
+ Domino[] dominoesArray = {new Domino(1, 2), new Domino(2, 3), new Domino(3, 1),
|
|
|
159
|
+ new Domino(1, 1), new Domino(2, 2), new Domino(3, 3)};
|
|
|
160
|
+ List<Domino> dominoesList = Arrays.asList(dominoesArray);
|
|
|
161
|
+
|
|
|
162
|
+ List<Domino> chain = dominoes.formChain(dominoesList);
|
|
|
163
|
+
|
|
|
164
|
+ assertValidChain(dominoesList, chain);
|
|
|
165
|
+ }
|
|
|
166
|
+
|
|
|
167
|
+ @Ignore("Remove to run test")
|
|
|
168
|
+ @Test
|
|
|
169
|
+ public void nineElementsTest() throws ChainNotFoundException {
|
|
|
170
|
+ Dominoes dominoes = new Dominoes();
|
|
|
171
|
+ Domino[] dominoesArray = {new Domino(1, 2), new Domino(5, 3), new Domino(3, 1),
|
|
|
172
|
+ new Domino(1, 2), new Domino(2, 4), new Domino(1, 6),
|
|
|
173
|
+ new Domino(2, 3), new Domino(3, 4), new Domino(5, 6)};
|
|
|
174
|
+ List<Domino> dominoesList = Arrays.asList(dominoesArray);
|
|
|
175
|
+
|
|
|
176
|
+ List<Domino> chain = dominoes.formChain(dominoesList);
|
|
|
177
|
+
|
|
|
178
|
+ assertValidChain(dominoesList, chain);
|
|
|
179
|
+ }
|
|
|
180
|
+
|
|
|
181
|
+ private void assertValidChain(List<Domino> inputDominoes, List<Domino> outputDominoes) {
|
|
|
182
|
+ assertSameDominoes(inputDominoes, outputDominoes);
|
|
|
183
|
+ assertEndDominoesMatch(outputDominoes);
|
|
|
184
|
+ assertConsecutiveDominoes(outputDominoes);
|
|
|
185
|
+ }
|
|
|
186
|
+
|
|
|
187
|
+ private void assertEndDominoesMatch(List<Domino> outputDominoes) {
|
|
|
188
|
+ int leftValueOfFirstDomino = outputDominoes.get(0).getLeft();
|
|
|
189
|
+ int rightValueOfLastDomino = outputDominoes.get(outputDominoes.size() - 1).getRight();
|
|
|
190
|
+ String errorMessage = "The left value of the first domino ("
|
|
|
191
|
+ + leftValueOfFirstDomino
|
|
|
192
|
+ + ") needs to match the right value of the last domino ("
|
|
|
193
|
+ + rightValueOfLastDomino
|
|
|
194
|
+ + ").";
|
|
|
195
|
+
|
|
|
196
|
+ assertEquals(errorMessage, leftValueOfFirstDomino, rightValueOfLastDomino);
|
|
|
197
|
+ }
|
|
|
198
|
+
|
|
|
199
|
+ private void assertSameDominoes(List<Domino> inputDominoes, List<Domino> outputDominoes) {
|
|
|
200
|
+ String errorMessage = "The number of dominoes in the input list (" + inputDominoes.size()
|
|
|
201
|
+ + ") needs to match the number of dominoes in the output chain (" + outputDominoes.size() + ")";
|
|
|
202
|
+
|
|
|
203
|
+ assertEquals(errorMessage, inputDominoes.size(), outputDominoes.size());
|
|
|
204
|
+
|
|
|
205
|
+ for (Domino domino : inputDominoes) {
|
|
|
206
|
+ int inputFrequency = Collections.frequency(inputDominoes, domino);
|
|
|
207
|
+ int outputFrequency = Collections.frequency(outputDominoes, domino);
|
|
|
208
|
+
|
|
|
209
|
+ String frequencyErrorMessage = "The frequency of domino (" + domino.getLeft() + ", " + domino.getRight() + ")"
|
|
|
210
|
+ + " in the input is (" + inputFrequency + "), but (" + outputFrequency + ") in the output.";
|
|
|
211
|
+
|
|
|
212
|
+ assertEquals(frequencyErrorMessage, inputFrequency, outputFrequency);
|
|
|
213
|
+ }
|
|
|
214
|
+ }
|
|
|
215
|
+
|
|
|
216
|
+ private void assertConsecutiveDominoes(List<Domino> dominoes) {
|
|
|
217
|
+ for (int i = 0; i < dominoes.size() - 1; i++) {
|
|
|
218
|
+
|
|
|
219
|
+ int rightValueOfIthDomino = dominoes.get(i).getRight();
|
|
|
220
|
+ int leftValueOfNextDomino = dominoes.get(i+1).getLeft();
|
|
|
221
|
+ String errorMessage = "The right value of domino number " + i + " ("
|
|
|
222
|
+ + rightValueOfIthDomino
|
|
|
223
|
+ + ") needs to match the left value of domino number " + (i+1) + " ("
|
|
|
224
|
+ + leftValueOfNextDomino
|
|
|
225
|
+ + ").";
|
|
|
226
|
+
|
|
|
227
|
+ assertEquals(errorMessage, dominoes.get(i).getRight(), dominoes.get(i+1).getLeft());
|
|
|
228
|
+ }
|
|
|
229
|
+ }
|
|
|
230
|
+}
|