Add some unit tests to a few geometry functions.

This commit is contained in:
Alex R. Delp 2021-01-29 08:41:37 -08:00
parent 75e7b4120b
commit 842ae8d470
2 changed files with 44 additions and 1 deletions

View File

@ -23,6 +23,49 @@ func closeEnough(got, expected float64, decimalAccuracy int) bool {
return math.Trunc(gotShifted) == math.Trunc(expectedShifted)
}
type clampTest struct {
number float64
min float64
max float64
expected float64
}
func TestClamp(t *testing.T) {
tests := []clampTest{
{number: 1, min: 0, max: 5, expected: 1},
{number: 2, min: 0, max: 5, expected: 2},
{number: 8, min: 0, max: 5, expected: 5},
{number: -5, min: 0, max: 5, expected: 0},
{number: -5, min: -4, max: 5, expected: -4},
}
for _, tc := range tests {
result := pixel.Clamp(tc.number, tc.min, tc.max)
if result != tc.expected {
t.Error(fmt.Sprintf("Clamping %v with min %v and max %v should have given %v, but gave %v", tc.number, tc.min, tc.max, tc.expected, result))
}
}
}
type floorTest struct {
input pixel.Vec
expected pixel.Vec
}
func TestFloor(t *testing.T) {
tests := []floorTest{
{input: pixel.V(4.50, 6.70), expected: pixel.V(4, 6)},
{input: pixel.V(9.0, 6.70), expected: pixel.V(9, 6)},
}
for _, tc := range tests {
result := tc.input.Floor()
if result != tc.expected {
t.Error(fmt.Sprintf("Expected %v but got %v", tc.expected, result))
}
}
}
func TestRect_Edges(t *testing.T) {
type fields struct {
Min pixel.Vec

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.12
require (
github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380
github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 // indirect
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72
github.com/go-gl/mathgl v0.0.0-20190416160123-c4601bc793c7
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0