1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-12 10:40:42 +00:00
OpenDiablo2/d2common/d2math/math_test.go
danhale-git 029cb62972
Vector float64 (#565)
* Fixed nil pointer in Copy()

* Position added

Added Floor() and String() methods to Vector.

Also added Position which declares an embedded Vector2 and returns various forms of it.

* d2vector.Vector2 renamed to d2vector.BigFloat

* vector.go renamed to big_float.go

* Float64 stub and more renaming

* Vector value getters

* Separate vector types with initial methods.

* Divide and lint warnings.

* Distance and Length.

* Scale, Abs and Negate.

* CompareFloat64Fuzzy delta direction reversed.

* Refactor vector_test.go.

* Renamed Approx methods.

* Distance and Length.

* Distance and Length.

* Removed BigFloat and Vector, renamed Float64 to Vector, simplified tests.

* Angle, SignedAngle and other small functions.

* Receiver rename.

* SingedAngle and test fixed

* Rotate.

* SetLength.

* Cross.

* NinetyAnti and NinetyClock.

* Lerp and Clamp.

* Reflect and ReflectSurface.

* Cardinal convenience functions.

* Comments.

* Panic on NaN and Inf in Position.

* Lint warnings and comments.
2020-07-09 08:30:55 -04:00

114 lines
1.9 KiB
Go

package d2math
import (
"testing"
)
func TestCompareFloat64Fuzzy(t *testing.T) {
subEpsilon := Epsilon / 3
want := 0
a, b := 1+subEpsilon, 1.0
got := CompareFloat64Fuzzy(a, b)
if got != want {
t.Errorf("compare %.2f and %.2f: wanted %d: got %d", a, b, want, got)
}
want = 1
a, b = 2, 1.0
got = CompareFloat64Fuzzy(a, b)
if got != want {
t.Errorf("compare %.2f and %.2f: wanted %d: got %d", a, b, want, got)
}
want = -1
a, b = -2, 1.0
got = CompareFloat64Fuzzy(a, b)
if got != want {
t.Errorf("compare %.2f and %.2f: wanted %d: got %d", a, b, want, got)
}
}
func TestClampFloat64(t *testing.T) {
want := 0.5
a := 0.5
got := ClampFloat64(a, 0, 1)
if got != want {
t.Errorf("clamped %.2f between 0 and 1: wanted %.2f: got %.2f", a, want, got)
}
want = 0.0
a = -1.0
got = ClampFloat64(a, 0, 1)
if got != want {
t.Errorf("clamped %.2f between 0 and 1: wanted %.2f: got %.2f", a, want, got)
}
want = 1.0
a = 2.0
got = ClampFloat64(a, 0, 1)
if got != want {
t.Errorf("clamped %.2f between 0 and 1: wanted %.2f: got %.2f", a, want, got)
}
}
func TestSign(t *testing.T) {
want := 1
a := 0.5
got := Sign(a)
if got != want {
t.Errorf("sign of %.2f: wanted %df: got %d", a, want, got)
}
want = -1
a = -3
got = Sign(a)
if got != want {
t.Errorf("sign of %.2f: wanted %df: got %d", a, want, got)
}
want = 0
a = 0.0
got = Sign(a)
if got != want {
t.Errorf("sign of %.2f: wanted %df: got %d", a, want, got)
}
}
func TestLerp(t *testing.T) {
want := 3.0
x := 0.3
a, b := 0.0, 10.0
got := Lerp(a, b, x)
d := "linear interpolation between %.2f and %.2f with interpolator %.2f: wanted %.2f: got %.2f"
if got != want {
t.Errorf(d, a, b, x, want, got)
}
}
func TestUnlerp(t *testing.T) {
want := 0.3
x := 3.0
a, b := 0.0, 10.0
got := Unlerp(a, b, x)
d := "find the interpolator of %.2f between %.2f and %.2f: wanted %.2f: got %.2f"
if got != want {
t.Errorf(d, x, a, b, want, got)
}
}