-
Notifications
You must be signed in to change notification settings - Fork 0
/
pk_dataset_test.go
86 lines (76 loc) · 1.67 KB
/
pk_dataset_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
package pk_test
import (
_ "embed"
"math"
"strconv"
"strings"
"testing"
"github.com/ringsaturn/pk"
)
//go:embed example_geos.csv
var exampleGeosCSV []byte
//go:embed example_distances.tsv
var exampleDistanceTSV []byte
func TestPlaceKeyDataset(t *testing.T) {
lines := strings.Split(string(exampleGeosCSV), "\n")
for index, line := range lines {
if index == 0 {
continue
}
rawparts := strings.Split(line, ",")
if len(rawparts) != 8 {
continue
}
lat_str := rawparts[0]
lng_str := rawparts[1]
placekey_str := rawparts[4]
lat_float, err := strconv.ParseFloat(lat_str, 64)
if err != nil {
panic(err)
}
lng_float, err := strconv.ParseFloat(lng_str, 64)
if err != nil {
panic(err)
}
placeKey, err := pk.GeoToPlacekey(lat_float, lng_float)
if err != nil {
panic(err)
}
if placeKey != placekey_str {
t.Errorf("bad result %v got %v\n", line, placeKey)
}
}
}
func TestGeoDistanceDataset(t *testing.T) {
lines := strings.Split(string(exampleDistanceTSV), "\n")
for index, line := range lines {
if index == 0 {
continue
}
rawparts := strings.Split(line, "\t")
if len(rawparts) != 8 {
continue
}
// placekey_1 geo_1 placekey_2 geo_2 distance(km) error
pk1 := rawparts[0]
pk2 := rawparts[2]
expectDistStr := rawparts[4]
expectDistErrStr := rawparts[5]
expectDist, err := strconv.ParseFloat(expectDistStr, 64)
if err != nil {
panic(err)
}
expectDistErr, err := strconv.ParseFloat(expectDistErrStr, 64)
if err != nil {
panic(err)
}
dist, err := pk.PlacekeyDistance(pk1, pk2)
if err != nil {
t.Error(err)
continue
}
if math.Abs(dist/1000-expectDist) > expectDistErr {
t.Error("bad")
}
}
}