From c7170a2650b0a17dc618f93d798acac2160eae02 Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Wed, 13 Jul 2016 11:50:22 -0700 Subject: [PATCH] some regression tests --- chart_test.go | 39 +++++++++++++++++++++++++++++++++++++++ defaults.go | 4 +++- tick.go | 5 +++++ time_series.go | 2 +- util.go | 13 +++++++++++++ 5 files changed, 61 insertions(+), 2 deletions(-) diff --git a/chart_test.go b/chart_test.go index ae1b06f..2dceba6 100644 --- a/chart_test.go +++ b/chart_test.go @@ -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.") +} diff --git a/defaults.go b/defaults.go index ace5ba7..c87ebbb 100644 --- a/defaults.go +++ b/defaults.go @@ -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 diff --git a/tick.go b/tick.go index ace38ad..850adb7 100644 --- a/tick.go +++ b/tick.go @@ -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 } diff --git a/time_series.go b/time_series.go index aec84d8..becf213 100644 --- a/time_series.go +++ b/time_series.go @@ -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 } diff --git a/util.go b/util.go index a609c8a..3e18a64 100644 --- a/util.go +++ b/util.go @@ -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 +}