github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/core/bloombits/generator_test.go (about)

     1  package bloombits
     2  
     3  import (
     4  	"bytes"
     5  	"math/rand"
     6  	"testing"
     7  
     8  	"github.com/quickchainproject/quickchain/core/types"
     9  )
    10  
    11  // Tests that batched bloom bits are correctly rotated from the input bloom
    12  // filters.
    13  func TestGenerator(t *testing.T) {
    14  	// Generate the input and the rotated output
    15  	var input, output [types.BloomBitLength][types.BloomByteLength]byte
    16  
    17  	for i := 0; i < types.BloomBitLength; i++ {
    18  		for j := 0; j < types.BloomBitLength; j++ {
    19  			bit := byte(rand.Int() % 2)
    20  
    21  			input[i][j/8] |= bit << byte(7-j%8)
    22  			output[types.BloomBitLength-1-j][i/8] |= bit << byte(7-i%8)
    23  		}
    24  	}
    25  	// Crunch the input through the generator and verify the result
    26  	gen, err := NewGenerator(types.BloomBitLength)
    27  	if err != nil {
    28  		t.Fatalf("failed to create bloombit generator: %v", err)
    29  	}
    30  	for i, bloom := range input {
    31  		if err := gen.AddBloom(uint(i), bloom); err != nil {
    32  			t.Fatalf("bloom %d: failed to add: %v", i, err)
    33  		}
    34  	}
    35  	for i, want := range output {
    36  		have, err := gen.Bitset(uint(i))
    37  		if err != nil {
    38  			t.Fatalf("output %d: failed to retrieve bits: %v", i, err)
    39  		}
    40  		if !bytes.Equal(have, want[:]) {
    41  			t.Errorf("output %d: bit vector mismatch have %x, want %x", i, have, want)
    42  		}
    43  	}
    44  }