-
Notifications
You must be signed in to change notification settings - Fork 447
/
histogram_test.go
59 lines (56 loc) · 1.15 KB
/
histogram_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 imaging
import (
"image"
"testing"
)
func TestHistogram(t *testing.T) {
testCases := []struct {
name string
img image.Image
want [256]float64
}{
{
name: "grayscale",
img: &image.RGBA{
Rect: image.Rect(-1, -1, 1, 1),
Stride: 2 * 4,
Pix: []uint8{
0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x80, 0x80, 0x80, 0xff,
},
},
want: [256]float64{0x00: 0.25, 0x80: 0.25, 0xff: 0.5},
},
{
name: "colorful",
img: &image.RGBA{
Rect: image.Rect(-1, -1, 1, 1),
Stride: 2 * 4,
Pix: []uint8{
0x00, 0x00, 0x00, 0xff, 0x33, 0x44, 0x55, 0xff,
0x55, 0x44, 0x33, 0xff, 0x77, 0x66, 0x55, 0xff,
},
},
want: [256]float64{0x00: 0.25, 0x41: 0.25, 0x47: 0.25, 0x69: 0.25},
},
{
name: "zero",
img: &image.RGBA{},
want: [256]float64{},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := Histogram(tc.img)
if got != tc.want {
t.Fatalf("got histogram %#v want %#v", got, tc.want)
}
})
}
}
func BenchmarkHistogram(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
Histogram(testdataBranchesJPG)
}
}