-
Notifications
You must be signed in to change notification settings - Fork 49
/
bounds_test.go
76 lines (63 loc) · 1.71 KB
/
bounds_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
package osm
import (
"fmt"
"testing"
"github.com/paulmach/orb/maptile"
)
func TestNewBoundFromTile(t *testing.T) {
if _, err := NewBoundsFromTile(maptile.New(1000, 1, 3)); err == nil {
t.Errorf("should return error for x out of bound")
}
if _, err := NewBoundsFromTile(maptile.New(1, 1000, 3)); err == nil {
t.Errorf("should return error for y out of bound")
}
bounds, _ := NewBoundsFromTile(maptile.New(7, 8, 9))
// check 9 tiles around bounds
for i := -1; i <= 1; i++ {
for j := -1; j <= 1; j++ {
t.Run(fmt.Sprintf("i %d j %d", i, j), func(t *testing.T) {
n := centroid(mustBounds(t, uint32(7+i), uint32(8+j), 9))
if i == 0 && j == 0 {
if !bounds.ContainsNode(n) {
t.Errorf("should contain point")
}
} else {
if bounds.ContainsNode(n) {
t.Errorf("should not contain point")
}
}
})
}
}
}
func TestBounds_ContainsNode(t *testing.T) {
b := &Bounds{}
if v := b.ContainsNode(&Node{}); !v {
t.Errorf("should contain node on boundary")
}
if v := b.ContainsNode(&Node{Lat: -1}); v {
t.Errorf("should not contain node outside bounds")
}
if v := b.ContainsNode(&Node{Lat: 1}); v {
t.Errorf("should not contain node outside bounds")
}
if v := b.ContainsNode(&Node{Lon: -1}); v {
t.Errorf("should not contain node outside bounds")
}
if v := b.ContainsNode(&Node{Lon: 1}); v {
t.Errorf("should not contain node outside bounds")
}
}
func mustBounds(t *testing.T, x, y uint32, z maptile.Zoom) *Bounds {
bounds, err := NewBoundsFromTile(maptile.New(x, y, z))
if err != nil {
t.Fatalf("invalid bounds: %v", err)
}
return bounds
}
func centroid(b *Bounds) *Node {
return &Node{
Lon: (b.MinLon + b.MaxLon) / 2,
Lat: (b.MinLat + b.MaxLat) / 2,
}
}