4
4
package sampler
5
5
6
6
import (
7
- "fmt"
8
7
"math"
9
8
"slices"
10
9
"testing"
11
10
12
11
"github.com/stretchr/testify/require"
13
12
)
14
13
15
- var (
16
- uniformSamplers = []struct {
17
- name string
18
- sampler Uniform
19
- }{
20
- {
21
- name : "replacer" ,
22
- sampler : & uniformReplacer {
23
- rng : globalRNG ,
24
- },
25
- },
26
- {
27
- name : "resampler" ,
28
- sampler : & uniformResample {
29
- rng : globalRNG ,
30
- },
31
- },
32
- {
33
- name : "best" ,
34
- sampler : NewBestUniform (30 ),
35
- },
36
- }
37
- uniformTests = []struct {
38
- name string
39
- test func (* testing.T , Uniform )
40
- }{
41
- {
42
- name : "can sample large values" ,
43
- test : UniformInitializeMaxUint64Test ,
44
- },
45
- {
46
- name : "out of range" ,
47
- test : UniformOutOfRangeTest ,
48
- },
49
- {
50
- name : "empty" ,
51
- test : UniformEmptyTest ,
52
- },
53
- {
54
- name : "singleton" ,
55
- test : UniformSingletonTest ,
56
- },
57
- {
58
- name : "distribution" ,
59
- test : UniformDistributionTest ,
60
- },
61
- {
62
- name : "over sample" ,
63
- test : UniformOverSampleTest ,
64
- },
65
- {
66
- name : "lazily sample" ,
67
- test : UniformLazilySample ,
68
- },
69
- }
70
- )
71
-
72
- func TestAllUniform (t * testing.T ) {
73
- for _ , s := range uniformSamplers {
74
- for _ , test := range uniformTests {
75
- t .Run (fmt .Sprintf ("sampler %s test %s" , s .name , test .name ), func (t * testing.T ) {
76
- test .test (t , s .sampler )
77
- })
78
- }
79
- }
80
- }
81
-
82
- func UniformInitializeMaxUint64Test (t * testing.T , s Uniform ) {
14
+ func TestUniformInitializeMaxUint64 (t * testing.T ) {
15
+ s := NewUniform ()
83
16
s .Initialize (math .MaxUint64 )
84
17
85
18
for {
@@ -92,15 +25,17 @@ func UniformInitializeMaxUint64Test(t *testing.T, s Uniform) {
92
25
}
93
26
}
94
27
95
- func UniformOutOfRangeTest (t * testing.T , s Uniform ) {
28
+ func TestUniformOutOfRange (t * testing.T ) {
29
+ s := NewUniform ()
96
30
s .Initialize (0 )
97
31
98
32
_ , ok := s .Sample (1 )
99
33
require .False (t , ok )
100
34
}
101
35
102
- func UniformEmptyTest (t * testing.T , s Uniform ) {
36
+ func TestUniformEmpty (t * testing.T ) {
103
37
require := require .New (t )
38
+ s := NewUniform ()
104
39
105
40
s .Initialize (1 )
106
41
@@ -109,8 +44,9 @@ func UniformEmptyTest(t *testing.T, s Uniform) {
109
44
require .Empty (val )
110
45
}
111
46
112
- func UniformSingletonTest (t * testing.T , s Uniform ) {
47
+ func TestUniformSingleton (t * testing.T ) {
113
48
require := require .New (t )
49
+ s := NewUniform ()
114
50
115
51
s .Initialize (1 )
116
52
@@ -119,8 +55,9 @@ func UniformSingletonTest(t *testing.T, s Uniform) {
119
55
require .Equal ([]uint64 {0 }, val )
120
56
}
121
57
122
- func UniformDistributionTest (t * testing.T , s Uniform ) {
58
+ func TestUniformDistribution (t * testing.T ) {
123
59
require := require .New (t )
60
+ s := NewUniform ()
124
61
125
62
s .Initialize (3 )
126
63
@@ -131,15 +68,17 @@ func UniformDistributionTest(t *testing.T, s Uniform) {
131
68
require .Equal ([]uint64 {0 , 1 , 2 }, val )
132
69
}
133
70
134
- func UniformOverSampleTest (t * testing.T , s Uniform ) {
71
+ func TestUniformOverSample (t * testing.T ) {
72
+ s := NewUniform ()
135
73
s .Initialize (3 )
136
74
137
75
_ , ok := s .Sample (4 )
138
76
require .False (t , ok )
139
77
}
140
78
141
- func UniformLazilySample (t * testing.T , s Uniform ) {
79
+ func TestUniformLazilySample (t * testing.T ) {
142
80
require := require .New (t )
81
+ s := NewUniform ()
143
82
144
83
s .Initialize (3 )
145
84
0 commit comments