-
Notifications
You must be signed in to change notification settings - Fork 3
/
geohash_int_test.go
113 lines (96 loc) · 2.82 KB
/
geohash_int_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
package geohash
import (
"math"
"math/rand"
"testing"
)
func TestEncodeIntBasic(t *testing.T) {
var expected int64 = 4064984913515641
result := EncodeInt(37.8324, 112.5584, MaxBitDepth)
if expected != result {
t.Errorf("Expected %+v but was %+v", expected, result)
}
}
func TestDecodeIntBasic(t *testing.T) {
var expectedLat float64 = 37.8324
var expectedLng float64 = 112.5584
resultLat, resultLng, _, _ := DecodeInt(4064984913515641, MaxBitDepth)
if math.Abs(expectedLat-resultLat) > 0.0001 {
t.Errorf("Expected %+v but was %+v", expectedLat, resultLat)
}
if math.Abs(expectedLng-resultLng) > 0.0001 {
t.Errorf("Expected %+v but was %+v", expectedLng, resultLng)
}
}
func TestDecodeBboxInt(t *testing.T) {
var expectedMinLat float64 = 37.8324
var expectedMinLng float64 = 112.5584
var expectedMaxLat float64 = 37.8324
var expectedMaxLng float64 = 112.5584
minLat, minLng, maxLat, maxLng := DecodeBboxInt(4064984913515641, MaxBitDepth)
if math.Abs(expectedMinLat-minLat) > 0.0001 {
t.Errorf("Expected %+v but was %+v", expectedMinLat, minLat)
}
if math.Abs(expectedMinLng-minLng) > 0.0001 {
t.Errorf("Expected %+v but was %+v", expectedMinLng, minLng)
}
if math.Abs(expectedMaxLat-maxLat) > 0.0001 {
t.Errorf("Expected %+v but was %+v", expectedMaxLat, maxLat)
}
if math.Abs(expectedMaxLng-maxLng) > 0.0001 {
t.Errorf("Expected %+v but was %+v", expectedMaxLng, maxLng)
}
}
func TestNeighborInt(t *testing.T) {
result := NeighborInt(1702789509, North, 32)
var expected int64 = 1702789520
if expected != result {
t.Errorf("Expected %+v but was %+v", expected, result)
}
result = NeighborInt(27898503327470, SouthWest, 46)
expected = 27898503327465
if expected != result {
t.Errorf("Expected %+v but was %+v", expected, result)
}
}
func TestNeighborsInt(t *testing.T) {
expected := []int64{1702789520, 1702789522, 1702789511, 1702789510, 1702789508, 1702789422, 1702789423, 1702789434, 1702789509}
results := NeighborsInt(1702789509, 32)
for _, expectedValue := range expected {
found := false
for _, resultValue := range results {
if expectedValue == resultValue {
found = true
}
}
if !found {
t.Errorf("Expected value %+v not found.", expectedValue)
}
}
}
func TestBBoxesInt(t *testing.T) {
results := BboxesInt(30, 120, 30.0001, 120.0001, 50)
expected := EncodeInt(30.0001, 120.0001, 50)
found := false
for _, resultValue := range results {
if expected == resultValue {
found = true
}
}
if !found {
t.Errorf("Expected value %+v not found.", expected)
}
}
func TestGetBit(t *testing.T) {
for i := 0; i < 100; i++ { // 100 tests
geohash := rand.Int63()
bits := int64(0)
for position := int64(63); position >= 0; position-- {
bits *= 2
bits += getBit(geohash, position)
}
if geohash != bits {
t.Errorf("getBit() failed with %v != %v", geohash, bits)
}
}
}