-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
tzf_fuzzy_test.go
91 lines (81 loc) · 2.22 KB
/
tzf_fuzzy_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
package tzf_test
import (
"fmt"
"testing"
"github.com/loov/hrtime/hrtesting"
gocitiesjson "github.com/ringsaturn/go-cities.json"
"github.com/ringsaturn/tzf"
tzfrellite "github.com/ringsaturn/tzf-rel-lite"
"github.com/ringsaturn/tzf/pb"
"google.golang.org/protobuf/proto"
)
var (
fuzzyFinder tzf.F
)
func init() {
input := &pb.PreindexTimezones{}
if err := proto.Unmarshal(tzfrellite.PreindexData, input); err != nil {
panic(err)
}
_fuzzyFinder, err := tzf.NewFuzzyFinderFromPB(input)
if err != nil {
panic(err)
}
fuzzyFinder = _fuzzyFinder
}
func TestFuzzySupports(t *testing.T) {
failCount := 0
for city := range gocitiesjson.All(false) {
name := fuzzyFinder.GetTimezoneName(city.Lng, city.Lat)
if name == "" {
failCount += 1
}
}
// more than 10%
if failCount/len(gocitiesjson.Cities)*100 > 10 {
t.Errorf("has too many covered cities %v", failCount)
}
}
func ExampleFuzzyFinder_GetTimezoneName() {
input := &pb.PreindexTimezones{}
if err := proto.Unmarshal(tzfrellite.PreindexData, input); err != nil {
panic(err)
}
finder, _ := tzf.NewFuzzyFinderFromPB(input)
fmt.Println(finder.GetTimezoneName(116.6386, 40.0786))
// Output: Asia/Shanghai
}
func ExampleFuzzyFinder_GetTimezoneNames() {
input := &pb.PreindexTimezones{}
if err := proto.Unmarshal(tzfrellite.PreindexData, input); err != nil {
panic(err)
}
finder, _ := tzf.NewFuzzyFinderFromPB(input)
fmt.Println(finder.GetTimezoneNames(87.6168, 43.8254))
// Output: [Asia/Shanghai Asia/Urumqi] <nil>
}
func ExampleFuzzyFinder_TimezoneNames() {
input := &pb.PreindexTimezones{}
if err := proto.Unmarshal(tzfrellite.PreindexData, input); err != nil {
panic(err)
}
finder, _ := tzf.NewFuzzyFinderFromPB(input)
fmt.Println(finder.TimezoneNames())
}
func BenchmarkFuzzyFinder_GetTimezoneName_Random_WorldCities(b *testing.B) {
bench := hrtesting.NewBenchmark(b)
defer bench.Report()
for bench.Next() {
p := gocitiesjson.Random()
_ = fuzzyFinder.GetTimezoneName(p.Lng, p.Lat)
}
}
func FuzzFuzzyFinder_GetTimezoneName(f *testing.F) {
f.Add(116.3883, 39.9289)
f.Fuzz(func(t *testing.T, a float64, b float64) {
ret, err := fuzzyFinder.GetTimezoneNames(a, b)
if err == nil && len(ret) == 0 {
t.Errorf("bad return %v, %v", ret, err)
}
})
}