| 1 |
package chart |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/wcharczuk/go-chart/v2/testutil" |
| 7 |
) |
| 8 |
|
| 9 |
func TestSeqEach(t *testing.T) { |
| 10 |
|
| 11 |
|
| 12 |
values := Seq{NewArray(1, 2, 3, 4)} |
| 13 |
values.Each(func(i int, v float64) { |
| 14 |
testutil.AssertEqual(t, i, v-1) |
| 15 |
}) |
| 16 |
} |
| 17 |
|
| 18 |
func TestSeqMap(t *testing.T) { |
| 19 |
|
| 20 |
|
| 21 |
values := Seq{NewArray(1, 2, 3, 4)} |
| 22 |
mapped := values.Map(func(i int, v float64) float64 { |
| 23 |
testutil.AssertEqual(t, i, v-1) |
| 24 |
return v * 2 |
| 25 |
}) |
| 26 |
testutil.AssertEqual(t, 4, mapped.Len()) |
| 27 |
} |
| 28 |
|
| 29 |
func TestSeqFoldLeft(t *testing.T) { |
| 30 |
|
| 31 |
|
| 32 |
values := Seq{NewArray(1, 2, 3, 4)} |
| 33 |
ten := values.FoldLeft(func(_ int, vp, v float64) float64 { |
| 34 |
return vp + v |
| 35 |
}) |
| 36 |
testutil.AssertEqual(t, 10, ten) |
| 37 |
|
| 38 |
orderTest := Seq{NewArray(10, 3, 2, 1)} |
| 39 |
four := orderTest.FoldLeft(func(_ int, vp, v float64) float64 { |
| 40 |
return vp - v |
| 41 |
}) |
| 42 |
testutil.AssertEqual(t, 4, four) |
| 43 |
} |
| 44 |
|
| 45 |
func TestSeqFoldRight(t *testing.T) { |
| 46 |
|
| 47 |
|
| 48 |
values := Seq{NewArray(1, 2, 3, 4)} |
| 49 |
ten := values.FoldRight(func(_ int, vp, v float64) float64 { |
| 50 |
return vp + v |
| 51 |
}) |
| 52 |
testutil.AssertEqual(t, 10, ten) |
| 53 |
|
| 54 |
orderTest := Seq{NewArray(10, 3, 2, 1)} |
| 55 |
notFour := orderTest.FoldRight(func(_ int, vp, v float64) float64 { |
| 56 |
return vp - v |
| 57 |
}) |
| 58 |
testutil.AssertEqual(t, -14, notFour) |
| 59 |
} |
| 60 |
|
| 61 |
func TestSeqSum(t *testing.T) { |
| 62 |
|
| 63 |
|
| 64 |
values := Seq{NewArray(1, 2, 3, 4)} |
| 65 |
testutil.AssertEqual(t, 10, values.Sum()) |
| 66 |
} |
| 67 |
|
| 68 |
func TestSeqAverage(t *testing.T) { |
| 69 |
|
| 70 |
|
| 71 |
values := Seq{NewArray(1, 2, 3, 4)} |
| 72 |
testutil.AssertEqual(t, 2.5, values.Average()) |
| 73 |
|
| 74 |
valuesOdd := Seq{NewArray(1, 2, 3, 4, 5)} |
| 75 |
testutil.AssertEqual(t, 3, valuesOdd.Average()) |
| 76 |
} |
| 77 |
|
| 78 |
func TestSequenceVariance(t *testing.T) { |
| 79 |
|
| 80 |
|
| 81 |
values := Seq{NewArray(1, 2, 3, 4, 5)} |
| 82 |
testutil.AssertEqual(t, 2, values.Variance()) |
| 83 |
} |
| 84 |
|
| 85 |
func TestSequenceNormalize(t *testing.T) { |
| 86 |
|
| 87 |
|
| 88 |
normalized := ValueSequence(1, 2, 3, 4, 5).Normalize().Values() |
| 89 |
|
| 90 |
testutil.AssertNotEmpty(t, normalized) |
| 91 |
testutil.AssertLen(t, normalized, 5) |
| 92 |
testutil.AssertEqual(t, 0, normalized[0]) |
| 93 |
testutil.AssertEqual(t, 0.25, normalized[1]) |
| 94 |
testutil.AssertEqual(t, 1, normalized[4]) |
| 95 |
} |
| 96 |
|
| 97 |
func TestLinearRange(t *testing.T) { |
| 98 |
|
| 99 |
|
| 100 |
values := LinearRange(1, 100) |
| 101 |
testutil.AssertLen(t, values, 100) |
| 102 |
testutil.AssertEqual(t, 1, values[0]) |
| 103 |
testutil.AssertEqual(t, 100, values[99]) |
| 104 |
} |
| 105 |
|
| 106 |
func TestLinearRangeWithStep(t *testing.T) { |
| 107 |
|
| 108 |
|
| 109 |
values := LinearRangeWithStep(0, 100, 5) |
| 110 |
testutil.AssertEqual(t, 100, values[20]) |
| 111 |
testutil.AssertLen(t, values, 21) |
| 112 |
} |
| 113 |
|
| 114 |
func TestLinearRangeReversed(t *testing.T) { |
| 115 |
|
| 116 |
|
| 117 |
values := LinearRange(10.0, 1.0) |
| 118 |
testutil.AssertEqual(t, 10, len(values)) |
| 119 |
testutil.AssertEqual(t, 10.0, values[0]) |
| 120 |
testutil.AssertEqual(t, 1.0, values[9]) |
| 121 |
} |
| 122 |
|
| 123 |
func TestLinearSequenceRegression(t *testing.T) { |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
linearProvider := NewLinearSequence().WithStart(1.0).WithEnd(100.0) |
| 128 |
testutil.AssertEqual(t, 1, linearProvider.Start()) |
| 129 |
testutil.AssertEqual(t, 100, linearProvider.End()) |
| 130 |
testutil.AssertEqual(t, 100, linearProvider.Len()) |
| 131 |
|
| 132 |
values := Seq{linearProvider}.Values() |
| 133 |
testutil.AssertLen(t, values, 100) |
| 134 |
testutil.AssertEqual(t, 1.0, values[0]) |
| 135 |
testutil.AssertEqual(t, 100, values[99]) |
| 136 |
} |