github.com/iotexproject/iotex-core@v1.14.1-rc1/state/candidate_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  	"sort"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/iotexproject/go-pkgs/hash"
    16  	"github.com/iotexproject/iotex-address/address"
    17  	"github.com/iotexproject/iotex-core/test/identityset"
    18  )
    19  
    20  func TestCandidateEqual(t *testing.T) {
    21  	r := require.New(t)
    22  	testCases := []struct {
    23  		cands []*Candidate
    24  		flag  bool
    25  	}{
    26  		{
    27  			[]*Candidate{
    28  				{
    29  					Address: identityset.Address(29).String(),
    30  					Votes:   big.NewInt(2),
    31  				},
    32  				{
    33  					Address: identityset.Address(29).String(),
    34  					Votes:   big.NewInt(2),
    35  				},
    36  			},
    37  			true,
    38  		},
    39  		{
    40  			[]*Candidate{
    41  				{
    42  					Address: identityset.Address(29).String(),
    43  					Votes:   big.NewInt(2),
    44  				},
    45  				{
    46  					Address: identityset.Address(29).String(),
    47  					Votes:   big.NewInt(1),
    48  				},
    49  			},
    50  			false,
    51  		},
    52  	}
    53  	for _, c := range testCases {
    54  		r.Equal(c.flag, c.cands[0].Equal(c.cands[1]))
    55  	}
    56  }
    57  
    58  func TestCandidateClone(t *testing.T) {
    59  	r := require.New(t)
    60  	cand1 := &Candidate{
    61  		Address: identityset.Address(29).String(),
    62  		Votes:   big.NewInt(2),
    63  	}
    64  	r.True(cand1.Equal(cand1.Clone()))
    65  }
    66  
    67  func TestCandidateListSerializeAndDeserialize(t *testing.T) {
    68  	r := require.New(t)
    69  	list1 := CandidateList{
    70  		&Candidate{
    71  			Address: identityset.Address(2).String(),
    72  			Votes:   big.NewInt(2),
    73  		},
    74  	}
    75  	sbytes, err := list1.Serialize()
    76  	r.NoError(err)
    77  
    78  	list2 := CandidateList{}
    79  	err = list2.Deserialize(sbytes)
    80  	r.NoError(err)
    81  	r.Equal(list1.Len(), list2.Len())
    82  	for i, c := range list2 {
    83  		r.True(c.Equal(list1[i]))
    84  	}
    85  }
    86  
    87  func TestCandidate(t *testing.T) {
    88  	require := require.New(t)
    89  
    90  	cand1 := &Candidate{
    91  		Address: identityset.Address(28).String(),
    92  		Votes:   big.NewInt(1),
    93  	}
    94  	cand2 := &Candidate{
    95  		Address: identityset.Address(29).String(),
    96  		Votes:   big.NewInt(2),
    97  	}
    98  	cand3 := &Candidate{
    99  		Address: identityset.Address(30).String(),
   100  		Votes:   big.NewInt(3),
   101  	}
   102  
   103  	cand1Addr, err := address.FromString(cand1.Address)
   104  	require.NoError(err)
   105  	cand1Hash := hash.BytesToHash160(cand1Addr.Bytes())
   106  
   107  	cand2Addr, err := address.FromString(cand2.Address)
   108  	require.NoError(err)
   109  	cand2Hash := hash.BytesToHash160(cand2Addr.Bytes())
   110  
   111  	cand3Addr, err := address.FromString(cand3.Address)
   112  	require.NoError(err)
   113  	cand3Hash := hash.BytesToHash160(cand3Addr.Bytes())
   114  
   115  	candidateMap := make(CandidateMap)
   116  	candidateMap[cand1Hash] = cand1
   117  	candidateMap[cand2Hash] = cand2
   118  	candidateMap[cand3Hash] = cand3
   119  
   120  	candidateList, err := MapToCandidates(candidateMap)
   121  	require.NoError(err)
   122  	require.Equal(3, len(candidateList))
   123  	sort.Sort(candidateList)
   124  
   125  	require.Equal(identityset.Address(30).String(), candidateList[0].Address)
   126  	require.Equal(identityset.Address(29).String(), candidateList[1].Address)
   127  	require.Equal(identityset.Address(28).String(), candidateList[2].Address)
   128  
   129  	candidatesBytes, err := candidateList.Serialize()
   130  	require.NoError(err)
   131  	var candidates CandidateList
   132  	require.NoError(candidates.Deserialize(candidatesBytes))
   133  	require.Equal(3, len(candidates))
   134  	require.Equal(identityset.Address(30).String(), candidates[0].Address)
   135  	require.Equal(identityset.Address(29).String(), candidates[1].Address)
   136  	require.Equal(identityset.Address(28).String(), candidates[2].Address)
   137  
   138  	candidateMap, err = CandidatesToMap(candidateList)
   139  	require.NoError(err)
   140  	require.Equal(3, len(candidateMap))
   141  	require.Equal(uint64(1), candidateMap[cand1Hash].Votes.Uint64())
   142  	require.Equal(uint64(2), candidateMap[cand2Hash].Votes.Uint64())
   143  	require.Equal(uint64(3), candidateMap[cand3Hash].Votes.Uint64())
   144  }