Refactored BlockData test case to reduce code duplication.

This commit is contained in:
TomyLobo 2011-09-29 07:24:48 +02:00
parent ccfabd94b9
commit f354ab392f

View File

@ -64,6 +64,13 @@ public class BlockDataTest {
} }
} }
private static final TreeSet<Integer> datasTemplate = new TreeSet<Integer>();
static {
for (int data = 0; data < 16; ++data) {
datasTemplate.add(data);
}
}
@Test @Test
public void testCycle() { public void testCycle() {
// Test monotony // Test monotony
@ -84,35 +91,14 @@ public class BlockDataTest {
} }
} }
// Test cyclicity // Test cyclicity forwards
final TreeSet<Integer> datasTemplate = new TreeSet<Integer>(); testCycle(1);
for (int data = 0; data < 16; ++data) {
datasTemplate.add(data);
}
// Forwards...
for (int type = 0; type < 256; ++type) {
@SuppressWarnings("unchecked")
final TreeSet<Integer> datas = (TreeSet<Integer>) datasTemplate.clone();
while (!datas.isEmpty()) {
final int start = datas.pollFirst();
String message = type+"/"+start;
int current = start;
boolean first = true;
while (true) {
current = BlockData.cycle(type, current, 1);
if (first && current == -1) break;
first = false;
message += "->"+current;
assertTrue(message, current >= 0);
assertTrue(message, current < 16);
if (current == start) break;
assertTrue(message, datas.remove(current));
}
}
}
// ...and backwards // ...and backwards
testCycle(-1);
}
private static void testCycle(final int increment) {
for (int type = 0; type < 256; ++type) { for (int type = 0; type < 256; ++type) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final TreeSet<Integer> datas = (TreeSet<Integer>) datasTemplate.clone(); final TreeSet<Integer> datas = (TreeSet<Integer>) datasTemplate.clone();
@ -122,7 +108,7 @@ public class BlockDataTest {
int current = start; int current = start;
boolean first = true; boolean first = true;
while (true) { while (true) {
current = BlockData.cycle(type, current, -1); current = BlockData.cycle(type, current, increment);
if (first && current == -1) break; if (first && current == -1) break;
first = false; first = false;
message += "->"+current; message += "->"+current;
@ -135,4 +121,3 @@ public class BlockDataTest {
} }
} }
} }