2016-07-14 15:30:57 -04:00
|
|
|
package chart
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-10-27 22:52:38 -04:00
|
|
|
"git.smarteching.com/zeni/go-chart/v2/testutil"
|
2016-07-14 15:30:57 -04:00
|
|
|
)
|
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
type mockValuesProvider struct {
|
2016-07-14 15:30:57 -04:00
|
|
|
X []float64
|
|
|
|
Y []float64
|
|
|
|
}
|
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
func (m mockValuesProvider) Len() int {
|
2019-02-13 19:09:26 -05:00
|
|
|
return MinInt(len(m.X), len(m.Y))
|
2016-07-14 15:30:57 -04:00
|
|
|
}
|
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
func (m mockValuesProvider) GetValues(index int) (x, y float64) {
|
2016-07-17 14:10:04 -04:00
|
|
|
if index < 0 {
|
|
|
|
panic("negative index at GetValue()")
|
|
|
|
}
|
2019-02-13 19:09:26 -05:00
|
|
|
if index >= 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) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-07-14 15:30:57 -04:00
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
mockSeries := mockValuesProvider{
|
2019-02-13 21:55:13 -05:00
|
|
|
LinearRange(1.0, 10.0),
|
|
|
|
LinearRange(10, 1.0),
|
2016-07-14 15:30:57 -04:00
|
|
|
}
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 10, mockSeries.Len())
|
2016-07-14 15:30:57 -04:00
|
|
|
|
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++ {
|
2017-05-12 20:12:23 -04:00
|
|
|
_, y := mas.GetValues(x)
|
2016-07-14 15:30:57 -04:00
|
|
|
yvalues = append(yvalues, y)
|
|
|
|
}
|
|
|
|
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 10.0, yvalues[0])
|
|
|
|
testutil.AssertEqual(t, 9.5, yvalues[1])
|
|
|
|
testutil.AssertEqual(t, 9.0, yvalues[2])
|
|
|
|
testutil.AssertEqual(t, 8.5, yvalues[3])
|
|
|
|
testutil.AssertEqual(t, 8.0, yvalues[4])
|
|
|
|
testutil.AssertEqual(t, 7.5, yvalues[5])
|
|
|
|
testutil.AssertEqual(t, 7.0, yvalues[6])
|
|
|
|
testutil.AssertEqual(t, 6.5, yvalues[7])
|
|
|
|
testutil.AssertEqual(t, 6.0, yvalues[8])
|
2016-07-14 15:30:57 -04:00
|
|
|
}
|
2016-07-14 15:31:42 -04:00
|
|
|
|
2016-07-17 14:10:04 -04:00
|
|
|
func TestSMASeriesGetLastValueWindowOverlap(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-07-14 15:31:42 -04:00
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
mockSeries := mockValuesProvider{
|
2019-02-13 21:55:13 -05:00
|
|
|
LinearRange(1.0, 10.0),
|
|
|
|
LinearRange(10, 1.0),
|
2016-07-14 15:31:42 -04:00
|
|
|
}
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 10, mockSeries.Len())
|
2016-07-14 15:31:42 -04:00
|
|
|
|
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++ {
|
2017-05-12 20:12:23 -04:00
|
|
|
_, y := mas.GetValues(x)
|
2016-07-14 16:14:02 -04:00
|
|
|
yvalues = append(yvalues, y)
|
|
|
|
}
|
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
lx, ly := mas.GetLastValues()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 10.0, lx)
|
|
|
|
testutil.AssertEqual(t, 5.5, ly)
|
|
|
|
testutil.AssertEqual(t, 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) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-07-14 16:11:03 -04:00
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
mockSeries := mockValuesProvider{
|
2019-02-13 21:55:13 -05:00
|
|
|
LinearRange(1.0, 100.0),
|
|
|
|
LinearRange(100, 1.0),
|
2016-07-14 16:11:03 -04:00
|
|
|
}
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 100, mockSeries.Len())
|
2016-07-14 16:11:03 -04:00
|
|
|
|
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++ {
|
2017-05-12 20:12:23 -04:00
|
|
|
_, y := mas.GetValues(x)
|
2016-07-14 16:13:35 -04:00
|
|
|
yvalues = append(yvalues, y)
|
|
|
|
}
|
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
lx, ly := mas.GetLastValues()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 100.0, lx)
|
|
|
|
testutil.AssertEqual(t, 6, ly)
|
|
|
|
testutil.AssertEqual(t, yvalues[len(yvalues)-1], ly)
|
2016-07-14 16:11:03 -04:00
|
|
|
}
|