github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/core/bloombits/generator_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package bloombits
    13  
    14  import (
    15  	"bytes"
    16  	"math/rand"
    17  	"testing"
    18  
    19  	"github.com/Sberex/go-sberex/core/types"
    20  )
    21  
    22  // Tests that batched bloom bits are correctly rotated from the input bloom
    23  // filters.
    24  func TestGenerator(t *testing.T) {
    25  	// Generate the input and the rotated output
    26  	var input, output [types.BloomBitLength][types.BloomByteLength]byte
    27  
    28  	for i := 0; i < types.BloomBitLength; i++ {
    29  		for j := 0; j < types.BloomBitLength; j++ {
    30  			bit := byte(rand.Int() % 2)
    31  
    32  			input[i][j/8] |= bit << byte(7-j%8)
    33  			output[types.BloomBitLength-1-j][i/8] |= bit << byte(7-i%8)
    34  		}
    35  	}
    36  	// Crunch the input through the generator and verify the result
    37  	gen, err := NewGenerator(types.BloomBitLength)
    38  	if err != nil {
    39  		t.Fatalf("failed to create bloombit generator: %v", err)
    40  	}
    41  	for i, bloom := range input {
    42  		if err := gen.AddBloom(uint(i), bloom); err != nil {
    43  			t.Fatalf("bloom %d: failed to add: %v", i, err)
    44  		}
    45  	}
    46  	for i, want := range output {
    47  		have, err := gen.Bitset(uint(i))
    48  		if err != nil {
    49  			t.Fatalf("output %d: failed to retrieve bits: %v", i, err)
    50  		}
    51  		if !bytes.Equal(have, want[:]) {
    52  			t.Errorf("output %d: bit vector mismatch have %x, want %x", i, have, want)
    53  		}
    54  	}
    55  }