-
Notifications
You must be signed in to change notification settings - Fork 5
/
moving_average_exponential_test.go
98 lines (89 loc) · 2.19 KB
/
moving_average_exponential_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
/*
Copyright 2020 Binh Nguyen
Licensed under terms of MIT license (see LICENSE)
*/
package tago
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
)
func TestNewExponentialMovingAverage(t *testing.T) {
tests := map[string]struct {
input int
want *ExponentialMovingAverage
wantErr error
}{
"negative n": {input: -3, want: nil, wantErr: ErrInvalidParameters},
"zero n": {input: 0, want: nil, wantErr: ErrInvalidParameters},
"positive n": {input: 9, want: &ExponentialMovingAverage{n: 9, k: 2. / 10., isNew: true}, wantErr: nil},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
gotSD, gotErr := NewExponentialMovingAverage(tc.input)
if tc.wantErr != nil { // only check error returned if expecting one
assert.EqualError(t, gotErr, tc.wantErr.Error(), "must return the correct error")
}
assert.Equal(t, tc.want, gotSD, "must return the correct value")
})
}
}
func TestExponentialMovingAverageNext(t *testing.T) {
sd, _ := NewExponentialMovingAverage(3)
tests := []struct {
input float64
want float64
}{
{input: 2., want: 2.},
{input: 5., want: 3.5},
{input: 1., want: 2.25},
{input: 6.25, want: 4.25},
}
for _, tc := range tests {
t.Run("", func(t *testing.T) {
got := sd.Next(tc.input)
diff := cmp.Diff(tc.want, got, floatComparer)
if diff != "" {
t.Fatalf(diff)
}
})
}
}
func TestExponentialMovingAverageReset(t *testing.T) {
sd, _ := NewExponentialMovingAverage(3)
tests := []struct {
input float64
want float64
}{
{input: 2., want: 2.},
{input: 5., want: 3.5},
{input: 1., want: 2.25},
}
for _, tc := range tests {
t.Run("", func(t *testing.T) {
got := sd.Next(tc.input)
diff := cmp.Diff(tc.want, got, floatComparer)
if diff != "" {
t.Fatalf(diff)
}
})
}
diff := cmp.Diff(4., sd.Next(4.), floatComparer)
if diff == "" {
t.Fatal(diff)
}
sd.Reset()
diff = cmp.Diff(4., sd.Next(4.), floatComparer)
if diff != "" {
t.Fatalf(diff)
}
}
func TestExponentialMovingAverageString(t *testing.T) {
sd, _ := NewExponentialMovingAverage(4)
want := "EMA(4)"
got := sd.String()
diff := cmp.Diff(want, got, floatComparer)
if diff != "" {
t.Fatalf(diff)
}
}