~vtuson/scopecreator/twitter-template

« back to all changes in this revision

Viewing changes to src/go/src/github.com/kellydunn/golang-geo/point_test.go

  • Committer: Victor Palau
  • Date: 2015-03-11 14:24:42 UTC
  • Revision ID: vtuson@gmail.com-20150311142442-f2pxp111c8ynv232
public release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package geo
 
2
 
 
3
import (
 
4
        "encoding/json"
 
5
        "fmt"
 
6
        "log"
 
7
        "testing"
 
8
)
 
9
 
 
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)
 
13
 
 
14
        if p == nil {
 
15
                t.Error("Expected to get a pointer to a new point, but got nil instead.")
 
16
        }
 
17
 
 
18
        if p.lat != 40.5 {
 
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)
 
20
        }
 
21
 
 
22
        if p.lng != 120.5 {
 
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)
 
24
        }
 
25
}
 
26
 
 
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)
 
30
 
 
31
        lat := p.Lat()
 
32
 
 
33
        if lat != 40.5 {
 
34
                t.Error("Expected a call to GetLat() to return the same lat value as was set before, but got %f instead", lat)
 
35
        }
 
36
}
 
37
 
 
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)
 
41
 
 
42
        lng := p.Lng()
 
43
 
 
44
        if lng != 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)
 
46
        }
 
47
}
 
48
 
 
49
// Seems brittle :\
 
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
 
55
 
 
56
        dist := sea.GreatCircleDistance(sfo)
 
57
 
 
58
        if !(dist < (sfoToSea+0.1) && dist > (sfoToSea-0.1)) {
 
59
                t.Error("Unnacceptable result.", dist)
 
60
        }
 
61
}
 
62
 
 
63
func TestPointAtDistanceAndBearing(t *testing.T) {
 
64
        sea := &Point{lat: 47.44745785, lng: -122.308065668024}
 
65
        p := sea.PointAtDistanceAndBearing(1090.7, 180)
 
66
 
 
67
        // Expected results of transposing point
 
68
        // ~1091km at bearing of 180 degrees
 
69
        resultLat := 37.638557
 
70
        resultLng := -122.308066
 
71
 
 
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))
 
76
        }
 
77
}
 
78
 
 
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)
 
83
 
 
84
        // Expected bearing 60 degrees
 
85
        resultBearing := 100.610833
 
86
 
 
87
        withinBearingBounds := bearing < resultBearing+0.001 && bearing > resultBearing-0.001
 
88
        if !withinBearingBounds {
 
89
                t.Error("Unnacceptable result.", fmt.Sprintf("%f", bearing))
 
90
        }
 
91
}
 
92
 
 
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)
 
97
 
 
98
        if err != nil {
 
99
                log.Print(err)
 
100
                t.Error("Should not encounter an error when attempting to Marshal a Point to JSON")
 
101
        }
 
102
 
 
103
        if string(res) == "{lat:40.7486, lng:-73.9864}" {
 
104
                t.Error("Point should correctly Marshal to JSON")
 
105
        }
 
106
}
 
107
 
 
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}`)
 
111
        p := &Point{}
 
112
        err := p.UnmarshalJSON(data)
 
113
 
 
114
        if err != nil {
 
115
                t.Errorf("Should not encounter an error when attempting to Unmarshal a Point from JSON")
 
116
        }
 
117
 
 
118
        if p.Lat() != 40.7486 || p.Lng() != -73.9864 {
 
119
                t.Errorf("Point has mismatched data after Unmarshalling from JSON")
 
120
        }
 
121
}