-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
52 lines (45 loc) · 1.13 KB
/
json_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
package senml
import "testing"
func TestEncodeJSONExamples(t *testing.T) {
for n, example := range testVectors {
b, err := EncodeJSON(example.Result)
if err != nil {
t.Errorf("Error encoding %s: %s", n, err)
return
}
t.Logf("Result for %s (%v bytes): %s", n, len(b), b)
}
}
func TestExamplesDecodeJSON(t *testing.T) {
AutoTime = false
for n, example := range testVectors {
res, err := DecodeJSON([]byte(example.JSON))
if err != nil {
t.Errorf("Decode error in example %s: %s", n, err)
continue
}
if !equal(res, example.Result) {
t.Errorf("Decode for example %s incorrect, got:\n%s\nexpected:\n%s", n, toString(res), toString(example.Result))
}
}
}
func BenchmarkEncodeJSON(b *testing.B) {
v := "Multiple Measurements"
ms := testVectors[v].Result
for n := 0; n < b.N; n++ {
_, err := EncodeJSON(ms)
if err != nil {
b.Fatalf("Error encoding %s: %s", v, err)
}
}
}
func BenchmarkDecodeJSON(b *testing.B) {
v := "Multiple Data Points 2"
t := []byte(testVectors[v].JSON)
for n := 0; n < b.N; n++ {
_, err := DecodeJSON(t)
if err != nil {
b.Fatalf("Error encoding %s: %s", v, err)
}
}
}