2017-05-12 17:17:43 -04:00
|
|
|
package seq
|
2017-04-30 00:12:39 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
assert "github.com/blendlabs/go-assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSequenceEach(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-04-30 02:17:28 -04:00
|
|
|
values := Seq{NewArray(1, 2, 3, 4)}
|
2017-04-30 00:12:39 -04:00
|
|
|
values.Each(func(i int, v float64) {
|
2017-04-30 02:17:28 -04:00
|
|
|
assert.Equal(i, v-1)
|
2017-04-30 00:12:39 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSequenceMap(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-04-30 02:17:28 -04:00
|
|
|
values := Seq{NewArray(1, 2, 3, 4)}
|
2017-04-30 00:12:39 -04:00
|
|
|
mapped := values.Map(func(i int, v float64) float64 {
|
2017-04-30 02:17:28 -04:00
|
|
|
assert.Equal(i, v-1)
|
2017-04-30 00:12:39 -04:00
|
|
|
return v * 2
|
|
|
|
})
|
|
|
|
assert.Equal(4, mapped.Len())
|
|
|
|
}
|
2017-04-30 02:17:28 -04:00
|
|
|
|
|
|
|
func TestSequenceFoldLeft(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
values := Seq{NewArray(1, 2, 3, 4)}
|
|
|
|
ten := values.FoldLeft(func(_ int, vp, v float64) float64 {
|
|
|
|
return vp + v
|
|
|
|
})
|
|
|
|
assert.Equal(10, ten)
|
|
|
|
|
|
|
|
orderTest := Seq{NewArray(10, 3, 2, 1)}
|
|
|
|
four := orderTest.FoldLeft(func(_ int, vp, v float64) float64 {
|
|
|
|
return vp - v
|
|
|
|
})
|
|
|
|
assert.Equal(4, four)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSequenceFoldRight(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
values := Seq{NewArray(1, 2, 3, 4)}
|
|
|
|
ten := values.FoldRight(func(_ int, vp, v float64) float64 {
|
|
|
|
return vp + v
|
|
|
|
})
|
|
|
|
assert.Equal(10, ten)
|
|
|
|
|
|
|
|
orderTest := Seq{NewArray(10, 3, 2, 1)}
|
|
|
|
notFour := orderTest.FoldRight(func(_ int, vp, v float64) float64 {
|
|
|
|
return vp - v
|
|
|
|
})
|
|
|
|
assert.Equal(-14, notFour)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSequenceSum(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
values := Seq{NewArray(1, 2, 3, 4)}
|
|
|
|
assert.Equal(10, values.Sum())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSequenceAverage(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
values := Seq{NewArray(1, 2, 3, 4)}
|
|
|
|
assert.Equal(2.5, values.Average())
|
|
|
|
|
|
|
|
valuesOdd := Seq{NewArray(1, 2, 3, 4, 5)}
|
|
|
|
assert.Equal(3, valuesOdd.Average())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSequenceVariance(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
values := Seq{NewArray(1, 2, 3, 4, 5)}
|
|
|
|
assert.Equal(2, values.Variance())
|
|
|
|
}
|
2017-05-02 01:18:52 -04:00
|
|
|
|
|
|
|
func TestSequenceNormalize(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-05-12 17:17:43 -04:00
|
|
|
normalized := Values(1, 2, 3, 4, 5).Normalize().Array()
|
2017-05-02 01:18:52 -04:00
|
|
|
|
|
|
|
assert.NotEmpty(normalized)
|
|
|
|
assert.Len(normalized, 5)
|
|
|
|
assert.Equal(0, normalized[0])
|
2017-05-02 01:34:58 -04:00
|
|
|
assert.Equal(0.25, normalized[1])
|
2017-05-02 01:18:52 -04:00
|
|
|
assert.Equal(1, normalized[4])
|
|
|
|
}
|