murmurhash3_test.go raw

   1  package bloom_test
   2  
   3  import (
   4  	"testing"
   5  	
   6  	"github.com/p9c/p9/pkg/bloom"
   7  )
   8  
   9  // TestMurmurHash3 ensure the MurmurHash3 function produces the correct hash when given various seeds and data.
  10  func TestMurmurHash3(t *testing.T) {
  11  	var tests = []struct {
  12  		seed uint32
  13  		data []byte
  14  		out  uint32
  15  	}{
  16  		{0x00000000, []byte{}, 0x00000000},
  17  		{0xfba4c795, []byte{}, 0x6a396f08},
  18  		{0xffffffff, []byte{}, 0x81f16f39},
  19  		{0x00000000, []byte{0x00}, 0x514e28b7},
  20  		{0xfba4c795, []byte{0x00}, 0xea3f0b17},
  21  		{0x00000000, []byte{0xff}, 0xfd6cf10d},
  22  		{0x00000000, []byte{0x00, 0x11}, 0x16c6b7ab},
  23  		{0x00000000, []byte{0x00, 0x11, 0x22}, 0x8eb51c3d},
  24  		{0x00000000, []byte{0x00, 0x11, 0x22, 0x33}, 0xb4471bf8},
  25  		{0x00000000, []byte{0x00, 0x11, 0x22, 0x33, 0x44}, 0xe2301fa8},
  26  		{0x00000000, []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55}, 0xfc2e4a15},
  27  		{0x00000000, []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}, 0xb074502c},
  28  		{0x00000000, []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}, 0x8034d2a0},
  29  		{0x00000000, []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, 0xb4698def},
  30  	}
  31  	for i, test := range tests {
  32  		result := bloom.MurmurHash3(test.seed, test.data)
  33  		if result != test.out {
  34  			t.Errorf("MurmurHash3 test #%d failed: got %v want %v\n",
  35  				i, result, test.out,
  36  			)
  37  			continue
  38  		}
  39  	}
  40  }
  41