forked from kyokomi/lottery
/
lottery_test.go
146 lines (120 loc) · 2.86 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
package lottery_test
import (
"fmt"
"math/rand"
"testing"
"time"
"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.New(rand.New(rand.NewSource(time.Now().UnixNano())))
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.New(rand.New(rand.NewSource(time.Now().UnixNano())))
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.New(rand.New(rand.NewSource(time.Now().UnixNano())))
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.New(rand.New(rand.NewSource(time.Now().UnixNano())))
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 %f%%", testCase.prob)
}
}
}
func TestGetRandomInt (t *testing.T){
}