-
Notifications
You must be signed in to change notification settings - Fork 12
/
tz_test.go
181 lines (173 loc) · 3.05 KB
/
tz_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package tz
import (
"fmt"
"reflect"
"testing"
)
type result struct {
zones []string
err error
}
var tt = []struct {
name string
point Point
result
}{
{
"Riga",
Point{24.105078, 56.946285},
result{
zones: []string{"Europe/Riga"},
err: nil,
},
},
{
"Tokyo",
Point{139.7594549, 35.6828387},
result{
zones: []string{"Asia/Tokyo"},
err: nil,
},
},
{
"Urumqi/Shanghai",
Point{87.319461, 43.419754},
result{
zones: []string{"Asia/Shanghai", "Asia/Urumqi"},
err: nil,
},
},
{
"Tuvalu",
Point{178.1167698, -7.768959},
result{
zones: []string{"Pacific/Funafuti"},
err: nil,
},
},
{
"Baker Island",
Point{-176.474331436, 0.190165906},
result{
zones: []string{"Etc/GMT+12"},
err: nil,
},
},
{
"Dubai (hole)",
Point{56.276871, 25.276084},
result{
zones: []string{"Asia/Dubai"},
err: nil,
},
},
{
"Muscat (in hole)",
Point{56.25347, 25.25701},
result{
zones: []string{"Asia/Muscat"},
err: nil,
},
},
{
"Kazakhstan",
Point{76.9457275, 43.2363924},
result{
zones: []string{"Asia/Almaty"},
err: nil,
},
},
}
func TestGetZone(t *testing.T) {
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
tzid, err := GetZone(tc.point)
if err != tc.err {
t.Errorf("expected err %v; got %v", tc.err, err)
}
if !reflect.DeepEqual(tzid, tc.zones) {
t.Errorf("expected zones %v; got %v", tc.zones, tzid)
}
})
}
}
func BenchmarkZones(b *testing.B) {
b.Run("polygon centers", func(b *testing.B) {
Loop:
for n := 0; n < b.N; {
for _, v := range centerCache {
for i := range v {
if n > b.N {
break Loop
}
_, err := GetZone(v[i])
if err != nil {
b.Errorf("point %v did not return a zone", v[i])
}
n++
}
}
}
})
b.Run("test cases", func(b *testing.B) {
Loop:
for n := 0; n < b.N; {
for _, tc := range tt {
if n > b.N {
break Loop
}
GetZone(tc.point)
n++
}
}
})
}
func TestNautical(t *testing.T) {
tt := []struct {
lon float64
zone string
}{
{-180, "Etc/GMT+12"},
{180, "Etc/GMT-12"},
{-172.5, "Etc/GMT+12"},
{172.5, "Etc/GMT-12"},
{-172, "Etc/GMT+11"},
{172, "Etc/GMT-11"},
{0, "Etc/GMT"},
{7.49, "Etc/GMT"},
{-7.49, "Etc/GMT"},
{7.5, "Etc/GMT-1"},
{-7.5, "Etc/GMT+1"},
}
for _, tc := range tt {
t.Run(fmt.Sprintf("%f %s", tc.lon, tc.zone), func(t *testing.T) {
z, _ := getNauticalZone(&Point{Lat: 0, Lon: tc.lon})
if z[0] != tc.zone {
t.Errorf("expected %s got %s", tc.zone, z[0])
}
})
}
}
func TestOutOfRange(t *testing.T) {
tt := []struct {
p Point
err error
}{
{Point{180, 0}, nil},
{Point{-180, 0}, nil},
{Point{0, 90}, nil},
{Point{0, -90}, nil},
{Point{181, 0}, ErrOutOfRange},
{Point{-181, 0}, ErrOutOfRange},
{Point{0, 91}, ErrOutOfRange},
{Point{0, -91}, ErrOutOfRange},
}
for _, tc := range tt {
t.Run(fmt.Sprintf("%f %f", tc.p.Lon, tc.p.Lat), func(t *testing.T) {
_, err := GetZone(tc.p)
if err != tc.err {
t.Errorf("expected error %v got %v", tc.err, err)
}
})
}
}