forked from jbenet/test-ugorji-cbor
/
rt_test.go
67 lines (55 loc) · 1.08 KB
/
rt_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
package canonicaltest
import (
"bytes"
"encoding/json"
"testing"
cbor "github.com/whyrusleeping/cbor/go"
)
func RoundTripTest(t *testing.T, o1, o2 interface{}) bool {
var buf1 bytes.Buffer
var buf2 bytes.Buffer
var buf3 bytes.Buffer
enc1 := cbor.NewEncoder(&buf1)
enc2 := cbor.NewEncoder(&buf2)
dec := cbor.NewDecoder(&buf3)
if err := enc1.Encode(o1); err != nil {
t.Error(err)
return false
}
m1 := buf1.Bytes()
if _, err := buf3.Write(m1); err != nil {
t.Error(err)
return false
}
if err := dec.Decode(o2); err != nil {
t.Error(err)
return false
}
if err := enc2.Encode(o2); err != nil {
t.Error(err)
return false
}
m2 := buf2.Bytes()
if !bytes.Equal(m1, m2) {
t.Error("marshalled values not equal")
t.Log(m1)
t.Log(m2)
t.Log(string(m1))
t.Log(string(m2))
return false
}
return true
}
func OutputJSON(t *testing.T, thing interface{}) {
b, err := json.Marshal(thing)
if err != nil {
t.Error(err)
return
}
var buf bytes.Buffer
if err := json.Indent(&buf, b, "", "\t"); err != nil {
t.Error(err)
return
}
t.Log(buf.String())
}