things work?

This commit is contained in:
Will Charczuk 2017-05-01 22:51:23 -07:00
commit fde118dd7c
6 changed files with 56 additions and 45 deletions

View File

@ -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,37 @@ 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)
fi := float64(index)
if lg.start < lg.end {
return lg.start + (fi * lg.step)
}
return lg.start - (fi * 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
}

View File

@ -15,7 +15,7 @@ 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)
@ -23,6 +23,11 @@ func TestValueWithStep(t *testing.T) {
assert.Len(values, 21)
}
func TestValuesLen(t *testing.T) {
func TestValuesReversed(t *testing.T) {
assert := assert.New(t)
values := Values(10.0, 1.0)
assert.Equal(10, len(values))
assert.Equal(10.0, values[0])
assert.Equal(1.0, values[9])
}

View File

@ -27,7 +27,6 @@ func (s Seq) Array() (output []float64) {
return
}
println(s.Len())
output = make([]float64, s.Len())
for i := 0; i < s.Len(); i++ {
output[i] = s.GetValue(i)

View File

@ -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)
}

View File

@ -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()))

View File

@ -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)