-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec_test.go
249 lines (232 loc) · 6.36 KB
/
codec_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright 2019 Thibault NORMAND
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package geopoint_test
import (
"testing"
"go.zenithar.org/geopoint"
)
func TestEncoder_EncodeDecode(t *testing.T) {
lat, lon := 43.603574, 1.442917
p := geopoint.Encode(lat, lon)
pLat, pLon, err := geopoint.Decode(p)
if err != nil {
t.Fatalf("error should not be raise, %v", err)
}
if lat != pLat {
t.Fatalf("invalid latitude, expected: %v, got %v", lat, pLat)
}
if lon != pLon {
t.Fatalf("invalid longitude, expected: %v, got %v", lon, pLon)
}
}
func TestEncoder_Encode(t *testing.T) {
tcl := []struct {
name string
lat float64
lon float64
expectedPoint geopoint.Value
expectedCode string
expectedBase58 string
}{
{
name: "Pôle Nord",
lat: 90,
lon: 0,
expectedPoint: geopoint.Value(101528903708835840),
expectedCode: "168B4:00000:00000",
},
{
name: "Pôle Sud",
lat: -90,
lon: 0,
expectedPoint: geopoint.Value(197912092999680),
expectedCode: "000B4:00000:00000",
},
{
name: "Place du capitole, Toulouse, France",
lat: 43.603574,
lon: 1.442917,
expectedPoint: geopoint.Value(75071809151126838),
expectedCode: "10AB5:69A51:94D36",
},
{
name: "Mairie de Toulouse, Toulouse, France",
lat: 43.604297,
lon: 1.443677,
expectedPoint: geopoint.Value(75071809155908323),
expectedCode: "10AB5:69A56:242E3",
},
{
name: "Tour Eiffel, Paris, France",
lat: 48.858373,
lon: 2.292292,
expectedPoint: geopoint.Value(77887690747650097),
expectedCode: "114B6:712B6:3A031",
},
{
name: "Montréal, Quebec, Canada",
lat: 45.558196,
lon: -73.870384,
expectedPoint: geopoint.Value(76116863733120784),
expectedCode: "10E6B:E2603:ABF10",
},
{
name: "Buenos Aires, Argentina",
lat: -34.615662,
lon: -58.503337,
expectedPoint: geopoint.Value(31659800001010902),
expectedCode: "0707A:6B9CB:85CD6",
},
}
for _, tc := range tcl {
t.Run(tc.name, func(t *testing.T) {
out := geopoint.Encode(tc.lat, tc.lon)
if out != tc.expectedPoint {
t.Fatalf("Invalid result: expected %d but got %d", tc.expectedPoint, uint64(out))
}
if out.Code() != tc.expectedCode {
t.Fatalf("Invalid result: expected %s but got %s", tc.expectedCode, out.Code())
}
})
}
}
func TestEncoder_Decode(t *testing.T) {
tcl := []struct {
name string
point geopoint.Value
expectedLat float64
expectedLon float64
expectedErr error
}{
{
name: "Place du capitole, Toulouse, France",
point: geopoint.Value(75071809151126838),
expectedLat: 43.603574,
expectedLon: 1.442917,
},
{
name: "Mairie de Toulouse, Toulouse, France",
point: geopoint.Value(75071809155908323),
expectedLat: 43.604297,
expectedLon: 1.443677,
},
{
name: "Tour Eiffel, Paris, France",
point: geopoint.Value(77887690747650097),
expectedLat: 48.858373,
expectedLon: 2.292292,
},
{
name: "Montréal, Quebec, Canada",
point: geopoint.Value(76116863733120784),
expectedLat: 45.558196,
expectedLon: -73.870384,
},
{
name: "Buenos Aires, Argentina",
point: geopoint.Value(31659800001010902),
expectedLat: -34.615662,
expectedLon: -58.503337,
},
}
for _, tc := range tcl {
t.Run(tc.name, func(t *testing.T) {
lat, lon, err := geopoint.Decode(tc.point)
if err != tc.expectedErr {
t.Fatalf("Invalid result: Error expected %v, go %v.", tc.expectedErr, err)
}
if lat != tc.expectedLat {
t.Fatalf("Invalid result: expected latitude %0.10f but got %0.10f", tc.expectedLat, lat)
}
if lon != tc.expectedLon {
t.Fatalf("Invalid result: expected latitude %0.10f but got %0.10f", tc.expectedLon, lon)
}
})
}
}
func TestEncoder_DecodeString(t *testing.T) {
tcl := []struct {
name string
input string
expectedLat float64
expectedLon float64
expectedErr error
}{
{
name: "Place du capitole, Toulouse, France",
input: "10AB5:69A51:94D36",
expectedLat: 43.603574,
expectedLon: 1.442917,
},
{
name: "Mairie de Toulouse, Toulouse, France",
input: "10AB5:69A56:242E3",
expectedLat: 43.604297,
expectedLon: 1.443677,
},
{
name: "Tour Eiffel, Paris, France",
input: "114B6:712B6:3A031",
expectedLat: 48.858373,
expectedLon: 2.292292,
},
{
name: "Montréal, Quebec, Canada",
input: "10E6B:E2603:ABF10",
expectedLat: 45.558196,
expectedLon: -73.870384,
},
{
name: "Buenos Aires, Argentina",
input: "0707A:6B9CB:85CD6",
expectedLat: -34.615662,
expectedLon: -58.503337,
},
{
name: "CryptoPan - Place du capitole, Toulouse, France",
input: "10A4D:53A5D:54CC6",
expectedLat: 43.868266,
expectedLon: -103.116777,
},
}
for _, tc := range tcl {
t.Run(tc.name, func(t *testing.T) {
lat, lon, err := geopoint.FromString(tc.input)
if err != tc.expectedErr {
t.Fatalf("Invalid result: Error expected %v, go %v.", tc.expectedErr, err)
}
if lat != tc.expectedLat {
t.Fatalf("Invalid result: expected latitude %0.10f but got %0.10f", tc.expectedLat, lat)
}
if lon != tc.expectedLon {
t.Fatalf("Invalid result: expected longitude %0.10f but got %0.10f", tc.expectedLon, lon)
}
})
}
}
// -----------------------------------------------------------------------------
func BenchmarkDecoder_Decode(b *testing.B) {
p := geopoint.Value(31659983379082793)
for i := 0; i < b.N; i++ {
_, _, _ = geopoint.Decode(p)
}
}
func BenchmarkCodec_Encode(b *testing.B) {
lat, lon := 43.603574, 1.442917
for i := 0; i < b.N; i++ {
_ = geopoint.Encode(lat, lon)
}
}