-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhist_test.go
128 lines (121 loc) · 3.08 KB
/
hist_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package xirho
import (
"fmt"
"image"
"image/color"
"sync"
"testing"
"unsafe"
"golang.org/x/image/draw"
)
func TestHistMem(t *testing.T) {
z := []int{0, 1, 10}
for _, i := range z {
for _, j := range z {
w, h := 1<<i, 1<<j
t.Run(fmt.Sprintf("%dx%d", w, h), func(t *testing.T) {
est := uintptr(HistMem(w, h))
hist := NewHist(w, h)
act := unsafe.Sizeof(*hist) + uintptr(len(hist.counts))*unsafe.Sizeof(hist.counts[0])
if est != act {
t.Error("wrong histogram size; estimated", est, "but actual size is", act)
}
})
}
}
}
func TestHistReset(t *testing.T) {
h := NewHist(1, 1)
h.counts[0] = histBin{r: 1, g: 2, b: 3, n: 4}
ar := &h.counts[0]
h.Reset(1, 1)
if &h.counts[0] != ar {
t.Error("same-size reset reallocated memory")
}
if h.counts[0] != (histBin{}) {
t.Error("reset failed to zero bin: have", h.counts[0])
}
h.Reset(1, 2)
if &h.counts[0] == ar {
t.Error("different-size reset failed to reallocate memory")
}
ar = &h.counts[0]
h.Reset(2, 1)
if &h.counts[0] == ar {
t.Error("same-size different-dim reset failed to reallocate memory")
}
}
func TestHistAdd(t *testing.T) {
h := NewHist(1, 1)
c := color.RGBA64{R: 1, G: 10, B: 100, A: 1000}
h.Add(0, 0, c)
bin := h.counts[0]
if bin.r != uint64(c.R) {
t.Error("wrong red: want", uint64(c.R), "have", bin.r)
}
if bin.g != uint64(c.G) {
t.Error("wrong green: want", uint64(c.G), "have", bin.g)
}
if bin.b != uint64(c.B) {
t.Error("wrong blue: want", uint64(c.B), "have", bin.b)
}
if bin.n != uint64(c.A) {
t.Error("wrong alpha: want", uint64(c.A), "have", bin.n)
}
for i := 1; i < 10; i++ {
h.Add(0, 0, c)
}
bin = h.counts[0]
if bin.r != 10*uint64(c.R) {
t.Error("wrong red: want", 10*uint64(c.R), "have", bin.r)
}
if bin.g != 10*uint64(c.G) {
t.Error("wrong green: want", 10*uint64(c.G), "have", bin.g)
}
if bin.b != 10*uint64(c.B) {
t.Error("wrong blue: want", 10*uint64(c.B), "have", bin.b)
}
if bin.n != 10*uint64(c.A) {
t.Error("wrong alpha: want", 10*uint64(c.A), "have", bin.n)
}
t.Run("concurrent", func(t *testing.T) {
h.Reset(1, 1)
const procs = 10
const iters = 10000
var wg sync.WaitGroup
wg.Add(procs)
for i := 0; i < procs; i++ {
go func() {
for i := 0; i < iters; i++ {
h.Add(0, 0, c)
}
wg.Done()
}()
}
wg.Wait()
bin := h.counts[0]
if bin.r != procs*iters*uint64(c.R) {
t.Error("wrong red: want", procs*iters*uint64(c.R), "have", bin.r)
}
if bin.g != procs*iters*uint64(c.G) {
t.Error("wrong green: want", procs*iters*uint64(c.G), "have", bin.g)
}
if bin.b != procs*iters*uint64(c.B) {
t.Error("wrong blue: want", procs*iters*uint64(c.B), "have", bin.b)
}
if bin.n != procs*iters*uint64(c.A) {
t.Error("wrong alpha: want", procs*iters*uint64(c.A), "have", bin.n)
}
})
}
func BenchmarkScale(b *testing.B) {
const sz = 128
const osa = 10
h := NewHist(sz*osa, sz*osa)
img := h.Image(ToneMap{Brightness: 1, Gamma: 1}, 1, 1, osa)
onto := image.NewNRGBA(image.Rect(0, 0, sz, sz))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
draw.BiLinear.Scale(onto, onto.Bounds(), img, img.Bounds(), draw.Over, nil)
}
}