github.com/iotexproject/iotex-core@v1.14.1-rc1/state/iterator_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 TestIterator(t *testing.T) {
    18  	r := require.New(t)
    19  
    20  	cands := CandidateList{
    21  		&Candidate{
    22  			Address: identityset.Address(4).String(),
    23  			Votes:   big.NewInt(4),
    24  		},
    25  		&Candidate{
    26  			Address: identityset.Address(3).String(),
    27  			Votes:   big.NewInt(3),
    28  		},
    29  		&Candidate{
    30  			Address: identityset.Address(2).String(),
    31  			Votes:   big.NewInt(2),
    32  		},
    33  		&Candidate{
    34  			Address: identityset.Address(1).String(),
    35  			Votes:   big.NewInt(1),
    36  		},
    37  	}
    38  
    39  	states := make([][]byte, len(cands))
    40  	for i, cand := range cands {
    41  		bytes, err := cand.Serialize()
    42  		r.NoError(err)
    43  		states[i] = bytes
    44  	}
    45  
    46  	iter := NewIterator(states)
    47  	r.Equal(iter.Size(), len(states))
    48  
    49  	for _, cand := range cands {
    50  		c := &Candidate{}
    51  		err := iter.Next(c)
    52  		r.NoError(err)
    53  
    54  		r.True(c.Equal(cand))
    55  	}
    56  
    57  	var noneExistCand Candidate
    58  	r.Equal(iter.Next(&noneExistCand), ErrOutOfBoundary)
    59  }