forked from RomanZhu/FixedPoint-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFP_fixmathTests.cs
321 lines (268 loc) · 10.3 KB
/
FP_fixmathTests.cs
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using Deterministic;
using Deterministic.FixedPoint;
using FluentAssertions;
using NUnit.Framework;
namespace UnitTests {
public class FP_fixmathTests {
[Test]
public void CountLeadingZerosTest() {
FixedMath.CountLeadingZeroes(5435345).Should().Be(9);
FixedMath.CountLeadingZeroes(4).Should().Be(29);
}
[Test]
public void ExpTest() {
var from = -5f;
var to = 5f;
var delta = 0.001f;
for (float v = from; v < to; v += delta) {
var correctAnswer = (float)Math.Exp(v);
var parsedFp = Fixed.ParseUnsafe(v);
var answer = FixedMath.Exp(parsedFp);
answer.AsFloat.Should().BeApproximately(correctAnswer, 0.01f);
}
from = 5f;
to = 5.33f;
delta = 0.001f;
for (float v = from; v < to; v += delta) {
var correctAnswer = (float)Math.Exp(v);
var parsedFp = Fixed.ParseUnsafe(v);
var answer = FixedMath.Exp(parsedFp);
answer.AsFloat.Should().BeApproximately(correctAnswer, 1f);
}
}
[Test]
public void Atan_2Test() {
var from = -1f;
var to = 1f;
var delta = 0.001f;
for (float v = from; v < to; v += delta) {
var correctAnswer = (float)Math.Atan(v);
var parsedFp = Fixed.ParseUnsafe(v);
var answer = FixedMath.AtanApproximated(parsedFp);
answer.AsFloat.Should().BeApproximately(correctAnswer, 0.01f);
}
}
[Test]
public void AtanTest() {
var from = -1f;
var to = 1f;
var delta = 0.001f;
for (float v = from; v < to; v += delta) {
var correctAnswer = (float)Math.Atan(v);
var parsedFp = Fixed.ParseUnsafe(v);
var answer = FixedMath.Atan(parsedFp);
answer.AsFloat.Should().BeApproximately(correctAnswer, 0.001f);
}
}
[Test]
public void Atan2Test() {
var from1 = 0.1f;
var to1 = 10f;
var from2 = 0.1f;
var to2 = 10f;
var delta = 0.01f;
for (float x = from1; x < to1; x += delta) {
for (float y = from2; y < to2; y += delta) {
var correctAnswer = (float) Math.Atan2(x, y);
var parsedFp1 = Fixed.ParseUnsafe(x);
var parsedFp2 = Fixed.ParseUnsafe(y);
var answer = FixedMath.Atan2(parsedFp1, parsedFp2);
answer.AsFloat.Should().BeApproximately(correctAnswer, 0.01f);
}
}
}
[Test]
public void TanTest() {
for (long i = Fixed.minus_one.value; i <= Fixed._1.value; i++) {
Fixed val;
val.value = i;
var dValue = val.AsDouble;
var answer = FixedMath.Tan(val);
answer.AsDouble.Should().BeApproximately(Math.Tan(dValue), 0.001d);
}
}
[Test]
public void AcosTest() {
for (long i = Fixed.minus_one.value; i <= Fixed._1.value; i++) {
Fixed val;
val.value = i;
var dValue = val.AsDouble;
var answer = FixedMath.Acos(val);
answer.AsDouble.Should().BeApproximately(Math.Acos(dValue), 0.0001d);
}
}
[Test]
public void AsinTest() {
for (long i = Fixed.minus_one.value; i <= Fixed._1.value; i++) {
Fixed val;
val.value = i;
var dValue = val.AsDouble;
var answer = FixedMath.Asin(val);
answer.AsDouble.Should().BeApproximately(Math.Asin(dValue), 0.0001d);
}
}
[Test]
public void CosTest() {
for (long i = -Fixed.pi.value*2; i <= Fixed.pi.value*2; i++) {
Fixed val;
val.value = i;
var dValue = val.AsDouble;
var answer = FixedMath.Cos(val);
answer.AsDouble.Should().BeApproximately(Math.Cos(dValue), 0.001d);
}
}
[Test]
public void SinTest() {
for (long i = -Fixed.pi.value*2; i <= Fixed.pi.value*2; i++) {
Fixed val;
val.value = i;
var dValue = val.AsDouble;
var answer = FixedMath.Sin(val);
answer.AsDouble.Should().BeApproximately(Math.Sin(dValue), 0.001d);
}
}
[Test]
public void SinCosTest() {
for (long i = -Fixed.pi.value*2; i <= Fixed.pi.value*2; i++) {
Fixed val;
val.value = i;
var dValue = val.AsDouble;
FixedMath.SinCos(val, out var sin, out var cos);
sin.AsDouble.Should().BeApproximately(Math.Sin(dValue), 0.001d);
cos.AsDouble.Should().BeApproximately(Math.Cos(dValue), 0.001d);
}
}
[Test]
public void RcpTest() {
var value = Fixed._0_25;
var result = FixedMath.Rcp(value);
result.Should().Be(Fixed._4);
value = Fixed._4;
result = FixedMath.Rcp(value);
result.Should().Be(Fixed._0_25);
}
[Test]
public void SqrtTest() {
var from = 0.1f;
var to = 65000;
var delta = 0.1f;
for (float v = from; v < to; v += delta) {
var correct = (float)Math.Sqrt(v);
var parsedFp = Fixed.ParseUnsafe(v);
var answer = FixedMath.Sqrt(parsedFp);
answer.AsFloat.Should().BeApproximately(correct, 0.01f);
}
}
[Test]
public void FloorTest() {
var value = Fixed._0_25;
var result = FixedMath.Floor(value);
result.Should().Be(Fixed._0);
result = FixedMath.Floor(-value);
result.Should().Be(-Fixed._1);
}
[Test]
public void CeilTest() {
var value = Fixed._0_25;
var result = FixedMath.Ceil(value);
result.Should().Be(Fixed._1);
result = FixedMath.Ceil(-Fixed._4 - Fixed._0_25);
result.Should().Be(-Fixed._4);
}
[Test]
public void RoundToIntTest() {
var value = Fixed._5 + Fixed._0_25;
var result = FixedMath.RoundToInt(value);
result.Should().Be(5);
result = FixedMath.RoundToInt(value + Fixed._0_33);
result.Should().Be(6);
result = FixedMath.RoundToInt(value + Fixed._0_25);
result.Should().Be(6);
}
[Test]
public void MinTest() {
var value1 = Fixed._0_25;
var value2 = Fixed._0_33;
var result = FixedMath.Min(value1, value2);
result.Should().Be(value1);
result = FixedMath.Min(-value1, -value2);
result.Should().Be(-value2);
}
[Test]
public void MaxTest() {
var value1 = Fixed._0_25;
var value2 = Fixed._0_33;
var result = FixedMath.Max(value1, value2);
result.Should().Be(value2);
result = FixedMath.Max(-value1, -value2);
result.Should().Be(-value1);
}
[Test]
public void AbsTest() {
var from = -5;
var to = 5;
var delta = 0.1f;
for (float v = from; v < to; v += delta) {
var correctAnswer = Math.Abs(v);
var parsedFp = Fixed.ParseUnsafe(v);
var answer = FixedMath.Abs(parsedFp);
answer.AsFloat.Should().BeApproximately(correctAnswer, 0.1f);
}
}
[Test]
public void ClampTest() {
var from = -5;
var to = 5;
var delta = 0.1f;
for (float v = from; v < to; v += delta) {
var correctAnswer = Math.Clamp(v, -3, 3);
var parsedFp = Fixed.ParseUnsafe(v);
var answer = FixedMath.Clamp(parsedFp, -Fixed._3, Fixed._3);
answer.AsFloat.Should().BeApproximately(correctAnswer, 0.1f);
}
}
[Test]
public void LerpTest() {
var result = FixedMath.Lerp(Fixed._2, Fixed._4, Fixed._0_25);
result.Should().Be(Fixed._2 + Fixed._0_50);
result = FixedMath.Lerp(Fixed._2, Fixed._4, Fixed._0);
result.Should().Be(Fixed._2);
result = FixedMath.Lerp(Fixed._2, Fixed._4, Fixed._1);
result.Should().Be(Fixed._4);
result = FixedMath.Lerp(Fixed._2, Fixed._4, Fixed._0_50);
result.Should().Be(Fixed._3);
}
[Test]
public void MulTest() {
Fixed a = 5;
var result1 = a * Fixed._0_01;
result1.AsFloat.Should().BeApproximately(0.05f, 0.01f);
var result2 = Fixed._0_01 * a;
result2.AsFloat.Should().BeApproximately(0.05f, 0.01f);
var result3 = Fixed._0_01 * Fixed._0_01;
result3.AsFloat.Should().BeApproximately(0.001f, 0.002f);
}
[Test]
public void SignTest() {
var from = -5;
var to = 5;
var delta = 0.12f;
for (float v = from; v < to; v += delta) {
var correctAnswer = Math.Sign(v);
var parsedFp = Fixed.ParseUnsafe(v);
var answer = FixedMath.Sign(parsedFp);
answer.AsFloat.Should().BeApproximately(correctAnswer, 0.1f);
}
}
[Test]
public void IsOppositeSignTest() {
var result = FixedMath.IsOppositeSign(Fixed._0_25, -Fixed._0_20);
result.Should().Be(true);
result = FixedMath.IsOppositeSign(Fixed._0_25, Fixed._0_20);
result.Should().Be(false);
result = FixedMath.IsOppositeSign(-Fixed._0_25, -Fixed._0_20);
result.Should().Be(false);
}
}
}