Debian Codegraph @

sma_series_test.go

raw symbols SCIP
1 package chart
2
3 import (
4 "testing"
5
6 "github.com/wcharczuk/go-chart/v2/testutil"
7 )
8
9 type mockValuesProvider struct {
10 X []float64
11 Y []float64
12 }
13
14 func (m mockValuesProvider) Len() int {
15 return MinInt(len(m.X), len(m.Y))
16 }
17
18 func (m mockValuesProvider) GetValues(index int) (x, y float64) {
19 if index < 0 {
20 panic("negative index at GetValue()")
21 }
22 if index >= MinInt(len(m.X), len(m.Y)) {
23 panic("index is outside the length of m.X or m.Y")
24 }
25 x = m.X[index]
26 y = m.Y[index]
27 return
28 }
29
30 func TestSMASeriesGetValue(t *testing.T) {
31 // replaced new assertions helper
32
33 mockSeries := mockValuesProvider{
34 LinearRange(1.0, 10.0),
35 LinearRange(10, 1.0),
36 }
37 testutil.AssertEqual(t, 10, mockSeries.Len())
38
39 mas := &SMASeries{
40 InnerSeries: mockSeries,
41 Period: 10,
42 }
43
44 var yvalues []float64
45 for x := 0; x < mas.Len(); x++ {
46 _, y := mas.GetValues(x)
47 yvalues = append(yvalues, y)
48 }
49
50 testutil.AssertEqual(t, 10.0, yvalues[0])
51 testutil.AssertEqual(t, 9.5, yvalues[1])
52 testutil.AssertEqual(t, 9.0, yvalues[2])
53 testutil.AssertEqual(t, 8.5, yvalues[3])
54 testutil.AssertEqual(t, 8.0, yvalues[4])
55 testutil.AssertEqual(t, 7.5, yvalues[5])
56 testutil.AssertEqual(t, 7.0, yvalues[6])
57 testutil.AssertEqual(t, 6.5, yvalues[7])
58 testutil.AssertEqual(t, 6.0, yvalues[8])
59 }
60
61 func TestSMASeriesGetLastValueWindowOverlap(t *testing.T) {
62 // replaced new assertions helper
63
64 mockSeries := mockValuesProvider{
65 LinearRange(1.0, 10.0),
66 LinearRange(10, 1.0),
67 }
68 testutil.AssertEqual(t, 10, mockSeries.Len())
69
70 mas := &SMASeries{
71 InnerSeries: mockSeries,
72 Period: 15,
73 }
74
75 var yvalues []float64
76 for x := 0; x < mas.Len(); x++ {
77 _, y := mas.GetValues(x)
78 yvalues = append(yvalues, y)
79 }
80
81 lx, ly := mas.GetLastValues()
82 testutil.AssertEqual(t, 10.0, lx)
83 testutil.AssertEqual(t, 5.5, ly)
84 testutil.AssertEqual(t, yvalues[len(yvalues)-1], ly)
85 }
86
87 func TestSMASeriesGetLastValue(t *testing.T) {
88 // replaced new assertions helper
89
90 mockSeries := mockValuesProvider{
91 LinearRange(1.0, 100.0),
92 LinearRange(100, 1.0),
93 }
94 testutil.AssertEqual(t, 100, mockSeries.Len())
95
96 mas := &SMASeries{
97 InnerSeries: mockSeries,
98 Period: 10,
99 }
100
101 var yvalues []float64
102 for x := 0; x < mas.Len(); x++ {
103 _, y := mas.GetValues(x)
104 yvalues = append(yvalues, y)
105 }
106
107 lx, ly := mas.GetLastValues()
108 testutil.AssertEqual(t, 100.0, lx)
109 testutil.AssertEqual(t, 6, ly)
110 testutil.AssertEqual(t, yvalues[len(yvalues)-1], ly)
111 }