aboutsummaryrefslogtreecommitdiffstats
path: root/common/bitutil/compress_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'common/bitutil/compress_test.go')
-rw-r--r--common/bitutil/compress_test.go99
1 files changed, 64 insertions, 35 deletions
diff --git a/common/bitutil/compress_test.go b/common/bitutil/compress_test.go
index ef38bc7b3..805ab0369 100644
--- a/common/bitutil/compress_test.go
+++ b/common/bitutil/compress_test.go
@@ -24,8 +24,8 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
)
-// Tests that data compression and decompression works correctly.
-func TestCompressCycle(t *testing.T) {
+// Tests that data bitset encoding and decoding works and is bijective.
+func TestEncodingCycle(t *testing.T) {
tests := []string{
// Tests generated by go-fuzz to maximize code coverage
"0x000000000000000000",
@@ -50,7 +50,7 @@ func TestCompressCycle(t *testing.T) {
for i, tt := range tests {
data := hexutil.MustDecode(tt)
- proc, err := DecompressBytes(CompressBytes(data), len(data))
+ proc, err := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
if err != nil {
t.Errorf("test %d: failed to decompress compressed data: %v", i, err)
continue
@@ -61,8 +61,8 @@ func TestCompressCycle(t *testing.T) {
}
}
-// Tests that data decompression works
-func TestDecompress(t *testing.T) {
+// Tests that data bitset decoding and rencoding works and is bijective.
+func TestDecodingCycle(t *testing.T) {
tests := []struct {
size int
input string
@@ -71,22 +71,22 @@ func TestDecompress(t *testing.T) {
{size: 0, input: "0x"},
// Crashers generated by go-fuzz
- {size: 0, input: "0x0020", fail: ErrUnreferencedData},
- {size: 0, input: "0x30", fail: ErrUnreferencedData},
- {size: 1, input: "0x00", fail: ErrUnreferencedData},
- {size: 2, input: "0x07", fail: ErrMissingData},
- {size: 1024, input: "0x8000", fail: ErrZeroContent},
+ {size: 0, input: "0x0020", fail: errUnreferencedData},
+ {size: 0, input: "0x30", fail: errUnreferencedData},
+ {size: 1, input: "0x00", fail: errUnreferencedData},
+ {size: 2, input: "0x07", fail: errMissingData},
+ {size: 1024, input: "0x8000", fail: errZeroContent},
// Tests generated by go-fuzz to maximize code coverage
- {size: 29490, input: "0x343137343733323134333839373334323073333930783e3078333930783e70706336346c65303e", fail: ErrMissingData},
- {size: 59395, input: "0x00", fail: ErrUnreferencedData},
- {size: 52574, input: "0x70706336346c65c0de", fail: ErrExceededTarget},
- {size: 42264, input: "0x07", fail: ErrMissingData},
- {size: 52, input: "0xa5045bad48f4", fail: ErrExceededTarget},
- {size: 52574, input: "0xc0de", fail: ErrMissingData},
+ {size: 29490, input: "0x343137343733323134333839373334323073333930783e3078333930783e70706336346c65303e", fail: errMissingData},
+ {size: 59395, input: "0x00", fail: errUnreferencedData},
+ {size: 52574, input: "0x70706336346c65c0de", fail: errExceededTarget},
+ {size: 42264, input: "0x07", fail: errMissingData},
+ {size: 52, input: "0xa5045bad48f4", fail: errExceededTarget},
+ {size: 52574, input: "0xc0de", fail: errMissingData},
{size: 52574, input: "0x"},
- {size: 29490, input: "0x34313734373332313433383937333432307333393078073034333839373334323073333930783e3078333937333432307333393078073061333930783e70706336346c65303e", fail: ErrMissingData},
- {size: 29491, input: "0x3973333930783e30783e", fail: ErrMissingData},
+ {size: 29490, input: "0x34313734373332313433383937333432307333393078073034333839373334323073333930783e3078333937333432307333393078073061333930783e70706336346c65303e", fail: errMissingData},
+ {size: 29491, input: "0x3973333930783e30783e", fail: errMissingData},
{size: 1024, input: "0x808080608080"},
{size: 1024, input: "0x808470705e3632383337363033313434303137393130306c6580ef46806380635a80"},
@@ -101,37 +101,66 @@ func TestDecompress(t *testing.T) {
for i, tt := range tests {
data := hexutil.MustDecode(tt.input)
- orig, err := DecompressBytes(data, tt.size)
+ orig, err := bitsetDecodeBytes(data, tt.size)
if err != tt.fail {
t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.fail)
}
if err != nil {
continue
}
- if comp := CompressBytes(orig); !bytes.Equal(comp, data) {
+ if comp := bitsetEncodeBytes(orig); !bytes.Equal(comp, data) {
t.Errorf("test %d: decompress/compress mismatch: have %x, want %x", i, comp, data)
}
}
}
+// TestCompression tests that compression works by returning either the bitset
+// encoded input, or the actual input if the bitset version is longer.
+func TestCompression(t *testing.T) {
+ // Check the the compression returns the bitset encoding is shorter
+ in := hexutil.MustDecode("0x4912385c0e7b64000000")
+ out := hexutil.MustDecode("0x80fe4912385c0e7b64")
+
+ if data := CompressBytes(in); bytes.Compare(data, out) != 0 {
+ t.Errorf("encoding mismatch for sparse data: have %x, want %x", data, out)
+ }
+ if data, err := DecompressBytes(out, len(in)); err != nil || bytes.Compare(data, in) != 0 {
+ t.Errorf("decoding mismatch for sparse data: have %x, want %x, error %v", data, in, err)
+ }
+ // Check the the compression returns the input if the bitset encoding is longer
+ in = hexutil.MustDecode("0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb")
+ out = hexutil.MustDecode("0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb")
+
+ if data := CompressBytes(in); bytes.Compare(data, out) != 0 {
+ t.Errorf("encoding mismatch for dense data: have %x, want %x", data, out)
+ }
+ if data, err := DecompressBytes(out, len(in)); err != nil || bytes.Compare(data, in) != 0 {
+ t.Errorf("decoding mismatch for dense data: have %x, want %x, error %v", data, in, err)
+ }
+ // Check that decompressing a longer input than the target fails
+ if _, err := DecompressBytes([]byte{0xc0, 0x01, 0x01}, 2); err != errExceededTarget {
+ t.Errorf("decoding error mismatch for long data: have %v, want %v", err, errExceededTarget)
+ }
+}
+
// Crude benchmark for compressing random slices of bytes.
-func BenchmarkCompress1KBVerySparse(b *testing.B) { benchmarkCompress(b, 1024, 0.0001) }
-func BenchmarkCompress2KBVerySparse(b *testing.B) { benchmarkCompress(b, 2048, 0.0001) }
-func BenchmarkCompress4KBVerySparse(b *testing.B) { benchmarkCompress(b, 4096, 0.0001) }
+func BenchmarkEncoding1KBVerySparse(b *testing.B) { benchmarkEncoding(b, 1024, 0.0001) }
+func BenchmarkEncoding2KBVerySparse(b *testing.B) { benchmarkEncoding(b, 2048, 0.0001) }
+func BenchmarkEncoding4KBVerySparse(b *testing.B) { benchmarkEncoding(b, 4096, 0.0001) }
-func BenchmarkCompress1KBSparse(b *testing.B) { benchmarkCompress(b, 1024, 0.001) }
-func BenchmarkCompress2KBSparse(b *testing.B) { benchmarkCompress(b, 2048, 0.001) }
-func BenchmarkCompress4KBSparse(b *testing.B) { benchmarkCompress(b, 4096, 0.001) }
+func BenchmarkEncoding1KBSparse(b *testing.B) { benchmarkEncoding(b, 1024, 0.001) }
+func BenchmarkEncoding2KBSparse(b *testing.B) { benchmarkEncoding(b, 2048, 0.001) }
+func BenchmarkEncoding4KBSparse(b *testing.B) { benchmarkEncoding(b, 4096, 0.001) }
-func BenchmarkCompress1KBDense(b *testing.B) { benchmarkCompress(b, 1024, 0.1) }
-func BenchmarkCompress2KBDense(b *testing.B) { benchmarkCompress(b, 2048, 0.1) }
-func BenchmarkCompress4KBDense(b *testing.B) { benchmarkCompress(b, 4096, 0.1) }
+func BenchmarkEncoding1KBDense(b *testing.B) { benchmarkEncoding(b, 1024, 0.1) }
+func BenchmarkEncoding2KBDense(b *testing.B) { benchmarkEncoding(b, 2048, 0.1) }
+func BenchmarkEncoding4KBDense(b *testing.B) { benchmarkEncoding(b, 4096, 0.1) }
-func BenchmarkCompress1KBSaturated(b *testing.B) { benchmarkCompress(b, 1024, 0.5) }
-func BenchmarkCompress2KBSaturated(b *testing.B) { benchmarkCompress(b, 2048, 0.5) }
-func BenchmarkCompress4KBSaturated(b *testing.B) { benchmarkCompress(b, 4096, 0.5) }
+func BenchmarkEncoding1KBSaturated(b *testing.B) { benchmarkEncoding(b, 1024, 0.5) }
+func BenchmarkEncoding2KBSaturated(b *testing.B) { benchmarkEncoding(b, 2048, 0.5) }
+func BenchmarkEncoding4KBSaturated(b *testing.B) { benchmarkEncoding(b, 4096, 0.5) }
-func benchmarkCompress(b *testing.B, bytes int, fill float64) {
+func benchmarkEncoding(b *testing.B, bytes int, fill float64) {
// Generate a random slice of bytes to compress
random := rand.NewSource(0) // reproducible and comparable
@@ -143,10 +172,10 @@ func benchmarkCompress(b *testing.B, bytes int, fill float64) {
bit := uint(random.Int63() % 8)
data[idx] |= 1 << bit
}
- // Reset the benchmark and measure compression/decompression
+ // Reset the benchmark and measure encoding/decoding
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
- DecompressBytes(CompressBytes(data), len(data))
+ bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
}
}