-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathimage_test.go
70 lines (55 loc) · 1.42 KB
/
image_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package gocarina
import (
"image"
"image/color"
"testing"
)
// just validating my understanding of image.Rectangle API
func TestGeometry(t *testing.T) {
r := image.Rect(0, 0, 16, 16)
if r.Dx() != 16 {
t.Fatalf("expected width to be 16")
}
if r.Dy() != 16 {
t.Fatalf("expected height to be 16")
}
if r.Min.X != 0 {
t.Fatalf("expected starting X coord to be 0")
}
// the max is not intended to be included in the range
if r.Max.X != 16 {
t.Fatalf("expected ending X coord to be 16")
}
if r.Min.Y != 0 {
t.Fatalf("expected starting Y coord to be 0")
}
// the max is not intended to be included in the range
if r.Max.Y != 16 {
t.Fatalf("expected ending Y coord to be 16")
}
}
func TestBoundingBox(t *testing.T) {
r := image.Rect(0, 0, 16, 16)
img := image.NewRGBA(r)
// top left
img.Set(3, 3, color.Black)
// bottom right
img.Set(12, 12, color.Black)
bbox := BoundingBox(img, 0)
assertWidth(bbox, 10, t)
assertHeight(bbox, 10, t)
// now test with border
bbox = BoundingBox(img, 1)
assertWidth(bbox, 12, t)
assertHeight(bbox, 12, t)
}
func assertWidth(rect image.Rectangle, w int, t *testing.T) {
if rect.Bounds().Dx() != w {
t.Fatalf("expected rect.Bounds().Dx() to be %d, was: %d", w, rect.Bounds().Dx())
}
}
func assertHeight(rect image.Rectangle, h int, t *testing.T) {
if rect.Bounds().Dy() != h {
t.Fatalf("expected rect.Bounds().Dy() to be %d, was: %d", h, rect.Bounds().Dy())
}
}