This commit is contained in:
Will Charczuk 2017-04-29 22:44:17 -07:00
parent 6a4f3936c6
commit 674e75d285
5 changed files with 53 additions and 94 deletions

View file

@ -165,7 +165,7 @@ func TestNewBuffer(t *testing.T) {
empty := NewBuffer() empty := NewBuffer()
assert.NotNil(empty) assert.NotNil(empty)
assert.Zero(empty.Len()) assert.Zero(empty.Len())
assert.Equal(valueBufferDefaultCapacity, empty.Capacity()) assert.Equal(bufferDefaultCapacity, empty.Capacity())
assert.Zero(empty.Peek()) assert.Zero(empty.Peek())
assert.Zero(empty.PeekBack()) assert.Zero(empty.PeekBack())
} }
@ -176,7 +176,7 @@ func TestNewBufferWithValues(t *testing.T) {
values := NewBuffer(1, 2, 3, 4, 5) values := NewBuffer(1, 2, 3, 4, 5)
assert.NotNil(values) assert.NotNil(values)
assert.Equal(5, values.Len()) assert.Equal(5, values.Len())
assert.Equal(valueBufferDefaultCapacity, values.Capacity()) assert.Equal(bufferDefaultCapacity, values.Capacity())
assert.Equal(1, values.Peek()) assert.Equal(1, values.Peek())
assert.Equal(5, values.PeekBack()) assert.Equal(5, values.PeekBack())
} }

View file

@ -1,61 +0,0 @@
package sequence
import (
"math/rand"
"time"
)
var (
// Generate contains some sequence generation utilities.
// These utilities can be useful for generating test data.
Generate = &generate{
rnd: rand.New(rand.NewSource(time.Now().Unix())),
}
)
type generate struct {
rnd *rand.Rand
}
// Values produces an array of floats from [start,end] by optional steps.
func (g generate) Values(start, end float64, steps ...float64) Seq {
var values []float64
step := 1.0
if len(steps) > 0 {
step = steps[0]
}
if start < end {
for x := start; x <= end; x += step {
values = append(values, x)
}
} else {
for x := start; x >= end; x = x - step {
values = append(values, x)
}
}
return Seq{Array(values)}
}
// Random generates a fixed length sequence of random values between (0, scale).
func (g generate) RandomValues(samples int, scale float64) Seq {
values := make([]float64, samples)
for x := 0; x < samples; x++ {
values[x] = g.rnd.Float64() * scale
}
return Seq{Array(values)}
}
// Random generates a fixed length sequence of random values with a given average, above and below that average by (-scale, scale)
func (g generate) RandomValuesWithAverage(samples int, average, scale float64) Seq {
values := make([]float64, samples)
for x := 0; x < samples; x++ {
jitter := scale - (g.rnd.Float64() * (2 * scale))
values[x] = average + jitter
}
return Seq{Array(values)}
}

View file

@ -1,21 +1,50 @@
package sequence package sequence
import "math/rand" import (
"math"
"math/rand"
)
// Random is a random number sequence generator.
type Random struct { type Random struct {
rnd *rand.Rand rnd *rand.Rand
scale *float64 scale *float64
average *float64 average *float64
len int len *int
} }
// Len returns the number of elements that will be generated.
func (r *Random) Len() int { func (r *Random) Len() int {
return r.len if r.len != nil {
return *r.len
}
return math.MaxInt32
} }
// GetValue returns the value.
func (r *Random) GetValue(_ int) float64 { func (r *Random) GetValue(_ int) float64 {
if r.scale != nil { if r.average != nil && r.scale != nil {
return *r.average + *r.scale - (r.rnd.Float64() * (2 * *r.scale))
} else if r.scale != nil {
return r.rnd.Float64() * *r.scale return r.rnd.Float64() * *r.scale
} }
return r.rnd.Float64() return r.rnd.Float64()
} }
// WithLen sets a maximum len
func (r *Random) WithLen(length int) *Random {
r.len = &length
return r
}
// WithScale sets the scale and returns the Random.
func (r *Random) WithScale(scale float64) *Random {
r.scale = &scale
return r
}
// WithAverage sets the average and returns the Random.
func (r *Random) WithAverage(average float64) *Random {
r.average = &average
return r
}

View file

@ -123,8 +123,8 @@ func (ts timeSequence) Hours(start time.Time, totalHours int) []time.Time {
// HoursFilled adds zero values for the data bounded by the start and end of the xdata array. // HoursFilled adds zero values for the data bounded by the start and end of the xdata array.
func (ts timeSequence) HoursFilled(xdata []time.Time, ydata []float64) ([]time.Time, []float64) { func (ts timeSequence) HoursFilled(xdata []time.Time, ydata []float64) ([]time.Time, []float64) {
start := util.Date.Start(xdata) start := Time.Start(xdata)
end := util.Date.End(xdata) end := Time.End(xdata)
totalHours := util.Math.AbsInt(util.Date.DiffHours(start, end)) totalHours := util.Math.AbsInt(util.Date.DiffHours(start, end))

View file

@ -5,52 +5,43 @@ import (
"time" "time"
assert "github.com/blendlabs/go-assert" assert "github.com/blendlabs/go-assert"
"github.com/wcharczuk/go-chart/util"
) )
func TestSequenceFloat64(t *testing.T) { func TestTimeMarketHours(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
asc := Generate.Float64(1.0, 10.0) today := time.Date(2016, 07, 01, 12, 0, 0, 0, util.Date.Eastern())
assert.Len(asc, 10) mh := Time.MarketHours(today, today, util.NYSEOpen(), util.NYSEClose(), util.Date.IsNYSEHoliday)
desc := Generate.Float64(10.0, 1.0)
assert.Len(desc, 10)
}
func TestSequenceMarketHours(t *testing.T) {
assert := assert.New(t)
today := time.Date(2016, 07, 01, 12, 0, 0, 0, Date.Eastern())
mh := Generate.MarketHours(today, today, NYSEOpen(), NYSEClose(), Date.IsNYSEHoliday)
assert.Len(mh, 8) assert.Len(mh, 8)
assert.Equal(Date.Eastern(), mh[0].Location()) assert.Equal(util.Date.Eastern(), mh[0].Location())
} }
func TestSequenceMarketQuarters(t *testing.T) { func TestTimeMarketHourQuarters(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
today := time.Date(2016, 07, 01, 12, 0, 0, 0, Date.Eastern()) today := time.Date(2016, 07, 01, 12, 0, 0, 0, util.Date.Eastern())
mh := Generate.MarketHourQuarters(today, today, NYSEOpen(), NYSEClose(), Date.IsNYSEHoliday) mh := Time.MarketHourQuarters(today, today, util.NYSEOpen(), util.NYSEClose(), util.Date.IsNYSEHoliday)
assert.Len(mh, 4) assert.Len(mh, 4)
assert.Equal(9, mh[0].Hour()) assert.Equal(9, mh[0].Hour())
assert.Equal(30, mh[0].Minute()) assert.Equal(30, mh[0].Minute())
assert.Equal(Date.Eastern(), mh[0].Location()) assert.Equal(util.Date.Eastern(), mh[0].Location())
assert.Equal(12, mh[1].Hour()) assert.Equal(12, mh[1].Hour())
assert.Equal(00, mh[1].Minute()) assert.Equal(00, mh[1].Minute())
assert.Equal(Date.Eastern(), mh[1].Location()) assert.Equal(util.Date.Eastern(), mh[1].Location())
assert.Equal(14, mh[2].Hour()) assert.Equal(14, mh[2].Hour())
assert.Equal(00, mh[2].Minute()) assert.Equal(00, mh[2].Minute())
assert.Equal(Date.Eastern(), mh[2].Location()) assert.Equal(util.Date.Eastern(), mh[2].Location())
} }
func TestSequenceHours(t *testing.T) { func TestTimeHours(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
today := time.Date(2016, 07, 01, 12, 0, 0, 0, time.UTC) today := time.Date(2016, 07, 01, 12, 0, 0, 0, time.UTC)
seq := Generate.Hours(today, 24) seq := Time.Hours(today, 24)
end := Date.End(seq) end := Time.End(seq)
assert.Len(seq, 24) assert.Len(seq, 24)
assert.Equal(2016, end.Year()) assert.Equal(2016, end.Year())
assert.Equal(07, int(end.Month())) assert.Equal(07, int(end.Month()))
@ -81,8 +72,8 @@ func TestSequenceHoursFill(t *testing.T) {
0.6, 0.6,
} }
filledTimes, filledValues := Generate.HoursFilled(xdata, ydata) filledTimes, filledValues := Time.HoursFilled(xdata, ydata)
assert.Len(filledTimes, Date.DiffHours(Date.Start(xdata), Date.End(xdata))+1) assert.Len(filledTimes, util.Date.DiffHours(Time.Start(xdata), Time.End(xdata))+1)
assert.Equal(len(filledValues), len(filledTimes)) assert.Equal(len(filledValues), len(filledTimes))
assert.NotZero(filledValues[0]) assert.NotZero(filledValues[0])