summaryrefslogtreecommitdiffstats
path: root/test/jrummikub/model/StoneSetTest.java
blob: 6f6e69890d8ecdf1af4ef6cfd8b518b205fea158 (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
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
371
372
package jrummikub.model;

import static jrummikub.model.StoneColor.*;
import static jrummikub.model.StoneSet.Type.*;
import static org.junit.Assert.*;

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

import jrummikub.util.Pair;

import org.junit.Before;
import org.junit.Test;

/**
 * Tests for {@link StoneSet}
 */
public class StoneSetTest {
	private GameSettings defaultSettings = new GameSettings();
	private GameSettings moreColorSettings = new GameSettings();
	private GameSettings lessColorSettings = new GameSettings();
	private GameSettings higherValueSettings = new GameSettings();
	private GameSettings lowerValueSettings = new GameSettings();
	private GameSettings noLimitsSettings = new GameSettings();
	private GameSettings noLimitsLowerValueSettings = new GameSettings();
	private GameSettings moreColorLowerValueSettings = new GameSettings();

	/** */
	@Before
	public void setUpSettings() {
		moreColorSettings.setStoneColors(EnumSet.allOf(StoneColor.class));
		lessColorSettings.setStoneColors(new HashSet<StoneColor>(Arrays.asList(
				StoneColor.BLUE, StoneColor.RED, StoneColor.BLACK)));
		higherValueSettings.setHighestValue(17);
		lowerValueSettings.setHighestValue(10);
		noLimitsSettings.setNoLimits(true);
		noLimitsLowerValueSettings.setHighestValue(10);
		noLimitsLowerValueSettings.setNoLimits(true);
		moreColorLowerValueSettings.setStoneColors(EnumSet
				.allOf(StoneColor.class));
		moreColorLowerValueSettings.setHighestValue(5);
	}

	private void assertSet(StoneSet.Type expectedType, Integer expectedValue,
			List<Stone> stones, GameSettings settings) {
		StoneSet set = new StoneSet(stones);
		assertSame(expectedType, set.classify(settings).getFirst());
		assertEquals(expectedValue, set.classify(settings).getSecond());
	}

	// valid+point count
	/** */
	@Test
	public void doubleJokerValid3Stones() {
		// JJZ
		assertSet(GROUP, 3, Arrays.asList(new Stone(RED), new Stone(BLACK),
				new Stone(1, BLACK)), defaultSettings);
		// JZJ
		assertSet(GROUP, 3, Arrays.asList(new Stone(RED), new Stone(1, BLACK),
				new Stone(BLACK)), defaultSettings);
		// ZJJ
		assertSet(RUN, 6, Arrays.asList(new Stone(1, RED), new Stone(RED),
				new Stone(BLACK)), defaultSettings);
		assertSet(RUN, 33, Arrays.asList(new Stone(10, RED), new Stone(RED),
				new Stone(BLACK)), defaultSettings);
		assertSet(GROUP, 30, Arrays.asList(new Stone(10, RED), new Stone(RED),
				new Stone(BLACK)), lowerValueSettings);
		assertSet(GROUP, 39, Arrays.asList(new Stone(13, RED), new Stone(RED),
				new Stone(BLACK)), defaultSettings);
		assertSet(RUN, 42, Arrays.asList(new Stone(13, RED), new Stone(RED),
				new Stone(BLACK)), higherValueSettings);
	}

	/** */
	@Test
	public void doubleJokerValid4Stones() {
		// 4 Stones
		// JJZZ
		assertSet(GROUP, 4, Arrays.asList(new Stone(RED), new Stone(BLACK),
				new Stone(1, BLACK), new Stone(1, RED)), defaultSettings);
		assertSet(RUN, 10, Arrays.asList(new Stone(RED), new Stone(BLACK),
				new Stone(3, RED), new Stone(4, RED)), defaultSettings);
		// ZZJJ
		assertSet(RUN, 10, Arrays.asList(new Stone(1, RED), new Stone(2, RED),
				new Stone(BLACK), new Stone(RED)), defaultSettings);
		assertSet(GROUP, 4, Arrays.asList(new Stone(1, RED),
				new Stone(1, BLACK), new Stone(BLACK), new Stone(RED)),
				defaultSettings);
		// ZJZJ
		assertSet(GROUP, 4, Arrays.asList(new Stone(1, RED), new Stone(BLACK),
				new Stone(1, BLACK), new Stone(RED)), defaultSettings);
		assertSet(RUN, 10, Arrays.asList(new Stone(1, RED), new Stone(RED),
				new Stone(3, RED), new Stone(BLACK)), defaultSettings);
		// JZJZ
		assertSet(GROUP, 4, Arrays.asList(new Stone(RED), new Stone(1, BLACK),
				new Stone(BLACK), new Stone(1, RED)), defaultSettings);
		assertSet(RUN, 10, Arrays.asList(new Stone(RED), new Stone(2, RED),
				new Stone(BLACK), new Stone(4, RED)), defaultSettings);
		// JZZJ
		assertSet(GROUP, 4, Arrays.asList(new Stone(RED), new Stone(1, BLACK),
				new Stone(1, RED), new Stone(BLACK)), defaultSettings);
		assertSet(RUN, 10, Arrays.asList(new Stone(RED), new Stone(2, RED),
				new Stone(3, RED), new Stone(BLACK)), defaultSettings);
		// ZJJZ
		assertSet(GROUP, 4, Arrays.asList(new Stone(1, RED), new Stone(BLACK),
				new Stone(RED), new Stone(1, BLACK)), defaultSettings);
		assertSet(RUN, 10, Arrays.asList(new Stone(1, RED), new Stone(RED),
				new Stone(BLACK), new Stone(4, RED)), defaultSettings);
		// More than 4 stones
		assertSet(GROUP, 6, Arrays.asList(new Stone(1, RED),
				new Stone(1, BLUE), new Stone(1, GREEN), new Stone(1, BLACK),
				new Stone(BLACK), new Stone(RED)), moreColorSettings);
	}

	/** */
	@Test
	public void groups() {
		assertSet(GROUP, 3, Arrays.asList(new Stone(1, RED),
				new Stone(1, BLACK), new Stone(1, BLUE)), defaultSettings);
		assertSet(GROUP, 3, Arrays.asList(new Stone(1, RED),
				new Stone(1, BLACK), new Stone(1, BLUE)), lessColorSettings);
		assertSet(GROUP, 4, Arrays.asList(new Stone(1, RED),
				new Stone(1, BLACK), new Stone(1, BLUE), new Stone(1, ORANGE)),
				defaultSettings);
		assertSet(GROUP, 6, Arrays.asList(new Stone(1, RED),
				new Stone(1, GREEN), new Stone(1, GRAY), new Stone(1, BLACK),
				new Stone(1, BLUE), new Stone(1, ORANGE)), moreColorSettings);
	}

	/** */
	@Test
	public void runs() {
		assertSet(RUN, 6, Arrays.asList(new Stone(1, RED), new Stone(2, RED),
				new Stone(3, RED)), defaultSettings);
		assertSet(RUN, 6, Arrays.asList(new Stone(1, RED), new Stone(2, RED),
				new Stone(3, RED)), lowerValueSettings);
		assertSet(RUN, 6, Arrays.asList(new Stone(1, RED), new Stone(2, RED),
				new Stone(3, RED)), higherValueSettings);
		assertSet(RUN, 22, Arrays.asList(new Stone(4, BLUE),
				new Stone(5, BLUE), new Stone(6, BLUE), new Stone(7, BLUE)),
				defaultSettings);
		assertSet(RUN, 42, Arrays.asList(new Stone(13, RED),
				new Stone(14, RED), new Stone(15, RED)), higherValueSettings);
	}

	/** */
	@Test
	public void singleJoker() {
		// ZJZ
		assertSet(GROUP, 3, Arrays.asList(new Stone(1, RED), new Stone(BLACK),
				new Stone(1, BLACK)), defaultSettings);
		assertSet(RUN, 6, Arrays.asList(new Stone(1, RED), new Stone(RED),
				new Stone(3, RED)), defaultSettings);
		// JZZ
		assertSet(GROUP, 3, Arrays.asList(new Stone(RED), new Stone(1, RED),
				new Stone(1, BLACK)), defaultSettings);
		assertSet(RUN, 6, Arrays.asList(new Stone(RED), new Stone(2, RED),
				new Stone(3, RED)), defaultSettings);
		// ZZJ
		assertSet(GROUP, 3, Arrays.asList(new Stone(1, RED),
				new Stone(1, BLACK), new Stone(BLACK)), defaultSettings);
		assertSet(RUN, 6, Arrays.asList(new Stone(1, RED), new Stone(2, RED),
				new Stone(RED)), defaultSettings);
		assertSet(RUN, 39, Arrays.asList(new Stone(12, RED),
				new Stone(13, RED), new Stone(RED)), higherValueSettings);
	}

	/** */
	@Test
	public void noLimitsTest() {
		assertSet(RUN, 16, Arrays.asList(new Stone(RED), new Stone(1, RED),
				new Stone(2, RED)), noLimitsSettings);
		assertSet(RUN, 26, Arrays.asList(new Stone(12, RED),
				new Stone(13, RED), new Stone(RED)), noLimitsSettings);
		assertSet(RUN, 20, Arrays.asList(new Stone(9, RED), new Stone(10, RED),
				new Stone(RED)), noLimitsLowerValueSettings);
		assertSet(RUN, 28, Arrays.asList(new Stone(RED), new Stone(BLACK),
				new Stone(1, RED), new Stone(2, RED)), noLimitsSettings);
		assertSet(RUN, 26, Arrays.asList(new Stone(12, RED),
				new Stone(13, RED), new Stone(1, RED)), noLimitsSettings);
		assertSet(RUN, 16, Arrays.asList(new Stone(13, RED), new Stone(1, RED),
				new Stone(2, RED)), noLimitsSettings);

		assertSet(INVALID, 0, Arrays.asList(new Stone(13, RED), new Stone(RED),
				new Stone(1, RED)), noLimitsSettings);
	}

	// invalid
	/** */
	@Test
	public void doubleJokerInvalid() {
		assertSet(INVALID, 0, Arrays.asList(new Stone(RED), new Stone(BLACK),
				new Stone(1, BLACK), new Stone(1, RED)), lessColorSettings);
		// More than 4 stones
		assertSet(INVALID, 0, Arrays.asList(new Stone(1, RED), new Stone(1,
				BLUE), new Stone(1, GREEN), new Stone(1, BLACK), new Stone(1,
				VIOLET), new Stone(1, ORANGE), new Stone(1, AQUA), new Stone(
				BLACK), new Stone(RED)), moreColorSettings);
	}

	/** */
	@Test
	public void outOfBounds() {
		assertSet(INVALID, 0, Arrays.asList(new Stone(RED), new Stone(1, RED),
				new Stone(2, RED)), defaultSettings);
		assertSet(INVALID, 0, Arrays.asList(new Stone(12, RED), new Stone(13,
				RED), new Stone(RED)), defaultSettings);
		assertSet(INVALID, 0, Arrays.asList(new Stone(9, RED), new Stone(10,
				RED), new Stone(RED)), lowerValueSettings);
		assertSet(INVALID, 0, Arrays.asList(new Stone(RED), new Stone(BLACK),
				new Stone(1, RED), new Stone(2, RED)), defaultSettings);
		assertSet(INVALID, 0, Arrays.asList(new Stone(12, RED), new Stone(13,
				RED), new Stone(1, RED)), defaultSettings);
		assertSet(INVALID, 0, Arrays.asList(new Stone(13, RED), new Stone(1,
				RED), new Stone(2, RED)), defaultSettings);
	}

	/** */
	@Test
	public void sameColor() {
		assertSet(INVALID, 0, Arrays.asList(new Stone(1, RED),
				new Stone(1, RED), new Stone(1, BLUE)), defaultSettings);
		assertSet(INVALID, 0, Arrays.asList(new Stone(1, RED), new Stone(1,
				BLUE), new Stone(1, BLACK), new Stone(1, ORANGE),
				new Stone(RED)), defaultSettings);
		assertSet(INVALID, 0, Arrays.asList(new Stone(1, RED), new Stone(1,
				BLUE), new Stone(1, GREEN), new Stone(1, BLACK), new Stone(1,
				VIOLET), new Stone(1, GRAY), new Stone(1, ORANGE), new Stone(1,
				AQUA), new Stone(RED)), moreColorSettings);
	}

	/** */
	@Test
	public void incorrectOrder() {
		assertSet(INVALID, 0, Arrays.asList(new Stone(4, RED),
				new Stone(6, RED), new Stone(5, RED)), defaultSettings);
		assertSet(INVALID, 0, Arrays.asList(new Stone(4, RED),
				new Stone(6, RED), new Stone(RED)), defaultSettings);
		assertSet(INVALID, 0, Arrays.asList(new Stone(4, RED), new Stone(RED),
				new Stone(5, RED)), defaultSettings);
	}

	/** */
	@Test
	public void otherInvalid() {
		assertSet(INVALID, 0, Arrays.asList(new Stone(4, RED),
				new Stone(5, RED), new Stone(7, RED)), defaultSettings);
		assertSet(INVALID, 0, Arrays.asList(new Stone(4, RED), new Stone(5,
				BLUE), new Stone(6, RED)), defaultSettings);
		assertSet(INVALID, 0, Arrays.asList(new Stone(4, RED),
				new Stone(5, RED)), defaultSettings);
		assertSet(INVALID, 0, Arrays.asList(new Stone(4, BLUE), new Stone(5,
				RED), new Stone(6, RED)), defaultSettings);
		// Regression test:
		assertSet(INVALID, 0, Arrays.asList(new Stone(12, ORANGE), new Stone(
				12, BLACK), new Stone(7, BLUE)), defaultSettings);
	}

	/** */
	@Test
	public void manyJokersValid() {
		assertSet(GROUP, 3 * 13, Arrays.asList(new Stone(RED), new Stone(RED),
				new Stone(RED)), defaultSettings);
		assertSet(GROUP, 4 * 13, Arrays.asList(new Stone(RED), new Stone(RED),
				new Stone(RED), new Stone(RED)), defaultSettings);
		assertSet(RUN, 13 + 12 + 11 + 10 + 9,
				Arrays.asList(new Stone(RED), new Stone(RED), new Stone(RED),
						new Stone(RED), new Stone(RED)), defaultSettings);
		assertSet(RUN, 5 * 10, Arrays.asList(new Stone(RED), new Stone(RED),
				new Stone(RED), new Stone(RED), new Stone(12, RED)),
				defaultSettings);
		assertSet(GROUP, 6 * 5, Arrays
				.asList(new Stone(RED), new Stone(RED), new Stone(RED),
						new Stone(RED), new Stone(RED), new Stone(AQUA)),
				moreColorLowerValueSettings);
	}

	/** */
	@Test
	public void manyJokersInvalid() {
		List<Stone> stones = new ArrayList<Stone>();
		for (int i = 0; i < 14; i++) {
			stones.add(new Stone(RED));
		}
		assertSet(INVALID, 0, stones, defaultSettings);
	}

	// 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().getSize());
		assertEquals(2, newSets.getSecond().getSize());
		// 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.getSize());
		// 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.getSize());
	}

	/**
	 * 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[[BLUE 2],[BLUE 3],[BLUE 4]]", testSet.toString());
	}
}