2019-02-13 21:55:13 -05:00
|
|
|
package chart
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-10-27 22:52:38 -04:00
|
|
|
"git.smarteching.com/zeni/go-chart/v2/testutil"
|
2019-02-13 21:55:13 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSeqEach(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// 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) {
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, i, v-1)
|
2019-02-13 21:55:13 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSeqMap(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// 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 {
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, i, v-1)
|
2019-02-13 21:55:13 -05:00
|
|
|
return v * 2
|
|
|
|
})
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 4, mapped.Len())
|
2019-02-13 21:55:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSeqFoldLeft(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// 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
|
|
|
|
})
|
2020-11-22 19:45:10 -05:00
|
|
|
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
|
|
|
|
})
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 4, four)
|
2019-02-13 21:55:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSeqFoldRight(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// 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
|
|
|
|
})
|
2020-11-22 19:45:10 -05:00
|
|
|
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
|
|
|
|
})
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, -14, notFour)
|
2019-02-13 21:55:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSeqSum(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2019-02-13 21:55:13 -05:00
|
|
|
|
|
|
|
values := Seq{NewArray(1, 2, 3, 4)}
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 10, values.Sum())
|
2019-02-13 21:55:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSeqAverage(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2019-02-13 21:55:13 -05:00
|
|
|
|
|
|
|
values := Seq{NewArray(1, 2, 3, 4)}
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 2.5, values.Average())
|
2019-02-13 21:55:13 -05:00
|
|
|
|
|
|
|
valuesOdd := Seq{NewArray(1, 2, 3, 4, 5)}
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 3, valuesOdd.Average())
|
2019-02-13 21:55:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSequenceVariance(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2019-02-13 21:55:13 -05:00
|
|
|
|
|
|
|
values := Seq{NewArray(1, 2, 3, 4, 5)}
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 2, values.Variance())
|
2019-02-13 21:55:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSequenceNormalize(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2019-02-13 21:55:13 -05:00
|
|
|
|
|
|
|
normalized := ValueSequence(1, 2, 3, 4, 5).Normalize().Values()
|
|
|
|
|
2020-11-22 19:45:10 -05:00
|
|
|
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) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2019-02-13 21:55:13 -05:00
|
|
|
|
|
|
|
values := LinearRange(1, 100)
|
2020-11-22 19:45:10 -05:00
|
|
|
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) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2019-02-13 21:55:13 -05:00
|
|
|
|
|
|
|
values := LinearRangeWithStep(0, 100, 5)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 100, values[20])
|
|
|
|
testutil.AssertLen(t, values, 21)
|
2019-02-13 21:55:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinearRangeReversed(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2019-02-13 21:55:13 -05:00
|
|
|
|
|
|
|
values := LinearRange(10.0, 1.0)
|
2020-11-22 19:45:10 -05:00
|
|
|
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) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// 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)
|
2020-11-22 19:45:10 -05:00
|
|
|
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()
|
2020-11-22 19:45:10 -05:00
|
|
|
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
|
|
|
}
|