prysm-pulse/sharding/utils/marshal_test.go
nisdas 52cd2b04ed sharding/utils: Changing from interfaces to rawblobs(#92)
Former-commit-id: 5901594a02b82d0cd11e85593f7021d0cb0a6d62 [formerly dd57899b147bfb258557c20917b971338bc79f84]
Former-commit-id: 047157dcdaf452f1e9d329684dd632c4a44c8af6
2018-05-17 06:25:44 +08:00

94 lines
2.0 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
}
/*
Might be required in the future for part 2 of serialization
func TestConvertInterface(t *testing.T) {
slice := []interface{}{0, 1, 2, 3, 4, 5}
convertedValue, err := ConvertInterface(slice, reflect.Slice)
if err != nil {
t.Fatalf("Error: %v %v", err, convertedValue)
}
} */
func TestSize(t *testing.T) {
size := int64(800)
blob := buildrawblob(size)
chunksafterSerialize := size / chunkDataSize
terminalchunk := size % chunkDataSize
if terminalchunk != 0 {
chunksafterSerialize = chunksafterSerialize + 1
}
chunksafterSerialize = chunksafterSerialize * size
sizeafterSerialize := chunksafterSerialize * chunkSize
serializedblob, err := Serialize(blob)
if err != nil {
t.Fatalf("Error Serializing blob:%v %v", err, serializedblob)
}
if int64(len(serializedblob)) != sizeafterSerialize {
t.Fatalf("Error Serializing blobs the lengths are not the same: %v , %v", int64(len(serializedblob)), sizeafterSerialize)
}
}
func TestSerializeAndDeserializeblob(t *testing.T) {
var testbody interface{}
blob := buildrawblob(10)
serializedblob, err := Serialize(blob)
if err != nil {
t.Fatalf("Error Serializing blob:%v %v", err, serializedblob)
}
raw, err2 := Deserialize(serializedblob)
if err2 != nil {
t.Fatalf("Error Serializing blob:%v due to %v", raw, err2)
}
if !reflect.DeepEqual(blob, testbody) {
t.Fatalf("Error Serializing blobs, the serialized and deserialized versions are not the same: %v ------%v ------ %v", blob, serializedblob, testbody)
}
}