github.com/klaytn/klaytn@v1.12.1/blockchain/bloombits/generator_test.go (about)

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