xxhash_test.go 4.1 KB

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