hash_test.go 722 B

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