compress_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package bitutil
  17. import (
  18. "bytes"
  19. "math/rand"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common/hexutil"
  22. )
  23. // Tests that data compression and decompression works correctly.
  24. func TestCompressCycle(t *testing.T) {
  25. tests := []string{
  26. // Tests generated by go-fuzz to maximize code coverage
  27. "0x000000000000000000",
  28. "0xef0400",
  29. "0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb",
  30. "0x7b64000000",
  31. "0x000034000000000000",
  32. "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f0000000000000000000",
  33. "0x4912385c0e7b64000000",
  34. "0x000034000000000000000000000000000000",
  35. "0x00",
  36. "0x000003e834ff7f0000",
  37. "0x0000",
  38. "0x0000000000000000000000000000000000000000000000000000000000ff00",
  39. "0x895f0c6a020f850c6a020f85f88df88d",
  40. "0xdf7070533534333636313639343638373432313536346c1bc3315aac2f65fefb",
  41. "0x0000000000",
  42. "0xdf70706336346c65fefb",
  43. "0x00006d643634000000",
  44. "0xdf7070533534333636313639343638373532313536346c1bc333393438373130707063363430353639343638373532313536346c1bc333393438336336346c65fe",
  45. }
  46. for i, tt := range tests {
  47. data := hexutil.MustDecode(tt)
  48. proc, err := DecompressBytes(CompressBytes(data), len(data))
  49. if err != nil {
  50. t.Errorf("test %d: failed to decompress compressed data: %v", i, err)
  51. continue
  52. }
  53. if !bytes.Equal(data, proc) {
  54. t.Errorf("test %d: compress/decompress mismatch: have %x, want %x", i, proc, data)
  55. }
  56. }
  57. }
  58. // Tests that data decompression works
  59. func TestDecompress(t *testing.T) {
  60. tests := []struct {
  61. size int
  62. input string
  63. fail error
  64. }{
  65. {size: 0, input: "0x"},
  66. // Crashers generated by go-fuzz
  67. {size: 0, input: "0x0020", fail: ErrUnreferencedData},
  68. {size: 0, input: "0x30", fail: ErrUnreferencedData},
  69. {size: 1, input: "0x00", fail: ErrUnreferencedData},
  70. {size: 2, input: "0x07", fail: ErrMissingData},
  71. {size: 1024, input: "0x8000", fail: ErrZeroContent},
  72. // Tests generated by go-fuzz to maximize code coverage
  73. {size: 29490, input: "0x343137343733323134333839373334323073333930783e3078333930783e70706336346c65303e", fail: ErrMissingData},
  74. {size: 59395, input: "0x00", fail: ErrUnreferencedData},
  75. {size: 52574, input: "0x70706336346c65c0de", fail: ErrExceededTarget},
  76. {size: 42264, input: "0x07", fail: ErrMissingData},
  77. {size: 52, input: "0xa5045bad48f4", fail: ErrExceededTarget},
  78. {size: 52574, input: "0xc0de", fail: ErrMissingData},
  79. {size: 52574, input: "0x"},
  80. {size: 29490, input: "0x34313734373332313433383937333432307333393078073034333839373334323073333930783e3078333937333432307333393078073061333930783e70706336346c65303e", fail: ErrMissingData},
  81. {size: 29491, input: "0x3973333930783e30783e", fail: ErrMissingData},
  82. {size: 1024, input: "0x808080608080"},
  83. {size: 1024, input: "0x808470705e3632383337363033313434303137393130306c6580ef46806380635a80"},
  84. {size: 1024, input: "0x8080808070"},
  85. {size: 1024, input: "0x808070705e36346c6580ef46806380635a80"},
  86. {size: 1024, input: "0x80808046802680"},
  87. {size: 1024, input: "0x4040404035"},
  88. {size: 1024, input: "0x4040bf3ba2b3f684402d353234373438373934409fe5b1e7ada94ebfd7d0505e27be4035"},
  89. {size: 1024, input: "0x404040bf3ba2b3f6844035"},
  90. {size: 1024, input: "0x40402d35323437343837393440bfd7d0505e27be4035"},
  91. }
  92. for i, tt := range tests {
  93. data := hexutil.MustDecode(tt.input)
  94. orig, err := DecompressBytes(data, tt.size)
  95. if err != tt.fail {
  96. t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.fail)
  97. }
  98. if err != nil {
  99. continue
  100. }
  101. if comp := CompressBytes(orig); !bytes.Equal(comp, data) {
  102. t.Errorf("test %d: decompress/compress mismatch: have %x, want %x", i, comp, data)
  103. }
  104. }
  105. }
  106. // Crude benchmark for compressing random slices of bytes.
  107. func BenchmarkCompress1KBVerySparse(b *testing.B) { benchmarkCompress(b, 1024, 0.0001) }
  108. func BenchmarkCompress2KBVerySparse(b *testing.B) { benchmarkCompress(b, 2048, 0.0001) }
  109. func BenchmarkCompress4KBVerySparse(b *testing.B) { benchmarkCompress(b, 4096, 0.0001) }
  110. func BenchmarkCompress1KBSparse(b *testing.B) { benchmarkCompress(b, 1024, 0.001) }
  111. func BenchmarkCompress2KBSparse(b *testing.B) { benchmarkCompress(b, 2048, 0.001) }
  112. func BenchmarkCompress4KBSparse(b *testing.B) { benchmarkCompress(b, 4096, 0.001) }
  113. func BenchmarkCompress1KBDense(b *testing.B) { benchmarkCompress(b, 1024, 0.1) }
  114. func BenchmarkCompress2KBDense(b *testing.B) { benchmarkCompress(b, 2048, 0.1) }
  115. func BenchmarkCompress4KBDense(b *testing.B) { benchmarkCompress(b, 4096, 0.1) }
  116. func BenchmarkCompress1KBSaturated(b *testing.B) { benchmarkCompress(b, 1024, 0.5) }
  117. func BenchmarkCompress2KBSaturated(b *testing.B) { benchmarkCompress(b, 2048, 0.5) }
  118. func BenchmarkCompress4KBSaturated(b *testing.B) { benchmarkCompress(b, 4096, 0.5) }
  119. func benchmarkCompress(b *testing.B, bytes int, fill float64) {
  120. // Generate a random slice of bytes to compress
  121. random := rand.NewSource(0) // reproducible and comparable
  122. data := make([]byte, bytes)
  123. bits := int(float64(bytes) * 8 * fill)
  124. for i := 0; i < bits; i++ {
  125. idx := random.Int63() % int64(len(data))
  126. bit := uint(random.Int63() % 8)
  127. data[idx] |= 1 << bit
  128. }
  129. // Reset the benchmark and measure compression/decompression
  130. b.ResetTimer()
  131. b.ReportAllocs()
  132. for i := 0; i < b.N; i++ {
  133. DecompressBytes(CompressBytes(data), len(data))
  134. }
  135. }