-
Notifications
You must be signed in to change notification settings - Fork 19
/
fast_parse_test.go
68 lines (58 loc) · 1.58 KB
/
fast_parse_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
package ais
import (
"bufio"
"fmt"
"os"
"reflect"
"testing"
)
func TestPositionParse(t *testing.T) {
fastParse := CodecNewFast(false, false, true)
/* Convenience conversion disabled to avoid float inaccuracies */
fastParse.FloatWithoutConversion = true
slowParse := CodecNewFast(false, false, false)
slowParse.FloatWithoutConversion = true
for msgID := 1; msgID <= 27; msgID++ {
msgFile := fmt.Sprintf("testmsg/%d.msg", msgID)
t.Logf("Testing message file %s", msgFile)
f, err := os.Open(msgFile)
if err != nil {
t.Error("Failed to open file", msgID)
return
}
r := bufio.NewReader(f)
for index := 0; true; index++ {
line, err := r.ReadString('\n')
if err != nil {
break
}
line = line[:len(line)-2]
source := []byte(line)
/* Convert ascii '0' and '1' to real 0 and 1 */
for i := 0; i < len(source); i++ {
source[i] -= '0'
}
/* Decode the packet */
decoded := fastParse.DecodePacket(source)
if decoded == nil {
/* Failed to decode... */
t.Error("Could not decode with fastParse", msgID, index)
return
}
decodedSlow := slowParse.DecodePacket(source)
if decodedSlow == nil {
/* Failed to decode... */
t.Error("Could not decode with reflection", msgID, index)
return
}
if !reflect.DeepEqual(decoded, decodedSlow) {
t.Error("Decoded messages are not equal", msgID, index)
t.Log("FastParse/slowparse:\n", decoded, "\n", decodedSlow)
t.Logf("error on file msgID %d at index %d", msgID, index)
}
}
f.Close()
// also run another the encoding/decoding
tryFile(t, fastParse, msgID)
}
}