-
Notifications
You must be signed in to change notification settings - Fork 0
/
PieceTest.java
204 lines (152 loc) · 5.76 KB
/
PieceTest.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import junit.framework.TestCase;
import java.util.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
/*
Unit test for Piece class -- starter shell.
*/
public class PieceTest extends TestCase {
private Piece stickR1, stickR2;
private Piece L1R1, L1R2, L1R3, L1R4;
private Piece L2R1, L2R2, L2R3, L2R4;
private Piece S1R1, S1R2;
private Piece S2R1, S2R2;
private Piece sqr;
private Piece pyrR1, pyrR2, pyrR3, pyrR4;
private Piece weird1, weird2;
protected void setUp() throws Exception {
super.setUp();
stickR1 = new Piece(Piece.STICK_STR);
L1R1 = new Piece(Piece.L1_STR);
L2R1 = new Piece(Piece.L2_STR);
S1R1 = new Piece(Piece.S1_STR);
S2R1 = new Piece(Piece.S2_STR);
sqr = new Piece(Piece.SQUARE_STR);
pyrR1 = new Piece(Piece.PYRAMID_STR);
weird1 = new Piece("2 0 3 0 4 0 5 0 1 1 2 2 0 2 0 3");
weird2 = new Piece("1 0 0 1 1 1 2 1 3 1 4 2 5 1 6 2 6 3 6 4 6 5");
getSlowRotations();
}
private void getSlowRotations() {
stickR2 = stickR1.computeNextRotation();
L1R2 = L1R1.computeNextRotation();
L1R3 = L1R2.computeNextRotation();
L1R4 = L1R3.computeNextRotation();
L2R2 = L2R1.computeNextRotation();
L2R3 = L2R2.computeNextRotation();
L2R4 = L2R3.computeNextRotation();
S1R2 = S1R1.computeNextRotation();
S2R2 = S2R1.computeNextRotation();
pyrR2 = pyrR1.computeNextRotation();
pyrR3 = pyrR2.computeNextRotation();
pyrR4 = pyrR3.computeNextRotation();
}
public void testSizeBasic() {
assertEquals(1, stickR1.getWidth());
assertEquals(4, stickR1.getHeight());
assertEquals(2, L1R1.getWidth());
assertEquals(3, L1R1.getHeight());
assertEquals(2, L2R1.getWidth());
assertEquals(3, L2R1.getHeight());
assertEquals(3, S1R1.getWidth());
assertEquals(2, S1R1.getHeight());
assertEquals(3, S2R1.getWidth());
assertEquals(2, S2R1.getHeight());
assertEquals(2, sqr.getWidth());
assertEquals(2, sqr.getHeight());
assertEquals(3, pyrR1.getWidth());
assertEquals(2, pyrR1.getHeight());
}
public void testSizeRotated() {
assertEquals(4, stickR2.getWidth());
assertEquals(1, stickR2.getHeight());
assertEquals(2, L1R3.getWidth());
assertEquals(3, L1R3.getHeight());
assertEquals(3, L2R4.getWidth());
assertEquals(2, L2R4.getHeight());
assertEquals(2, S1R2.getWidth());
assertEquals(3, S1R2.getHeight());
assertEquals(2, S2R2.getWidth());
assertEquals(3, S2R2.getHeight());
assertEquals(2, sqr.computeNextRotation().getWidth());
assertEquals(2, sqr.computeNextRotation().getHeight());
assertEquals(2, pyrR2.getWidth());
assertEquals(3, pyrR2.getHeight());
}
public void testSizeWeird() {
assertEquals(6, weird1.getWidth());
assertEquals(4, weird1.getHeight());
assertEquals(7, weird2.getWidth());
assertEquals(6, weird2.getHeight());
// test weird rotated
assertEquals(4, weird1.computeNextRotation().getWidth());
assertEquals(6, weird1.computeNextRotation().getHeight());
assertEquals(6, weird2.computeNextRotation().getWidth());
assertEquals(7, weird2.computeNextRotation().getHeight());
}
public void testEqualsBasic() {
Piece newStick = new Piece(Piece.STICK_STR);
assertTrue(newStick.equals(newStick));
assertTrue(newStick.equals(stickR1));
assertTrue(newStick.equals(stickR2.computeNextRotation()));
assertFalse(newStick.equals(pyrR1));
}
public void testEqualsWeird() {
Piece longerStick = new Piece(Piece.STICK_STR + " 0 4 0 5");
assertFalse(longerStick.equals(stickR1));
assertFalse(stickR1.equals(longerStick));
assertFalse(longerStick.equals(sqr));
assertFalse(weird1.equals(weird2));
assertTrue(weird1.equals(weird1));
assertFalse(longerStick.equals(new TPoint(1, 2)));
}
public void testSkirtBasic() {
assertTrue(Arrays.equals(new int[] {0}, stickR1.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 0}, L1R1.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 0}, L2R1.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 0, 1}, S1R1.getSkirt()));
assertTrue(Arrays.equals(new int[] {1, 0, 0}, S2R1.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 0, 0}, pyrR1.getSkirt()));
}
public void testSkirtRotated() {
assertTrue(Arrays.equals(new int[] {0, 0, 0, 0}, stickR2.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 0, 0}, L1R2.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 2}, L2R3.getSkirt()));
assertTrue(Arrays.equals(new int[] {1, 0}, S1R2.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 1}, S2R2.getSkirt()));
assertTrue(Arrays.equals(new int[] {0, 1}, pyrR4.getSkirt()));
}
public void testSkirtWeird() {
assertTrue(Arrays.equals(new int[]{2, 1, 0, 0, 0, 0}, weird1.getSkirt()));
assertTrue(Arrays.equals(new int[]{1, 0, 1, 1, 2, 1, 2}, weird2.getSkirt()));
}
// rerun the previous tests on fast rotated pieces
public void testFastRotations () {
getFastRotations();
testSizeRotated();
testSkirtRotated();
assertTrue(pyrR1.equals(pyrR4.fastRotation()));
Piece.getPieces();
}
// change all slow rotated pieces with fast rotated ones
private void getFastRotations() {
Piece[] pieces = Piece.getPieces();
stickR2 = pieces[Piece.STICK].fastRotation();
L1R2 = pieces[Piece.L1].fastRotation();
L1R3 = L1R2.fastRotation();
L1R4 = L1R3.fastRotation();
L2R2 = pieces[Piece.L2].fastRotation();
L2R3 = L2R2.fastRotation();
L2R4 = L2R3.fastRotation();
S1R2 = pieces[Piece.S1].fastRotation();
S2R2 = pieces[Piece.S2].fastRotation();
pyrR2 = pieces[Piece.PYRAMID].fastRotation();
pyrR3 = pyrR2.fastRotation();
pyrR4 = pyrR3.fastRotation();
}
public void testParsingException() {
Exception exception = assertThrows(RuntimeException.class, () -> new Piece("7 7 8-"));
String expectedMessage = "Could not parse x,y string:";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}
}