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
205
206
207
208
209
|
package jrummikub.model;
import java.util.ArrayList;
import java.util.List;
import jrummikub.util.Pair;
import org.junit.*;
import static org.junit.Assert.*;
/**
* Tests for {@link StoneTray}
*/
public class StoneTrayTest {
class Thing implements Sizeable {
private float width;
private float height;
public Thing(float width, float height) {
this.width = width;
this.height = height;
}
@Override
public float getWidth() {
return width;
}
@Override
public float getHeight() {
return height;
}
}
private StoneTray<Thing> testTray;
/** */
@Before
public void createTray() {
testTray = new StoneTray<Thing>();
}
/** */
@Test
public void testDrop() {
Thing firstThing = new Thing(3, 4);
testTray.drop(firstThing, new Position(5, 23));
Thing secondThing = new Thing(5, 8);
testTray.drop(secondThing, new Position(42, 8.5f));
Position firstPosition = testTray.getPosition(firstThing);
Position secondPosition = testTray.getPosition(secondThing);
assertEquals(5, firstPosition.getX(), 0.00001);
assertEquals(23, firstPosition.getY(), 0.00001);
assertEquals(42, secondPosition.getX(), 0.00001);
assertEquals(8.5, secondPosition.getY(), 0.00001);
}
/** */
@Test
public void testDropNull() {
testTray.drop(null, new Position(0, 0));
assertFalse(testTray.iterator().hasNext());
}
// Leftshift
/** */
@Test
public void testLeftDrop() {
Thing firstThing = new Thing(5, 5);
testTray.drop(firstThing, new Position(0, 0));
Thing secondThing = new Thing(3, 3);
testTray.drop(secondThing, new Position(4, 1));
Position firstPosition = testTray.getPosition(firstThing);
Position secondPosition = testTray.getPosition(secondThing);
assertEquals(-1, firstPosition.getX(), 0.00001);
assertEquals(0, firstPosition.getY(), 0.00001);
assertEquals(4, secondPosition.getX(), 0.00001);
assertEquals(1, secondPosition.getY(), 0.00001);
}
// Rightshift
/** */
@Test
public void testRightDrop() {
Thing firstThing = new Thing(5, 5);
testTray.drop(firstThing, new Position(0, 0));
Thing secondThing = new Thing(3, 3);
testTray.drop(secondThing, new Position(-2, 1));
Position firstPosition = testTray.getPosition(firstThing);
Position secondPosition = testTray.getPosition(secondThing);
assertEquals(1, firstPosition.getX(), 0.00001);
assertEquals(0, firstPosition.getY(), 0.00001);
assertEquals(-2, secondPosition.getX(), 0.00001);
assertEquals(1, secondPosition.getY(), 0.00001);
}
// Upshift
/** */
@Test
public void testUpDrop() {
Thing firstThing = new Thing(5, 5);
testTray.drop(firstThing, new Position(0, 0));
Thing secondThing = new Thing(3, 3);
testTray.drop(secondThing, new Position(1, 4));
Position firstPosition = testTray.getPosition(firstThing);
Position secondPosition = testTray.getPosition(secondThing);
assertEquals(0, firstPosition.getX(), 0.00001);
assertEquals(-1, firstPosition.getY(), 0.00001);
assertEquals(1, secondPosition.getX(), 0.00001);
assertEquals(4, secondPosition.getY(), 0.00001);
}
// Downshift
/** */
@Test
public void testDownDrop() {
Thing firstThing = new Thing(5, 5);
testTray.drop(firstThing, new Position(0, 0));
Thing secondThing = new Thing(3, 3);
testTray.drop(secondThing, new Position(1, -2));
Position firstPosition = testTray.getPosition(firstThing);
Position secondPosition = testTray.getPosition(secondThing);
assertEquals(0, firstPosition.getX(), 0.00001);
assertEquals(1, firstPosition.getY(), 0.00001);
assertEquals(1, secondPosition.getX(), 0.00001);
assertEquals(-2, secondPosition.getY(), 0.00001);
}
/** */
@Test
public void testDoubleShift() {
Thing firstThing = new Thing(5, 5);
testTray.drop(firstThing, new Position(0, 0));
Thing secondThing = new Thing(5, 0.1f);
testTray.drop(secondThing, new Position(5, 0));
Thing thirdThing = new Thing(3, 3);
testTray.drop(thirdThing, new Position(-2, 1));
Position firstPosition = testTray.getPosition(firstThing);
Position secondPosition = testTray.getPosition(secondThing);
Position thirdPosition = testTray.getPosition(thirdThing);
assertEquals(1, firstPosition.getX(), 0.00001);
assertEquals(0, firstPosition.getY(), 0.00001);
assertEquals(6, secondPosition.getX(), 0.00001);
assertEquals(0, secondPosition.getY(), 0.00001);
assertEquals(-2, thirdPosition.getX(), 0.00001);
assertEquals(1, thirdPosition.getY(), 0.00001);
}
/** */
@Test
public void testPickUpByObject() {
Thing firstThing = new Thing(5, 5);
testTray.drop(firstThing, new Position(0, 0));
Thing secondThing = new Thing(5, 5);
testTray.drop(secondThing, new Position(5, 0));
testTray.pickUp(firstThing);
for (Pair<Thing, Position> i : testTray) {
assertSame(i.getFirst(), secondThing);
}
assertTrue(testTray.iterator().hasNext());
}
/** */
@Test
public void testIterate() {
List<Thing> testThings = new ArrayList<Thing>();
List<Position> testPositions = new ArrayList<Position>();
for (int i = 0; i < 4; i++) {
Thing newThing = new Thing(1, 1);
Position newPosition = new Position(i, i);
testThings.add(newThing);
testPositions.add(newPosition);
testTray.drop(newThing, newPosition);
}
for (Pair<Thing, Position> i : testTray) {
int index = testThings.indexOf(i.getFirst());
assertFalse(index == -1);
assertEquals(i.getSecond(), testPositions.get(index));
testThings.remove(index);
testPositions.remove(index);
}
assertTrue(testThings.isEmpty());
assertTrue(testPositions.isEmpty());
}
/** */
@Test
public void testClone() {
Thing firstThing = new Thing(5, 5);
testTray.drop(firstThing, new Position(0, 0));
Thing secondThing = new Thing(3, 3);
testTray.drop(secondThing, new Position(-5, -5));
IStoneTray<Thing> trayCopy = testTray.clone();
testTray.pickUp(firstThing);
for (Pair<Thing, Position> i : testTray) {
assertSame(i.getFirst(), secondThing);
}
assertTrue(testTray.iterator().hasNext());
trayCopy.pickUp(secondThing);
for (Pair<Thing, Position> i : trayCopy) {
assertSame(i.getFirst(), firstThing);
}
assertTrue(trayCopy.iterator().hasNext());
}
}
|