From 2f84185f464fff74f45764fa3fe346a8ea6b8020 Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Mon, 1 May 2017 22:33:49 -0700 Subject: [PATCH] changes. --- sequence/linear.go | 32 +++++++++++++++++++------------- sequence/linear_test.go | 10 +++++++++- sequence/time_test.go | 28 ++++++++++++++++++++++++++++ stacked_bar_chart.go | 2 +- util/date_test.go | 28 ---------------------------- 5 files changed, 57 insertions(+), 43 deletions(-) diff --git a/sequence/linear.go b/sequence/linear.go index ea703b9..7aabcbd 100644 --- a/sequence/linear.go +++ b/sequence/linear.go @@ -2,12 +2,12 @@ package sequence // Values returns the array values of a linear sequence with a given start, end and optional step. func Values(start, end float64) []float64 { - return Seq{NewLinear().WithOffset(start).WithLimit(end).WithStep(1.0)}.Array() + return Seq{NewLinear().WithStart(start).WithEnd(end).WithStep(1.0)}.Array() } // ValuesWithStep returns the array values of a linear sequence with a given start, end and optional step. func ValuesWithStep(start, end, step float64) []float64 { - return Seq{NewLinear().WithOffset(start).WithLimit(end).WithStep(step)}.Array() + return Seq{NewLinear().WithStart(start).WithEnd(end).WithStep(step)}.Array() } // NewLinear returns a new linear generator. @@ -17,30 +17,36 @@ func NewLinear() *Linear { // Linear is a stepwise generator. type Linear struct { - offset float64 - limit float64 - step float64 + start float64 + end float64 + step float64 } // Len returns the number of elements in the sequence. func (lg Linear) Len() int { - return (int((lg.limit - lg.offset) / lg.step)) + 1 + if lg.start < lg.end { + return int((lg.end-lg.start)/lg.step) + 1 + } + return int((lg.start-lg.end)/lg.step) + 1 } // GetValue returns the value at a given index. func (lg Linear) GetValue(index int) float64 { - return lg.offset + (float64(index) * lg.step) + if lg.start < lg.end { + return lg.start + (float64(index) * lg.step) + } + return lg.end + (float64(index) * lg.step) } -// WithOffset sets the offset and returns the linear generator. -func (lg *Linear) WithOffset(offset float64) *Linear { - lg.offset = offset +// WithStart sets the start and returns the linear generator. +func (lg *Linear) WithStart(start float64) *Linear { + lg.start = start return lg } -// WithLimit sets the step and returns the linear generator. -func (lg *Linear) WithLimit(limit float64) *Linear { - lg.limit = limit +// WithEnd sets the end and returns the linear generator. +func (lg *Linear) WithEnd(end float64) *Linear { + lg.end = end return lg } diff --git a/sequence/linear_test.go b/sequence/linear_test.go index d3c7e21..8042ba0 100644 --- a/sequence/linear_test.go +++ b/sequence/linear_test.go @@ -15,10 +15,18 @@ func TestValues(t *testing.T) { assert.Equal(100, values[99]) } -func TestValueWithStep(t *testing.T) { +func TestValuesWithStep(t *testing.T) { assert := assert.New(t) values := ValuesWithStep(0, 100, 5) assert.Equal(100, values[20]) assert.Len(values, 21) } + +func TestValuesReversed(t *testing.T) { + assert := assert.New(t) + + values := Values(10.0, 1.0) + assert.NotEmpty(values) + assert.Len(values, 10) +} diff --git a/sequence/time_test.go b/sequence/time_test.go index c783472..a26be04 100644 --- a/sequence/time_test.go +++ b/sequence/time_test.go @@ -81,3 +81,31 @@ func TestSequenceHoursFill(t *testing.T) { assert.NotZero(filledValues[16]) } + +func TestTimeStart(t *testing.T) { + assert := assert.New(t) + + times := []time.Time{ + time.Now().AddDate(0, 0, -4), + time.Now().AddDate(0, 0, -2), + time.Now().AddDate(0, 0, -1), + time.Now().AddDate(0, 0, -3), + time.Now().AddDate(0, 0, -5), + } + + assert.InTimeDelta(Time.Start(times), times[4], time.Millisecond) +} + +func TestTimeEnd(t *testing.T) { + assert := assert.New(t) + + times := []time.Time{ + time.Now().AddDate(0, 0, -4), + time.Now().AddDate(0, 0, -2), + time.Now().AddDate(0, 0, -1), + time.Now().AddDate(0, 0, -3), + time.Now().AddDate(0, 0, -5), + } + + assert.InTimeDelta(Time.End(times), times[2], time.Millisecond) +} diff --git a/stacked_bar_chart.go b/stacked_bar_chart.go index 33dd5b1..5bd5959 100644 --- a/stacked_bar_chart.go +++ b/stacked_bar_chart.go @@ -202,7 +202,7 @@ func (sbc StackedBarChart) drawYAxis(r Renderer, canvasBox Box) { r.LineTo(canvasBox.Right+DefaultHorizontalTickWidth, canvasBox.Bottom) r.Stroke() - ticks := sequence.Seq{Provider: sequence.NewLinear().WithLimit(1.0).WithStep(0.2)}.Array() + ticks := sequence.ValuesWithStep(0.0, 1.0, 0.2) for _, t := range ticks { axisStyle.GetStrokeOptions().WriteToRenderer(r) ty := canvasBox.Bottom - int(t*float64(canvasBox.Height())) diff --git a/util/date_test.go b/util/date_test.go index 9b9547e..223ebae 100644 --- a/util/date_test.go +++ b/util/date_test.go @@ -237,34 +237,6 @@ func TestDateIsNYSEHoliday(t *testing.T) { assert.Equal(holidays, 55) } -func TestTimeStart(t *testing.T) { - assert := assert.New(t) - - times := []time.Time{ - time.Now().AddDate(0, 0, -4), - time.Now().AddDate(0, 0, -2), - time.Now().AddDate(0, 0, -1), - time.Now().AddDate(0, 0, -3), - time.Now().AddDate(0, 0, -5), - } - - assert.InTimeDelta(Date.Start(times), times[4], time.Millisecond) -} - -func TestTimeEnd(t *testing.T) { - assert := assert.New(t) - - times := []time.Time{ - time.Now().AddDate(0, 0, -4), - time.Now().AddDate(0, 0, -2), - time.Now().AddDate(0, 0, -1), - time.Now().AddDate(0, 0, -3), - time.Now().AddDate(0, 0, -5), - } - - assert.InTimeDelta(Date.End(times), times[2], time.Millisecond) -} - func TestDateDiffDays(t *testing.T) { assert := assert.New(t)