/
aprs_test.go
313 lines (275 loc) · 8.4 KB
/
aprs_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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package aprs
import (
"bufio"
"compress/bzip2"
"encoding/json"
"io"
"math"
"os"
"reflect"
"testing"
)
const (
christmasMsg = "KG6HWF>APX200,WIDE1-1,WIDE2-1:=3722.1 N/12159.1 W-Merry Christmas!"
)
const SAMPLE2 = `K7FED-1>APNX01,qAR,W6MSU-7:!3739.12N112132.05W#PHG5750 W1, K7FED FILL-IN LLNL S300`
var samples = []struct {
src string
expected Position
}{
{`K6LRG-C>APJI23,WIDE1-1,WIDE2-1:!3729.98ND12152.33W&RNG0060 2m Voice 145.070 +1.495 Mhz`,
Position{37.49966666666667, -121.87216666666667, 0, Velocity{}, Symbol{'D', '&'}}},
{`K7FED-1>APNX01,qAR,W6MSU-7:!3739.12N112132.05W#PHG5750 W1, K7FED FILL-IN LLNL S300`,
Position{37.652, -121.534167, 0, Velocity{}, Symbol{'1', '#'}}},
{`WINLINK>APWL2K,TCPIP*,qAC,T2LAX:;KE6AFE-10*160752z3658. NW12202. Wa144.910MHz 1200 R6m Public Winlink Gateway`,
Position{36.975, -122.0416666, 2, Velocity{}, Symbol{'W', 'a'}}},
{`KE6AFE-13>APKH2Z,TCPIP*,qAC,CORE-2:;VP@CM86XX*162000z3658.94N/12200.86W? KE6AFE-13 8800`,
Position{36.9823333, -122.014333, 0, Velocity{}, Symbol{'/', '?'}}},
}
func assert(t *testing.T, name string, got interface{}, expected interface{}) {
if got != expected {
t.Fatalf("Expected %v for %v, got %v", expected, name, got)
}
// t.Logf("Looks like %s was %s", name, expected)
}
func assertEpsilon(t *testing.T, field string, expected, got float64) {
if math.Abs(got-expected) > 0.0001 {
t.Fatalf("Expected %v for %v, got %v -- off by %v",
expected, field, got, math.Abs(got-expected))
}
}
func TestAddressConversion(t *testing.T) {
testaddrs := []struct {
Src string
Exp Address
}{
{"KG6HWF", Address{"KG6HWF", ""}},
{"KG6HWF-9", Address{"KG6HWF", "9"}},
{"KG6HWF-C", Address{"KG6HWF", "C"}},
{"KG6HWF-FLY", Address{"KG6HWF", "FLY"}},
}
for _, ta := range testaddrs {
a := AddressFromString(ta.Src)
if !reflect.DeepEqual(a, ta.Exp) {
t.Fatalf("Expected %v for %v, got %v", ta.Exp, ta.Src, a)
}
if ta.Exp.String() != ta.Src {
t.Fatalf("Expected to string %v as %s, got %s",
ta.Exp, ta.Src, ta.Exp.String())
}
}
}
func TestCallPass(t *testing.T) {
testaddrs := []struct {
Call Address
Exp int16
}{
{AddressFromString("KG6HWF-9"), 22955},
{AddressFromString("KG6HWF"), 22955},
{AddressFromString("KE6AFE-13"), 18595},
{AddressFromString("K6MGD"), 12691},
}
for _, c := range testaddrs {
if c.Call.CallPass() != c.Exp {
t.Errorf("Expected %v for %v, got %v", c.Exp, c.Call, c.Call.CallPass())
}
}
}
func TestAPRS(t *testing.T) {
v := ParseFrame(christmasMsg)
assert(t, "Source", v.Source.String(), "KG6HWF")
assert(t, "Dest", v.Dest.String(), "APX200")
assert(t, "len(Path)", len(v.Path), 2)
assert(t, "Path[0]", v.Path[0].String(), "WIDE1-1")
assert(t, "Path[1]", v.Path[1].String(), "WIDE2-1")
assert(t, "Body", string(v.Body), "=3722.1 N/12159.1 W-Merry Christmas!")
pos, err := v.Body.Position()
if err != nil {
t.Fatalf("Couldn't parse body position: %v", err)
}
assertEpsilon(t, "lat", 37.3691667, pos.Lat)
assertEpsilon(t, "lon", -121.985833, pos.Lon)
assert(t, "ambiguity", 1, pos.Ambiguity)
assert(t, "table", byte('/'), pos.Symbol.Table)
assert(t, "symbol", byte('-'), pos.Symbol.Symbol)
assert(t, "String()", v.String(), christmasMsg)
}
func TestInvalid(t *testing.T) {
v := ParseFrame("Invalid")
if v.IsValid() {
t.Fatalf("Expected invalid data out of the parse")
}
assert(t, "Source", v.Source.String(), "")
assert(t, "Dest", v.Dest.String(), "")
assert(t, "len(Path)", len(v.Path), 0)
assert(t, "Body", string(v.Body), "")
v = ParseFrame("Invalid:Thing")
if v.IsValid() {
t.Fatalf("Expected invalid data out of the parse")
}
assert(t, "Source", v.Source.String(), "")
assert(t, "Dest", v.Dest.String(), "")
assert(t, "len(Path)", len(v.Path), 0)
assert(t, "Body", string(v.Body), "")
}
func TestSamples(t *testing.T) {
for _, s := range samples {
v := ParseFrame(s.src)
pos, err := v.Body.Position()
if err != nil {
t.Fatalf("Error getting position from %v: %v", s.src, err)
}
assert(t, "ambiguity", s.expected.Ambiguity, pos.Ambiguity)
assert(t, "table", s.expected.Symbol.Table, pos.Symbol.Table)
assert(t, "symbol", s.expected.Symbol.Symbol, pos.Symbol.Symbol)
assert(t, "course", s.expected.Velocity.Course, pos.Velocity.Course)
assert(t, "speed", s.expected.Velocity.Speed, pos.Velocity.Speed)
assertEpsilon(t, "lat", s.expected.Lat, pos.Lat)
assertEpsilon(t, "lon", s.expected.Lon, pos.Lon)
}
}
type SampleDoc struct {
Src string `json:"src"`
Result map[string]interface{} `Json:"result"`
Failed int `json:"failed"`
Misunderstood bool
}
func assertLatLon(t *testing.T, pos Position, doc SampleDoc) {
slat, haslat := doc.Result["latitude"].(float64)
slon, haslon := doc.Result["longitude"].(float64)
if !(haslat && haslon) {
return
}
if math.Abs(pos.Lat-slat) > 0.001 || math.Abs(pos.Lon-slon) > 0.001 {
t.Fatalf("Error parsing lat/lon from %v, got %v; expected %v,%v",
doc.Src, pos, slat, slon)
}
tbl := doc.Result["symboltable"].(string)[0]
if pos.Symbol.Table != tbl {
t.Fatalf("Expected symbol table %v, got %v for %v",
tbl, pos.Symbol.Table, doc.Src)
}
symbol := doc.Result["symbolcode"].(string)[0]
if pos.Symbol.Symbol != symbol {
t.Fatalf("Expected symbol %v, got %v for %v",
symbol, pos.Symbol.Symbol, doc.Src)
}
course, _ := doc.Result["course"].(float64)
assertEpsilon(t, "course of "+doc.Src, course, pos.Velocity.Course)
speed, _ := doc.Result["speed"].(float64)
assertEpsilon(t, "speed of "+doc.Src, speed, pos.Velocity.Speed)
}
func negAssertLatLon(t *testing.T, pos Position, doc SampleDoc) {
slat, haslat := doc.Result["latitude"].(float64)
slon, haslon := doc.Result["longitude"].(float64)
if !(haslat && haslon) {
return
}
if !(math.Abs(pos.Lat-slat) > 0.001 || math.Abs(pos.Lon-slon) > 0.001) {
t.Fatalf("Expected to fail parsing lat/lon from %v, got %v; expected %v,%v",
doc.Src, pos, slat, slon)
}
}
func assertWX(t *testing.T, body Info, want interface{}) {
t.Logf("Looking for wx data from %v, want %v", body, want)
}
func TestFAP(t *testing.T) {
expSuccess := 29
var samples []SampleDoc
r, err := os.Open("samples/faptests.json")
if err != nil {
t.Fatalf("Error opening sample.json")
}
defer r.Close()
err = json.NewDecoder(r).Decode(&samples)
if err != nil {
t.Fatalf("Error reading JSON: %v", err)
}
t.Logf("Found %d messages", len(samples))
positions := 0
misunderstood := 0
for _, sample := range samples {
if sample.Failed != 1 {
v := ParseFrame(sample.Src)
assert(t, "Source", v.Source.String(), sample.Result["srccallsign"])
assert(t, "Dest", v.Dest.String(), sample.Result["dstcallsign"])
assert(t, "Body", string(v.Body), sample.Result["body"])
if sample.Misunderstood {
misunderstood++
pos, err := v.Body.Position()
if err == nil {
negAssertLatLon(t, pos, sample)
}
t.Logf("Misunderstood: %s", sample.Src)
} else {
pos, err := v.Body.Position()
if err == nil {
assertLatLon(t, pos, sample)
positions++
}
if wx, ok := sample.Result["wx"]; ok {
assertWX(t, v.Body, wx)
}
}
}
}
if positions != expSuccess {
t.Fatalf("Expected to pass at %v position tests, got %v",
expSuccess, positions)
}
t.Logf("Found %v positions", positions)
t.Logf("Misunderstood %v", misunderstood)
}
func getSampleLines(path string) []string {
file, err := os.Open(path)
if err != nil {
panic("Could not open sample file: " + err.Error())
}
defer file.Close()
bz := bzip2.NewReader(file)
rv := make([]string, 0, 250000)
bio := bufio.NewReader(bz)
bytesread := int64(0)
done := false
for !done {
line, err := bio.ReadString('\n')
switch err {
case nil:
rv = append(rv, line)
bytesread += int64(len(line))
case io.EOF:
done = true
default:
panic("Could not load samples: " + err.Error())
}
}
return rv
}
var largeSample []string
func loadLargeSample(b *testing.B) {
if largeSample == nil {
largeSample = getSampleLines("samples/large.log.bz2")
b.ResetTimer()
}
}
func BenchmarkMessages(b *testing.B) {
loadLargeSample(b)
var read int64
for i := 0; i < b.N; i++ {
src := largeSample[i%len(largeSample)]
ParseFrame(src)
read += int64(len(src))
}
b.SetBytes(read / int64(b.N))
}
func BenchmarkPositionsFromLog(b *testing.B) {
loadLargeSample(b)
var read int64
for i := 0; i < b.N; i++ {
src := largeSample[i%len(largeSample)]
msg := ParseFrame(src)
msg.Body.Position()
read += int64(len(src))
}
b.SetBytes(read / int64(b.N))
}