github.com/iotexproject/iotex-core@v1.14.1-rc1/state/state_test.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package state
     7  
     8  import (
     9  	"math/big"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/iotexproject/iotex-core/test/identityset"
    15  )
    16  
    17  func TestSerializeAndDeserialize(t *testing.T) {
    18  	r := require.New(t)
    19  
    20  	s1 := struct{}{}
    21  	r.Panics(func() { Serialize(&s1) }, "data holder doesn't implement Serializer interface!")
    22  	r.Panics(func() { Deserialize(s1, []byte{0xa, 0x2e}) }, "data holder doesn't implement Deserializer interface!")
    23  
    24  	list1 := CandidateList{
    25  		&Candidate{
    26  			Address: identityset.Address(2).String(),
    27  			Votes:   big.NewInt(2),
    28  		},
    29  	}
    30  	bytes, err := Serialize(&list1)
    31  	r.NoError(err)
    32  
    33  	var list2 CandidateList
    34  	err = Deserialize(&list2, bytes)
    35  	r.NoError(err)
    36  	r.Equal(list2.Len(), list1.Len())
    37  	for i, c := range list2 {
    38  		r.True(c.Equal(list1[i]))
    39  	}
    40  }