xxh32zero_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package xxh32_test
  2. import (
  3. "encoding/binary"
  4. "hash/crc32"
  5. "hash/fnv"
  6. "testing"
  7. qt "github.com/frankban/quicktest"
  8. "github.com/pierrec/lz4/internal/xxh32"
  9. )
  10. type test struct {
  11. sum uint32
  12. data string
  13. }
  14. var testdata = []test{
  15. {0x02cc5d05, ""},
  16. {0x550d7456, "a"},
  17. {0x4999fc53, "ab"},
  18. {0x32d153ff, "abc"},
  19. {0xa3643705, "abcd"},
  20. {0x9738f19b, "abcde"},
  21. {0x8b7cd587, "abcdef"},
  22. {0x9dd093b3, "abcdefg"},
  23. {0x0bb3c6bb, "abcdefgh"},
  24. {0xd03c13fd, "abcdefghi"},
  25. {0x8b988cfe, "abcdefghij"},
  26. {0x9d2d8b62, "abcdefghijklmnop"},
  27. {0x42ae804d, "abcdefghijklmnopqrstuvwxyz0123456789"},
  28. {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."},
  29. }
  30. func TestZeroBlockSize(t *testing.T) {
  31. var xxh xxh32.XXHZero
  32. if s := xxh.BlockSize(); s <= 0 {
  33. t.Errorf("invalid BlockSize: %d", s)
  34. }
  35. }
  36. func TestZeroSize(t *testing.T) {
  37. var xxh xxh32.XXHZero
  38. if s := xxh.Size(); s != 4 {
  39. t.Errorf("invalid Size: got %d expected 4", s)
  40. }
  41. }
  42. func TestZeroData(t *testing.T) {
  43. c := qt.New(t)
  44. for _, td := range testdata {
  45. var xxh xxh32.XXHZero
  46. data := []byte(td.data)
  47. _, _ = xxh.Write(data)
  48. c.Assert(xxh.Sum32(), qt.Equals, td.sum)
  49. c.Assert(xxh32.ChecksumZero(data), qt.Equals, td.sum)
  50. }
  51. }
  52. func TestZeroSplitData(t *testing.T) {
  53. c := qt.New(t)
  54. for _, td := range testdata {
  55. var xxh xxh32.XXHZero
  56. data := []byte(td.data)
  57. l := len(data) / 2
  58. _, _ = xxh.Write(data[0:l])
  59. _, _ = xxh.Write(data[l:])
  60. c.Assert(xxh.Sum32(), qt.Equals, td.sum)
  61. }
  62. }
  63. func TestZeroSum(t *testing.T) {
  64. c := qt.New(t)
  65. for _, td := range testdata {
  66. var xxh xxh32.XXHZero
  67. data := []byte(td.data)
  68. _, _ = xxh.Write(data)
  69. b := xxh.Sum(data)
  70. h := binary.LittleEndian.Uint32(b[len(data):])
  71. c.Assert(h, qt.Equals, td.sum)
  72. }
  73. }
  74. func TestZeroChecksum(t *testing.T) {
  75. c := qt.New(t)
  76. for _, td := range testdata {
  77. data := []byte(td.data)
  78. h := xxh32.ChecksumZero(data)
  79. c.Assert(h, qt.Equals, td.sum)
  80. }
  81. }
  82. func TestZeroReset(t *testing.T) {
  83. c := qt.New(t)
  84. var xxh xxh32.XXHZero
  85. for _, td := range testdata {
  86. _, _ = xxh.Write([]byte(td.data))
  87. h := xxh.Sum32()
  88. c.Assert(h, qt.Equals, td.sum)
  89. xxh.Reset()
  90. }
  91. }
  92. ///////////////////////////////////////////////////////////////////////////////
  93. // Benchmarks
  94. //
  95. var testdata1 = []byte(testdata[len(testdata)-1].data)
  96. func Benchmark_XXH32(b *testing.B) {
  97. var h xxh32.XXHZero
  98. for n := 0; n < b.N; n++ {
  99. _, _ = h.Write(testdata1)
  100. h.Sum32()
  101. h.Reset()
  102. }
  103. }
  104. func Benchmark_XXH32_Checksum(b *testing.B) {
  105. for n := 0; n < b.N; n++ {
  106. xxh32.ChecksumZero(testdata1)
  107. }
  108. }
  109. func Benchmark_CRC32(b *testing.B) {
  110. t := crc32.MakeTable(0)
  111. for i := 0; i < b.N; i++ {
  112. crc32.Checksum(testdata1, t)
  113. }
  114. }
  115. func Benchmark_Fnv32(b *testing.B) {
  116. h := fnv.New32()
  117. for i := 0; i < b.N; i++ {
  118. _, _ = h.Write(testdata1)
  119. h.Sum32()
  120. h.Reset()
  121. }
  122. }