2018-05-08 14:03:49 +08:00
package utils
import (
"math/rand"
"reflect"
"testing"
)
2018-05-16 11:07:31 +08:00
func buildRawBlob ( size int64 ) [ ] RawBlob {
2018-05-14 18:13:45 +08:00
tempbody := make ( [ ] RawBlob , size )
2018-05-08 14:03:49 +08:00
for i := int64 ( 0 ) ; i < size ; i ++ {
2018-05-14 18:13:45 +08:00
var rawblob RawBlob
2018-05-16 11:07:31 +08:00
rawblob . data = buildBlob ( size )
2018-05-14 18:13:45 +08:00
flagset := byte ( rand . Int ( ) ) >> 7
if flagset == byte ( 1 ) {
rawblob . flags . skipEvmExecution = true
}
tempbody [ i ] = rawblob
2018-05-08 14:03:49 +08:00
}
2018-05-14 18:13:45 +08:00
2018-05-16 13:38:26 -04:00
return tempbody
2018-05-08 14:03:49 +08:00
}
2018-05-16 11:07:31 +08:00
func buildBlob ( size int64 ) [ ] byte {
2018-05-14 18:13:45 +08:00
tempbody := make ( [ ] byte , size )
2018-05-08 14:03:49 +08:00
for i := int64 ( 0 ) ; i < size ; i ++ {
tempbody [ i ] = byte ( rand . Int ( ) )
}
2018-05-16 13:38:26 -04:00
return tempbody
2018-05-08 14:03:49 +08:00
}
2018-05-14 18:13:45 +08:00
2018-05-08 14:03:49 +08:00
func TestSize ( t * testing . T ) {
2018-05-14 20:03:18 +08:00
for i := 0 ; i < 300 ; i ++ {
size := int64 ( i )
2018-05-16 11:07:31 +08:00
blob := buildRawBlob ( size )
2018-05-14 20:03:18 +08:00
chunksafterSerialize := size / chunkDataSize
terminalchunk := size % chunkDataSize
if terminalchunk != 0 {
chunksafterSerialize = chunksafterSerialize + 1
}
chunksafterSerialize = chunksafterSerialize * size
sizeafterSerialize := chunksafterSerialize * chunkSize
2018-05-15 22:15:33 +08:00
drefbody := make ( [ ] * RawBlob , len ( blob ) )
for s := 0 ; s < len ( blob ) ; s ++ {
drefbody [ s ] = & ( blob [ s ] )
}
serializedblob , err := Serialize ( drefbody )
2018-05-14 20:03:18 +08:00
if err != nil {
t . Errorf ( "Error Serializing blob:%v\n %v" , err , serializedblob )
}
2018-05-08 14:03:49 +08:00
2018-05-14 20:03:18 +08:00
if int64 ( len ( serializedblob ) ) != sizeafterSerialize {
t . Errorf ( "Error Serializing blobs the lengths are not the same:\n %d \n %d" , int64 ( len ( serializedblob ) ) , sizeafterSerialize )
}
2018-05-08 14:03:49 +08:00
}
}
func TestSerializeAndDeserializeblob ( t * testing . T ) {
2018-05-14 20:03:18 +08:00
for i := 1 ; i < 300 ; i ++ {
2018-05-08 14:03:49 +08:00
2018-05-16 11:07:31 +08:00
blob := buildRawBlob ( int64 ( i ) )
2018-05-08 14:03:49 +08:00
2018-05-15 22:15:33 +08:00
drefbody := make ( [ ] * RawBlob , len ( blob ) )
for s := 0 ; s < len ( blob ) ; s ++ {
drefbody [ s ] = & ( blob [ s ] )
}
serializedblob , err := Serialize ( drefbody )
2018-05-14 20:03:18 +08:00
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 )
}
2018-05-08 14:03:49 +08:00
2018-05-14 20:03:18 +08:00
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 )
}
2018-05-08 14:03:49 +08:00
}
}