hash_test.go 825 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 TestMd5Hex(t *testing.T) {
  19. actual := Md5Hex([]byte(text))
  20. assert.Equal(t, md5Digest, actual)
  21. }
  22. func BenchmarkHashFnv(b *testing.B) {
  23. for i := 0; i < b.N; i++ {
  24. h := fnv.New32()
  25. new(big.Int).SetBytes(h.Sum([]byte(text))).Int64()
  26. }
  27. }
  28. func BenchmarkHashMd5(b *testing.B) {
  29. for i := 0; i < b.N; i++ {
  30. h := md5.New()
  31. bytes := h.Sum([]byte(text))
  32. new(big.Int).SetBytes(bytes).Int64()
  33. }
  34. }
  35. func BenchmarkMurmur3(b *testing.B) {
  36. for i := 0; i < b.N; i++ {
  37. Hash([]byte(text))
  38. }
  39. }