forked from samber/lo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_test.go
129 lines (106 loc) · 3.31 KB
/
math_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package lo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRange(t *testing.T) {
t.Parallel()
is := assert.New(t)
result1 := Range(4)
result2 := Range(-4)
result3 := Range(0)
is.Equal(result1, []int{0, 1, 2, 3})
is.Equal(result2, []int{0, -1, -2, -3})
is.Equal(result3, []int{})
}
func TestRangeFrom(t *testing.T) {
t.Parallel()
is := assert.New(t)
result1 := RangeFrom(1, 5)
result2 := RangeFrom(-1, -5)
result3 := RangeFrom(10, 0)
result4 := RangeFrom(2.0, 3)
result5 := RangeFrom(-2.0, -3)
is.Equal(result1, []int{1, 2, 3, 4, 5})
is.Equal(result2, []int{-1, -2, -3, -4, -5})
is.Equal(result3, []int{})
is.Equal(result4, []float64{2.0, 3.0, 4.0})
is.Equal(result5, []float64{-2.0, -3.0, -4.0})
}
func TestRangeClose(t *testing.T) {
t.Parallel()
is := assert.New(t)
result1 := RangeWithSteps(0, 20, 6)
result2 := RangeWithSteps(0, 3, -5)
result3 := RangeWithSteps(1, 1, 0)
result4 := RangeWithSteps(3, 2, 1)
result5 := RangeWithSteps(1.0, 4.0, 2.0)
result6 := RangeWithSteps[float32](-1.0, -4.0, -1.0)
is.Equal([]int{0, 6, 12, 18}, result1)
is.Equal([]int{}, result2)
is.Equal([]int{}, result3)
is.Equal([]int{}, result4)
is.Equal([]float64{1.0, 3.0}, result5)
is.Equal([]float32{-1.0, -2.0, -3.0}, result6)
}
func TestClamp(t *testing.T) {
t.Parallel()
is := assert.New(t)
result1 := Clamp(0, -10, 10)
result2 := Clamp(-42, -10, 10)
result3 := Clamp(42, -10, 10)
is.Equal(result1, 0)
is.Equal(result2, -10)
is.Equal(result3, 10)
}
func TestSum(t *testing.T) {
is := assert.New(t)
result1 := Sum([]float32{2.3, 3.3, 4, 5.3})
result2 := Sum([]int32{2, 3, 4, 5})
result3 := Sum([]uint32{2, 3, 4, 5})
result4 := Sum([]uint32{})
result5 := Sum([]complex128{4_4, 2_2})
is.Equal(result1, float32(14.900001))
is.Equal(result2, int32(14))
is.Equal(result3, uint32(14))
is.Equal(result4, uint32(0))
is.Equal(result5, complex128(6_6))
}
func TestSumBy(t *testing.T) {
t.Parallel()
is := assert.New(t)
result1 := SumBy([]float32{2.3, 3.3, 4, 5.3}, func(n float32) float32 { return n })
result2 := SumBy([]int32{2, 3, 4, 5}, func(n int32) int32 { return n })
result3 := SumBy([]uint32{2, 3, 4, 5}, func(n uint32) uint32 { return n })
result4 := SumBy([]uint32{}, func(n uint32) uint32 { return n })
result5 := SumBy([]complex128{4_4, 2_2}, func(n complex128) complex128 { return n })
is.Equal(result1, float32(14.900001))
is.Equal(result2, int32(14))
is.Equal(result3, uint32(14))
is.Equal(result4, uint32(0))
is.Equal(result5, complex128(6_6))
}
func TestMean(t *testing.T) {
t.Parallel()
is := assert.New(t)
result1 := Mean([]float32{2.3, 3.3, 4, 5.3})
result2 := Mean([]int32{2, 3, 4, 5})
result3 := Mean([]uint32{2, 3, 4, 5})
result4 := Mean([]uint32{})
is.Equal(result1, float32(3.7250001))
is.Equal(result2, int32(3))
is.Equal(result3, uint32(3))
is.Equal(result4, uint32(0))
}
func TestMeanBy(t *testing.T) {
t.Parallel()
is := assert.New(t)
result1 := MeanBy([]float32{2.3, 3.3, 4, 5.3}, func(n float32) float32 { return n })
result2 := MeanBy([]int32{2, 3, 4, 5}, func(n int32) int32 { return n })
result3 := MeanBy([]uint32{2, 3, 4, 5}, func(n uint32) uint32 { return n })
result4 := MeanBy([]uint32{}, func(n uint32) uint32 { return n })
is.Equal(result1, float32(3.7250001))
is.Equal(result2, int32(3))
is.Equal(result3, uint32(3))
is.Equal(result4, uint32(0))
}