Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsy committed Nov 15, 2017
1 parent 6661246 commit 402cb74
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/core/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ func NewPerspective(fov, aspect, near, far Float) *Transform {
return NewScale(s, s, 1.0).Multiply(NewTransform(pers))
}

// At returns an element of Transform.
func (t *Transform) At(i, j int) Float {
if i < 0 || j < 0 || i >= 4 || j >= 4 {
panic("Index out of bounds!")
}
return t.mat[i][j]
}

// Multiply computes the multiplications of Transforms.
func (t1 *Transform) Multiply(t2 *Transform) *Transform {
ret := new(Transform)
Expand Down
56 changes: 56 additions & 0 deletions src/core/transform_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package core

import (
"math"
"testing"
)

func TestTransformAt(t *testing.T) {
m := NewTransform([4][4]Float{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16},
})

for i := 0; i < 4; i++ {
for j := 0; j < 4; j++ {
expected := i*4 + j + 1
if m.At(i, j) != Float(expected) {
t.Errorf("m.At(%d, %d) != %f, detected %d", i, j, expected, m.At(i, j))
}
}
}

defer func() {
if p := recover(); p != nil {
}
}()

m.At(-1, -1)
t.Error("Out of bounds subscription did not panic.")
}

func TestTransformInverted(t *testing.T) {
m := NewTransform([4][4]Float{
{1, 2, 3, 4},
{2, 2, 3, 4},
{3, 3, 3, 4},
{4, 4, 4, 4},
})

mi := m.Multiply(m.Inverted())
for i := 0; i < 4; i++ {
for j := 0; j < 4; j++ {
if i == j {
if math.Abs(mi.At(i, j)-1.0) >= Eps {
t.Errorf("Matrix inversion failed!")
}
} else {
if math.Abs(mi.At(i, j)) >= Eps {
t.Errorf("Matrix inversion failed!")
}
}
}
}
}

0 comments on commit 402cb74

Please sign in to comment.