-
Notifications
You must be signed in to change notification settings - Fork 4
/
series_test.go
68 lines (54 loc) · 1.86 KB
/
series_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
package main
import (
"math"
"runtime/debug"
"testing"
)
const float64EqualityThreshold = 1e-9
func floatEq(a, b float64) bool {
return math.Abs(a-b) <= float64EqualityThreshold
}
func assertEq(t *testing.T, a, b float64) {
if math.IsNaN(a) && math.IsNaN(b) {
return
}
if !floatEq(a, b) {
t.Errorf("Not equal: %f != %f", a, b)
t.Log(string(debug.Stack()))
}
}
func assertSliceEq(t *testing.T, a, b []float64) {
if len(a) != len(b) {
t.Errorf("Unequal slice lengths: %d != %d", len(a), len(b))
t.Log(string(debug.Stack()))
return
}
for i := 0; i < len(a); i++ {
assertEq(t, a[i], b[i])
}
}
func TestBoundedSeriesSmoothing(t *testing.T) {
series := NewBoundedSeries(5)
series.AddValue(0)
assertSliceEq(t, series.Values(), []float64{0})
assertSliceEq(t, series.SmoothedValues(3), []float64{0, math.NaN(), math.NaN(), math.NaN(), math.NaN()})
series.AddValue(1)
assertSliceEq(t, series.Values(), []float64{0, 1})
assertSliceEq(t, series.SmoothedValues(3), []float64{0, 0.5, math.NaN(), math.NaN(), math.NaN()})
series.AddValue(2)
assertSliceEq(t, series.Values(), []float64{0, 1, 2})
assertSliceEq(t, series.SmoothedValues(3), []float64{0, 0.5, 1, math.NaN(), math.NaN()})
series.AddValue(3)
assertSliceEq(t, series.Values(), []float64{0, 1, 2, 3})
assertSliceEq(t, series.SmoothedValues(3), []float64{0, 0.5, 1, 2, math.NaN()})
series.AddValue(4)
assertSliceEq(t, series.Values(), []float64{0, 1, 2, 3, 4})
assertSliceEq(t, series.SmoothedValues(3), []float64{0, 0.5, 1, 2, 3})
series.AddValue(5)
assertSliceEq(t, series.Values(), []float64{1, 2, 3, 4, 5})
assertSliceEq(t, series.SmoothedValues(3), []float64{0.5, 1, 2, 3, 4})
series.AddValue(6)
assertSliceEq(t, series.Values(), []float64{2, 3, 4, 5, 6})
assertSliceEq(t, series.SmoothedValues(1), []float64{2, 3, 4, 5, 6})
assertSliceEq(t, series.SmoothedValues(3), []float64{1, 2, 3, 4, 5})
}