-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPottsLocation2DTest.java
More file actions
370 lines (297 loc) · 13.4 KB
/
Copy pathPottsLocation2DTest.java
File metadata and controls
370 lines (297 loc) · 13.4 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package arcade.potts.env.location;
import java.util.ArrayList;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import ec.util.MersenneTwisterFast;
import arcade.core.util.Vector;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static arcade.potts.env.location.Voxel.VOXEL_COMPARATOR;
public class PottsLocation2DTest {
static MersenneTwisterFast randomDoubleZero;
static MersenneTwisterFast randomDoubleOne;
static ArrayList<Voxel> voxelListA;
static ArrayList<Voxel> voxelListB;
static ArrayList<Voxel> voxelListC;
static ArrayList<Voxel> voxelListAC;
static ArrayList<Voxel> voxelListCA;
static ArrayList<Voxel> voxelListBC;
static ArrayList<Voxel> voxelListAB;
@BeforeAll
public static void setupMocks() {
randomDoubleZero = mock(MersenneTwisterFast.class);
when(randomDoubleZero.nextDouble()).thenReturn(0.0);
randomDoubleOne = mock(MersenneTwisterFast.class);
when(randomDoubleOne.nextDouble()).thenReturn(1.0);
}
@BeforeAll
public static void setupLists() {
/*
* Lattice site shape:
*
* x x x x
* x x x
* x x x
*
* Each list is a subset of the shape:
*
* (A) (B) (C) (A) + (C) (B) + (C) (A) + (B)
* x x . . . . x x . . . . x x . . . . x x x x x x
* x . . . . x . x . x x . . x x x . x
* x . . . . . . x x x x x . x x x .
*/
voxelListA = new ArrayList<>();
voxelListA.add(new Voxel(0, 0, 0));
voxelListA.add(new Voxel(0, 1, 0));
voxelListA.add(new Voxel(1, 0, 0));
voxelListA.add(new Voxel(0, 2, 0));
voxelListB = new ArrayList<>();
voxelListB.add(new Voxel(2, 0, 0));
voxelListB.add(new Voxel(3, 0, 0));
voxelListB.add(new Voxel(3, 1, 0));
voxelListC = new ArrayList<>();
voxelListC.add(new Voxel(2, 1, 0));
voxelListC.add(new Voxel(3, 2, 0));
voxelListC.add(new Voxel(4, 2, 0));
voxelListAC = new ArrayList<>(voxelListA);
voxelListAC.addAll(voxelListC);
voxelListCA = new ArrayList<>(voxelListC);
voxelListCA.addAll(voxelListA);
voxelListBC = new ArrayList<>(voxelListB);
voxelListBC.addAll(voxelListC);
voxelListAB = new ArrayList<>(voxelListA);
voxelListAB.addAll(voxelListB);
}
@Test
public void makeLocation_givenList_createsObject() {
ArrayList<Voxel> voxels = new ArrayList<>();
voxels.add(new Voxel(0, 0, 0));
PottsLocation2D oldLoc = new PottsLocation2D(new ArrayList<>());
PottsLocation newLoc = oldLoc.makeLocation(voxels);
assertTrue(newLoc instanceof PottsLocation2D);
assertEquals(1, newLoc.voxels.size());
}
@Test
public void checkVoxels_noVoxels_returnsNull() {
PottsLocation2D loc = new PottsLocation2D(new ArrayList<>());
ArrayList<Voxel> voxels = new ArrayList<>();
assertNull(PottsLocation.checkVoxels(voxels, loc, randomDoubleZero, false));
assertNull(PottsLocation.checkVoxels(voxels, loc, randomDoubleZero, true));
}
@Test
public void checkVoxels_connectedVoxels_returnsNull() {
PottsLocation2D loc = new PottsLocation2D(new ArrayList<>());
ArrayList<Voxel> voxels = new ArrayList<>(voxelListA);
assertNull(PottsLocation.checkVoxels(voxels, loc, randomDoubleZero, false));
assertNull(PottsLocation.checkVoxels(voxels, loc, randomDoubleZero, true));
}
@Test
public void checkVoxels_unconnectedVoxelsWithoutUpdateLargerVisited_returnsList() {
PottsLocation2D loc = new PottsLocation2D(new ArrayList<>());
ArrayList<Voxel> voxels = new ArrayList<>(voxelListAC);
assertEquals(voxelListC, PottsLocation.checkVoxels(voxels, loc, randomDoubleZero, false));
}
@Test
public void checkVoxels_unconnectedVoxelsWithoutUpdateLargerUnvisited_returnsList() {
PottsLocation2D loc = new PottsLocation2D(new ArrayList<>());
ArrayList<Voxel> voxels = new ArrayList<>(voxelListCA);
ArrayList<Voxel> unvisited = new ArrayList<>();
unvisited.add(voxelListC.get(0));
assertEquals(unvisited, PottsLocation.checkVoxels(voxels, loc, randomDoubleZero, false));
}
@Test
public void checkVoxels_unconnectedVoxelsWithUpdateLargerVisited_updatesList() {
PottsLocation2D loc = new PottsLocation2D(new ArrayList<>());
ArrayList<Voxel> voxels = new ArrayList<>(voxelListAC);
assertEquals(voxelListC, PottsLocation.checkVoxels(voxels, loc, randomDoubleZero, true));
assertEquals(voxelListA, voxels);
}
@Test
public void checkVoxels_unconnectedVoxelsWithUpdateLargerUnvisited_updatesList() {
PottsLocation2D loc = new PottsLocation2D(new ArrayList<>());
ArrayList<Voxel> voxels = new ArrayList<>(voxelListCA);
ArrayList<Voxel> unvisited = new ArrayList<>();
unvisited.add(voxelListC.get(0));
ArrayList<Voxel> visited = new ArrayList<>(voxelListCA);
visited.remove(voxelListC.get(0));
assertEquals(unvisited, PottsLocation.checkVoxels(voxels, loc, randomDoubleZero, true));
assertEquals(visited, voxels);
}
@Test
public void connectVoxels_bothListsConnected_doesNothing() {
PottsLocation2D loc = new PottsLocation2D(new ArrayList<>());
ArrayList<Voxel> voxelsA = new ArrayList<>(voxelListA);
ArrayList<Voxel> voxelsB = new ArrayList<>(voxelListB);
PottsLocation.connectVoxels(voxelsA, voxelsB, loc, randomDoubleZero);
assertEquals(voxelListA, voxelsA);
assertEquals(voxelListB, voxelsB);
}
@Test
public void connectVoxels_oneListUnconnected_updatesLists() {
PottsLocation2D loc = new PottsLocation2D(new ArrayList<>());
ArrayList<Voxel> voxelsA1 = new ArrayList<>(voxelListAC);
ArrayList<Voxel> voxelsB1 = new ArrayList<>(voxelListB);
PottsLocation.connectVoxels(voxelsA1, voxelsB1, loc, randomDoubleZero);
assertEquals(voxelListA, voxelsA1);
assertEquals(voxelListBC, voxelsB1);
ArrayList<Voxel> voxelsA2 = new ArrayList<>(voxelListAC);
ArrayList<Voxel> voxelsB2 = new ArrayList<>(voxelListB);
PottsLocation.connectVoxels(voxelsB2, voxelsA2, loc, randomDoubleZero);
assertEquals(voxelListA, voxelsA2);
assertEquals(voxelListBC, voxelsB2);
}
@Test
public void balanceVoxels_balancedLists_doesNothing() {
PottsLocation2D loc = new PottsLocation2D(new ArrayList<>());
ArrayList<Voxel> voxelsA = new ArrayList<>(voxelListA);
ArrayList<Voxel> voxelsB = new ArrayList<>(voxelListB);
PottsLocation.balanceVoxels(voxelsA, voxelsB, loc, randomDoubleZero);
assertEquals(voxelListA, voxelsA);
assertEquals(voxelListB, voxelsB);
}
@Test
public void balanceVoxels_unbalancedLists_updatesLists() {
PottsLocation2D loc = new PottsLocation2D(new ArrayList<>());
ArrayList<Voxel> voxelsA = new ArrayList<>(voxelListAB);
ArrayList<Voxel> voxelsB = new ArrayList<>();
voxelsA.remove(new Voxel(3, 1, 0));
voxelsB.add(new Voxel(3, 1, 0));
PottsLocation.balanceVoxels(voxelsA, voxelsB, loc, randomDoubleZero);
voxelListA.sort(VOXEL_COMPARATOR);
voxelListB.sort(VOXEL_COMPARATOR);
voxelsA.sort(VOXEL_COMPARATOR);
voxelsB.sort(VOXEL_COMPARATOR);
assertEquals(voxelListA, voxelsA);
assertEquals(voxelListB, voxelsB);
}
@Test
public void balanceVoxels_unconnectedLists_updatesLists() {
PottsLocation2D loc = new PottsLocation2D(new ArrayList<>());
ArrayList<Voxel> voxelsA = new ArrayList<>();
ArrayList<Voxel> voxelsB = new ArrayList<>(voxelListAC);
voxelsB.add(new Voxel(1, 1, 0));
voxelsB.add(new Voxel(3, 1, 0));
voxelsB.remove(new Voxel(1, 0, 0));
voxelsA.add(new Voxel(1, 0, 0));
PottsLocation.balanceVoxels(voxelsA, voxelsB, loc, randomDoubleZero);
ArrayList<Voxel> voxelListAX = new ArrayList<>(voxelListA);
ArrayList<Voxel> voxelListCX = new ArrayList<>(voxelListC);
voxelListAX.add(new Voxel(1, 1, 0));
voxelListCX.add(new Voxel(3, 1, 0));
voxelListAX.sort(VOXEL_COMPARATOR);
voxelListCX.sort(VOXEL_COMPARATOR);
voxelsA.sort(VOXEL_COMPARATOR);
voxelsB.sort(VOXEL_COMPARATOR);
assertEquals(voxelListAX, voxelsA);
assertEquals(voxelListCX, voxelsB);
}
@Test
public void balanceVoxels_emptyList_updatesLists() {
PottsLocation2D loc = new PottsLocation2D(new ArrayList<>());
ArrayList<Voxel> voxelsA = new ArrayList<>(voxelListAB);
ArrayList<Voxel> voxelsB = new ArrayList<>();
PottsLocation.balanceVoxels(voxelsA, voxelsB, loc, randomDoubleZero);
assertTrue(voxelsA.size() != 0);
assertTrue(voxelsB.size() != 0);
}
@Test
public void split_balanceableLocationRandomZero_returnsList() {
PottsLocation2D loc = new PottsLocation2D(voxelListAB);
PottsLocation2D split = (PottsLocation2D) loc.split(randomDoubleZero);
ArrayList<Voxel> locVoxels = new ArrayList<>(voxelListA);
locVoxels.remove(new Voxel(1, 0, 0));
ArrayList<Voxel> splitVoxels = new ArrayList<>(voxelListB);
splitVoxels.add(new Voxel(1, 0, 0));
locVoxels.sort(VOXEL_COMPARATOR);
loc.voxels.sort(VOXEL_COMPARATOR);
splitVoxels.sort(VOXEL_COMPARATOR);
split.voxels.sort(VOXEL_COMPARATOR);
assertEquals(locVoxels, loc.voxels);
assertEquals(splitVoxels, split.voxels);
}
@Test
public void split_balanceableLocationRandomOne_returnsList() {
PottsLocation2D loc = new PottsLocation2D(voxelListAB);
PottsLocation2D split = (PottsLocation2D) loc.split(randomDoubleOne);
ArrayList<Voxel> locVoxels = new ArrayList<>(voxelListB);
ArrayList<Voxel> splitVoxels = new ArrayList<>(voxelListA);
locVoxels.sort(VOXEL_COMPARATOR);
loc.voxels.sort(VOXEL_COMPARATOR);
splitVoxels.sort(VOXEL_COMPARATOR);
split.voxels.sort(VOXEL_COMPARATOR);
assertEquals(locVoxels, loc.voxels);
assertEquals(splitVoxels, split.voxels);
}
@Test
public void getOffsetInApicalFrame2D_offsetAtCenter_returnsExpectedVoxel() {
ArrayList<Voxel> voxels = new ArrayList<>();
// 3x3 grid centered at (0,0)
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
voxels.add(new Voxel(x, y, 0));
}
}
PottsLocation2D loc = new PottsLocation2D(voxels);
Vector apicalAxis = new Vector(0, 1, 0); // Y-axis
ArrayList<Integer> offsets = new ArrayList<>();
offsets.add(50); // middle of X axis
offsets.add(50); // middle of Y axis
Voxel result = loc.getOffsetInApicalFrame(offsets, apicalAxis);
assertEquals(new Voxel(0, 0, 0), result);
}
@Test
public void getOffsetInApicalFrame2D_offsetUpperRight_returnsExpectedVoxel() {
ArrayList<Voxel> voxels = new ArrayList<>();
for (int x = 0; x <= 4; x++) {
for (int y = 0; y <= 4; y++) {
voxels.add(new Voxel(x, y, 0));
}
}
PottsLocation2D loc = new PottsLocation2D(voxels);
Vector apicalAxis = new Vector(0, 1, 0); // Y-axis
ArrayList<Integer> offsets = new ArrayList<>();
offsets.add(100); // far right of X axis
offsets.add(100); // top of Y axis
Voxel result = loc.getOffsetInApicalFrame(offsets, apicalAxis);
assertEquals(new Voxel(4, 4, 0), result);
}
@Test
public void getOffsetInApicalFrame2D_emptyVoxels_returnsNull() {
PottsLocation2D loc = new PottsLocation2D(new ArrayList<>());
Vector apicalAxis = new Vector(1, 0, 0);
ArrayList<Integer> offsets = new ArrayList<>();
offsets.add(50);
offsets.add(50);
Voxel result = loc.getOffsetInApicalFrame(offsets, apicalAxis);
assertNull(result);
}
@Test
public void getOffsetInApicalFrame2D_invalidOffset_throwsException() {
ArrayList<Voxel> voxels = new ArrayList<>();
voxels.add(new Voxel(0, 0, 0));
PottsLocation2D loc = new PottsLocation2D(voxels);
Vector apicalAxis = new Vector(1, 0, 0);
ArrayList<Integer> badOffset = new ArrayList<>();
badOffset.add(50); // only one element
assertThrows(
IllegalArgumentException.class,
() -> {
loc.getOffsetInApicalFrame(badOffset, apicalAxis);
});
}
@Test
public void getOffsetInApicalFrame2D_nonOrthogonalAxis_returnsExpected() {
ArrayList<Voxel> voxels = new ArrayList<>();
voxels.add(new Voxel(0, 0, 0));
voxels.add(new Voxel(1, 1, 0));
voxels.add(new Voxel(2, 2, 0));
voxels.add(new Voxel(3, 3, 0));
PottsLocation2D loc = new PottsLocation2D(voxels);
Vector apicalAxis = new Vector(1, 1, 0); // diagonal
ArrayList<Integer> offsets = new ArrayList<>();
offsets.add(0); // lowest orthogonal axis
offsets.add(100); // farthest along apical
Voxel result = loc.getOffsetInApicalFrame(offsets, apicalAxis);
assertEquals(new Voxel(3, 3, 0), result);
}
}