/
reciprocal_uint64_test.go
119 lines (101 loc) · 2.96 KB
/
reciprocal_uint64_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
package sample
import (
"fmt"
"math"
"testing"
)
func TestReciprocalUint64NilSampler(t *testing.T) {
var sampler Sampler
var err error
sampler, err = NewReciprocalUint64(0)
if err == nil {
t.Fatal("NewReciprocalUint64 must error if rate = 0", err)
}
_ = sampler
}
func TestReciprocalUint64Rate1(t *testing.T) {
var sampler Sampler
var err error
// Sample 1
sampler, err = NewReciprocalUint64(1)
if err != nil {
t.Fatal("NewReciprocalUint64 must not error", err)
}
// must return true for all cases
if sampler.SampleFrom(0) != true {
t.Error("sampling 0 with rate 1 did not return true")
}
if sampler.SampleFrom(math.MaxUint64) != true {
t.Error("sampling MaxUint64 with rate 1 did not return true")
}
for i := 1; i < 20; i++ {
if sampler.Sample() != true {
t.Error("sampling with rate 1 did not return true")
}
}
}
func TestReciprocalUint64SampleFrom(t *testing.T) {
var sampler Sampler
var err error
// Sample 13 with from
sampler, err = NewReciprocalUint64(13)
if err != nil {
t.Fatal("NewReciprocalUint64 must not error", err)
}
// true
if sampler.SampleFrom(0) != true {
t.Error("sampling 0 with rate 13 did not return true")
}
if sampler.SampleFrom(13) != true {
t.Error("sampling 0 with rate 13 did not return true")
}
// false
if sampler.SampleFrom(12) != false {
t.Error("sampling 1023 with rate 13 did not return false")
}
if sampler.SampleFrom(14) != false {
t.Error("sampling 1024 with rate 13 did not return false")
}
if sampler.SampleFrom(math.MaxUint64) != false {
t.Error("sampling MaxUint64 with rate 13 did not return false")
}
}
func TestReciprocalUint64SeedSample(t *testing.T) {
var sampler Sampler
var err error
// Sample with seed, false
sampler, err = NewReciprocalUint64Seeded(13, 0)
if err != nil {
t.Fatal("NewReciprocalUint64 must not error", err)
}
if sampler.Sample() != false {
t.Error("sampling with seed 0 and rate 13 did not return false")
}
// Sample with seed, true
sampler, err = NewReciprocalUint64Seeded(13, seedUint64)
if err != nil {
t.Fatal("NewReciprocalUint64 must not error", err)
}
if sampler.Sample() != true {
t.Error("sampling with seed", seedUint64, "and rate 13 did not return true")
}
}
func TestReciprocalUint64String(t *testing.T) {
var sampler Sampler
sampler = &reciprocalUint64{sampleState: sampleState{rate: 64, seed: 0, sampleCount: 1000, trueCount: 100}, rateuint64: 64, reciprocal: 0}
if sampler.String() != "&{sampleState:{rate:64 seed:0 sampleCount:1000 trueCount:100 rnd:<nil>} rateuint64:64 reciprocal:0}" {
t.Error("Expected: &{sampleState:{rate:64 seed:0 sampleCount:1000 trueCount:100 rnd:<nil>} rateuint64:64 reciprocal:0}, Got:", sampler.String())
}
}
func ExampleNewReciprocalUint64Seeded() {
s, err := NewReciprocalUint64Seeded(10, 0)
if err != nil {
fmt.Println("Unable to initialize sampler", err)
}
for i := 0; i < 100; i++ {
if s.Sample() {
fmt.Println(i, "got sampled by ReciprocalUint64 sampler")
}
}
fmt.Println(Stats(s))
}