forked from On-Time-Software-Developers/ConsoleGameHub-OSD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMasterMindGameTest.java
More file actions
45 lines (30 loc) · 1.05 KB
/
MasterMindGameTest.java
File metadata and controls
45 lines (30 loc) · 1.05 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
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.*;
import java.util.regex.*;
public class MasterMindGameTest {
private static final String SYMBOLS = "0123456789";
private static final Pattern CODE_PATTERN =
Pattern.compile("[" + Pattern.quote(SYMBOLS) + "]{4}");
@Test
void isExactlyFourSymbols() {
String code = CodeGenerator.generateCode();
assertEquals(4, code.length(), "Code should be 4 symbol long");
}
@Test
void areFromSymbolSet() {
String code = CodeGenerator.generateCode();
assertTrue(CODE_PATTERN.matcher(code).matches(), "Code must be from this symbol set: " + SYMBOLS);
}
@Test
void isNotMoreThanTwiceTheSize() {
Map<String, Integer> counts = new HashMap<>();
int sampleSize = 100;
for (int i = 0; i < sampleSize; i++) {
String code = CodeGenerator.generateCode();
counts.merge(code, 1, Integer::sum);
}
counts.forEach((code, count) ->
assertTrue(count <= 2, "Code '" + code + "' appeared " + count + " times"));
}
}