adding tests.

This commit is contained in:
Will Charczuk 2016-07-14 12:30:57 -07:00
parent c7e5443982
commit a756db412b
3 changed files with 79 additions and 2 deletions

View file

@ -0,0 +1,53 @@
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) {
x = m.X[index]
y = m.Y[index]
return
}
func TestMovingAverageSeriesGetValue(t *testing.T) {
assert := assert.New(t)
mockSeries := mockValueProvider{
Seq(1.0, 10.0),
Seq(10, 1.0),
}
assert.Equal(10, mockSeries.Len())
mas := &MovingAverageSeries{
InnerSeries: mockSeries,
WindowSize: 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])
}

View file

@ -130,9 +130,16 @@ func Seq(start, end float64, steps ...float64) []float64 {
if len(steps) > 0 {
step = steps[0]
}
if start < end {
for x := start; x <= end; x += step {
values = append(values, x)
}
} else {
for x := start; x >= end; x = x - step {
values = append(values, x)
}
}
return values
}

View file

@ -83,3 +83,20 @@ func TestGetRoundToForDelta(t *testing.T) {
assert.Equal(10.0, GetRoundToForDelta(101.00))
assert.Equal(1.0, GetRoundToForDelta(11.00))
}
func TestSeq(t *testing.T) {
assert := assert.New(t)
asc := Seq(1.0, 10.0)
assert.Len(asc, 10)
desc := Seq(10.0, 1.0)
assert.Len(desc, 10)
}
func TestPercentDifference(t *testing.T) {
assert := assert.New(t)
assert.Equal(0.5, PercentDifference(1.0, 1.5))
assert.Equal(-0.5, PercentDifference(2.0, 1.0))
}