github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/io/size_test.go (about)

     1  package io_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/nspcc-dev/neo-go/pkg/io"
     8  	"github.com/nspcc-dev/neo-go/pkg/util"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  // Mock structure to test getting size of an array of serializable things.
    13  type smthSerializable struct {
    14  	some [42]byte
    15  }
    16  
    17  func (*smthSerializable) DecodeBinary(*io.BinReader) {}
    18  
    19  func (ss *smthSerializable) EncodeBinary(bw *io.BinWriter) {
    20  	bw.WriteBytes(ss.some[:])
    21  }
    22  
    23  // Mock structure that gives error in EncodeBinary().
    24  type smthNotReallySerializable struct{}
    25  
    26  func (*smthNotReallySerializable) DecodeBinary(*io.BinReader) {}
    27  
    28  func (*smthNotReallySerializable) EncodeBinary(bw *io.BinWriter) {
    29  	bw.Err = fmt.Errorf("smth bad happened in smthNotReallySerializable")
    30  }
    31  
    32  func TestVarSize(t *testing.T) {
    33  	testCases := []struct {
    34  		variable any
    35  		name     string
    36  		expected int
    37  	}{
    38  		{
    39  			252,
    40  			"test_int_1",
    41  			1,
    42  		},
    43  		{
    44  			253,
    45  			"test_int_2",
    46  			3,
    47  		},
    48  		{
    49  			65535,
    50  			"test_int_3",
    51  			3,
    52  		},
    53  		{
    54  			65536,
    55  			"test_int_4",
    56  			5,
    57  		},
    58  		{
    59  			4294967295,
    60  			"test_int_5",
    61  			5,
    62  		},
    63  		{
    64  			uint(252),
    65  			"test_uint_1",
    66  			1,
    67  		},
    68  		{
    69  			uint(253),
    70  			"test_uint_2",
    71  			3,
    72  		},
    73  		{
    74  			uint(65535),
    75  			"test_uint_3",
    76  			3,
    77  		},
    78  		{
    79  			uint(65536),
    80  			"test_uint_4",
    81  			5,
    82  		},
    83  		{
    84  			uint(4294967295),
    85  			"test_uint_5",
    86  			5,
    87  		},
    88  		{
    89  			[]byte{1, 2, 4, 5, 6},
    90  			"test_[]byte_1",
    91  			6,
    92  		},
    93  		{
    94  			// The neo C# implementation doe not allowed this!
    95  			util.Uint160{1, 2, 4, 5, 6},
    96  			"test_Uint160_1",
    97  			21,
    98  		},
    99  
   100  		{[20]uint8{1, 2, 3, 4, 5, 6},
   101  			"test_uint8_1",
   102  			21,
   103  		},
   104  		{[20]uint8{1, 2, 3, 4, 5, 6, 8, 9},
   105  			"test_uint8_2",
   106  			21,
   107  		},
   108  
   109  		{[32]uint8{1, 2, 3, 4, 5, 6},
   110  			"test_uint8_3",
   111  			33,
   112  		},
   113  		{[10]uint16{1, 2, 3, 4, 5, 6},
   114  			"test_uint16_1",
   115  			21,
   116  		},
   117  
   118  		{[10]uint16{1, 2, 3, 4, 5, 6, 10, 21},
   119  			"test_uint16_2",
   120  			21,
   121  		},
   122  		{[30]uint32{1, 2, 3, 4, 5, 6, 10, 21},
   123  			"test_uint32_2",
   124  			121,
   125  		},
   126  		{[30]uint64{1, 2, 3, 4, 5, 6, 10, 21},
   127  			"test_uint64_2",
   128  			241,
   129  		},
   130  		{[20]int8{1, 2, 3, 4, 5, 6},
   131  			"test_int8_1",
   132  			21,
   133  		},
   134  		{[20]int8{-1, 2, 3, 4, 5, 6, 8, 9},
   135  			"test_int8_2",
   136  			21,
   137  		},
   138  
   139  		{[32]int8{-1, 2, 3, 4, 5, 6},
   140  			"test_int8_3",
   141  			33,
   142  		},
   143  		{[10]int16{-1, 2, 3, 4, 5, 6},
   144  			"test_int16_1",
   145  			21,
   146  		},
   147  
   148  		{[10]int16{-1, 2, 3, 4, 5, 6, 10, 21},
   149  			"test_int16_2",
   150  			21,
   151  		},
   152  		{[30]int32{-1, 2, 3, 4, 5, 6, 10, 21},
   153  			"test_int32_2",
   154  			121,
   155  		},
   156  		{[30]int64{-1, 2, 3, 4, 5, 6, 10, 21},
   157  			"test_int64_2",
   158  			241,
   159  		},
   160  		// The neo C# implementation doe not allowed this!
   161  		{util.Uint256{1, 2, 3, 4, 5, 6},
   162  			"test_Uint256_1",
   163  			33,
   164  		},
   165  
   166  		{"abc",
   167  			"test_string_1",
   168  			4,
   169  		},
   170  		{"abcĂ ",
   171  			"test_string_2",
   172  			6,
   173  		},
   174  		{"2d3b96ae1bcc5a585e075e3b81920210dec16302",
   175  			"test_string_3",
   176  			41,
   177  		},
   178  		{[]*smthSerializable{{}, {}},
   179  			"test_Serializable",
   180  			2*42 + 1,
   181  		},
   182  	}
   183  
   184  	for _, tc := range testCases {
   185  		t.Run(fmt.Sprintf("run: %s", tc.name), func(t *testing.T) {
   186  			result := io.GetVarSize(tc.variable)
   187  			assert.Equal(t, tc.expected, result)
   188  		})
   189  	}
   190  }
   191  
   192  func panicVarSize(t *testing.T, v any) {
   193  	defer func() {
   194  		r := recover()
   195  		assert.NotNil(t, r)
   196  	}()
   197  
   198  	_ = io.GetVarSize(v)
   199  	// this should never execute
   200  	assert.Nil(t, t)
   201  }
   202  
   203  func TestVarSizePanic(t *testing.T) {
   204  	panicVarSize(t, t)
   205  	panicVarSize(t, struct{}{})
   206  	panicVarSize(t, &smthNotReallySerializable{})
   207  }