xxh32zero_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package xxh32_test
  2. import (
  3. "encoding/binary"
  4. "hash/crc32"
  5. "hash/fnv"
  6. "testing"
  7. "github.com/pierrec/lz4/v4/internal/xxh32"
  8. )
  9. type test struct {
  10. sum uint32
  11. data string
  12. }
  13. var testdata = []test{
  14. {0x02cc5d05, ""},
  15. {0x550d7456, "a"},
  16. {0x4999fc53, "ab"},
  17. {0x32d153ff, "abc"},
  18. {0xa3643705, "abcd"},
  19. {0x9738f19b, "abcde"},
  20. {0x8b7cd587, "abcdef"},
  21. {0x9dd093b3, "abcdefg"},
  22. {0x0bb3c6bb, "abcdefgh"},
  23. {0xd03c13fd, "abcdefghi"},
  24. {0x8b988cfe, "abcdefghij"},
  25. {0x9d2d8b62, "abcdefghijklmnop"},
  26. {0x42ae804d, "abcdefghijklmnopqrstuvwxyz0123456789"},
  27. {0x62b4ed00, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."},
  28. }
  29. func TestZeroBlockSize(t *testing.T) {
  30. var xxh xxh32.XXHZero
  31. if s := xxh.BlockSize(); s <= 0 {
  32. t.Errorf("invalid BlockSizeIndex: %d", s)
  33. }
  34. }
  35. func TestZeroSize(t *testing.T) {
  36. var xxh xxh32.XXHZero
  37. if s := xxh.Size(); s != 4 {
  38. t.Errorf("invalid Size: got %d expected 4", s)
  39. }
  40. }
  41. func TestZeroData(t *testing.T) {
  42. for _, td := range testdata {
  43. var xxh xxh32.XXHZero
  44. data := []byte(td.data)
  45. _, _ = xxh.Write(data)
  46. if got, want := xxh.Sum32(), td.sum; got != want {
  47. t.Fatalf("got %x; want %x", got, want)
  48. }
  49. if got, want := xxh32.ChecksumZero(data), td.sum; got != want {
  50. t.Errorf("got %x; want %x", got, want)
  51. }
  52. }
  53. }
  54. func TestZeroSplitData(t *testing.T) {
  55. for _, td := range testdata {
  56. var xxh xxh32.XXHZero
  57. data := []byte(td.data)
  58. l := len(data) / 2
  59. _, _ = xxh.Write(data[0:l])
  60. _, _ = xxh.Write(data[l:])
  61. if got, want := xxh.Sum32(), td.sum; got != want {
  62. t.Fatalf("got %x; want %x", got, want)
  63. }
  64. }
  65. }
  66. func TestZeroSum(t *testing.T) {
  67. for _, td := range testdata {
  68. var xxh xxh32.XXHZero
  69. data := []byte(td.data)
  70. _, _ = xxh.Write(data)
  71. b := xxh.Sum(data)
  72. h := binary.LittleEndian.Uint32(b[len(data):])
  73. if got, want := h, td.sum; got != want {
  74. t.Fatalf("got %x; want %x", got, want)
  75. }
  76. }
  77. }
  78. func TestZeroChecksum(t *testing.T) {
  79. for _, td := range testdata {
  80. data := []byte(td.data)
  81. h := xxh32.ChecksumZero(data)
  82. if got, want := h, td.sum; got != want {
  83. t.Errorf("got %x; want %x", got, want)
  84. }
  85. }
  86. }
  87. func TestZeroReset(t *testing.T) {
  88. var xxh xxh32.XXHZero
  89. for _, td := range testdata {
  90. _, _ = xxh.Write([]byte(td.data))
  91. h := xxh.Sum32()
  92. if got, want := h, td.sum; got != want {
  93. t.Errorf("got %x; want %x", got, want)
  94. }
  95. xxh.Reset()
  96. }
  97. }
  98. func TestNil(t *testing.T) {
  99. want := xxh32.ChecksumZero([]byte(""))
  100. var xxh xxh32.XXHZero
  101. xxh.Write(nil)
  102. got := xxh.Sum32()
  103. if got != want {
  104. t.Errorf("got %x; want %x", got, want)
  105. }
  106. got = xxh32.ChecksumZero(nil)
  107. if got != want {
  108. t.Errorf("got %x; want %x", got, want)
  109. }
  110. }
  111. func TestUnaligned(t *testing.T) {
  112. zeros := make([]byte, 100)
  113. ha := xxh32.ChecksumZero(zeros[:len(zeros)-1])
  114. hu := xxh32.ChecksumZero(zeros[1:])
  115. if ha != hu {
  116. t.Errorf("mismatch: %x != %x", ha, hu)
  117. }
  118. var xxh xxh32.XXHZero
  119. xxh.Write(zeros[:len(zeros)-1])
  120. ha = xxh.Sum32()
  121. xxh.Reset()
  122. xxh.Write(zeros[1:])
  123. hu = xxh32.ChecksumZero(zeros[1:])
  124. if ha != hu {
  125. t.Errorf("mismatch: %x != %x", ha, hu)
  126. }
  127. }
  128. ///////////////////////////////////////////////////////////////////////////////
  129. // Benchmarks
  130. //
  131. var testdata1 = []byte(testdata[len(testdata)-1].data)
  132. func Benchmark_XXH32(b *testing.B) {
  133. var h xxh32.XXHZero
  134. for n := 0; n < b.N; n++ {
  135. _, _ = h.Write(testdata1)
  136. h.Sum32()
  137. h.Reset()
  138. }
  139. }
  140. func Benchmark_XXH32_Checksum(b *testing.B) {
  141. for n := 0; n < b.N; n++ {
  142. xxh32.ChecksumZero(testdata1)
  143. }
  144. }
  145. // The following two benchmark the case where 3/4 calls are not 4-byte-aligned.
  146. func Benchmark_XXH32Unaligned(b *testing.B) {
  147. var h xxh32.XXHZero
  148. for n := 0; n < b.N; n++ {
  149. _, _ = h.Write(testdata1[n%4:])
  150. h.Sum32()
  151. h.Reset()
  152. }
  153. }
  154. func Benchmark_XXH32_ChecksumUnaligned(b *testing.B) {
  155. for n := 0; n < b.N; n++ {
  156. xxh32.ChecksumZero(testdata1[n%4:])
  157. }
  158. }
  159. func Benchmark_CRC32(b *testing.B) {
  160. t := crc32.MakeTable(0)
  161. for i := 0; i < b.N; i++ {
  162. crc32.Checksum(testdata1, t)
  163. }
  164. }
  165. func Benchmark_Fnv32(b *testing.B) {
  166. h := fnv.New32()
  167. for i := 0; i < b.N; i++ {
  168. _, _ = h.Write(testdata1)
  169. h.Sum32()
  170. h.Reset()
  171. }
  172. }