-
Notifications
You must be signed in to change notification settings - Fork 6
/
aliasmethod_test.go
71 lines (56 loc) · 1.56 KB
/
aliasmethod_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
package aliasmethod_test
import (
"github.com/smartwalle/aliasmethod"
"testing"
)
type Christmas struct {
name string
weight int32
}
func (this Christmas) GetWeight() int32 {
return this.weight
}
func Test_AliasMethod(t *testing.T) {
var results = make(map[string]int)
var m = aliasmethod.New[Christmas]()
m.Add(Christmas{name: "1", weight: 10})
m.Add(Christmas{name: "2", weight: 10})
m.Add(Christmas{name: "3", weight: 10})
m.Add(Christmas{name: "4", weight: 10})
m.Add(Christmas{name: "5", weight: 10})
m.Add(Christmas{name: "6", weight: 10})
m.Add(Christmas{name: "7", weight: 10})
m.Add(Christmas{name: "8", weight: 10})
m.Add(Christmas{name: "9", weight: 10})
m.Add(Christmas{name: "10", weight: 110})
if ok := m.Prepare(); !ok {
t.Fatal(ok)
}
for i := 0; i < 1000; i++ {
var p = m.NextItem()
results[p.name] = results[p.name] + 1
}
t.Log(results)
}
func BenchmarkAliasMethod_NextItem(b *testing.B) {
var results = make(map[string]int)
var m = aliasmethod.New[Christmas]()
m.Add(Christmas{name: "1", weight: 10})
m.Add(Christmas{name: "2", weight: 10})
m.Add(Christmas{name: "3", weight: 10})
m.Add(Christmas{name: "4", weight: 10})
m.Add(Christmas{name: "5", weight: 10})
m.Add(Christmas{name: "6", weight: 10})
m.Add(Christmas{name: "7", weight: 10})
m.Add(Christmas{name: "8", weight: 10})
m.Add(Christmas{name: "9", weight: 10})
m.Add(Christmas{name: "10", weight: 110})
if ok := m.Prepare(); !ok {
b.Fatal(ok)
}
for i := 0; i < b.N; i++ {
var p = m.NextItem()
results[p.name] = results[p.name] + 1
}
b.Log(results)
}