go-chart/seq_test.go

137 lines
3.4 KiB
Go
Raw Normal View History

2019-02-13 21:55:13 -05:00
package chart
import (
"testing"
"github.com/wcharczuk/go-chart/v2/testutil"
2019-02-13 21:55:13 -05:00
)
func TestSeqEach(t *testing.T) {
// replaced new assertions helper
2019-02-13 21:55:13 -05:00
values := Seq{NewArray(1, 2, 3, 4)}
values.Each(func(i int, v float64) {
testutil.AssertEqual(t, i, v-1)
2019-02-13 21:55:13 -05:00
})
}
func TestSeqMap(t *testing.T) {
// replaced new assertions helper
2019-02-13 21:55:13 -05:00
values := Seq{NewArray(1, 2, 3, 4)}
mapped := values.Map(func(i int, v float64) float64 {
testutil.AssertEqual(t, i, v-1)
2019-02-13 21:55:13 -05:00
return v * 2
})
testutil.AssertEqual(t, 4, mapped.Len())
2019-02-13 21:55:13 -05:00
}
func TestSeqFoldLeft(t *testing.T) {
// replaced new assertions helper
2019-02-13 21:55:13 -05:00
values := Seq{NewArray(1, 2, 3, 4)}
ten := values.FoldLeft(func(_ int, vp, v float64) float64 {
return vp + v
})
testutil.AssertEqual(t, 10, ten)
2019-02-13 21:55:13 -05:00
orderTest := Seq{NewArray(10, 3, 2, 1)}
four := orderTest.FoldLeft(func(_ int, vp, v float64) float64 {
return vp - v
})
testutil.AssertEqual(t, 4, four)
2019-02-13 21:55:13 -05:00
}
func TestSeqFoldRight(t *testing.T) {
// replaced new assertions helper
2019-02-13 21:55:13 -05:00
values := Seq{NewArray(1, 2, 3, 4)}
ten := values.FoldRight(func(_ int, vp, v float64) float64 {
return vp + v
})
testutil.AssertEqual(t, 10, ten)
2019-02-13 21:55:13 -05:00
orderTest := Seq{NewArray(10, 3, 2, 1)}
notFour := orderTest.FoldRight(func(_ int, vp, v float64) float64 {
return vp - v
})
testutil.AssertEqual(t, -14, notFour)
2019-02-13 21:55:13 -05:00
}
func TestSeqSum(t *testing.T) {
// replaced new assertions helper
2019-02-13 21:55:13 -05:00
values := Seq{NewArray(1, 2, 3, 4)}
testutil.AssertEqual(t, 10, values.Sum())
2019-02-13 21:55:13 -05:00
}
func TestSeqAverage(t *testing.T) {
// replaced new assertions helper
2019-02-13 21:55:13 -05:00
values := Seq{NewArray(1, 2, 3, 4)}
testutil.AssertEqual(t, 2.5, values.Average())
2019-02-13 21:55:13 -05:00
valuesOdd := Seq{NewArray(1, 2, 3, 4, 5)}
testutil.AssertEqual(t, 3, valuesOdd.Average())
2019-02-13 21:55:13 -05:00
}
func TestSequenceVariance(t *testing.T) {
// replaced new assertions helper
2019-02-13 21:55:13 -05:00
values := Seq{NewArray(1, 2, 3, 4, 5)}
testutil.AssertEqual(t, 2, values.Variance())
2019-02-13 21:55:13 -05:00
}
func TestSequenceNormalize(t *testing.T) {
// replaced new assertions helper
2019-02-13 21:55:13 -05:00
normalized := ValueSequence(1, 2, 3, 4, 5).Normalize().Values()
testutil.AssertNotEmpty(t, normalized)
testutil.AssertLen(t, normalized, 5)
testutil.AssertEqual(t, 0, normalized[0])
testutil.AssertEqual(t, 0.25, normalized[1])
testutil.AssertEqual(t, 1, normalized[4])
2019-02-13 21:55:13 -05:00
}
func TestLinearRange(t *testing.T) {
// replaced new assertions helper
2019-02-13 21:55:13 -05:00
values := LinearRange(1, 100)
testutil.AssertLen(t, values, 100)
testutil.AssertEqual(t, 1, values[0])
testutil.AssertEqual(t, 100, values[99])
2019-02-13 21:55:13 -05:00
}
func TestLinearRangeWithStep(t *testing.T) {
// replaced new assertions helper
2019-02-13 21:55:13 -05:00
values := LinearRangeWithStep(0, 100, 5)
testutil.AssertEqual(t, 100, values[20])
testutil.AssertLen(t, values, 21)
2019-02-13 21:55:13 -05:00
}
func TestLinearRangeReversed(t *testing.T) {
// replaced new assertions helper
2019-02-13 21:55:13 -05:00
values := LinearRange(10.0, 1.0)
testutil.AssertEqual(t, 10, len(values))
testutil.AssertEqual(t, 10.0, values[0])
testutil.AssertEqual(t, 1.0, values[9])
2019-02-13 21:55:13 -05:00
}
func TestLinearSequenceRegression(t *testing.T) {
// replaced new assertions helper
2019-02-13 21:55:13 -05:00
// note; this assumes a 1.0 step is implicitly set in the constructor.
linearProvider := NewLinearSequence().WithStart(1.0).WithEnd(100.0)
testutil.AssertEqual(t, 1, linearProvider.Start())
testutil.AssertEqual(t, 100, linearProvider.End())
testutil.AssertEqual(t, 100, linearProvider.Len())
2019-02-13 21:55:13 -05:00
values := Seq{linearProvider}.Values()
testutil.AssertLen(t, values, 100)
testutil.AssertEqual(t, 1.0, values[0])
testutil.AssertEqual(t, 100, values[99])
2019-02-13 21:55:13 -05:00
}