forked from kellydunn/golang-geo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoint_test.go
138 lines (108 loc) · 3.77 KB
/
point_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
129
130
131
132
133
134
135
136
137
138
package geo
import (
"encoding/json"
"fmt"
"log"
"testing"
)
// Tests that a call to NewPoint should return a pointer to a Point with the specified values assigned correctly.
func TestNewPoint(t *testing.T) {
p := NewPoint(40.5, 120.5)
if p == nil {
t.Error("Expected to get a pointer to a new point, but got nil instead.")
}
if p.lat != 40.5 {
t.Errorf("Expected to be able to specify 40.5 as the lat value of a new point, but got %f instead", p.lat)
}
if p.lng != 120.5 {
t.Errorf("Expected to be able to specify 120.5 as the lng value of a new point, but got %f instead", p.lng)
}
}
// Tests that calling GetLat() after creating a new point returns the expected lat value.
func TestLat(t *testing.T) {
p := NewPoint(40.5, 120.5)
lat := p.Lat()
if lat != 40.5 {
t.Error("Expected a call to GetLat() to return the same lat value as was set before, but got %f instead", lat)
}
}
// Tests that calling GetLng() after creating a new point returns the expected lng value.
func TestLng(t *testing.T) {
p := NewPoint(40.5, 120.5)
lng := p.Lng()
if lng != 120.5 {
t.Error("Expected a call to GetLng() to return the same lat value as was set before, but got %f instead", lng)
}
}
// Seems brittle :\
func TestGreatCircleDistance(t *testing.T) {
// Test that SEA and SFO are ~ 1091km apart, accurate to 100 meters.
sea := &Point{lat: 47.4489, lng: -122.3094}
sfo := &Point{lat: 37.6160933, lng: -122.3924223}
sfoToSea := 1093.379199082169
dist := sea.GreatCircleDistance(sfo)
if !(dist < (sfoToSea+0.1) && dist > (sfoToSea-0.1)) {
t.Error("Unnacceptable result.", dist)
}
}
func TestPointAtDistanceAndBearing(t *testing.T) {
sea := &Point{lat: 47.44745785, lng: -122.308065668024}
p := sea.PointAtDistanceAndBearing(1090.7, 180)
// Expected results of transposing point
// ~1091km at bearing of 180 degrees
resultLat := 37.638557
resultLng := -122.308066
withinLatBounds := p.lat < resultLat+0.001 && p.lat > resultLat-0.001
withinLngBounds := p.lng < resultLng+0.001 && p.lng > resultLng-0.001
if !(withinLatBounds && withinLngBounds) {
t.Error("Unnacceptable result.", fmt.Sprintf("[%f, %f]", p.lat, p.lng))
}
}
func TestBearingTo(t *testing.T) {
p1 := &Point{lat: 40.7486, lng: -73.9864}
p2 := &Point{lat: 0.0, lng: 0.0}
bearing := p1.BearingTo(p2)
// Expected bearing 60 degrees
resultBearing := 100.610833
withinBearingBounds := bearing < resultBearing+0.001 && bearing > resultBearing-0.001
if !withinBearingBounds {
t.Error("Unnacceptable result.", fmt.Sprintf("%f", bearing))
}
}
func TestMidpointTo(t *testing.T) {
p1 := &Point{lat: 52.205, lng: 0.119}
p2 := &Point{lat: 48.857, lng: 2.351}
p := p1.MidpointTo(p2)
// Expected midpoint 50.5363°N, 001.2746°E
resultLat := 50.53632
resultLng := 1.274614
withinLatBounds := p.lat < resultLat+0.001 && p.lat > resultLat-0.001
withinLngBounds := p.lng < resultLng+0.001 && p.lng > resultLng-0.001
if !(withinLatBounds && withinLngBounds) {
t.Error("Unnacceptable result.", fmt.Sprintf("[%f, %f]", p.lat, p.lng))
}
}
// Enures that a point can be marhalled into JSON
func TestMarshalJSON(t *testing.T) {
p := NewPoint(40.7486, -73.9864)
res, err := json.Marshal(p)
if err != nil {
log.Print(err)
t.Error("Should not encounter an error when attempting to Marshal a Point to JSON")
}
if string(res) != `{"lat":40.7486,"lng":-73.9864}` {
t.Error("Point should correctly Marshal to JSON")
}
}
// Enures that a point can be unmarhalled from JSON
func TestUnmarshalJSON(t *testing.T) {
data := []byte(`{"lat":40.7486,"lng":-73.9864}`)
p := &Point{}
err := p.UnmarshalJSON(data)
if err != nil {
t.Errorf("Should not encounter an error when attempting to Unmarshal a Point from JSON")
}
if p.lat != 40.7486 || p.lng != -73.9864 {
t.Errorf("Point has mismatched data after Unmarshalling from JSON")
}
}