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