-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopher_test.go
59 lines (46 loc) · 1.14 KB
/
gopher_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
package main
import (
"image"
"image/color"
"math"
"math/rand"
"testing"
"time"
)
type XY struct {
x, y int
}
type mockImage struct {
image.Image
dim int
at func(x, y int) color.Color
}
func (m mockImage) Bounds() image.Rectangle {
return image.Rect(0, 0, m.dim, m.dim)
}
func (m mockImage) At(x, y int) color.Color {
return m.at(x, y)
}
func TestColorGopherFunc(t *testing.T) {
rand.Seed(time.Now().UnixNano())
dim := rand.Intn(100) + 1
newDim := rand.Intn(dim) + 1
recX, recY := math.MinInt64, math.MinInt64
res := color.Gray{Y: uint8(rand.Intn(255))}
m := mockImage{dim: dim, at: func(x, y int) color.Color {
recX, recY = x, y
return res
}}
x, y := rand.Intn(newDim), rand.Intn(newDim)
xOffset, yOffset := 1, 1
c := ColorGopherFunc(m).Color(x, xOffset, y, yOffset, newDim)
if c != res {
t.Fatalf("ColorGopherFunc returned Unexpected color. Exepected: %v. Got: %v", res, c)
}
expectedX := (x - xOffset) * dim / newDim
expectedY := (y - yOffset) * dim / newDim
if recX != expectedX || recY != expectedY {
t.Fatalf("Unexpected request to At. Expected At(%d,%d) but got At(%d,%d)",
expectedX, expectedY, recX, recY)
}
}