-
Notifications
You must be signed in to change notification settings - Fork 2
/
lottery_test.go
203 lines (166 loc) · 3.98 KB
/
lottery_test.go
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
package lottery_test
import (
"fmt"
"testing"
"github.com/golang/mock/gomock"
"github.com/kyokomi/lottery"
)
type DropItem struct {
ItemName string
DropProb int
}
func (d DropItem) Prob() int {
return d.DropProb
}
var _ lottery.Interface = (*DropItem)(nil)
type Trap struct {
TrapName string
prob int
}
func (t Trap) Prob() int {
return t.prob
}
var _ lottery.Interface = (*Trap)(nil)
func TestLots(t *testing.T) {
l := lottery.NewDefault()
dropItems := []lottery.Interface{
DropItem{ItemName: "エリクサ", DropProb: 5},
DropItem{ItemName: "エーテル", DropProb: 10},
DropItem{ItemName: "ポーション", DropProb: 20},
DropItem{ItemName: "ハズレ", DropProb: 50},
Trap{TrapName: "地雷", prob: 5},
Trap{TrapName: "トラバサミ", prob: 10},
}
check := 2000000
countMap := map[lottery.Interface]int{}
for i := 0; i < check; i++ {
lotIdx := l.Lots(dropItems...)
if lotIdx == -1 {
t.Fatal("lot error")
}
switch d := dropItems[lotIdx].(type) {
case DropItem, Trap:
countMap[d]++
}
}
for item, count := range countMap {
result := float64(count) / float64(check) * 100
prob := float64(item.Prob())
name := ""
switch t := item.(type) {
case DropItem:
name = t.ItemName
case Trap:
name = t.TrapName
}
// 0.1 check
if (prob-0.1) <= result && result < (prob+0.1) {
fmt.Printf("ok %3.5f%%(%7d) : %s\n", result, count, name)
} else {
t.Errorf("error %3.5f%%(%7d) : %s\n", result, count, name)
}
}
}
func TestLot(t *testing.T) {
l := lottery.NewDefault()
check := 1000000
prob := float64(4.0) // 4%
count := 0
for i := 0; i < check; i++ {
if l.Lot(int(prob)) {
count++
}
}
result := float64(count) / float64(check) * 100
// 0.1 check
if (prob-0.1) <= result && result < (prob+0.1) {
fmt.Printf("lottery ok %f%%\n", result)
} else {
t.Errorf("lottery error %f%%", result)
}
}
func TestLotOf(t *testing.T) {
l := lottery.NewDefault()
check := 1000000
prob := float64(0.5) // 0.5%
count := 0
for i := 0; i < check; i++ {
// 10000 minutes rate
if l.LotOf(int(prob/100*10000), 10000) {
count++
}
}
result := float64(count) / float64(check) * 100
// 0.1 check
if (prob-0.1) <= result && result < (prob+0.1) {
fmt.Printf("lottery ok %f%%\n", result)
} else {
t.Errorf("lottery error %f%%", result)
}
}
func TestLot_0to100(t *testing.T) {
l := lottery.NewDefault()
testCases := []struct {
prob int
result bool
}{
{prob: 120, result: true},
{prob: 100, result: true},
{prob: 0, result: false},
{prob: -1, result: false},
}
for _, testCase := range testCases {
if l.Lot(testCase.prob) != testCase.result {
t.Errorf("lottery error not %x%%", testCase.prob)
}
}
}
func TestLots_error(t *testing.T) {
l := lottery.NewDefault()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
probMock := lottery.NewMockInterface(ctrl)
probMock.EXPECT().Prob().Return(0)
probMock.EXPECT().Prob().Return(0)
idx := l.Lots(probMock, probMock)
if idx != -1 {
t.Errorf("lots idx error %d != %d", idx, -1)
}
}
func TestLotMock(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
lotMock := lottery.NewMockLottery(ctrl)
lotMock.EXPECT().Lot(1).Return(true)
lotMock.EXPECT().Lot(2).Return(false)
if !lotMock.Lot(1) {
t.Errorf("mock error")
}
if lotMock.Lot(2) {
t.Errorf("mock error")
}
}
func TestLotOfMock(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
lotMock := lottery.NewMockLottery(ctrl)
lotMock.EXPECT().LotOf(1, 100).Return(true)
lotMock.EXPECT().LotOf(2, 100).Return(false)
if !lotMock.LotOf(1, 100) {
t.Errorf("mock error")
}
if lotMock.LotOf(2, 100) {
t.Errorf("mock error")
}
}
func TestLotsMock(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
probMock1 := lottery.NewMockInterface(ctrl)
probMock2 := lottery.NewMockInterface(ctrl)
lotMock := lottery.NewMockLottery(ctrl)
lotMock.EXPECT().Lots(probMock1, probMock2).Return(1)
if lotMock.Lots(probMock1, probMock2) != 1 {
t.Errorf("mock error")
}
}