github.com/koko1123/flow-go-1@v0.29.6/model/flow/identifierList_test.go (about)

     1  package flow_test
     2  
     3  import (
     4  	"bytes"
     5  	"math/rand"
     6  	"sort"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/koko1123/flow-go-1/model/flow"
    13  	"github.com/koko1123/flow-go-1/utils/unittest"
    14  )
    15  
    16  // TestIdentifierListSort tests the IdentityList against its implemented sort interface
    17  // it generates and sorts a list of ids, and then evaluates sorting in ascending order
    18  func TestIdentifierListSort(t *testing.T) {
    19  	count := 10
    20  	// creates an identifier list of 10 ids
    21  	var ids flow.IdentifierList = unittest.IdentifierListFixture(count)
    22  
    23  	// shuffles array before sorting to enforce some pseudo-randomness
    24  	rand.Seed(time.Now().UnixNano())
    25  	rand.Shuffle(ids.Len(), ids.Swap)
    26  
    27  	sort.Sort(ids)
    28  
    29  	before := ids[0]
    30  	// compares each id being greater than or equal to its previous one
    31  	// on the sorted list
    32  	for _, id := range ids {
    33  		if bytes.Compare(id[:], before[:]) == -1 {
    34  			// test fails due to id < before which is in contrast to the
    35  			// ascending order assumption of sort
    36  			require.Fail(t, "sort does not work in ascending order")
    37  		}
    38  		before = id
    39  	}
    40  }
    41  
    42  // TestIdentifierListContains tests the IdentifierList against its Contains method implementation.
    43  func TestIdentifierListContains(t *testing.T) {
    44  	count := 10
    45  	// creates an identifier list of 10 ids
    46  	var ids flow.IdentifierList = unittest.IdentifierListFixture(count)
    47  
    48  	// all identifiers in the list should have a valid Contains result.
    49  	for _, id := range ids {
    50  		require.True(t, ids.Contains(id))
    51  	}
    52  
    53  	// non-existent identifier should have a negative Contains result.
    54  	nonExistent := unittest.IdentifierFixture()
    55  	require.False(t, ids.Contains(nonExistent))
    56  }