-
Notifications
You must be signed in to change notification settings - Fork 12
/
hashfill_test.go
105 lines (85 loc) · 3.23 KB
/
hashfill_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
package hashfill
import (
"testing"
"github.com/stretchr/testify/assert"
geom "github.com/twpayne/go-geom"
)
func contains(haystack []string, needle string) bool {
for _, s := range haystack {
if needle == s {
return true
}
}
return false
}
func TestRecursiveFillIntersects(t *testing.T) {
geofence := readFileAsGeometry(t, "testdata/regents.geojson")
filler := NewRecursiveFiller(
WithMaxPrecision(6),
)
expected := []string{"gcpvh7", "gcpvhe", "gcpvhh", "gcpvhj", "gcpvhk", "gcpvhm", "gcpvhs", "gcpvht"}
hashes, err := filler.Fill(geofence, FillIntersects)
assert.NoError(t, err)
assert.Equal(t, expected, hashes)
}
func TestRecursiveFillIntersectsNotFixed(t *testing.T) {
geofence := readFileAsGeometry(t, "testdata/regents.geojson")
filler := NewRecursiveFiller(
WithMaxPrecision(8),
)
hashes, err := filler.Fill(geofence, FillIntersects)
assert.NoError(t, err)
assert.Len(t, hashes, 948)
assert.True(t, contains(hashes, "gcpvht0")) // precision 7
assert.True(t, contains(hashes, "gcpvhtb0")) // 8
}
func TestRecursiveFillIntersectsFixed(t *testing.T) {
geofence := readFileAsGeometry(t, "testdata/regents.geojson")
filler := NewRecursiveFiller(
WithMaxPrecision(8),
WithFixedPrecision(),
)
hashes, err := filler.Fill(geofence, FillIntersects)
assert.NoError(t, err)
assert.Len(t, hashes, 3242)
assert.False(t, contains(hashes, "gcpvht0")) // No precision 7
assert.True(t, contains(hashes, "gcpvhtb0")) // 8
}
func TestRecursiveFillContains(t *testing.T) {
geofence := readFileAsGeometry(t, "testdata/london.geojson")
filler := NewRecursiveFiller(
WithMaxPrecision(5),
)
expected := []string{"gcpsz", "gcptn", "gcptp", "gcpu6", "gcpu7", "gcpu8", "gcpu9", "gcpub", "gcpuc", "gcpud", "gcpue", "gcpuf", "gcpug", "gcpuk", "gcpum", "gcpur", "gcpus", "gcput", "gcpuu", "gcpuv", "gcpuw", "gcpux", "gcpuy", "gcpuz", "gcpv0", "gcpv1", "gcpv2", "gcpv3", "gcpv4", "gcpv5", "gcpv6", "gcpv7", "gcpve", "gcpvh", "gcpvj", "gcpvk", "gcpvm", "gcpvn", "gcpvp", "gcpvq", "gcpvr", "gcpvs", "gcpvt", "gcpvw", "gcpvx", "u10h2", "u10h3", "u10h8", "u10h9", "u10hb", "u10hc", "u10hd", "u10he", "u10hf", "u10hg", "u10hs", "u10hu", "u10hv", "u10j0", "u10j1", "u10j2", "u10j3", "u10j4", "u10j5", "u10j6", "u10j7", "u10j8", "u10jh"}
hashes, err := filler.Fill(geofence, FillContains)
assert.NoError(t, err)
assert.Equal(t, expected, hashes)
}
type mockPredicate struct {
res bool
err error
}
func (m mockPredicate) Intersects(geofence *geom.Polygon, hash string) (bool, error) {
return m.res, m.err
}
func (m mockPredicate) Contains(geofence *geom.Polygon, hash string) (bool, error) {
return m.res, m.err
}
func TestPredicateContainError(t *testing.T) {
geofence := readFileAsGeometry(t, "testdata/regents.geojson")
filler := NewRecursiveFiller(
WithMaxPrecision(8),
WithPredicates(mockPredicate{false, assert.AnError}, mockPredicate{true, nil}),
)
_, err := filler.Fill(geofence, FillIntersects)
assert.Error(t, err)
}
func TestPredicateIntersectsError(t *testing.T) {
geofence := readFileAsGeometry(t, "testdata/regents.geojson")
filler := NewRecursiveFiller(
WithMaxPrecision(8),
WithPredicates(mockPredicate{false, nil}, mockPredicate{false, assert.AnError}),
)
_, err := filler.Fill(geofence, FillIntersects)
assert.Error(t, err)
}