2020-07-04 19:25:53 -04:00
|
|
|
package d2vector
|
|
|
|
|
|
|
|
import (
|
2020-07-11 18:11:26 -04:00
|
|
|
"math"
|
|
|
|
"math/rand"
|
2020-07-04 19:25:53 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2020-07-13 09:06:50 -04:00
|
|
|
func TestNewPosition(t *testing.T) {
|
2020-10-26 02:38:15 -04:00
|
|
|
const maxXY = 1000
|
|
|
|
|
|
|
|
x, y := rand.Intn(maxXY), rand.Intn(maxXY) // nolint:gosec // just a test
|
2020-07-11 18:11:26 -04:00
|
|
|
locX, locY := float64(x), float64(y)
|
2020-07-13 09:06:50 -04:00
|
|
|
pos := NewPosition(locX, locY)
|
2020-07-11 18:11:26 -04:00
|
|
|
|
|
|
|
// old coordinate values Position equivalent
|
|
|
|
locationX := locX // .SubWord().X()
|
|
|
|
locationY := locY // .SubWord().Y()
|
|
|
|
tileX := float64(x / 5) // .Tile().X()
|
|
|
|
tileY := float64(y / 5) // .Tile().Y()
|
2020-07-13 09:06:50 -04:00
|
|
|
subcellX := 1 + math.Mod(locX, 5) // .RenderOffset().X()
|
|
|
|
subcellY := 1 + math.Mod(locY, 5) // .RenderOffset().Y()
|
2020-07-11 18:11:26 -04:00
|
|
|
|
|
|
|
want := NewVector(tileX, tileY)
|
|
|
|
got := pos.Tile()
|
|
|
|
|
|
|
|
if !got.Equals(want) {
|
|
|
|
t.Errorf("world position should match old value: got %s: want %s", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
want = NewVector(subcellX, subcellY)
|
2020-07-13 09:06:50 -04:00
|
|
|
got = pos.RenderOffset()
|
2020-07-11 18:11:26 -04:00
|
|
|
|
|
|
|
if !got.Equals(want) {
|
2020-07-13 09:06:50 -04:00
|
|
|
t.Errorf("render offset position should match old value: got %s: want %s", got, want)
|
2020-07-11 18:11:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
want = NewVector(locationX, locationY)
|
2020-07-13 09:06:50 -04:00
|
|
|
got = &pos.Vector
|
2020-07-11 18:11:26 -04:00
|
|
|
|
|
|
|
if !got.Equals(want) {
|
|
|
|
t.Errorf("sub tile position should match old value: got %s: want %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validate(description string, t *testing.T, original, got, want, unchanged Vector) {
|
2020-07-25 09:36:54 -04:00
|
|
|
if !got.EqualsApprox(&want) {
|
2020-07-11 18:11:26 -04:00
|
|
|
t.Errorf("%s: want %s: got %s", description, want, got)
|
2020-07-04 19:25:53 -04:00
|
|
|
}
|
|
|
|
|
2020-07-25 09:36:54 -04:00
|
|
|
if !original.EqualsApprox(&unchanged) {
|
2020-07-04 19:25:53 -04:00
|
|
|
t.Errorf("Position value %s was incorrectly changed to %s when calling this method", unchanged, original)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 09:06:50 -04:00
|
|
|
func TestPosition_World(t *testing.T) {
|
|
|
|
p := NewPosition(5, 10)
|
|
|
|
unchanged := p.Clone()
|
|
|
|
got := p.World()
|
|
|
|
want := NewVector(1, 2)
|
2020-07-04 19:25:53 -04:00
|
|
|
|
2020-07-25 09:36:54 -04:00
|
|
|
validate("world position", t, p.Vector, *got, *want, *unchanged)
|
2020-07-04 19:25:53 -04:00
|
|
|
}
|
|
|
|
|
2020-07-13 09:06:50 -04:00
|
|
|
func TestPosition_Tile(t *testing.T) {
|
|
|
|
p := NewPosition(23, 24)
|
|
|
|
unchanged := p.Clone()
|
|
|
|
got := p.Tile()
|
|
|
|
want := NewVector(4, 4)
|
2020-07-04 19:25:53 -04:00
|
|
|
|
2020-07-25 09:36:54 -04:00
|
|
|
validate("tile position", t, p.Vector, *got, *want, *unchanged)
|
2020-07-04 19:25:53 -04:00
|
|
|
}
|
|
|
|
|
2020-07-13 09:06:50 -04:00
|
|
|
func TestPosition_RenderOffset(t *testing.T) {
|
|
|
|
p := NewPosition(12.1, 14.2)
|
|
|
|
unchanged := p.Clone()
|
|
|
|
got := p.RenderOffset()
|
|
|
|
want := NewVector(3.1, 5.2)
|
2020-07-04 19:25:53 -04:00
|
|
|
|
2020-07-25 09:36:54 -04:00
|
|
|
validate("offset from sub tile", t, p.Vector, *got, *want, *unchanged)
|
2020-07-04 19:25:53 -04:00
|
|
|
}
|