forked from smallnest/weighted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_weighted_test.go
55 lines (41 loc) · 1.22 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
package weighted
import "testing"
func TestRandW_Next(t *testing.T) {
w := NewRandW()
w.Add("server1", 5)
w.Add("server2", 2)
w.Add("server3", 3)
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"], 0, 40) || !checkResults(results["server3"], 10, 50) {
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"], 0, 40) || !checkResults(results["server3"], 10, 50) {
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
}