-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathheatmap_test.go
More file actions
135 lines (110 loc) · 2.71 KB
/
heatmap_test.go
File metadata and controls
135 lines (110 loc) · 2.71 KB
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
129
130
131
132
133
134
135
package heatmap
import (
"image"
"io"
"math"
"math/rand"
"testing"
"github.com/dustin/go-heatmap/schemes"
"github.com/jteeuwen/imghash"
)
var testPoints = []DataPoint{}
func init() {
rand.Seed(4828458249)
for n := 0; n < 350; n++ {
testPoints = append(testPoints,
P(rand.Float64(), rand.Float64()))
}
}
func assertEpsilon(t *testing.T, field string, expected, got float64) {
if math.Abs(got-expected) > 0.00001 {
t.Fatalf("Expected %v for %v, got %v",
expected, field, got)
}
}
func TestFindLimits(t *testing.T) {
l := findLimits(testPoints)
assertEpsilon(t, "minx", 0.005923822722460793, l.Min.X())
assertEpsilon(t, "miny", 0.0038807964380815894, l.Min.Y())
assertEpsilon(t, "maxx", 0.984841695550329, l.Max.X())
assertEpsilon(t, "maxy", 0.9990553050102642, l.Max.Y())
assertEpsilon(t, "dx", 0.9789178728278681, l.Dx())
assertEpsilon(t, "dy", 0.9951745085721826, l.Dy())
}
func TestHeatmapKMLLimits(t *testing.T) {
tests := []DataPoint{
P(-200, 0),
P(200, 0),
P(0, 100),
P(0, -100),
}
for _, test := range tests {
lim := findLimits([]DataPoint{test})
if lim.inRange(-180, 180, -90, 90) {
t.Errorf("Expected out of range on %v", test)
}
}
}
const expHash = uint64(62624876249118208)
func TestMkImage(t *testing.T) {
got := imghash.Average(Heatmap(image.Rect(0, 0, 1024, 1024),
testPoints, 150, 128, schemes.AlphaFire))
if got != expHash {
t.Errorf("Expected hash = %v, got %v", expHash, got)
}
}
func TestMust(t *testing.T) {
must(nil) // no panic
panicked := false
func() {
defer func() { panicked = recover() != nil }()
must(io.EOF)
}()
if !panicked {
t.Fatalf("Expected a panic, but didn't get one")
}
}
func BenchmarkPlacement(b *testing.B) {
b.StopTimer()
l := findLimits(testPoints)
size := image.Rect(0, 0, 4096, 4096)
dot := mkDot(float64(100))
b.StartTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
bw := image.NewRGBA(size)
b.StartTimer()
placePoints(size, l, bw, testPoints, dot)
}
}
func BenchmarkWarming(b *testing.B) {
b.StopTimer()
l := findLimits(testPoints)
size := image.Rect(0, 0, 4096, 4096)
dot := mkDot(float64(100))
colors := schemes.AlphaFire
b.StartTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
bw := image.NewRGBA(size)
placePoints(size, l, bw, testPoints, dot)
out := image.NewRGBA(size)
b.StartTimer()
warm(out, bw, 64, colors)
}
}
func BenchmarkWarmingParallel(b *testing.B) {
l := findLimits(testPoints)
size := image.Rect(0, 0, 4096, 4096)
dot := mkDot(float64(100))
colors := schemes.AlphaFire
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
bw := image.NewRGBA(size)
placePoints(size, l, bw, testPoints, dot)
out := image.NewRGBA(size)
for pb.Next() {
warm(out, bw, 64, colors)
}
})
}