Updated tests

This commit is contained in:
Ben Cragg 2019-02-18 12:16:16 +00:00
parent 680d16c17b
commit b3246f6234
1 changed files with 23 additions and 9 deletions

View File

@ -542,6 +542,19 @@ func TestCircle_Intersect(t *testing.T) {
} }
func TestRect_IntersectCircle(t *testing.T) { func TestRect_IntersectCircle(t *testing.T) {
// closeEnough will shift the decimal point by the accuracy required, truncates the results and compares them.
// Effectively this compares two floats to a given decimal point.
// Example:
// closeEnough(100.125342432, 100.125, 2) == true
// closeEnough(math.Pi, 3.14, 2) == true
// closeEnough(0.1234, 0.1245, 3) == false
closeEnough := func(got, expected float64, decimalAccuracy int) bool {
gotShifted := got * math.Pow10(decimalAccuracy)
expectedShifted := expected * math.Pow10(decimalAccuracy)
return math.Trunc(gotShifted) == math.Trunc(expectedShifted)
}
type fields struct { type fields struct {
Min pixel.Vec Min pixel.Vec
Max pixel.Vec Max pixel.Vec
@ -576,26 +589,26 @@ func TestRect_IntersectCircle(t *testing.T) {
{ {
name: "Rect.IntersectCircle(): circle overlaps bottom-left corner", name: "Rect.IntersectCircle(): circle overlaps bottom-left corner",
fields: fields{Min: pixel.ZV, Max: pixel.V(10, 10)}, fields: fields{Min: pixel.ZV, Max: pixel.V(10, 10)},
args: args{c: pixel.C(pixel.V(0, 0), 1)}, args: args{c: pixel.C(pixel.V(-0.5, -0.5), 1)},
want: pixel.V(1, 0), want: pixel.V(-0.2, -0.2),
}, },
{ {
name: "Rect.IntersectCircle(): circle overlaps top-left corner", name: "Rect.IntersectCircle(): circle overlaps top-left corner",
fields: fields{Min: pixel.ZV, Max: pixel.V(10, 10)}, fields: fields{Min: pixel.ZV, Max: pixel.V(10, 10)},
args: args{c: pixel.C(pixel.V(0, 10), 1)}, args: args{c: pixel.C(pixel.V(-0.5, 10.5), 1)},
want: pixel.V(1, 0), want: pixel.V(-0.2, 0.2),
}, },
{ {
name: "Rect.IntersectCircle(): circle overlaps bottom-right corner", name: "Rect.IntersectCircle(): circle overlaps bottom-right corner",
fields: fields{Min: pixel.ZV, Max: pixel.V(10, 10)}, fields: fields{Min: pixel.ZV, Max: pixel.V(10, 10)},
args: args{c: pixel.C(pixel.V(10, 0), 1)}, args: args{c: pixel.C(pixel.V(10.5, -0.5), 1)},
want: pixel.V(-1, 0), want: pixel.V(0.2, -0.2),
}, },
{ {
name: "Rect.IntersectCircle(): circle overlaps top-right corner", name: "Rect.IntersectCircle(): circle overlaps top-right corner",
fields: fields{Min: pixel.ZV, Max: pixel.V(10, 10)}, fields: fields{Min: pixel.ZV, Max: pixel.V(10, 10)},
args: args{c: pixel.C(pixel.V(10, 10), 1)}, args: args{c: pixel.C(pixel.V(10.5, 10.5), 1)},
want: pixel.V(-1, 0), want: pixel.V(0.2, 0.2),
}, },
{ {
name: "Rect.IntersectCircle(): circle overlaps two corners", name: "Rect.IntersectCircle(): circle overlaps two corners",
@ -670,7 +683,8 @@ func TestRect_IntersectCircle(t *testing.T) {
Min: tt.fields.Min, Min: tt.fields.Min,
Max: tt.fields.Max, Max: tt.fields.Max,
} }
if got := r.IntersectCircle(tt.args.c); got != tt.want { got := r.IntersectCircle(tt.args.c)
if !closeEnough(got.X, tt.want.X, 2) || !closeEnough(got.Y, tt.want.Y, 2) {
t.Errorf("Rect.IntersectCircle() = %v, want %v", got, tt.want) t.Errorf("Rect.IntersectCircle() = %v, want %v", got, tt.want)
} }
}) })