-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreservoir_test.go
70 lines (55 loc) · 1.42 KB
/
reservoir_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
package sampling_test
import (
"math/rand"
"testing"
"time"
"github.com/epicchewy/sampling"
"github.com/stretchr/testify/assert"
)
// Very naive tests to see if results are random samples
func TestSimpleReservoir(t *testing.T) {
var items []int
for i := 0; i < 100; i++ {
items = append(items, i)
}
itr := sampling.NewIntInterator(items)
r := sampling.NewSimpleReservoir(10, itr)
samples := r.Sample()
itr2 := sampling.NewIntInterator(items)
r2 := sampling.NewSimpleReservoir(10, itr2)
samples2 := r2.Sample()
assert.NotEqual(t, samples, samples2)
}
func TestReservoir(t *testing.T) {
var items []int
for i := 0; i < 100; i++ {
items = append(items, i)
}
itr := sampling.NewIntInterator(items)
r := sampling.NewReservoir(10, itr)
samples := r.Sample()
itr2 := sampling.NewIntInterator(items)
r2 := sampling.NewReservoir(10, itr2)
samples2 := r2.Sample()
assert.NotEqual(t, samples, samples2)
}
func TestWeightedReservoir(t *testing.T) {
rand.Seed(time.Now().UnixNano())
var items []sampling.WeightedItem
for i := 0; i < 100; i++ {
items = append(
items,
sampling.WeightedItem{
Weight: rand.Float64(),
Obj: i,
},
)
}
itr := sampling.NewItemIterator(items)
r := sampling.NewWeightedReservoir(10, itr)
samples := r.Sample()
itr2 := sampling.NewItemIterator(items)
r2 := sampling.NewWeightedReservoir(10, itr2)
samples2 := r2.Sample()
assert.NotEqual(t, samples, samples2)
}