forked from wcharczuk/go-chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_test.go
162 lines (136 loc) · 3.62 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package chart
import (
"testing"
"time"
"github.com/blendlabs/go-assert"
)
func TestMinAndMax(t *testing.T) {
assert := assert.New(t)
values := []float64{1.0, 2.0, 3.0, 4.0}
min, max := Math.MinAndMax(values...)
assert.Equal(1.0, min)
assert.Equal(4.0, max)
}
func TestMinAndMaxReversed(t *testing.T) {
assert := assert.New(t)
values := []float64{4.0, 2.0, 3.0, 1.0}
min, max := Math.MinAndMax(values...)
assert.Equal(1.0, min)
assert.Equal(4.0, max)
}
func TestMinAndMaxEmpty(t *testing.T) {
assert := assert.New(t)
values := []float64{}
min, max := Math.MinAndMax(values...)
assert.Equal(0.0, min)
assert.Equal(0.0, max)
}
func TestMinAndMaxOfTime(t *testing.T) {
assert := assert.New(t)
values := []time.Time{
time.Now().AddDate(0, 0, -1),
time.Now().AddDate(0, 0, -2),
time.Now().AddDate(0, 0, -3),
time.Now().AddDate(0, 0, -4),
}
min, max := Math.MinAndMaxOfTime(values...)
assert.Equal(values[3], min)
assert.Equal(values[0], max)
}
func TestMinAndMaxOfTimeReversed(t *testing.T) {
assert := assert.New(t)
values := []time.Time{
time.Now().AddDate(0, 0, -4),
time.Now().AddDate(0, 0, -2),
time.Now().AddDate(0, 0, -3),
time.Now().AddDate(0, 0, -1),
}
min, max := Math.MinAndMaxOfTime(values...)
assert.Equal(values[0], min)
assert.Equal(values[3], max)
}
func TestMinAndMaxOfTimeEmpty(t *testing.T) {
assert := assert.New(t)
values := []time.Time{}
min, max := Math.MinAndMaxOfTime(values...)
assert.Equal(time.Time{}, min)
assert.Equal(time.Time{}, max)
}
func TestGetRoundToForDelta(t *testing.T) {
assert := assert.New(t)
assert.Equal(100.0, Math.GetRoundToForDelta(1001.00))
assert.Equal(10.0, Math.GetRoundToForDelta(101.00))
assert.Equal(1.0, Math.GetRoundToForDelta(11.00))
}
func TestRoundUp(t *testing.T) {
assert := assert.New(t)
assert.Equal(0.5, Math.RoundUp(0.49, 0.1))
assert.Equal(1.0, Math.RoundUp(0.51, 1.0))
assert.Equal(0.4999, Math.RoundUp(0.49988, 0.0001))
}
func TestRoundDown(t *testing.T) {
assert := assert.New(t)
assert.Equal(0.5, Math.RoundDown(0.51, 0.1))
assert.Equal(1.0, Math.RoundDown(1.01, 1.0))
assert.Equal(0.5001, Math.RoundDown(0.50011, 0.0001))
}
func TestPercentDifference(t *testing.T) {
assert := assert.New(t)
assert.Equal(0.5, Math.PercentDifference(1.0, 1.5))
assert.Equal(-0.5, Math.PercentDifference(2.0, 1.0))
}
func TestNormalize(t *testing.T) {
assert := assert.New(t)
values := []float64{10, 9, 8, 7, 6}
normalized := Math.Normalize(values...)
assert.Len(normalized, 5)
assert.Equal(0.25, normalized[0])
assert.Equal(0.1499, normalized[4])
}
var (
_degreesToRadians = map[float64]float64{
0: 0, // !_2pi b/c no irrational nums in floats.
45: _pi4,
90: _pi2,
135: _3pi4,
180: _pi,
225: _5pi4,
270: _3pi2,
315: _7pi4,
}
_compassToRadians = map[float64]float64{
0: _pi2,
45: _pi4,
90: 0, // !_2pi b/c no irrational nums in floats.
135: _7pi4,
180: _3pi2,
225: _5pi4,
270: _pi,
315: _3pi4,
}
)
func TestDegreesToRadians(t *testing.T) {
assert := assert.New(t)
for d, r := range _degreesToRadians {
assert.Equal(r, Math.DegreesToRadians(d))
}
}
func TestPercentToRadians(t *testing.T) {
assert := assert.New(t)
for d, r := range _degreesToRadians {
assert.Equal(r, Math.PercentToRadians(d/360.0))
}
}
func TestRadiansToDegrees(t *testing.T) {
assert := assert.New(t)
for d, r := range _degreesToRadians {
assert.Equal(d, Math.RadiansToDegrees(r))
}
}
func TestRadianAdd(t *testing.T) {
assert := assert.New(t)
assert.Equal(_pi, Math.RadianAdd(_pi2, _pi2))
assert.Equal(_3pi2, Math.RadianAdd(_pi2, _pi))
assert.Equal(_pi, Math.RadianAdd(_pi, _2pi))
assert.Equal(_pi, Math.RadianAdd(_pi, -_2pi))
}