more iterating on macd, need to get a better handle on how to test.
This commit is contained in:
parent
7858457772
commit
32dd907bf8
9 changed files with 464 additions and 246 deletions
111
sma_series_test.go
Normal file
111
sma_series_test.go
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
package chart
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/blendlabs/go-assert"
|
||||
)
|
||||
|
||||
type mockValueProvider struct {
|
||||
X []float64
|
||||
Y []float64
|
||||
}
|
||||
|
||||
func (m mockValueProvider) Len() int {
|
||||
return MinInt(len(m.X), len(m.Y))
|
||||
}
|
||||
|
||||
func (m mockValueProvider) GetValue(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) {
|
||||
assert := assert.New(t)
|
||||
|
||||
mockSeries := mockValueProvider{
|
||||
Seq(1.0, 10.0),
|
||||
Seq(10, 1.0),
|
||||
}
|
||||
assert.Equal(10, mockSeries.Len())
|
||||
|
||||
mas := &SMASeries{
|
||||
InnerSeries: mockSeries,
|
||||
Period: 10,
|
||||
}
|
||||
|
||||
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])
|
||||
}
|
||||
|
||||
func TestSMASeriesGetLastValueWindowOverlap(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
mockSeries := mockValueProvider{
|
||||
Seq(1.0, 10.0),
|
||||
Seq(10, 1.0),
|
||||
}
|
||||
assert.Equal(10, mockSeries.Len())
|
||||
|
||||
mas := &SMASeries{
|
||||
InnerSeries: mockSeries,
|
||||
Period: 15,
|
||||
}
|
||||
|
||||
var yvalues []float64
|
||||
for x := 0; x < mas.Len(); x++ {
|
||||
_, y := mas.GetValue(x)
|
||||
yvalues = append(yvalues, y)
|
||||
}
|
||||
|
||||
lx, ly := mas.GetLastValue()
|
||||
assert.Equal(10.0, lx)
|
||||
assert.Equal(5.5, ly)
|
||||
assert.Equal(yvalues[len(yvalues)-1], ly)
|
||||
}
|
||||
|
||||
func TestSMASeriesGetLastValue(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
mockSeries := mockValueProvider{
|
||||
Seq(1.0, 100.0),
|
||||
Seq(100, 1.0),
|
||||
}
|
||||
assert.Equal(100, mockSeries.Len())
|
||||
|
||||
mas := &SMASeries{
|
||||
InnerSeries: mockSeries,
|
||||
Period: 10,
|
||||
}
|
||||
|
||||
var yvalues []float64
|
||||
for x := 0; x < mas.Len(); x++ {
|
||||
_, y := mas.GetValue(x)
|
||||
yvalues = append(yvalues, y)
|
||||
}
|
||||
|
||||
lx, ly := mas.GetLastValue()
|
||||
assert.Equal(100.0, lx)
|
||||
assert.Equal(6, ly)
|
||||
assert.Equal(yvalues[len(yvalues)-1], ly)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue