fnv_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package fnv
  5. import (
  6. "bytes"
  7. "encoding/binary"
  8. "hash"
  9. "testing"
  10. )
  11. type golden struct {
  12. sum []byte
  13. text string
  14. }
  15. var golden32 = []golden{
  16. {[]byte{0x81, 0x1c, 0x9d, 0xc5}, ""},
  17. {[]byte{0x05, 0x0c, 0x5d, 0x7e}, "a"},
  18. {[]byte{0x70, 0x77, 0x2d, 0x38}, "ab"},
  19. {[]byte{0x43, 0x9c, 0x2f, 0x4b}, "abc"},
  20. }
  21. var golden32a = []golden{
  22. {[]byte{0x81, 0x1c, 0x9d, 0xc5}, ""},
  23. {[]byte{0xe4, 0x0c, 0x29, 0x2c}, "a"},
  24. {[]byte{0x4d, 0x25, 0x05, 0xca}, "ab"},
  25. {[]byte{0x1a, 0x47, 0xe9, 0x0b}, "abc"},
  26. }
  27. var golden64 = []golden{
  28. {[]byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25}, ""},
  29. {[]byte{0xaf, 0x63, 0xbd, 0x4c, 0x86, 0x01, 0xb7, 0xbe}, "a"},
  30. {[]byte{0x08, 0x32, 0x67, 0x07, 0xb4, 0xeb, 0x37, 0xb8}, "ab"},
  31. {[]byte{0xd8, 0xdc, 0xca, 0x18, 0x6b, 0xaf, 0xad, 0xcb}, "abc"},
  32. }
  33. var golden64a = []golden{
  34. {[]byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25}, ""},
  35. {[]byte{0xaf, 0x63, 0xdc, 0x4c, 0x86, 0x01, 0xec, 0x8c}, "a"},
  36. {[]byte{0x08, 0x9c, 0x44, 0x07, 0xb5, 0x45, 0x98, 0x6a}, "ab"},
  37. {[]byte{0xe7, 0x1f, 0xa2, 0x19, 0x05, 0x41, 0x57, 0x4b}, "abc"},
  38. }
  39. func TestGolden32(t *testing.T) {
  40. testGolden(t, New32(), golden32)
  41. }
  42. func TestGolden32a(t *testing.T) {
  43. testGolden(t, New32a(), golden32a)
  44. }
  45. func TestGolden64(t *testing.T) {
  46. testGolden(t, New64(), golden64)
  47. }
  48. func TestGolden64a(t *testing.T) {
  49. testGolden(t, New64a(), golden64a)
  50. }
  51. func testGolden(t *testing.T, hash hash.Hash, gold []golden) {
  52. for _, g := range gold {
  53. hash.Reset()
  54. done, error := hash.Write([]byte(g.text))
  55. if error != nil {
  56. t.Fatalf("write error: %s", error)
  57. }
  58. if done != len(g.text) {
  59. t.Fatalf("wrote only %d out of %d bytes", done, len(g.text))
  60. }
  61. if actual := hash.Sum(nil); !bytes.Equal(g.sum, actual) {
  62. t.Errorf("hash(%q) = 0x%x want 0x%x", g.text, actual, g.sum)
  63. }
  64. }
  65. }
  66. func TestIntegrity32(t *testing.T) {
  67. testIntegrity(t, New32())
  68. }
  69. func TestIntegrity32a(t *testing.T) {
  70. testIntegrity(t, New32a())
  71. }
  72. func TestIntegrity64(t *testing.T) {
  73. testIntegrity(t, New64())
  74. }
  75. func TestIntegrity64a(t *testing.T) {
  76. testIntegrity(t, New64a())
  77. }
  78. func testIntegrity(t *testing.T, h hash.Hash) {
  79. data := []byte{'1', '2', 3, 4, 5}
  80. h.Write(data)
  81. sum := h.Sum(nil)
  82. if size := h.Size(); size != len(sum) {
  83. t.Fatalf("Size()=%d but len(Sum())=%d", size, len(sum))
  84. }
  85. if a := h.Sum(nil); !bytes.Equal(sum, a) {
  86. t.Fatalf("first Sum()=0x%x, second Sum()=0x%x", sum, a)
  87. }
  88. h.Reset()
  89. h.Write(data)
  90. if a := h.Sum(nil); !bytes.Equal(sum, a) {
  91. t.Fatalf("Sum()=0x%x, but after Reset() Sum()=0x%x", sum, a)
  92. }
  93. h.Reset()
  94. h.Write(data[:2])
  95. h.Write(data[2:])
  96. if a := h.Sum(nil); !bytes.Equal(sum, a) {
  97. t.Fatalf("Sum()=0x%x, but with partial writes, Sum()=0x%x", sum, a)
  98. }
  99. switch h.Size() {
  100. case 4:
  101. sum32 := h.(hash.Hash32).Sum32()
  102. if sum32 != binary.BigEndian.Uint32(sum) {
  103. t.Fatalf("Sum()=0x%x, but Sum32()=0x%x", sum, sum32)
  104. }
  105. case 8:
  106. sum64 := h.(hash.Hash64).Sum64()
  107. if sum64 != binary.BigEndian.Uint64(sum) {
  108. t.Fatalf("Sum()=0x%x, but Sum64()=0x%x", sum, sum64)
  109. }
  110. }
  111. }
  112. func BenchmarkFnv32KB(b *testing.B) {
  113. benchmarkKB(b, New32())
  114. }
  115. func BenchmarkFnv32aKB(b *testing.B) {
  116. benchmarkKB(b, New32a())
  117. }
  118. func BenchmarkFnv64KB(b *testing.B) {
  119. benchmarkKB(b, New64())
  120. }
  121. func BenchmarkFnv64aKB(b *testing.B) {
  122. benchmarkKB(b, New64a())
  123. }
  124. func benchmarkKB(b *testing.B, h hash.Hash) {
  125. b.SetBytes(1024)
  126. data := make([]byte, 1024)
  127. for i := range data {
  128. data[i] = byte(i)
  129. }
  130. in := make([]byte, 0, h.Size())
  131. b.ResetTimer()
  132. for i := 0; i < b.N; i++ {
  133. h.Reset()
  134. h.Write(data)
  135. h.Sum(in)
  136. }
  137. }