summaryrefslogtreecommitdiffstats
path: root/test/jrummikub/model/StoneSetTest.java
blob: e2962d4a817785592a6f940c092a3d673d0ccf63 (plain)
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
package jrummikub.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import jrummikub.util.Pair;
import static jrummikub.model.StoneColor.*;
import static jrummikub.model.StoneSet.Type.*;

import org.junit.*;
import static org.junit.Assert.*;

/**
 * Tests for {@link StoneSet}
 */
public class StoneSetTest {

	private void assertSet(StoneSet.Type expectedType, List<Stone> stones) {
		StoneSet set = new StoneSet(stones);
		assertSame(expectedType, set.classify());
	}

	// valid
	/** */
	@Test
	public void doubleJoker() {
		assertSet(GROUP,
				Arrays.asList(new Stone(RED), new Stone(BLACK), new Stone(1, BLACK)));
	}

	/** */
	@Test
	public void groups() {
		assertSet(GROUP, Arrays.asList(new Stone(1, RED), new Stone(1, BLACK),
				new Stone(1, BLUE)));
		assertSet(GROUP, Arrays.asList(new Stone(1, RED), new Stone(1, BLACK),
				new Stone(1, BLUE), new Stone(1, ORANGE)));
	}

	/** */
	@Test
	public void runs() {
		assertSet(RUN,
				Arrays.asList(new Stone(1, RED), new Stone(2, RED), new Stone(3, RED)));
		assertSet(RUN, Arrays.asList(new Stone(4, BLUE), new Stone(5, BLUE),
				new Stone(6, BLUE)));
	}

	/** */
	@Test
	public void singleJoker() {
		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)));
	}

	// invalid
	/** */
	@Test
	public void outOfBounds() {
		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)));
		assertSet(INVALID, Arrays.asList(new Stone(RED), new Stone(BLACK),
				new Stone(1, RED), new Stone(2, RED)));
	}

	/** */
	@Test
	public void sameColor() {
		assertSet(INVALID,
				Arrays.asList(new Stone(1, RED), new Stone(1, RED), new Stone(1, BLUE)));
		assertSet(INVALID, Arrays.asList(new Stone(1, RED), new Stone(1, BLUE),
				new Stone(1, BLACK), new Stone(1, ORANGE), new Stone(RED)));
	}

	/** */
	@Test
	public void incorrectOrder() {
		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)));
	}

	/** */
	@Test
	public void otherInvalid() {

		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)));
		assertSet(INVALID, Arrays.asList(new Stone(4, RED), new Stone(5, RED)));
		assertSet(INVALID,
				Arrays.asList(new Stone(4, BLUE), new Stone(5, RED), new Stone(6, RED)));
		// Regression test:
		assertSet(INVALID, Arrays.asList(new Stone(12, ORANGE), new Stone(12,
				BLACK), new Stone(7, BLUE)));
	}

	// invalid Split
	/** */
	@Test
	public void testSplitInvalidLow() {
		StoneSet testSet = createTestSet();
		assertNull(testSet.splitAt(0).getFirst());

	}

	/** */
	@Test
	public void testSplitInvalidHigh() {
		StoneSet testSet = createTestSet();
		assertNull(testSet.splitAt(3).getSecond());
	}

	// 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>();
		stones.add(new Stone(1, BLUE));
		stones.add(new Stone(1, RED));
		stones.add(new Stone(1, BLACK));
		StoneSet testSet = new StoneSet(stones);
		return testSet;
	}

	// join
	/** */
	@Test
	public void testJoin() {
		StoneSet testSet = createTestSet();
		StoneSet secondSet = new StoneSet(new Stone(2, BLUE));
		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));
	}

	// iterator
	/** */
	@Test
	public void testIterator() {
		StoneSet testSet = createTestSet();
		int i = 0;

		for (Stone stone : testSet) {
			assertSame(stone, testSet.get(i));
			i++;
		}

		assertEquals(i, testSet.size());
	}

	/**
	 * String for Testing
	 */
	@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());
	}
}