-
Notifications
You must be signed in to change notification settings - Fork 43
/
random_weighted_test.go
66 lines (51 loc) · 1.49 KB
/
random_weighted_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
package weighted_test
import (
"testing"
"github.com/smallnest/weighted"
)
func TestRandW_Next(t *testing.T) {
w := weighted.NewRandW()
w.Add("server1", 4)
w.Add("server2", 2)
w.Add("server3", 3)
w.Add("server4", 1)
results := make(map[string]int)
for i := 0; i < 100; i++ {
s := w.Next().(string)
results[s]++
}
if !checkResults(results["server1"], 20, 70) || !checkResults(results["server2"], 1, 40) || !checkResults(results["server3"], 10, 50) || !checkResults(results["server4"], 1, 30) {
t.Error("the algorithm is wrong", results)
}
w.Reset()
results = make(map[string]int)
for i := 0; i < 100; i++ {
s := w.Next().(string)
results[s]++
}
if !checkResults(results["server1"], 20, 70) || !checkResults(results["server2"], 1, 40) || !checkResults(results["server3"], 10, 50) || !checkResults(results["server4"], 1, 30) {
t.Error("the algorithm is wrong", results)
}
w.RemoveAll()
w.Add("server1", 7)
w.Add("server2", 9)
w.Add("server3", 13)
results = make(map[string]int)
for i := 0; i < 29000; i++ {
s := w.Next().(string)
results[s]++
}
if !checkResults(results["server1"], 6000, 8000) || !checkResults(results["server2"], 8000, 10000) || !checkResults(results["server3"], 12000, 14000) {
t.Error("the algorithm is wrong", results)
}
t.Log("the results: ", results)
}
func checkResults(v, min, max int) bool {
return v >= min && v <= max
}
func TestRandWZero(t *testing.T) {
w := weighted.NewRandW()
w.Add("a", 0)
// test panic or not
_ = w.Next()
}