2016-07-14 15:30:57 -04:00
|
|
|
package chart
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/blendlabs/go-assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mockValueProvider struct {
|
|
|
|
X []float64
|
|
|
|
Y []float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockValueProvider) Len() int {
|
2016-07-29 21:24:25 -04:00
|
|
|
return Math.MinInt(len(m.X), len(m.Y))
|
2016-07-14 15:30:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockValueProvider) GetValue(index int) (x, y float64) {
|
2016-07-17 14:10:04 -04:00
|
|
|
if index < 0 {
|
|
|
|
panic("negative index at GetValue()")
|
|
|
|
}
|
2016-07-29 21:24:25 -04:00
|
|
|
if index > Math.MinInt(len(m.X), len(m.Y)) {
|
2016-07-17 14:10:04 -04:00
|
|
|
panic("index is outside the length of m.X or m.Y")
|
|
|
|
}
|
2016-07-14 15:30:57 -04:00
|
|
|
x = m.X[index]
|
|
|
|
y = m.Y[index]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-17 14:10:04 -04:00
|
|
|
func TestSMASeriesGetValue(t *testing.T) {
|
2016-07-14 15:30:57 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
mockSeries := mockValueProvider{
|
2016-07-29 21:24:25 -04:00
|
|
|
Sequence.Float64(1.0, 10.0),
|
|
|
|
Sequence.Float64(10, 1.0),
|
2016-07-14 15:30:57 -04:00
|
|
|
}
|
|
|
|
assert.Equal(10, mockSeries.Len())
|
|
|
|
|
2016-07-17 14:10:04 -04:00
|
|
|
mas := &SMASeries{
|
2016-07-14 15:30:57 -04:00
|
|
|
InnerSeries: mockSeries,
|
2016-07-17 14:10:04 -04:00
|
|
|
Period: 10,
|
2016-07-14 15:30:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var yvalues []float64
|
|
|
|
for x := 0; x < mas.Len(); x++ {
|
|
|
|
_, y := mas.GetValue(x)
|
|
|
|
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 15:31:42 -04:00
|
|
|
|
2016-07-17 14:10:04 -04:00
|
|
|
func TestSMASeriesGetLastValueWindowOverlap(t *testing.T) {
|
2016-07-14 15:31:42 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
mockSeries := mockValueProvider{
|
2016-07-29 21:24:25 -04:00
|
|
|
Sequence.Float64(1.0, 10.0),
|
|
|
|
Sequence.Float64(10, 1.0),
|
2016-07-14 15:31:42 -04:00
|
|
|
}
|
|
|
|
assert.Equal(10, mockSeries.Len())
|
|
|
|
|
2016-07-17 14:10:04 -04:00
|
|
|
mas := &SMASeries{
|
2016-07-14 15:31:42 -04:00
|
|
|
InnerSeries: mockSeries,
|
2016-07-17 14:10:04 -04:00
|
|
|
Period: 15,
|
2016-07-14 15:31:42 -04:00
|
|
|
}
|
|
|
|
|
2016-07-14 16:14:02 -04:00
|
|
|
var yvalues []float64
|
|
|
|
for x := 0; x < mas.Len(); x++ {
|
|
|
|
_, y := mas.GetValue(x)
|
|
|
|
yvalues = append(yvalues, y)
|
|
|
|
}
|
|
|
|
|
2016-07-14 15:31:42 -04:00
|
|
|
lx, ly := mas.GetLastValue()
|
|
|
|
assert.Equal(10.0, lx)
|
|
|
|
assert.Equal(5.5, ly)
|
2016-07-14 16:14:02 -04:00
|
|
|
assert.Equal(yvalues[len(yvalues)-1], ly)
|
2016-07-14 15:31:42 -04:00
|
|
|
}
|
2016-07-14 16:11:03 -04:00
|
|
|
|
2016-07-17 14:10:04 -04:00
|
|
|
func TestSMASeriesGetLastValue(t *testing.T) {
|
2016-07-14 16:11:03 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
mockSeries := mockValueProvider{
|
2016-07-29 21:24:25 -04:00
|
|
|
Sequence.Float64(1.0, 100.0),
|
|
|
|
Sequence.Float64(100, 1.0),
|
2016-07-14 16:11:03 -04:00
|
|
|
}
|
|
|
|
assert.Equal(100, mockSeries.Len())
|
|
|
|
|
2016-07-17 14:10:04 -04:00
|
|
|
mas := &SMASeries{
|
2016-07-14 16:11:03 -04:00
|
|
|
InnerSeries: mockSeries,
|
2016-07-17 14:10:04 -04:00
|
|
|
Period: 10,
|
2016-07-14 16:11:03 -04:00
|
|
|
}
|
|
|
|
|
2016-07-14 16:13:35 -04:00
|
|
|
var yvalues []float64
|
|
|
|
for x := 0; x < mas.Len(); x++ {
|
|
|
|
_, y := mas.GetValue(x)
|
|
|
|
yvalues = append(yvalues, y)
|
|
|
|
}
|
|
|
|
|
2016-07-14 16:11:03 -04:00
|
|
|
lx, ly := mas.GetLastValue()
|
|
|
|
assert.Equal(100.0, lx)
|
2016-07-17 14:10:04 -04:00
|
|
|
assert.Equal(6, ly)
|
2016-07-14 16:13:35 -04:00
|
|
|
assert.Equal(yvalues[len(yvalues)-1], ly)
|
2016-07-14 16:11:03 -04:00
|
|
|
}
|