go-chart/sma_series_test.go

114 lines
2.2 KiB
Go
Raw Normal View History

2016-07-14 21:30:57 +02:00
package chart
import (
"testing"
2018-04-05 09:42:38 +02:00
"github.com/blend/go-sdk/assert"
"github.com/wcharczuk/go-chart/seq"
"github.com/wcharczuk/go-chart/util"
2016-07-14 21:30:57 +02:00
)
type mockValuesProvider struct {
2016-07-14 21:30:57 +02:00
X []float64
Y []float64
}
func (m mockValuesProvider) Len() int {
return util.Math.MinInt(len(m.X), len(m.Y))
2016-07-14 21:30:57 +02:00
}
func (m mockValuesProvider) GetValues(index int) (x, y float64) {
if index < 0 {
panic("negative index at GetValue()")
}
if index >= util.Math.MinInt(len(m.X), len(m.Y)) {
panic("index is outside the length of m.X or m.Y")
}
2016-07-14 21:30:57 +02:00
x = m.X[index]
y = m.Y[index]
return
}
func TestSMASeriesGetValue(t *testing.T) {
2016-07-14 21:30:57 +02:00
assert := assert.New(t)
mockSeries := mockValuesProvider{
seq.Range(1.0, 10.0),
seq.Range(10, 1.0),
2016-07-14 21:30:57 +02:00
}
assert.Equal(10, mockSeries.Len())
mas := &SMASeries{
2016-07-14 21:30:57 +02:00
InnerSeries: mockSeries,
Period: 10,
2016-07-14 21:30:57 +02:00
}
var yvalues []float64
for x := 0; x < mas.Len(); x++ {
_, y := mas.GetValues(x)
2016-07-14 21:30:57 +02:00
yvalues = append(yvalues, y)
}
assert.Equal(10.0, yvalues[0])
assert.Equal(9.5, yvalues[1])
assert.Equal(9.0, yvalues[2])
assert.Equal(8.5, yvalues[3])
assert.Equal(8.0, yvalues[4])
assert.Equal(7.5, yvalues[5])
assert.Equal(7.0, yvalues[6])
assert.Equal(6.5, yvalues[7])
assert.Equal(6.0, yvalues[8])
}
2016-07-14 21:31:42 +02:00
func TestSMASeriesGetLastValueWindowOverlap(t *testing.T) {
2016-07-14 21:31:42 +02:00
assert := assert.New(t)
mockSeries := mockValuesProvider{
seq.Range(1.0, 10.0),
seq.Range(10, 1.0),
2016-07-14 21:31:42 +02:00
}
assert.Equal(10, mockSeries.Len())
mas := &SMASeries{
2016-07-14 21:31:42 +02:00
InnerSeries: mockSeries,
Period: 15,
2016-07-14 21:31:42 +02:00
}
2016-07-14 22:14:02 +02:00
var yvalues []float64
for x := 0; x < mas.Len(); x++ {
_, y := mas.GetValues(x)
2016-07-14 22:14:02 +02:00
yvalues = append(yvalues, y)
}
lx, ly := mas.GetLastValues()
2016-07-14 21:31:42 +02:00
assert.Equal(10.0, lx)
assert.Equal(5.5, ly)
2016-07-14 22:14:02 +02:00
assert.Equal(yvalues[len(yvalues)-1], ly)
2016-07-14 21:31:42 +02:00
}
2016-07-14 22:11:03 +02:00
func TestSMASeriesGetLastValue(t *testing.T) {
2016-07-14 22:11:03 +02:00
assert := assert.New(t)
mockSeries := mockValuesProvider{
seq.Range(1.0, 100.0),
seq.Range(100, 1.0),
2016-07-14 22:11:03 +02:00
}
assert.Equal(100, mockSeries.Len())
mas := &SMASeries{
2016-07-14 22:11:03 +02:00
InnerSeries: mockSeries,
Period: 10,
2016-07-14 22:11:03 +02:00
}
2016-07-14 22:13:35 +02:00
var yvalues []float64
for x := 0; x < mas.Len(); x++ {
_, y := mas.GetValues(x)
2016-07-14 22:13:35 +02:00
yvalues = append(yvalues, y)
}
lx, ly := mas.GetLastValues()
2016-07-14 22:11:03 +02:00
assert.Equal(100.0, lx)
assert.Equal(6, ly)
2016-07-14 22:13:35 +02:00
assert.Equal(yvalues[len(yvalues)-1], ly)
2016-07-14 22:11:03 +02:00
}