some regression tests

This commit is contained in:
Will Charczuk 2016-07-13 11:50:22 -07:00
parent 07c96b1948
commit c7170a2650
5 changed files with 61 additions and 2 deletions

View file

@ -2,6 +2,7 @@ package chart
import (
"bytes"
"math"
"testing"
"time"
@ -265,3 +266,41 @@ func TestChartSingleSeries(t *testing.T) {
c.Render(PNG, buffer)
assert.NotEmpty(buffer.Bytes())
}
func TestChartRegressionBadRanges(t *testing.T) {
assert := assert.New(t)
c := Chart{
Series: []Series{
ContinuousSeries{
XValues: []float64{math.Inf(1), math.Inf(1), math.Inf(1), math.Inf(1), math.Inf(1)},
YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5},
},
},
}
buffer := bytes.NewBuffer([]byte{})
c.Render(PNG, buffer)
assert.True(true, "Render needs to finish.")
}
func TestChartRegressionBadRangesByUser(t *testing.T) {
assert := assert.New(t)
c := Chart{
YAxis: YAxis{
Range: Range{
Min: math.Inf(-1),
Max: math.Inf(1), // this could really happen? eh.
},
},
Series: []Series{
ContinuousSeries{
XValues: Seq(1.0, 10.0),
YValues: Seq(1.0, 10.0),
},
},
}
buffer := bytes.NewBuffer([]byte{})
c.Render(PNG, buffer)
assert.True(true, "Render needs to finish.")
}

View file

@ -43,8 +43,10 @@ const (
//DefaultHorizontalTickWidth is half the margin.
DefaultHorizontalTickWidth = DefaultYAxisMargin >> 1
// DefaultTickCount is the defautl number of ticks to show
// DefaultTickCount is the default number of ticks to show
DefaultTickCount = 10
// DefaultTickCountSanityCheck is a hard limit on number of ticks to prevent infinite loops.
DefaultTickCountSanityCheck = 1 << 10 //1024
// DefaultMinimumTickHorizontalSpacing is the minimum distance between horizontal ticks.
DefaultMinimumTickHorizontalSpacing = 20

View file

@ -9,6 +9,11 @@ func GenerateTicksWithStep(ra Range, step float64, vf ValueFormatter) []Tick {
Value: cursor,
Label: vf(cursor),
})
// this guard is in place in case step is super, super small.
if len(ticks) > DefaultTickCountSanityCheck {
return ticks
}
}
return ticks
}

View file

@ -30,7 +30,7 @@ func (ts TimeSeries) Len() int {
// GetValue gets a value at a given index.
func (ts TimeSeries) GetValue(index int) (x float64, y float64) {
x = float64(ts.XValues[index].Unix())
x = TimeToFloat64(ts.XValues[index])
y = ts.YValues[index]
return
}

13
util.go
View file

@ -122,3 +122,16 @@ func AbsInt(value int) int {
}
return value
}
// Seq produces an array of floats from [start,end] by optional steps.
func Seq(start, end float64, steps ...float64) []float64 {
var values []float64
step := 1.0
if len(steps) > 0 {
step = steps[0]
}
for x := start; x <= end; x += step {
values = append(values, x)
}
return values
}