|
- package vector_test
-
- import (
- "math"
- "testing"
-
- "git.wtrh.nl/patterns/gopatterns/pkg/vector"
- "github.com/stretchr/testify/assert"
- )
-
- func TestVector_Abs(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{
- X: -1,
- Y: 1,
- }.Abs()
- exp := vector.Vector{
- X: 1,
- Y: 1,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Abs2(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{
- X: 0,
- Y: -2,
- }.Abs()
- exp := vector.Vector{
- X: 0,
- Y: 2,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Subtract(t *testing.T) {
- t.Parallel()
-
- v1 := vector.Vector{
- X: 12.5,
- Y: 4,
- }
- v2 := vector.Vector{
- X: 7,
- Y: -3,
- }
- act := v1.Subtract(v2)
- exp := vector.Vector{
- X: 5.5,
- Y: 7,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Add(t *testing.T) {
- t.Parallel()
-
- v1 := vector.Vector{
- X: 12.5,
- Y: 4,
- }
- v2 := vector.Vector{
- X: 7,
- Y: -3,
- }
- act := v1.Add(v2)
- exp := vector.Vector{
- X: 19.5,
- Y: 1,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Multiply(t *testing.T) {
- t.Parallel()
-
- v1 := vector.Vector{
- X: 12.5,
- Y: 4,
- }
- act := v1.Multiply(7)
- exp := vector.Vector{
- X: 87.5,
- Y: 28,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Divide(t *testing.T) {
- t.Parallel()
-
- v1 := vector.Vector{
- X: 15,
- Y: 3,
- }
- act := v1.Divide(3)
- exp := vector.Vector{
- X: 5,
- Y: 1,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Divide2(t *testing.T) {
- t.Parallel()
-
- v1 := vector.Vector{
- X: -15,
- Y: 3,
- }
- act := v1.Divide(0)
- exp := vector.Vector{
- X: math.Inf(-1),
- Y: math.Inf(1),
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Round(t *testing.T) {
- t.Parallel()
-
- v1 := vector.Vector{
- X: 12.5,
- Y: 4.3,
- }
- act := v1.Round()
- exp := vector.Vector{
- X: 13,
- Y: 4,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Floor(t *testing.T) {
- t.Parallel()
-
- v1 := vector.Vector{
- X: 12.9,
- Y: 4.3,
- }
- act := v1.Floor()
- exp := vector.Vector{
- X: 12,
- Y: 4,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Ciel(t *testing.T) {
- t.Parallel()
-
- v1 := vector.Vector{
- X: 12.9,
- Y: 4.3,
- }
- act := v1.Ceil()
- exp := vector.Vector{
- X: 13,
- Y: 5,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Scale(t *testing.T) {
- t.Parallel()
-
- v1 := vector.Vector{
- X: 12.5,
- Y: 4,
- }
- v2 := vector.Vector{
- X: 7,
- Y: -3,
- }
- act := v1.Scale(v2)
- exp := vector.Vector{
- X: 87.5,
- Y: -12,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_ScaleDown(t *testing.T) {
- t.Parallel()
-
- v1 := vector.Vector{
- X: 12.5,
- Y: 4,
- }
- v2 := vector.Vector{
- X: 5,
- Y: -2,
- }
- act := v1.ScaleDown(v2)
- exp := vector.Vector{
- X: 2.5,
- Y: -2,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Max(t *testing.T) {
- t.Parallel()
-
- v1 := vector.Vector{
- X: 12.5,
- Y: -4,
- }
- v2 := vector.Vector{
- X: 5,
- Y: 2,
- }
- act := v1.Max(v2)
- exp := vector.Vector{
- X: 12.5,
- Y: 2,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Min(t *testing.T) {
- t.Parallel()
-
- v1 := vector.Vector{
- X: 12.5,
- Y: -4,
- }
- v2 := vector.Vector{
- X: 5,
- Y: 2,
- }
- act := v1.Min(v2)
- exp := vector.Vector{
- X: 5,
- Y: -4,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_String(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{
- X: 12.5,
- Y: 4,
- }.String()
- exp := "12.5,4"
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Magnitude(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{
- X: 3,
- Y: 4,
- }.Magnitude()
- exp := 5.0
-
- assert.InDelta(t, exp, act, 1e-15)
- }
-
- func TestVector_Unit_Panic(t *testing.T) {
- t.Parallel()
-
- emptyVector := vector.Vector{}
-
- assert.Panics(t, func() { emptyVector.Unit() })
- }
-
- func TestVector_Unit(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{
- X: 3,
- Y: 4,
- }.Unit()
- exp := vector.Vector{
- X: 0.6,
- Y: 0.8,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Rotate(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{
- X: 2,
- Y: 0,
- }.Rotate(math.Pi / 2)
- exp := vector.Vector{
- X: 0,
- Y: 2,
- }
-
- assert.InDelta(t, exp.X, act.X, math.Pow10(-15))
- assert.InDelta(t, exp.Y, act.Y, math.Pow10(-15))
- }
-
- func TestVector_Rotate2(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{
- X: 2,
- Y: 0,
- }.Rotate(math.Pi)
- exp := vector.Vector{
- X: -2,
- Y: 0,
- }
-
- assert.InDelta(t, exp.X, act.X, math.Pow10(-15))
- assert.InDelta(t, exp.Y, act.Y, math.Pow10(-15))
- }
-
- func TestVector_Rotate3(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{
- X: 0,
- Y: 2,
- }.Rotate(math.Pi / 2)
- exp := vector.Vector{
- X: -2,
- Y: 0,
- }
-
- assert.InDelta(t, exp.X, act.X, math.Pow10(-15))
- assert.InDelta(t, exp.Y, act.Y, math.Pow10(-15))
- }
-
- func TestVector_Rotate4(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{
- X: 0,
- Y: 2,
- }.Rotate(math.Pi)
- exp := vector.Vector{
- X: 0,
- Y: -2,
- }
-
- assert.InDelta(t, exp.X, act.X, math.Pow10(-15))
- assert.InDelta(t, exp.Y, act.Y, math.Pow10(-15))
- }
-
- func TestVector_Chord(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{
- X: 2,
- Y: 0,
- }.Span(math.Pi / 2)
- exp := vector.Vector{
- X: 2,
- Y: -2,
- }
-
- assert.InDelta(t, exp.X, act.X, math.Pow10(-15))
- assert.InDelta(t, exp.Y, act.Y, math.Pow10(-15))
- }
-
- func TestVector_Distance(t *testing.T) {
- t.Parallel()
-
- v1 := vector.Vector{
- X: 3,
- Y: 0,
- }
- v2 := vector.Vector{
- X: 0,
- Y: 4,
- }
-
- act := v1.Distance(v2)
- exp := 5.0
- assert.InDelta(t, exp, act, 1e-15)
- }
-
- func TestVector_Angle(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{
- X: 3,
- Y: 3,
- }.Angle()
- exp := math.Pi / 4
-
- assert.InDelta(t, exp, act, 1e-15)
- }
-
- func TestVector_Angle2(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{
- X: -3,
- Y: 3,
- }.Angle()
- exp := 3 * math.Pi / 4
-
- assert.InDelta(t, exp, act, 1e-15)
- }
-
- func TestVector_AngleBetween(t *testing.T) {
- t.Parallel()
-
- v1 := vector.Vector{
- X: -1,
- Y: 3,
- }
- v2 := vector.Vector{
- X: 2,
- Y: 6,
- }
- act := v1.AngleBetween(v2)
- exp := math.Pi / 4
- assert.InDelta(t, exp, act, 1e-15)
- }
-
- func TestVector_Above(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{}.Above(1)
- exp := vector.Vector{
- X: 0,
- Y: 1,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Below(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{}.Below(1)
- exp := vector.Vector{
- X: 0,
- Y: -1,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Right(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{}.Right(1)
- exp := vector.Vector{
- X: 1,
- Y: 0,
- }
-
- assert.Equal(t, exp, act)
- }
-
- func TestVector_Left(t *testing.T) {
- t.Parallel()
-
- act := vector.Vector{}.Left(1)
- exp := vector.Vector{
- X: -1,
- Y: 0,
- }
-
- assert.Equal(t, exp, act)
- }
|