prysm-pulse/sharding/utils/marshal_test.go
nisdas 48977ba2f4 sharding: Adding comments to functions(#92)
Former-commit-id: 5b0e344d84b8d1ff8d12205376c9ae8715c7089e [formerly fe3697b50f74dc0b7fe43139c2d03744dfa32e80]
Former-commit-id: dfd0123b09906dae10bf4be245c5119ce958a236
2018-05-17 06:30:18 +08:00

94 lines
2.1 KiB
Go

package utils
import (
"math/rand"
"reflect"
"testing"
)
func buildrawblob(size int64) []RawBlob {
tempbody := make([]RawBlob, size)
for i := int64(0); i < size; i++ {
var rawblob RawBlob
rawblob.data = buildblob(size)
flagset := byte(rand.Int()) >> 7
if flagset == byte(1) {
rawblob.flags.skipEvmExecution = true
}
tempbody[i] = rawblob
}
return tempbody
}
func buildblob(size int64) []byte {
tempbody := make([]byte, size)
for i := int64(0); i < size; i++ {
tempbody[i] = byte(rand.Int())
}
return tempbody
}
func TestSize(t *testing.T) {
for i := 0; i < 300; i++ {
size := int64(i)
blob := buildrawblob(size)
chunksafterSerialize := size / chunkDataSize
terminalchunk := size % chunkDataSize
if terminalchunk != 0 {
chunksafterSerialize = chunksafterSerialize + 1
}
chunksafterSerialize = chunksafterSerialize * size
sizeafterSerialize := chunksafterSerialize * chunkSize
drefbody := make([]*RawBlob, len(blob))
for s := 0; s < len(blob); s++ {
drefbody[s] = &(blob[s])
}
serializedblob, err := Serialize(drefbody)
if err != nil {
t.Errorf("Error Serializing blob:%v\n %v", err, serializedblob)
}
if int64(len(serializedblob)) != sizeafterSerialize {
t.Errorf("Error Serializing blobs the lengths are not the same:\n %d \n %d", int64(len(serializedblob)), sizeafterSerialize)
}
}
}
func TestSerializeAndDeserializeblob(t *testing.T) {
for i := 1; i < 300; i++ {
blob := buildrawblob(int64(i))
drefbody := make([]*RawBlob, len(blob))
for s := 0; s < len(blob); s++ {
drefbody[s] = &(blob[s])
}
serializedblob, err := Serialize(drefbody)
if err != nil {
t.Errorf("Error Serializing blob at index %d:\n%v\n%v", i, err, serializedblob)
}
raw, err2 := Deserialize(serializedblob)
if err2 != nil {
t.Errorf("Error Serializing blob at index %d:\n%v due to \n%v", i, raw, err2)
}
if !reflect.DeepEqual(blob, raw) {
t.Errorf("Error Serializing blobs at index %d, the serialized and deserialized versions are not the same:\n\n %v \n\n %v \n\n %v", i, blob, serializedblob, raw)
}
}
}