/
sample_test.go
93 lines (70 loc) · 2.22 KB
/
sample_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
package sample
import (
"math/rand"
"testing"
)
func TestSampleStateNil(t *testing.T) {
var state *sampleState
if state.Rate() != 0 {
t.Error("rate is not 0")
}
if state.Calls() != 0 {
t.Error("calls is not 0")
}
if state.Count() != 0 {
t.Error("count is not 0")
}
}
func TestSampleStateRate(t *testing.T) {
state := sampleState{rate: 1000}
// Check rate
if state.Rate() != 1000 {
t.Error("rate is 1000")
}
}
func TestSampleStateReset(t *testing.T) {
state := sampleState{seed: 0, trueCount: 100, sampleCount: 200, rnd: rand.New(rand.NewSource(20))}
// Reset
state.Reset()
if state.Count() != 0 {
t.Error("sampling count after reset not 0")
}
if state.Calls() != 0 {
t.Error("sampling calls after reset not 0")
}
}
func TestSampleStateDeviation(t *testing.T) {
var state *sampleState
// Deviation without sampling
if Deviation(state) != 1.0 {
t.Error("without sampling deviation must be 1.0")
}
state = &sampleState{rate: 10, seed: 0, sampleCount: 1000, trueCount: 100}
// Deviation without sampling
if Deviation(state) != 0 {
t.Error("with sampleCount 1000 and trueCount 100, deviation must be 0, but deviation is", Deviation(state))
}
state = &sampleState{rate: 10, seed: 0, sampleCount: 1000, trueCount: 200}
// Deviation without sampling
if Deviation(state) != 0.5 {
t.Error("with sampleCount 1000 and trueCount 200, deviation must be 0.5, but deviation is", Deviation(state))
}
}
func TestSampleStateString(t *testing.T) {
state := &sampleState{rate: 10, seed: 0, sampleCount: 1000, trueCount: 100}
if state.String() != "&{rate:10 seed:0 sampleCount:1000 trueCount:100 rnd:<nil>}" {
t.Error("Expected: &{rate:10 seed:0 sampleCount:1000 trueCount:100 rnd:<nil>}, Got:", state.String())
}
}
func TestSampleStateStats(t *testing.T) {
var state State
// Stats without sampler
s := Stats(state)
if s != "No state provided" {
t.Error("without state stats must be \"No state provided\", but returned:", s)
}
state = &sampleState{rate: 10, seed: 0, sampleCount: 1000, trueCount: 100}
if Stats(state) != "Rate: 10, SampleCount: 1000, TrueCount: 100, Deviation: 0.0000%" {
t.Errorf("Expected: Rate: 10, SampleCount: 1000, TrueCount: 100, Deviation: 0.0000%%, Got: %s", Stats(state))
}
}