refactor and add test cases

This commit is contained in:
ALex Ogier 2017-07-31 16:29:23 +12:00
parent 5e2a944a48
commit eedeb85b14
1 changed files with 67 additions and 80 deletions

View File

@ -6,89 +6,76 @@ import (
"github.com/faiface/pixel"
)
type rectTestInput struct {
name string
rect pixel.Rect
}
type rectTestTransform struct {
name string
f func(pixel.Rect) pixel.Rect
}
func TestResizeRect(t *testing.T) {
testCases := []pixel.Rect{
pixel.R(-10, -10, 10, 10),
pixel.R(10, 10, 30, 30),
// rectangles
squareAroundOrigin := rectTestInput{"square around origin", pixel.R(-10, -10, 10, 10)}
squareAround2020 := rectTestInput{"square around 20, 20", pixel.R(10, 10, 30, 30)}
rectangleAroundOrigin := rectTestInput{"rectangle around origin", pixel.R(-20, -10, 20, 10)}
rectangleAround2020 := rectTestInput{"rectangle around 20, 20", pixel.R(0, 10, 40, 30)}
// resize transformations
resizeByHalfAroundCenter := rectTestTransform{"by half around center", func(rect pixel.Rect) pixel.Rect {
return rect.Resized(rect.Center(), rect.Size().Scaled(0.5))
}}
resizeByHalfAroundMin := rectTestTransform{"by half around Min", func(rect pixel.Rect) pixel.Rect {
return rect.Resized(rect.Min, rect.Size().Scaled(0.5))
}}
resizeByHalfAroundMax := rectTestTransform{"by half around Max", func(rect pixel.Rect) pixel.Rect {
return rect.Resized(rect.Max, rect.Size().Scaled(0.5))
}}
resizeByHalfAroundMiddleOfLeftSide := rectTestTransform{"by half around middle of left side", func(rect pixel.Rect) pixel.Rect {
return rect.Resized(pixel.V(rect.Min.X, rect.Center().Y), rect.Size().Scaled(0.5))
}}
resizeByHalfAroundOrigin := rectTestTransform{"by half around the origin", func(rect pixel.Rect) pixel.Rect {
return rect.Resized(pixel.ZV, rect.Size().Scaled(0.5))
}}
testCases := []struct {
input rectTestInput
transform rectTestTransform
answer pixel.Rect
}{
{squareAroundOrigin, resizeByHalfAroundCenter, pixel.R(-5, -5, 5, 5)},
{squareAround2020, resizeByHalfAroundCenter, pixel.R(15, 15, 25, 25)},
{rectangleAroundOrigin, resizeByHalfAroundCenter, pixel.R(-10, -5, 10, 5)},
{rectangleAround2020, resizeByHalfAroundCenter, pixel.R(10, 15, 30, 25)},
{squareAroundOrigin, resizeByHalfAroundMin, pixel.R(-10, -10, 0, 0)},
{squareAround2020, resizeByHalfAroundMin, pixel.R(10, 10, 20, 20)},
{rectangleAroundOrigin, resizeByHalfAroundMin, pixel.R(-20, -10, 0, 0)},
{rectangleAround2020, resizeByHalfAroundMin, pixel.R(0, 10, 20, 20)},
{squareAroundOrigin, resizeByHalfAroundMax, pixel.R(0, 0, 10, 10)},
{squareAround2020, resizeByHalfAroundMax, pixel.R(20, 20, 30, 30)},
{rectangleAroundOrigin, resizeByHalfAroundMax, pixel.R(0, 0, 20, 10)},
{rectangleAround2020, resizeByHalfAroundMax, pixel.R(20, 20, 40, 30)},
{squareAroundOrigin, resizeByHalfAroundMiddleOfLeftSide, pixel.R(-10, -5, 0, 5)},
{squareAround2020, resizeByHalfAroundMiddleOfLeftSide, pixel.R(10, 15, 20, 25)},
{rectangleAroundOrigin, resizeByHalfAroundMiddleOfLeftSide, pixel.R(-20, -5, 0, 5)},
{rectangleAround2020, resizeByHalfAroundMiddleOfLeftSide, pixel.R(0, 15, 20, 25)},
{squareAroundOrigin, resizeByHalfAroundOrigin, pixel.R(-5, -5, 5, 5)},
{squareAround2020, resizeByHalfAroundOrigin, pixel.R(5, 5, 15, 15)},
{rectangleAroundOrigin, resizeByHalfAroundOrigin, pixel.R(-10, -5, 10, 5)},
{rectangleAround2020, resizeByHalfAroundOrigin, pixel.R(0, 5, 20, 15)},
}
resizeAroundCenterAnswers := []pixel.Rect{
pixel.R(-5, -5, 5, 5),
pixel.R(15, 15, 25, 25),
}
for i, rect := range testCases {
answer := resizeAroundCenterAnswers[i]
// resize rectangle by 50% anchored at it's current center point
resizedRect := rect.Resized(rect.Center(), rect.Size().Scaled(0.5))
if resizedRect != answer {
t.Errorf("Rectangle resize was incorrect, got %v, want: %v.", resizedRect, answer)
}
}
resizeAroundMinAnswers := []pixel.Rect{
pixel.R(-10, -10, 0, 0),
pixel.R(10, 10, 20, 20),
}
for i, rect := range testCases {
answer := resizeAroundMinAnswers[i]
// resize rectangle by 50% anchored at it's Min coordinate
resizedRect := rect.Resized(rect.Min, rect.Size().Scaled(0.5))
if resizedRect != answer {
t.Errorf("Rectangle resize was incorrect, got %v, want: %v.", resizedRect, answer)
}
}
resizeAroundMaxAnswers := []pixel.Rect{
pixel.R(0, 0, 10, 10),
pixel.R(20, 20, 30, 30),
}
for i, rect := range testCases {
answer := resizeAroundMaxAnswers[i]
// resize rectangle by 50% anchored at it's Max coordinate
resizedRect := rect.Resized(rect.Max, rect.Size().Scaled(0.5))
if resizedRect != answer {
t.Errorf("Rectangle resize was incorrect, got %v, want: %v.", resizedRect, answer)
}
}
resizeAroundMiddleOfLeftSideAnswers := []pixel.Rect{
pixel.R(-10, -5, 0, 5),
pixel.R(10, 15, 20, 25),
}
for i, rect := range testCases {
answer := resizeAroundMiddleOfLeftSideAnswers[i]
// resize rectangle by 50% anchored at the middle of it's left side
resizedRect := rect.Resized(pixel.V(rect.Min.X, rect.Center().Y), rect.Size().Scaled(0.5))
if resizedRect != answer {
t.Errorf("Rectangle resize was incorrect, got %v, want: %v.", resizedRect, answer)
}
}
resizeAroundOriginAnswers := []pixel.Rect{
pixel.R(-5, -5, 5, 5),
pixel.R(5, 5, 15, 15),
}
for i, rect := range testCases {
answer := resizeAroundOriginAnswers[i]
// resize rectangle by 50% anchored at the origin
resizedRect := rect.Resized(pixel.ZV, rect.Size().Scaled(0.5))
if resizedRect != answer {
t.Errorf("Rectangle resize was incorrect, got %v, want: %v.", resizedRect, answer)
for _, testCase := range testCases {
testResult := testCase.transform.f(testCase.input.rect)
if testResult != testCase.answer {
t.Errorf("Resizing %s %s failed, got: %v, wanted: %v\n", testCase.input.name, testCase.transform.name, testResult, testCase.answer)
}
}
}