github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/vote/unproductivedelegate_test.go (about)

     1  // Copyright (c) 2020 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 vote
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestUnproductiveDelegate(t *testing.T) {
    15  	r := require.New(t)
    16  	upd, err := NewUnproductiveDelegate(2, 10)
    17  	r.NoError(err)
    18  
    19  	str1 := []string{"a", "b", "c", "d", "e"}
    20  	str2 := []string{"d"}
    21  	str3 := []string{"f", "g"}
    22  	str4 := []string{"a", "f", "g"}
    23  
    24  	r.NoError(upd.AddRecentUPD(str1))
    25  	r.NoError(upd.AddRecentUPD(str2))
    26  	r.NoError(upd.AddRecentUPD(str3))
    27  	oldestData := upd.ReadOldestUPD()
    28  	r.Equal(len(str2), len(oldestData))
    29  	for i, data := range oldestData {
    30  		r.Equal(str2[i], data)
    31  	}
    32  
    33  	r.NoError(upd.AddRecentUPD(str4))
    34  	oldestData = upd.ReadOldestUPD()
    35  	r.Equal(len(str3), len(oldestData))
    36  	for i, data := range oldestData {
    37  		r.Equal(str3[i], data)
    38  	}
    39  
    40  	sbytes, err := upd.Serialize()
    41  	r.NoError(err)
    42  
    43  	upd2, err := NewUnproductiveDelegate(3, 10)
    44  	r.NoError(err)
    45  	err = upd2.Deserialize(sbytes)
    46  	r.NoError(err)
    47  
    48  	r.True(upd.Equal(upd2))
    49  }