forked from ExplorerJun/java-baseball
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBallCombiGeneratorTest.java
More file actions
124 lines (99 loc) · 3.19 KB
/
BallCombiGeneratorTest.java
File metadata and controls
124 lines (99 loc) · 3.19 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
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
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
public class BallCombiGeneratorTest {
private BallCombiGenerator generator;
@Before
public void setUp() throws Exception {
generator = new BallCombiGenerator();
}
@Test
public void genBallCombi_generatedBallCombiAllValid() {
int n = 100;
for (int i = 0; i < n; i++) {
int [] ballCombi = generator.genBallCombi();
// System.out.println(Arrays.toString(ballCombi));
// System.out.flush();
boolean got = generator.isValid(ballCombi);
Assert.assertTrue(String.format("[%d] got: %b\n", i, got), got);
}
}
@Test
public void isValid_possibleValidBallCombis() {
// 입력
int [][] possibleBallCombis = genPossibleBallCombis();
for (int [] ballCombi: possibleBallCombis) {
// System.out.println(Arrays.toString(ballCombi));
// System.out.flush();
boolean got = generator.isValid(ballCombi);
Assert.assertTrue(String.format("ballCombi: %s", Arrays.toString(ballCombi)), got);
}
}
@Test
public void isValid_invalidBallCombis() {
// 입력
int [][] ballCombis = {
{1, 1, 1, 1},
{0, 1, 2},
{1, 2, 2},
{9, 9, 9},
};
for(int i = 0; i < ballCombis.length; i++) {
int [] ballCombi = ballCombis[i];
boolean got = generator.isValid(ballCombi);
Assert.assertFalse(String.format("[%d] ", i), got);
}
}
@Test
public void toBallCombi_usualInputStr() {
//
String [] strs = {
"hello",
"012344",
"adkfj",
"023",
"987",
"345",
};
int [][] wants = {
{},
{},
{},
{},
{9, 8, 7},
{3, 4, 5},
};
for (int i = 0; i < strs.length; i++) {
String str = strs[i];
int [] want = wants[i];
int [] got = generator.toBallCombi(str);
Assert.assertArrayEquals(
String.format("[%d] want: %s, got: %s", i, Arrays.toString(want), Arrays.toString(got)),
want,
got
);
}
}
private int[][] genPossibleBallCombis() {
int used[] = new int [10];
ArrayList<int[]> combis = new ArrayList<>();
for (int p1 = 1; p1 <= 9; p1++) {
used[p1]++;
for (int p2 = 1; p2 <= 9; p2++) {
if (used[p2] != 0) continue;
used[p2]++;
for(int p3 = 1; p3 <= 9; p3++) {
if (used[p3] != 0) continue;
// 생성해서 바로 넣는 방법이 없으려나??..
int [] ballCombi = {p1, p2, p3};
combis.add(ballCombi);
}
used[p2]--;
}
used[p1]--;
}
return combis.toArray(new int[combis.size()][]);
}
}