github.com/mavryk-network/mvgo@v1.19.9/hash/fnv_test.go (about)

     1  package hash
     2  
     3  import (
     4  	"encoding/hex"
     5  	"hash/fnv"
     6  	"testing"
     7  	"testing/quick"
     8  )
     9  
    10  // test implementation of FNV Hash64
    11  func TestNewHash64(t *testing.T) {
    12  	expect := func(data []byte) uint64 {
    13  		h := fnv.New64a()
    14  		if _, err := h.Write(data); err != nil {
    15  			t.Fatal(err)
    16  		}
    17  		return h.Sum64()
    18  	}
    19  	hash := func(data []byte) uint64 {
    20  		h := NewInlineFNV64a()
    21  		h.Write(data)
    22  		return h.Sum64()
    23  	}
    24  	if err := quick.CheckEqual(hash, expect, nil); err != nil {
    25  		t.Fatal(err)
    26  	}
    27  }
    28  
    29  func TestStaticHash64(t *testing.T) {
    30  	expect := func(data []byte) uint64 {
    31  		h := fnv.New64a()
    32  		if _, err := h.Write(data); err != nil {
    33  			t.Fatal(err)
    34  		}
    35  		return h.Sum64()
    36  	}
    37  	if err := quick.CheckEqual(Hash64, expect, nil); err != nil {
    38  		t.Fatal(err)
    39  	}
    40  }
    41  
    42  func BenchmarkNewHash64(b *testing.B) {
    43  	buf, _ := hex.DecodeString("029d4ed3161d644bedccb8673f30c6682b6e0a11756a3f75d7a739dede1cf29e")
    44  	b.SetBytes(32)
    45  	b.ReportAllocs()
    46  	for i := 0; i < b.N; i++ {
    47  		h := NewInlineFNV64a()
    48  		h.Write(buf)
    49  		_ = h.Sum64()
    50  	}
    51  }
    52  
    53  func BenchmarkStaticHash64(b *testing.B) {
    54  	buf, _ := hex.DecodeString("029d4ed3161d644bedccb8673f30c6682b6e0a11756a3f75d7a739dede1cf29e")
    55  	b.SetBytes(32)
    56  	b.ReportAllocs()
    57  	for i := 0; i < b.N; i++ {
    58  		_ = Hash64(buf)
    59  	}
    60  }