forked from prysmaticlabs/go-ssz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathround_trip_test.go
215 lines (201 loc) · 6.13 KB
/
round_trip_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package ssz_test
import (
"math/rand"
"reflect"
"testing"
ssz "github.com/prysmaticlabs/go-ssz"
)
type fork struct {
PreviousVersion [4]byte
CurrentVersion [4]byte
Epoch uint64
}
type nestedItem struct {
Field1 []uint64
Field2 *fork
Field3 [3]byte
}
type varItem struct {
Field2 []uint16
Field3 []uint16
}
type nestedVarItem struct {
Field1 []varItem
Field2 uint64
}
var (
forkExample = fork{
PreviousVersion: [4]byte{1, 2, 3, 4},
CurrentVersion: [4]byte{5, 6, 7, 8},
Epoch: 5,
}
nestedItemExample = nestedItem{
Field1: []uint64{1, 2, 3, 4},
Field2: &forkExample,
Field3: [3]byte{32, 33, 34},
}
nestedVarItemExample = nestedVarItem{
Field1: []varItem{},
Field2: 5,
}
varItemExample = varItem{
Field2: []uint16{},
Field3: []uint16{2, 3},
}
varItemAmbiguous = varItem{
Field3: []uint16{4, 5},
}
)
func TestMarshalUnmarshal(t *testing.T) {
tests := []struct {
input interface{}
ptr interface{}
}{
// Bool test cases.
{input: true, ptr: new(bool)},
{input: false, ptr: new(bool)},
// Uint8 test cases.
{input: byte(1), ptr: new(byte)},
{input: byte(0), ptr: new(byte)},
// Uint16 test cases.
{input: uint16(100), ptr: new(uint16)},
{input: uint16(232), ptr: new(uint16)},
// Int32 test cases.
{input: int32(1), ptr: new(int32)},
{input: int32(1029391), ptr: new(int32)},
// Uint32 test cases.
{input: uint32(1), ptr: new(uint32)},
{input: uint32(1029391), ptr: new(uint32)},
// Uint64 test cases.
{input: uint64(5), ptr: new(uint64)},
{input: uint64(23929309), ptr: new(uint64)},
// Byte slice, byte array test cases.
{input: [8]byte{1, 2, 3, 4, 5, 6, 7, 8}, ptr: new([8]byte)},
{input: []byte{9, 8, 9, 8}, ptr: new([]byte)},
// Basic type array test cases.
{input: [12]uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, ptr: new([12]uint64)},
{input: [100]bool{true, false, true, true}, ptr: new([100]bool)},
{input: [20]uint16{3, 4, 5}, ptr: new([20]uint16)},
{input: [20]uint32{4, 5}, ptr: new([20]uint32)},
{input: [20][2]uint32{{3, 4}, {5}, {8}, {9, 10}}, ptr: new([20][2]uint32)},
// Basic type slice test cases.
{input: []uint64{1, 2, 3}, ptr: new([]uint64)},
{input: []bool{true, false, true, true, true}, ptr: new([]bool)},
{input: []uint32{0, 0, 0}, ptr: new([]uint32)},
{input: []uint32{92939, 232, 222}, ptr: new([]uint32)},
// String test cases.
{input: "hello world", ptr: new(string)},
{input: nestedVarItemExample, ptr: new(nestedVarItem)},
{input: varItemExample, ptr: new(varItem)},
{input: varItemAmbiguous, ptr: new(varItem)},
// Non-basic type slice/array test cases.
{input: []fork{forkExample, forkExample}, ptr: new([]fork)},
{input: [][]uint64{{4, 3, 2}, {1}, {0}}, ptr: new([][]uint64)},
{input: [][][]uint64{{{1, 2}, {3}}, {{4, 5}}, {{0}}}, ptr: new([][][]uint64)},
{input: [][3]uint64{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, ptr: new([][3]uint64)},
{input: [3][]uint64{{1, 2}, {4, 5, 6}, {7}}, ptr: new([3][]uint64)},
{input: [][4]fork{{forkExample, forkExample, forkExample}}, ptr: new([][4]fork)},
{input: [2]fork{forkExample, forkExample}, ptr: new([2]fork)},
// Pointer-type test cases.
{input: &forkExample, ptr: new(fork)},
{input: &nestedItemExample, ptr: new(nestedItem)},
{input: []*fork{&forkExample, &forkExample}, ptr: new([]*fork)},
{input: []*nestedItem{&nestedItemExample, &nestedItemExample}, ptr: new([]*nestedItem)},
{input: [2]*nestedItem{&nestedItemExample, &nestedItemExample}, ptr: new([2]*nestedItem)},
{input: [2]*fork{&forkExample, &forkExample}, ptr: new([2]*fork)},
}
for _, tt := range tests {
if _, err := ssz.HashTreeRoot(tt.input); err != nil {
t.Fatal(err)
}
serializedItem, err := ssz.Marshal(tt.input)
if err != nil {
t.Fatal(err)
}
if err := ssz.Unmarshal(serializedItem, tt.ptr); err != nil {
t.Fatal(err)
}
output := reflect.ValueOf(tt.ptr)
inputVal := reflect.ValueOf(tt.input)
if inputVal.Kind() == reflect.Ptr {
if !ssz.DeepEqual(output.Interface(), tt.input) {
t.Errorf("Expected %v, received %v", tt.input, output.Interface())
}
} else {
got := output.Elem().Interface()
want := tt.input
if !ssz.DeepEqual(want, got) {
t.Errorf("Did not unmarshal properly: wanted %v, received %v", tt.input, output.Elem().Interface())
}
}
}
}
type UInt64InStruct struct {
UInt64 uint64
}
type allTypes struct {
Bool bool
UInt8 uint8
UInt16 uint16
UInt32 uint32
UInt64 uint64
Slice []byte
Array [100]byte
SubStruct UInt64InStruct
Pointer *UInt64InStruct
}
func randBytes(count int) []byte {
buf := make([]byte, count)
rand.Read(buf)
return buf
}
func generateData(count int) []allTypes {
data := make([]allTypes, count)
for i := 0; i < count; i++ {
data[i] = allTypes{
Bool: rand.Intn(2) == 1,
UInt8: uint8(rand.Intn(256)),
UInt16: uint16(rand.Intn(256 * 256)),
UInt32: rand.Uint32(),
UInt64: rand.Uint64(),
Slice: randBytes(rand.Intn(256)),
SubStruct: UInt64InStruct{UInt64: rand.Uint64()},
Pointer: &UInt64InStruct{UInt64: rand.Uint64()},
}
copy(data[i].Array[:], randBytes(len(data[i].Array)))
}
return data
}
func BenchmarkMarshal(b *testing.B) {
data := generateData(b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ssz.Marshal(data[i])
}
}
func BenchmarkUnmarshal(b *testing.B) {
data := generateData(b.N)
ser := make([][]byte, b.N)
for i, item := range data {
ser[i], _ = ssz.Marshal(item)
}
result := make([]allTypes, b.N)
b.ResetTimer()
for i, item := range ser {
ssz.Unmarshal(item, &result[i])
}
b.StopTimer()
// check Marshal/Unmarshal
for i := 0; i < b.N; i++ {
if data[i].Bool != result[i].Bool ||
data[i].UInt8 != result[i].UInt8 ||
data[i].UInt16 != result[i].UInt16 ||
data[i].UInt32 != result[i].UInt32 ||
data[i].UInt64 != result[i].UInt64 ||
(!reflect.DeepEqual(data[i].Slice, result[i].Slice) && !(len(data[i].Slice) == 0 && len(result[i].Slice) == 0)) ||
data[i].SubStruct.UInt64 != result[i].SubStruct.UInt64 ||
data[i].Pointer.UInt64 != result[i].Pointer.UInt64 {
b.Fatalf("incorrect marshal/unmarshal for\n%v\nser data:\n%v\nresult:\n%v\n(%v;%v)", data[i], ser[i], result[i], data[i].Slice, result[i].Slice)
}
}
}