forked from wcharczuk/go-chart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sma_series_test.go
111 lines (91 loc) · 2.36 KB
/
sma_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
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
package chart
import (
"testing"
"github.com/wcharczuk/go-chart/v2/testutil"
)
type mockValuesProvider struct {
X []float64
Y []float64
}
func (m mockValuesProvider) Len() int {
return MinInt(len(m.X), len(m.Y))
}
func (m mockValuesProvider) GetValues(index int) (x, y float64) {
if index < 0 {
panic("negative index at GetValue()")
}
if index >= MinInt(len(m.X), len(m.Y)) {
panic("index is outside the length of m.X or m.Y")
}
x = m.X[index]
y = m.Y[index]
return
}
func TestSMASeriesGetValue(t *testing.T) {
// replaced new assertions helper
mockSeries := mockValuesProvider{
LinearRange(1.0, 10.0),
LinearRange(10, 1.0),
}
testutil.AssertEqual(t, 10, mockSeries.Len())
mas := &SMASeries{
InnerSeries: mockSeries,
Period: 10,
}
var yvalues []float64
for x := 0; x < mas.Len(); x++ {
_, y := mas.GetValues(x)
yvalues = append(yvalues, y)
}
testutil.AssertEqual(t, 10.0, yvalues[0])
testutil.AssertEqual(t, 9.5, yvalues[1])
testutil.AssertEqual(t, 9.0, yvalues[2])
testutil.AssertEqual(t, 8.5, yvalues[3])
testutil.AssertEqual(t, 8.0, yvalues[4])
testutil.AssertEqual(t, 7.5, yvalues[5])
testutil.AssertEqual(t, 7.0, yvalues[6])
testutil.AssertEqual(t, 6.5, yvalues[7])
testutil.AssertEqual(t, 6.0, yvalues[8])
}
func TestSMASeriesGetLastValueWindowOverlap(t *testing.T) {
// replaced new assertions helper
mockSeries := mockValuesProvider{
LinearRange(1.0, 10.0),
LinearRange(10, 1.0),
}
testutil.AssertEqual(t, 10, mockSeries.Len())
mas := &SMASeries{
InnerSeries: mockSeries,
Period: 15,
}
var yvalues []float64
for x := 0; x < mas.Len(); x++ {
_, y := mas.GetValues(x)
yvalues = append(yvalues, y)
}
lx, ly := mas.GetLastValues()
testutil.AssertEqual(t, 10.0, lx)
testutil.AssertEqual(t, 5.5, ly)
testutil.AssertEqual(t, yvalues[len(yvalues)-1], ly)
}
func TestSMASeriesGetLastValue(t *testing.T) {
// replaced new assertions helper
mockSeries := mockValuesProvider{
LinearRange(1.0, 100.0),
LinearRange(100, 1.0),
}
testutil.AssertEqual(t, 100, mockSeries.Len())
mas := &SMASeries{
InnerSeries: mockSeries,
Period: 10,
}
var yvalues []float64
for x := 0; x < mas.Len(); x++ {
_, y := mas.GetValues(x)
yvalues = append(yvalues, y)
}
lx, ly := mas.GetLastValues()
testutil.AssertEqual(t, 100.0, lx)
testutil.AssertEqual(t, 6, ly)
testutil.AssertEqual(t, yvalues[len(yvalues)-1], ly)
}