-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_test.go
171 lines (154 loc) · 4.2 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
163
164
165
166
167
168
169
170
171
// (c) 2022 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)
package pi_test
import (
"fmt"
"math"
"testing"
"github.com/stretchr/testify/assert"
"github.com/elgopher/pi"
)
const delta = 0.000000000000001
func TestSin(t *testing.T) {
tests := map[float64]float64{
-2: 0,
-1.5: 0,
-1: 0,
-0.9: -0.5877852522924732,
-0.8: -0.9510565162951536,
-0.7: -0.9510565162951535,
-0.6: -0.587785252292473,
-0.5: 0,
-0.4: 0.5877852522924732,
-0.3: 0.9510565162951536,
-0.2: 0.9510565162951535,
-0.1: 0.5877852522924731,
0: 0,
0.1: -0.5877852522924731,
0.2: -0.9510565162951535,
0.3: -0.9510565162951536,
0.4: -0.5877852522924732,
0.5: 0,
0.6: 0.587785252292473,
0.7: 0.9510565162951535,
0.8: 0.9510565162951536,
0.9: 0.5877852522924732,
1: 0,
1.5: 0,
2: 0,
}
for angle, expected := range tests {
name := fmt.Sprintf("%f", angle)
t.Run(name, func(t *testing.T) {
actual := pi.Sin(angle)
assert.InDelta(t, expected, actual, delta)
})
}
}
func TestCos(t *testing.T) {
tests := map[float64]float64{
-2: 1,
-1.5: -1,
-1: 1,
-0.9: 0.8090169943749473,
-0.8: 0.30901699437494723,
-0.7: -0.30901699437494756,
-0.6: -0.8090169943749473,
-0.5: -1,
-0.4: -0.8090169943749475,
-0.3: -0.30901699437494734,
-0.2: 0.30901699437494734,
-0.1: 0.8090169943749473,
0: 1,
0.1: 0.8090169943749473,
0.2: 0.30901699437494723,
0.3: -0.30901699437494723,
0.4: -0.8090169943749473,
0.5: -1,
0.6: -0.8090169943749473,
0.7: -0.30901699437494723,
0.8: 0.30901699437494723,
0.9: 0.8090169943749473,
1: 1,
1.5: -1,
2: 1,
}
for angle, expected := range tests {
name := fmt.Sprintf("%f", angle)
t.Run(name, func(t *testing.T) {
actual := pi.Cos(angle)
assert.InDelta(t, expected, actual, delta)
})
}
}
func TestAtan2(t *testing.T) {
type params struct{ dx, dy float64 }
tests := map[params]float64{
{0, 0}: 0.75, // TODO In Pico-8 this is 0.25
{0.001, 0.001}: 0.875,
{0, 1}: 0.75,
{1, 0}: 0,
{1, 1}: 0.875,
{-1, -1}: 0.375,
{-1, 1}: 0.625,
{1, -1}: 0.125,
{1, 2}: 0.8237918088252166,
{1, -0.3}: 0.04638678953887121,
}
for p, expected := range tests {
name := fmt.Sprintf("%+v", p)
t.Run(name, func(t *testing.T) {
actual := pi.Atan2(p.dx, p.dy)
assert.InDelta(t, expected, actual, delta)
})
}
}
func TestMinInt(t *testing.T) {
assert.Equal(t, 0, pi.MinInt(0, 0))
assert.Equal(t, 1, pi.MinInt(1, 2))
assert.Equal(t, 1, pi.MinInt(1, 1))
assert.Equal(t, 1, pi.MinInt(2, 1))
assert.Equal(t, -2, pi.MinInt(-1, -2))
assert.Equal(t, -2, pi.MinInt(-2, 2))
}
func TestMaxInt(t *testing.T) {
assert.Equal(t, 0, pi.MaxInt(0, 0))
assert.Equal(t, 2, pi.MaxInt(2, 1))
assert.Equal(t, 1, pi.MaxInt(1, 1))
assert.Equal(t, 2, pi.MaxInt(1, 2))
assert.Equal(t, -1, pi.MaxInt(-1, -2))
assert.Equal(t, 2, pi.MaxInt(-2, 2))
}
func TestMidInt(t *testing.T) {
assert.Equal(t, 0, pi.MidInt(0, 0, 0))
assert.Equal(t, 1, pi.MidInt(0, 1, 2))
assert.Equal(t, 1, pi.MidInt(2, 1, 0))
assert.Equal(t, 1, pi.MidInt(1, 0, 2))
assert.Equal(t, 1, pi.MidInt(1, 2, 0))
assert.Equal(t, 1, pi.MidInt(2, 0, 1))
assert.Equal(t, 1, pi.MidInt(0, 2, 1))
assert.Equal(t, -1, pi.MidInt(0, -1, -2))
}
func TestMid(t *testing.T) {
assert.Equal(t, 0.0, pi.Mid(0, 0, 0))
assert.Equal(t, 1.0, pi.Mid(0, 1, 2))
assert.Equal(t, 1.0, pi.Mid(2, 1, 0))
assert.Equal(t, 1.0, pi.Mid(1, 0, 2))
assert.Equal(t, 1.0, pi.Mid(1, 2, 0))
assert.Equal(t, 1.0, pi.Mid(2, 0, 1))
assert.Equal(t, 1.0, pi.Mid(0, 2, 1))
assert.Equal(t, -1.0, pi.Mid(0, -1, -2))
assertNaN(t, pi.Mid(math.NaN(), math.NaN(), math.NaN()))
assertInf(t, pi.Mid(math.Inf(1), math.Inf(1), math.Inf(1)), 1)
assert.Equal(t, 1.0, pi.Mid(1.0, math.NaN(), 2.0)) // NaNs always go to the beginning
assertNaN(t, pi.Mid(math.NaN(), math.NaN(), 1.0))
assertNaN(t, pi.Mid(1.0, math.NaN(), math.NaN()))
assert.Equal(t, 1.0, pi.Mid(1.0, 2.0, math.NaN()))
assert.Equal(t, 1.0, pi.Mid(math.Inf(1), math.Inf(-1), 1.0))
}
func assertNaN(t *testing.T, v float64) {
assert.True(t, math.IsNaN(v))
}
func assertInf(t *testing.T, v float64, sign int) {
assert.True(t, math.IsInf(v, sign))
}