-
Notifications
You must be signed in to change notification settings - Fork 42
/
writer_test.go
162 lines (138 loc) · 4.01 KB
/
writer_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
package fit_test
import (
"bytes"
"encoding/binary"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/tormoder/fit"
)
func TestDecodeEncodeDecode(t *testing.T) {
goVersion := runtime.Version()
goVersionOK := strings.HasPrefix(goVersion, goMajorVersionForDecodeGolden)
if !goVersionOK {
t.Skipf(
"skipping round trip test due to Go version (enabled for %s.x, have %q)",
goMajorVersionForDecodeGolden,
goVersion,
)
}
t.Run("Group", func(t *testing.T) {
for i, file := range decodeTestFiles {
_, file := i, file // Capture range variables.
if file.wantErr || file.fingerprint == 0 || file.skipEncode {
continue
}
t.Run(fmt.Sprintf("%s/%s", file.folder, file.name), func(t *testing.T) {
t.Parallel()
fpath := filepath.Join(tdfolder, file.folder, file.name)
inData, err := os.ReadFile(fpath)
if err != nil {
t.Fatalf("reading file failed: %v", err)
}
inFile, err := fit.Decode(bytes.NewReader(inData))
if err != nil {
t.Fatalf("decode: got error, want none; error is: %v", err)
}
// Sanity check that decoding is OK
fp := fitFingerprint(inFile)
if fp != file.fingerprint {
t.Fatalf("decode: fit file fingerprint differs: got: %d, want: %d", fp, file.fingerprint)
}
outBuf := &bytes.Buffer{}
err = fit.Encode(outBuf, inFile, binary.LittleEndian)
if err != nil {
t.Fatalf("encode: got error, want none; error is: %v", err)
}
reFile, err := fit.Decode(bytes.NewReader(outBuf.Bytes()))
if err != nil {
t.Fatalf("re-decode: got error, want none; error is: %v", err)
}
// Wipe the CRCs. The serialized data is likely to be different,
// so we need to ignore the CRCs.
inFile.CRC = 0
inFile.Header.CRC = 0
reFile.CRC = 0
reFile.Header.CRC = 0
// Re-fingerprint without the CRCs
fp = fitFingerprint(inFile)
refp := fitFingerprint(reFile)
if refp != fp {
t.Errorf("re-decode: fit file fingerprint differs: got: %d, want: %d", refp, fp)
fpath = fpath + currentSuffix
err = fitUtterDump(reFile, fpath, false)
if err != nil {
t.Fatalf("error writing output: %v", err)
}
t.Logf("current output written to: %s", fpath)
fpath = fpath + ".in"
err = fitUtterDump(inFile, fpath, false)
if err != nil {
t.Fatalf("error writing output: %v", err)
}
t.Logf("current input written to: %s", fpath)
}
})
}
})
}
func TestSimpleEncode(t *testing.T) {
h := fit.NewHeader(fit.V20, false)
file, err := fit.NewFile(fit.FileTypeActivity, h)
if err != nil {
t.Fatalf("new file: got error, want none; error is: %v", err)
}
act, err := file.Activity()
if err != nil {
t.Fatalf("activity: got error, want none; error is: %v", err)
}
ev := fit.NewEventMsg()
ev.Timestamp = time.Now()
ev.Event = fit.EventTimer
ev.EventType = fit.EventTypeStart
act.Events = append(act.Events, ev)
outBuf := &bytes.Buffer{}
err = fit.Encode(outBuf, file, binary.LittleEndian)
if err != nil {
t.Fatalf("encode: got error, want none; error is: %v", err)
}
_, err = fit.Decode(bytes.NewReader(outBuf.Bytes()))
if err != nil {
t.Fatalf("decode: got error, want none; error is: %v", err)
}
}
func BenchmarkEncode(b *testing.B) {
files := []struct {
desc, path string
}{
{"ActivitySmall", activitySmallPath},
{"ActivityLarge", activityLargePath},
{"ActivityWithComponents", activityComponentsPath},
{"MonitoringFile", monitoringPath},
}
for _, file := range files {
b.Run(file.desc, func(b *testing.B) {
data, err := os.ReadFile(file.path)
if err != nil {
b.Fatalf("%q: error reading file: %v", file.path, err)
}
fitFile, err := fit.Decode(bytes.NewReader(data))
if err != nil {
b.Fatalf("%q: error decoding file: %v", file.path, err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var buf bytes.Buffer
err := fit.Encode(&buf, fitFile, binary.LittleEndian)
if err != nil {
b.Fatalf("%q: error encoding file: %v", file.path, err)
}
}
})
}
}