github.com/iotexproject/iotex-core@v1.14.1-rc1/blockindex/bloom_range_test.go (about)

     1  package blockindex
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestNewBloomRange(t *testing.T) {
    10  	require := require.New(t)
    11  
    12  	// unenough data
    13  	var br bloomRange
    14  	err := br.FromBytes(make([]byte, 16))
    15  	require.Error(err)
    16  
    17  	tests := []struct {
    18  		start, end uint64
    19  		success    bool
    20  	}{
    21  		{21, 20, false},
    22  		{0, 8, true},
    23  		{5, 8, true},
    24  	}
    25  
    26  	for _, v := range tests {
    27  		newBr, err := newBloomRange(64, 3)
    28  		require.NoError(err)
    29  		newBr.BloomFilter.Add([]byte("test"))
    30  		newBr.SetStart(v.start)
    31  		newBr.SetEnd(v.end)
    32  
    33  		data, err := newBr.Bytes()
    34  		if !v.success {
    35  			require.Error(err)
    36  			continue
    37  		} else {
    38  			require.NoError(err)
    39  		}
    40  		err = newBr.FromBytes(data)
    41  		require.NoError(err)
    42  		require.Equal(v.start, newBr.Start())
    43  		require.Equal(v.end, newBr.End())
    44  	}
    45  }