1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2025-02-08 17:46:24 -05:00
OpenDiablo2/d2common/d2math/math_test.go
danhale-git 894c60f77a
Map entity rework - vectors in mapEntity (#579)
* Fixed NaN on normalize 0 vector.

* Tentative implementation in getStepLength.

* mapEntity.TileX/Y removed.

* Fixed Position and Target not being initialised in createMapEntity.

* mapEntity.Position is no longer embedded.

* mapEntity.LocationX/Y no longer used outside map_entity.go.

* mapEntity.subCellX/Y removed.

* mapEntity.LocationX/Y removed.

* mapEntity.OffsetX/Y and TargetX/Y removed.

* Direction method added to Vector, returns 0-64 direction.

* Moved Vector.Direction() to Position.DirectionTo and refactored.

* Renamed RenderOffset from SubCell and tested IsZero.

* d2math.WrapInt added for use with directions.

* Tidied up position and tests.

* Refactored d2common.AdjustWithRemainder into d2mapEntity.

* Tidying up d2mapEntity.

* Final cleanup.

* Lint warnings.

* Spelling correction.
2020-07-13 09:06:50 -04:00

168 lines
2.8 KiB
Go

package d2math
import (
"testing"
)
func TestEqualsApprox(t *testing.T) {
subEpsilon := Epsilon / 3
a, b := 1+subEpsilon, 1.0
got := EqualsApprox(a, b)
if !got {
t.Errorf("compare %.2f and %.2f: wanted %t: got %t", a, b, true, got)
}
a, b = 1+Epsilon, 1.0
got = EqualsApprox(a, b)
if !got {
t.Errorf("compare %.2f and %.2f: wanted %t: got %t", a, b, false, got)
}
}
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)
}
}
func TestWrapInt(t *testing.T) {
want := 50
a, b := 1050, 100
got := WrapInt(a, b)
d := "wrap %d between 0 and %d: want %d: got %d"
if got != want {
t.Errorf(d, a, b, want, got)
}
want = 270
a, b = -1170, 360
got = WrapInt(a, b)
if got != want {
t.Errorf(d, a, b, want, got)
}
want = 270
a, b = 270, 360
got = WrapInt(a, b)
if got != want {
t.Errorf(d, a, b, want, got)
}
want = 90
a, b = -270, 360
got = WrapInt(a, b)
if got != want {
t.Errorf(d, a, b, want, got)
}
}