github.com/koko1123/flow-go-1@v0.29.6/module/id/fixed_provider_test.go (about)

     1  package id
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  
     7  	"github.com/koko1123/flow-go-1/model/flow"
     8  	"github.com/koko1123/flow-go-1/model/flow/filter"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/koko1123/flow-go-1/utils/unittest"
    13  )
    14  
    15  func TestFixedIdentifierProvider(t *testing.T) {
    16  	identifiers := make([]flow.Identifier, 10)
    17  	for i := 0; i < len(identifiers); i++ {
    18  		identifiers[i] = unittest.IdentifierFixture()
    19  	}
    20  
    21  	fp := NewFixedIdentifierProvider(identifiers)
    22  
    23  	in := identifiers[rand.Intn(10)]
    24  	out := unittest.IdentifierFixture()
    25  
    26  	assert.True(t, contains(fp.Identifiers(), in))
    27  	assert.False(t, contains(fp.Identifiers(), out))
    28  
    29  }
    30  
    31  func TestFixedIdentitiesProvider(t *testing.T) {
    32  	identities := make([]*flow.Identity, 10)
    33  	for i := 0; i < len(identities); i++ {
    34  		identities[i] = unittest.IdentityFixture()
    35  	}
    36  
    37  	fp := NewFixedIdentityProvider(identities)
    38  
    39  	in := identities[rand.Intn(10)]
    40  	out := unittest.IdentityFixture()
    41  
    42  	assert.True(t, idContains(fp.Identities(filter.Any), in))
    43  	assert.False(t, idContains(fp.Identities(filter.Any), out))
    44  
    45  }
    46  
    47  func contains(a []flow.Identifier, b flow.Identifier) bool {
    48  	for _, i := range a {
    49  		if b == i {
    50  			return true
    51  		}
    52  	}
    53  	return false
    54  }
    55  
    56  func idContains(a []*flow.Identity, b *flow.Identity) bool {
    57  	for _, i := range a {
    58  		if b == i {
    59  			return true
    60  		}
    61  	}
    62  	return false
    63  }