10
// Tests that a call to NewPoint should return a pointer to a Point with the specified values assigned correctly.
11
func TestNewPoint(t *testing.T) {
12
p := NewPoint(40.5, 120.5)
15
t.Error("Expected to get a pointer to a new point, but got nil instead.")
19
t.Errorf("Expected to be able to specify 40.5 as the lat value of a new point, but got %f instead", p.lat)
23
t.Errorf("Expected to be able to specify 120.5 as the lng value of a new point, but got %f instead", p.lng)
27
// Tests that calling GetLat() after creating a new point returns the expected lat value.
28
func TestLat(t *testing.T) {
29
p := NewPoint(40.5, 120.5)
34
t.Error("Expected a call to GetLat() to return the same lat value as was set before, but got %f instead", lat)
38
// Tests that calling GetLng() after creating a new point returns the expected lng value.
39
func TestLng(t *testing.T) {
40
p := NewPoint(40.5, 120.5)
45
t.Error("Expected a call to GetLng() to return the same lat value as was set before, but got %f instead", lng)
50
func TestGreatCircleDistance(t *testing.T) {
51
// Test that SEA and SFO are ~ 1091km apart, accurate to 100 meters.
52
sea := &Point{lat: 47.4489, lng: -122.3094}
53
sfo := &Point{lat: 37.6160933, lng: -122.3924223}
54
sfoToSea := 1093.379199082169
56
dist := sea.GreatCircleDistance(sfo)
58
if !(dist < (sfoToSea+0.1) && dist > (sfoToSea-0.1)) {
59
t.Error("Unnacceptable result.", dist)
63
func TestPointAtDistanceAndBearing(t *testing.T) {
64
sea := &Point{lat: 47.44745785, lng: -122.308065668024}
65
p := sea.PointAtDistanceAndBearing(1090.7, 180)
67
// Expected results of transposing point
68
// ~1091km at bearing of 180 degrees
69
resultLat := 37.638557
70
resultLng := -122.308066
72
withinLatBounds := p.lat < resultLat+0.001 && p.lat > resultLat-0.001
73
withinLngBounds := p.lng < resultLng+0.001 && p.lng > resultLng-0.001
74
if !(withinLatBounds && withinLngBounds) {
75
t.Error("Unnacceptable result.", fmt.Sprintf("[%f, %f]", p.lat, p.lng))
79
func TestBearingTo(t *testing.T) {
80
p1 := &Point{lat: 40.7486, lng: -73.9864}
81
p2 := &Point{lat: 0.0, lng: 0.0}
82
bearing := p1.BearingTo(p2)
84
// Expected bearing 60 degrees
85
resultBearing := 100.610833
87
withinBearingBounds := bearing < resultBearing+0.001 && bearing > resultBearing-0.001
88
if !withinBearingBounds {
89
t.Error("Unnacceptable result.", fmt.Sprintf("%f", bearing))
93
// Enures that a point can be marhalled into JSON
94
func TestMarshalJSON(t *testing.T) {
95
p := NewPoint(40.7486, -73.9864)
96
res, err := json.Marshal(p)
100
t.Error("Should not encounter an error when attempting to Marshal a Point to JSON")
103
if string(res) == "{lat:40.7486, lng:-73.9864}" {
104
t.Error("Point should correctly Marshal to JSON")
108
// Enures that a point can be unmarhalled from JSON
109
func TestUnmarshalJSON(t *testing.T) {
110
data := []byte(`{"lat":40.7486, "lng":-73.9864}`)
112
err := p.UnmarshalJSON(data)
115
t.Errorf("Should not encounter an error when attempting to Unmarshal a Point from JSON")
118
if p.Lat() != 40.7486 || p.Lng() != -73.9864 {
119
t.Errorf("Point has mismatched data after Unmarshalling from JSON")