xxhash_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package xxhash
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. "strings"
  7. "testing"
  8. )
  9. func TestAll(t *testing.T) {
  10. for _, tt := range []struct {
  11. name string
  12. input string
  13. want uint64
  14. }{
  15. {"empty", "", 0xef46db3751d8e999},
  16. {"a", "a", 0xd24ec4f1a98c6e5b},
  17. {"as", "as", 0x1c330fb2d66be179},
  18. {"asd", "asd", 0x631c37ce72a97393},
  19. {"asdf", "asdf", 0x415872f599cea71e},
  20. {
  21. "len=63",
  22. // Exactly 63 characters, which exercises all code paths.
  23. "Call me Ishmael. Some years ago--never mind how long precisely-",
  24. 0x02a2e85470d6fd96,
  25. },
  26. } {
  27. for chunkSize := 1; chunkSize <= len(tt.input); chunkSize++ {
  28. name := fmt.Sprintf("%s,chunkSize=%d", tt.name, chunkSize)
  29. t.Run(name, func(t *testing.T) {
  30. testDigest(t, tt.input, chunkSize, tt.want)
  31. })
  32. }
  33. t.Run(tt.name, func(t *testing.T) { testSum(t, tt.input, tt.want) })
  34. }
  35. }
  36. func testDigest(t *testing.T, input string, chunkSize int, want uint64) {
  37. d := New()
  38. ds := New() // uses WriteString
  39. for i := 0; i < len(input); i += chunkSize {
  40. chunk := input[i:]
  41. if len(chunk) > chunkSize {
  42. chunk = chunk[:chunkSize]
  43. }
  44. n, err := d.Write([]byte(chunk))
  45. if err != nil || n != len(chunk) {
  46. t.Fatalf("Digest.Write: got (%d, %v); want (%d, nil)", n, err, len(chunk))
  47. }
  48. n, err = ds.WriteString(chunk)
  49. if err != nil || n != len(chunk) {
  50. t.Fatalf("Digest.WriteString: got (%d, %v); want (%d, nil)", n, err, len(chunk))
  51. }
  52. }
  53. if got := d.Sum64(); got != want {
  54. t.Fatalf("Digest.Sum64: got 0x%x; want 0x%x", got, want)
  55. }
  56. if got := ds.Sum64(); got != want {
  57. t.Fatalf("Digest.Sum64 (WriteString): got 0x%x; want 0x%x", got, want)
  58. }
  59. var b [8]byte
  60. binary.BigEndian.PutUint64(b[:], want)
  61. if got := d.Sum(nil); !bytes.Equal(got, b[:]) {
  62. t.Fatalf("Sum: got %v; want %v", got, b[:])
  63. }
  64. }
  65. func testSum(t *testing.T, input string, want uint64) {
  66. if got := Sum64([]byte(input)); got != want {
  67. t.Fatalf("Sum64: got 0x%x; want 0x%x", got, want)
  68. }
  69. if got := Sum64String(input); got != want {
  70. t.Fatalf("Sum64String: got 0x%x; want 0x%x", got, want)
  71. }
  72. }
  73. func TestReset(t *testing.T) {
  74. parts := []string{"The quic", "k br", "o", "wn fox jumps", " ov", "er the lazy ", "dog."}
  75. d := New()
  76. for _, part := range parts {
  77. d.Write([]byte(part))
  78. }
  79. h0 := d.Sum64()
  80. d.Reset()
  81. d.Write([]byte(strings.Join(parts, "")))
  82. h1 := d.Sum64()
  83. if h0 != h1 {
  84. t.Errorf("0x%x != 0x%x", h0, h1)
  85. }
  86. }
  87. func TestBinaryMarshaling(t *testing.T) {
  88. d := New()
  89. d.WriteString("abc")
  90. b, err := d.MarshalBinary()
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. d = New()
  95. d.WriteString("junk")
  96. if err := d.UnmarshalBinary(b); err != nil {
  97. t.Fatal(err)
  98. }
  99. d.WriteString("def")
  100. if got, want := d.Sum64(), Sum64String("abcdef"); got != want {
  101. t.Fatalf("after MarshalBinary+UnmarshalBinary, got 0x%x; want 0x%x", got, want)
  102. }
  103. d0 := New()
  104. d1 := New()
  105. for i := 0; i < 64; i++ {
  106. b, err := d0.MarshalBinary()
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. d0 = new(Digest)
  111. if err := d0.UnmarshalBinary(b); err != nil {
  112. t.Fatal(err)
  113. }
  114. if got, want := d0.Sum64(), d1.Sum64(); got != want {
  115. t.Fatalf("after %d Writes, unmarshaled Digest gave sum 0x%x; want 0x%x", i, got, want)
  116. }
  117. d0.Write([]byte{'a'})
  118. d1.Write([]byte{'a'})
  119. }
  120. }
  121. var sink uint64
  122. func TestAllocs(t *testing.T) {
  123. const shortStr = "abcdefghijklmnop"
  124. // Sum64([]byte(shortString)) shouldn't allocate because the
  125. // intermediate []byte ought not to escape.
  126. // (See https://github.com/cespare/xxhash/pull/2.)
  127. t.Run("Sum64", func(t *testing.T) {
  128. testAllocs(t, func() {
  129. sink = Sum64([]byte(shortStr))
  130. })
  131. })
  132. // Creating and using a Digest shouldn't allocate because its methods
  133. // shouldn't make it escape. (A previous version of New returned a
  134. // hash.Hash64 which forces an allocation.)
  135. t.Run("Digest", func(t *testing.T) {
  136. b := []byte("asdf")
  137. testAllocs(t, func() {
  138. d := New()
  139. d.Write(b)
  140. sink = d.Sum64()
  141. })
  142. })
  143. }
  144. func testAllocs(t *testing.T, fn func()) {
  145. t.Helper()
  146. if allocs := int(testing.AllocsPerRun(10, fn)); allocs > 0 {
  147. t.Fatalf("got %d allocation(s) (want zero)", allocs)
  148. }
  149. }