github.com/lingyao2333/mo-zero@v1.4.1/core/hash/hash_test.go (about) 1 package hash 2 3 import ( 4 "crypto/md5" 5 "fmt" 6 "hash/fnv" 7 "math/big" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 const ( 14 text = "hello, world!\n" 15 md5Digest = "910c8bc73110b0cd1bc5d2bcae782511" 16 ) 17 18 func TestMd5(t *testing.T) { 19 actual := fmt.Sprintf("%x", Md5([]byte(text))) 20 assert.Equal(t, md5Digest, actual) 21 } 22 23 func TestMd5Hex(t *testing.T) { 24 actual := Md5Hex([]byte(text)) 25 assert.Equal(t, md5Digest, actual) 26 } 27 28 func BenchmarkHashFnv(b *testing.B) { 29 for i := 0; i < b.N; i++ { 30 h := fnv.New32() 31 new(big.Int).SetBytes(h.Sum([]byte(text))).Int64() 32 } 33 } 34 35 func BenchmarkHashMd5(b *testing.B) { 36 for i := 0; i < b.N; i++ { 37 h := md5.New() 38 bytes := h.Sum([]byte(text)) 39 new(big.Int).SetBytes(bytes).Int64() 40 } 41 } 42 43 func BenchmarkMurmur3(b *testing.B) { 44 for i := 0; i < b.N; i++ { 45 Hash([]byte(text)) 46 } 47 }