-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshalTester.go
88 lines (69 loc) · 1.8 KB
/
marshalTester.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
package testHelper
import (
"testing"
"bytes"
"github.com/DCNT-developer/dcnt/common/interfaces"
"github.com/DCNT-developer/dcnt/common/primitives/random"
)
// TestMarshaling will test a marshing and unmarshal operation. Do the comparison of equality yourself. Extrabytes adds a random number
// of bytes up to extrabytes in length
func TestMarshaling(a interfaces.BinaryMarshallable, b interfaces.BinaryMarshallable, extraBytes int, t *testing.T) {
data, err := a.MarshalBinary()
if err != nil {
t.Error(err)
}
extraData := random.RandByteSliceOfLen(extraBytes)
extraBytes = len(extraData)
extraData = append(data, extraData...)
nd, err := b.UnmarshalBinaryData(extraData)
if err != nil {
t.Error(err)
}
if len(nd) != extraBytes {
t.Errorf("Expect %d bytes remain, but %d do", extraBytes, len(nd))
t.FailNow()
}
data2, err := b.MarshalBinary()
if err != nil {
t.Error(err)
}
if bytes.Compare(data, data2) != 0 {
t.Errorf("Marshaling for a and b are not the same: \n %x\n %x", data, data2)
}
}
func TestABlockEntryFunctions(a interfaces.IABEntry, b interfaces.IABEntry, t *testing.T) {
as, err := a.JSONString()
if err != nil {
t.Error(err)
}
bs, err := b.JSONString()
if err != nil {
t.Error(err)
}
if as != bs {
t.Errorf("JSONString() does not match")
}
ab, err := a.JSONByte()
if err != nil {
t.Error(err)
}
bb, err := b.JSONByte()
if err != nil {
t.Error(err)
}
if bytes.Compare(ab, bb) != 0 {
t.Errorf("JSONByte() does not match")
}
if a.Interpret() != b.Interpret() {
t.Errorf("Interpret() does not match")
}
if a.IsInterpretable() != b.IsInterpretable() {
t.Errorf("IsInterpretable() does not match")
}
if a.String() != b.String() {
t.Errorf("String() does not match")
}
if !a.Hash().IsSameAs(b.Hash()) {
t.Errorf("Hash() does not match")
}
}