forked from TrueCloudLab/neoneo-go
util: add some tests to make GetVarSize() fully covered
This commit is contained in:
parent
7e8dc9ad66
commit
8c448ab890
2 changed files with 56 additions and 1 deletions
|
@ -43,7 +43,7 @@ func GetVarStringSize(value string) int {
|
||||||
|
|
||||||
// GetVarSize return the size om bytes of a variable. This implementation is not exactly like the C#
|
// GetVarSize return the size om bytes of a variable. This implementation is not exactly like the C#
|
||||||
// (reference: GetVarSize<T>(this T[] value), https://github.com/neo-project/neo/blob/master/neo/IO/Helper.cs#L53) as in the C# variable
|
// (reference: GetVarSize<T>(this T[] value), https://github.com/neo-project/neo/blob/master/neo/IO/Helper.cs#L53) as in the C# variable
|
||||||
// like Uint160, Uint256 are not supported. @TODO: make sure to have full unit tests coverage.
|
// like Uint160, Uint256 are not supported.
|
||||||
func GetVarSize(value interface{}) int {
|
func GetVarSize(value interface{}) int {
|
||||||
v := reflect.ValueOf(value)
|
v := reflect.ValueOf(value)
|
||||||
switch v.Kind() {
|
switch v.Kind() {
|
||||||
|
|
|
@ -2,11 +2,28 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Mock structure to test getting size of an array of serializable things
|
||||||
|
type smthSerializable struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*smthSerializable) DecodeBinary(io.Reader) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*smthSerializable) EncodeBinary(io.Writer) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*smthSerializable) Size() int {
|
||||||
|
return 42
|
||||||
|
}
|
||||||
|
|
||||||
func TestVarSize(t *testing.T) {
|
func TestVarSize(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
variable interface{}
|
variable interface{}
|
||||||
|
@ -38,6 +55,31 @@ func TestVarSize(t *testing.T) {
|
||||||
"test_int_5",
|
"test_int_5",
|
||||||
5,
|
5,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
uint(252),
|
||||||
|
"test_uint_1",
|
||||||
|
1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
uint(253),
|
||||||
|
"test_uint_2",
|
||||||
|
3,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
uint(65535),
|
||||||
|
"test_uint_3",
|
||||||
|
3,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
uint(65536),
|
||||||
|
"test_uint_4",
|
||||||
|
5,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
uint(4294967295),
|
||||||
|
"test_uint_5",
|
||||||
|
5,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
[]byte{1, 2, 4, 5, 6},
|
[]byte{1, 2, 4, 5, 6},
|
||||||
"test_[]byte_1",
|
"test_[]byte_1",
|
||||||
|
@ -128,6 +170,10 @@ func TestVarSize(t *testing.T) {
|
||||||
"test_string_3",
|
"test_string_3",
|
||||||
41,
|
41,
|
||||||
},
|
},
|
||||||
|
{[]*smthSerializable{&smthSerializable{}, &smthSerializable{}},
|
||||||
|
"test_Serializable",
|
||||||
|
2 * 42 + 1,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
@ -137,3 +183,12 @@ func TestVarSize(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestVarSizePanic(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
r := recover()
|
||||||
|
assert.NotNil(t, r)
|
||||||
|
}()
|
||||||
|
|
||||||
|
_ = GetVarSize(t)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue