-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparam_test.go
140 lines (133 loc) · 3.02 KB
/
param_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2019 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package diviner_test
import (
"math"
"math/rand"
"testing"
"github.com/grailbio/diviner"
)
func TestDiscrete(t *testing.T) {
rng := rand.New(rand.NewSource(0))
vals := []diviner.Value{diviner.String("x"), diviner.String("y"), diviner.String("z")}
d := diviner.NewDiscrete(vals...)
const N = 1000
for i := 0; i < N; i++ {
val := d.Sample(rng)
switch val.Str() {
case "x", "y", "z":
default:
t.Error("invalid value", val)
}
}
}
func TestRange(t *testing.T) {
const (
beg = 0.2
end = 23.4
N = 10000
)
// Naive test for uniformity.
var (
rng = rand.New(rand.NewSource(0))
r = diviner.NewRange(diviner.Float(beg), diviner.Float(end))
sum float64
samples = make([]float64, N)
)
for i := range samples {
v := r.Sample(rng).Float()
samples[i] = v
if v < beg || v >= end {
t.Errorf("invalid value %f", v)
}
sum += v
}
var (
mean = sum / N
sumSquares float64
)
for _, v := range samples {
sumSquares += (v - mean) * (v - mean)
}
stddev := math.Sqrt(sumSquares / (N - 1))
if stddev < 6 {
t.Errorf("stddev %f too low", stddev)
}
if mean < (end-beg)/2-1 || mean > (end-beg)/2+1 {
t.Errorf("mean %f out of range", mean)
}
}
func TestIsValid(t *testing.T) {
tests := []struct {
params diviner.Params
values diviner.Values
result bool
}{
{
params: diviner.Params{
"a": diviner.NewDiscrete(diviner.Float(0), diviner.Float(1), diviner.Float(2)),
"b": diviner.NewDiscrete(diviner.Int(7), diviner.Int(8)),
},
values: diviner.Values{
"a": diviner.Float(1),
},
result: false,
},
{
params: diviner.Params{
"a": diviner.NewDiscrete(diviner.Float(0), diviner.Float(1), diviner.Float(2)),
"b": diviner.NewDiscrete(diviner.Int(7), diviner.Int(8)),
},
values: diviner.Values{
"a": diviner.Float(1),
"b": diviner.Int(7),
},
result: true,
},
{
params: diviner.Params{
"a": diviner.NewDiscrete(diviner.Float(0), diviner.Float(1), diviner.Float(2)),
"b": diviner.NewDiscrete(diviner.Int(7), diviner.Int(8)),
},
values: diviner.Values{
"a": diviner.Float(1),
"b": diviner.Int(9),
},
result: false,
},
{
params: diviner.Params{
"a": diviner.NewRange(diviner.Int(7), diviner.Int(100)),
},
values: diviner.Values{
"a": diviner.Int(99),
},
result: true,
},
{
params: diviner.Params{
"a": diviner.NewRange(diviner.Int(7), diviner.Int(100)),
},
values: diviner.Values{
"a": diviner.Int(100),
},
result: false,
},
{
params: diviner.Params{
"a": diviner.NewRange(diviner.Int(7), diviner.Int(100)),
},
values: diviner.Values{
"a": diviner.Int(99),
"b": diviner.Int(0),
},
result: false,
},
}
for _, test := range tests {
if test.params.IsValid(test.values) != test.result {
t.Errorf("Wrong result for %v, %v: want %v", test.params, test.values, test.result)
}
}
}