2011-04-30 14:47:42 +02:00
|
|
|
package jrummikub.model;
|
|
|
|
|
2011-04-30 17:23:57 +02:00
|
|
|
import java.util.ArrayList;
|
2011-05-01 00:13:22 +02:00
|
|
|
import java.util.Arrays;
|
2011-04-30 17:23:57 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import jrummikub.util.Pair;
|
|
|
|
import static jrummikub.model.StoneColor.*;
|
2011-05-03 19:06:12 +02:00
|
|
|
import static jrummikub.model.StoneSet.Type.*;
|
2011-04-30 17:23:57 +02:00
|
|
|
|
|
|
|
import org.junit.*;
|
|
|
|
import static org.junit.Assert.*;
|
2011-04-30 15:45:11 +02:00
|
|
|
|
|
|
|
public class StoneSetTest {
|
2011-04-30 14:47:42 +02:00
|
|
|
|
2011-05-01 00:13:22 +02:00
|
|
|
// Is Valid-Test
|
|
|
|
// valid
|
2011-05-03 19:06:12 +02:00
|
|
|
public void assertSet(StoneSet.Type expectedType, List<Stone> stones) {
|
2011-05-01 00:13:22 +02:00
|
|
|
StoneSet set = new StoneSet(stones);
|
2011-05-03 19:06:12 +02:00
|
|
|
assertSame(expectedType, set.classify());
|
2011-05-01 00:13:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void doubleJoker() {
|
2011-05-10 02:42:31 +02:00
|
|
|
assertSet(GROUP,
|
|
|
|
Arrays.asList(new Stone(RED), new Stone(BLACK), new Stone(1, BLACK)));
|
2011-05-01 00:13:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void groups() {
|
2011-05-03 19:06:12 +02:00
|
|
|
assertSet(GROUP, Arrays.asList(new Stone(1, RED), new Stone(1, BLACK),
|
2011-05-01 00:13:22 +02:00
|
|
|
new Stone(1, BLUE)));
|
2011-05-03 19:06:12 +02:00
|
|
|
assertSet(GROUP, Arrays.asList(new Stone(1, RED), new Stone(1, BLACK),
|
2011-05-01 00:13:22 +02:00
|
|
|
new Stone(1, BLUE), new Stone(1, ORANGE)));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void runs() {
|
2011-05-10 02:42:31 +02:00
|
|
|
assertSet(RUN,
|
|
|
|
Arrays.asList(new Stone(1, RED), new Stone(2, RED), new Stone(3, RED)));
|
2011-05-03 19:06:12 +02:00
|
|
|
assertSet(RUN, Arrays.asList(new Stone(4, BLUE), new Stone(5, BLUE),
|
2011-05-01 00:13:22 +02:00
|
|
|
new Stone(6, BLUE)));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void singleJoker() {
|
2011-05-10 02:42:31 +02:00
|
|
|
assertSet(GROUP,
|
|
|
|
Arrays.asList(new Stone(1, RED), new Stone(1, BLACK), new Stone(RED)));
|
|
|
|
assertSet(RUN,
|
|
|
|
Arrays.asList(new Stone(2, RED), new Stone(3, RED), new Stone(BLACK)));
|
2011-05-01 00:13:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// invalid
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void outOfBounds() {
|
2011-05-10 02:42:31 +02:00
|
|
|
assertSet(INVALID,
|
|
|
|
Arrays.asList(new Stone(RED), new Stone(1, RED), new Stone(2, RED)));
|
|
|
|
assertSet(INVALID,
|
|
|
|
Arrays.asList(new Stone(12, RED), new Stone(13, RED), new Stone(RED)));
|
2011-05-03 19:06:12 +02:00
|
|
|
assertSet(INVALID, Arrays.asList(new Stone(RED), new Stone(BLACK),
|
2011-05-01 00:13:22 +02:00
|
|
|
new Stone(1, RED), new Stone(2, RED)));
|
|
|
|
}
|
2011-05-01 01:05:56 +02:00
|
|
|
|
2011-05-01 00:13:22 +02:00
|
|
|
@Test
|
|
|
|
public void sameColor() {
|
2011-05-10 02:42:31 +02:00
|
|
|
assertSet(INVALID,
|
|
|
|
Arrays.asList(new Stone(1, RED), new Stone(1, RED), new Stone(1, BLUE)));
|
2011-05-03 19:06:12 +02:00
|
|
|
assertSet(INVALID, Arrays.asList(new Stone(1, RED), new Stone(1, BLUE),
|
2011-05-01 00:13:22 +02:00
|
|
|
new Stone(1, BLACK), new Stone(1, ORANGE), new Stone(RED)));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void incorrectOrder() {
|
2011-05-10 02:42:31 +02:00
|
|
|
assertSet(INVALID,
|
|
|
|
Arrays.asList(new Stone(4, RED), new Stone(6, RED), new Stone(5, RED)));
|
|
|
|
assertSet(INVALID,
|
|
|
|
Arrays.asList(new Stone(4, RED), new Stone(6, RED), new Stone(RED)));
|
|
|
|
assertSet(INVALID,
|
|
|
|
Arrays.asList(new Stone(4, RED), new Stone(RED), new Stone(5, RED)));
|
2011-05-01 00:13:22 +02:00
|
|
|
}
|
2011-05-01 01:05:56 +02:00
|
|
|
|
2011-05-01 00:13:22 +02:00
|
|
|
@Test
|
|
|
|
public void otherInvalid() {
|
2011-05-01 01:05:56 +02:00
|
|
|
|
2011-05-10 02:42:31 +02:00
|
|
|
assertSet(INVALID,
|
|
|
|
Arrays.asList(new Stone(4, RED), new Stone(5, RED), new Stone(7, RED)));
|
|
|
|
assertSet(INVALID,
|
|
|
|
Arrays.asList(new Stone(4, RED), new Stone(5, BLUE), new Stone(6, RED)));
|
2011-05-03 19:06:12 +02:00
|
|
|
assertSet(INVALID, Arrays.asList(new Stone(4, RED), new Stone(5, RED)));
|
2011-05-10 02:42:31 +02:00
|
|
|
assertSet(INVALID,
|
|
|
|
Arrays.asList(new Stone(4, BLUE), new Stone(5, RED), new Stone(6, RED)));
|
2011-05-09 20:27:47 +02:00
|
|
|
// Regression test:
|
2011-05-10 02:42:31 +02:00
|
|
|
assertSet(INVALID, Arrays.asList(new Stone(12, ORANGE),
|
|
|
|
new Stone(12, BLACK), new Stone(7, BLUE)));
|
2011-05-01 00:13:22 +02:00
|
|
|
}
|
2011-05-01 01:05:56 +02:00
|
|
|
|
2011-04-30 17:23:57 +02:00
|
|
|
// invalid Split
|
2011-05-03 19:06:14 +02:00
|
|
|
@Test
|
2011-04-30 17:23:57 +02:00
|
|
|
public void testSplitInvalidLow() {
|
|
|
|
StoneSet testSet = createTestSet();
|
2011-05-03 19:06:14 +02:00
|
|
|
assertNull(testSet.splitAt(0).getFirst());
|
2011-04-30 17:23:57 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-05-03 19:06:14 +02:00
|
|
|
@Test
|
2011-04-30 17:23:57 +02:00
|
|
|
public void testSplitInvalidHigh() {
|
|
|
|
StoneSet testSet = createTestSet();
|
2011-05-03 19:06:14 +02:00
|
|
|
assertNull(testSet.splitAt(3).getSecond());
|
2011-04-30 17:23:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// valid Split
|
|
|
|
@Test
|
|
|
|
public void testSplitValid() {
|
|
|
|
StoneSet testSet = createTestSet();
|
|
|
|
Pair<StoneSet, StoneSet> newSets = testSet.splitAt(1);
|
|
|
|
// Sets have right size
|
|
|
|
assertEquals(1, newSets.getFirst().size());
|
|
|
|
assertEquals(2, newSets.getSecond().size());
|
|
|
|
// Set have right Stones
|
|
|
|
assertSame(testSet.get(0), newSets.getFirst().get(0));
|
|
|
|
assertSame(testSet.get(1), newSets.getSecond().get(0));
|
|
|
|
assertSame(testSet.get(2), newSets.getSecond().get(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
private StoneSet createTestSet() {
|
|
|
|
List<Stone> stones = new ArrayList<Stone>();
|
2011-05-03 17:13:12 +02:00
|
|
|
stones.add(new Stone(1, BLUE));
|
|
|
|
stones.add(new Stone(1, RED));
|
|
|
|
stones.add(new Stone(1, BLACK));
|
2011-04-30 17:23:57 +02:00
|
|
|
StoneSet testSet = new StoneSet(stones);
|
|
|
|
return testSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
// join
|
|
|
|
@Test
|
|
|
|
public void testJoin() {
|
|
|
|
StoneSet testSet = createTestSet();
|
2011-05-03 17:13:12 +02:00
|
|
|
StoneSet secondSet = new StoneSet(new Stone(2, BLUE));
|
2011-04-30 17:23:57 +02:00
|
|
|
StoneSet joinedSet = testSet.join(secondSet);
|
|
|
|
// Sets have right size
|
|
|
|
assertEquals(4, joinedSet.size());
|
|
|
|
// Set have right Stones
|
|
|
|
assertSame(testSet.get(0), joinedSet.get(0));
|
|
|
|
assertSame(testSet.get(1), joinedSet.get(1));
|
|
|
|
assertSame(testSet.get(2), joinedSet.get(2));
|
|
|
|
assertSame(secondSet.get(0), joinedSet.get(3));
|
|
|
|
}
|
2011-05-01 00:13:22 +02:00
|
|
|
|
|
|
|
// iterator
|
2011-04-30 18:21:22 +02:00
|
|
|
@Test
|
|
|
|
public void testIterator() {
|
2011-05-01 00:13:22 +02:00
|
|
|
StoneSet testSet = createTestSet();
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (Stone stone : testSet) {
|
|
|
|
assertSame(stone, testSet.get(i));
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(i, testSet.size());
|
2011-04-30 18:21:22 +02:00
|
|
|
}
|
2011-05-10 02:42:31 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testToString() {
|
|
|
|
StoneSet testSet = new StoneSet(Arrays.asList(new Stone(2, BLUE),
|
|
|
|
new Stone(3, BLUE), new Stone(4, BLUE)));
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
"StoneSet[Stone[value=2,color=BLUE],Stone[value=3,color=BLUE],Stone[value=4,color=BLUE]]",
|
|
|
|
testSet.toString());
|
|
|
|
}
|
2011-04-30 14:47:42 +02:00
|
|
|
}
|