-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpage_test.go
79 lines (73 loc) · 1.64 KB
/
page_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
package dexcom
import (
"fmt"
"os"
"testing"
)
type pageTestCase struct {
pageType PageType
pageNumber int
alternative int
}
func TestPage(t *testing.T) {
cases := []pageTestCase{
{ManufacturingData, 0, 0},
{SensorData, 469, 0},
{EGVData, 312, 0},
{CalibrationData, 1432, 0},
// The one record in this page has CRC = 63 FF, followed by FF bytes for padding.
{CalibrationData, 1432, 1},
// These pages contain 148-byte rev 2 calibration records.
{CalibrationData, 252, 0},
{CalibrationData, 845, 0},
}
for _, c := range cases {
t.Run(c.pageType.String(), func(t *testing.T) {
pageTest(t, c)
})
}
}
func pageTest(t *testing.T, c pageTestCase) {
testFile := testFileName(c)
f, err := os.Open(testFile + ".data")
if err != nil {
t.Error(err)
return
}
data, err := readBytes(f)
_ = f.Close()
if err != nil {
t.Error(err)
return
}
page, err := UnmarshalPage(data)
if err != nil {
t.Error(err)
return
}
if page.Type != c.pageType {
panic("page type mismatch")
}
if page.Number != c.pageNumber {
panic("page number mismatch")
}
decoded, err := UnmarshalRecords(c.pageType, page.Records)
if err != nil {
t.Errorf("UnmarshalRecords(%v, % X) returned %v", c.pageType, page.Records, err)
return
}
checkRecords(t, decoded, testFile+".json")
}
func testFileName(c pageTestCase) string {
s := fmt.Sprintf("%s/%d.%d", testDataDir, c.pageType, c.pageNumber)
if c.alternative != 0 {
s += fmt.Sprintf("-%d", c.alternative)
}
return s
}
func checkRecords(t *testing.T, decoded Records, jsonFile string) {
eq, msg := compareDataToJSON(decoded, jsonFile)
if !eq {
t.Errorf("JSON is different:\n%s\n", msg)
}
}