github.com/klaytn/klaytn@v1.12.1/common/bitutil/compress_test.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2017 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from common/bitutil/compress_test.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package bitutil
    22  
    23  import (
    24  	"bytes"
    25  	"math/rand"
    26  	"testing"
    27  
    28  	"github.com/klaytn/klaytn/common/hexutil"
    29  )
    30  
    31  // Tests that data bitset encoding and decoding works and is bijective.
    32  func TestEncodingCycle(t *testing.T) {
    33  	tests := []string{
    34  		// Tests generated by go-fuzz to maximize code coverage
    35  		"0x000000000000000000",
    36  		"0xef0400",
    37  		"0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb",
    38  		"0x7b64000000",
    39  		"0x000034000000000000",
    40  		"0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f0000000000000000000",
    41  		"0x4912385c0e7b64000000",
    42  		"0x000034000000000000000000000000000000",
    43  		"0x00",
    44  		"0x000003e834ff7f0000",
    45  		"0x0000",
    46  		"0x0000000000000000000000000000000000000000000000000000000000ff00",
    47  		"0x895f0c6a020f850c6a020f85f88df88d",
    48  		"0xdf7070533534333636313639343638373432313536346c1bc3315aac2f65fefb",
    49  		"0x0000000000",
    50  		"0xdf70706336346c65fefb",
    51  		"0x00006d643634000000",
    52  		"0xdf7070533534333636313639343638373532313536346c1bc333393438373130707063363430353639343638373532313536346c1bc333393438336336346c65fe",
    53  	}
    54  	for i, tt := range tests {
    55  		data := hexutil.MustDecode(tt)
    56  
    57  		proc, err := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
    58  		if err != nil {
    59  			t.Errorf("test %d: failed to decompress compressed data: %v", i, err)
    60  			continue
    61  		}
    62  		if !bytes.Equal(data, proc) {
    63  			t.Errorf("test %d: compress/decompress mismatch: have %x, want %x", i, proc, data)
    64  		}
    65  	}
    66  }
    67  
    68  // Tests that data bitset decoding and rencoding works and is bijective.
    69  func TestDecodingCycle(t *testing.T) {
    70  	tests := []struct {
    71  		size  int
    72  		input string
    73  		fail  error
    74  	}{
    75  		{size: 0, input: "0x"},
    76  
    77  		// Crashers generated by go-fuzz
    78  		{size: 0, input: "0x0020", fail: errUnreferencedData},
    79  		{size: 0, input: "0x30", fail: errUnreferencedData},
    80  		{size: 1, input: "0x00", fail: errUnreferencedData},
    81  		{size: 2, input: "0x07", fail: errMissingData},
    82  		{size: 1024, input: "0x8000", fail: errZeroContent},
    83  
    84  		// Tests generated by go-fuzz to maximize code coverage
    85  		{size: 29490, input: "0x343137343733323134333839373334323073333930783e3078333930783e70706336346c65303e", fail: errMissingData},
    86  		{size: 59395, input: "0x00", fail: errUnreferencedData},
    87  		{size: 52574, input: "0x70706336346c65c0de", fail: errExceededTarget},
    88  		{size: 42264, input: "0x07", fail: errMissingData},
    89  		{size: 52, input: "0xa5045bad48f4", fail: errExceededTarget},
    90  		{size: 52574, input: "0xc0de", fail: errMissingData},
    91  		{size: 52574, input: "0x"},
    92  		{size: 29490, input: "0x34313734373332313433383937333432307333393078073034333839373334323073333930783e3078333937333432307333393078073061333930783e70706336346c65303e", fail: errMissingData},
    93  		{size: 29491, input: "0x3973333930783e30783e", fail: errMissingData},
    94  
    95  		{size: 1024, input: "0x808080608080"},
    96  		{size: 1024, input: "0x808470705e3632383337363033313434303137393130306c6580ef46806380635a80"},
    97  		{size: 1024, input: "0x8080808070"},
    98  		{size: 1024, input: "0x808070705e36346c6580ef46806380635a80"},
    99  		{size: 1024, input: "0x80808046802680"},
   100  		{size: 1024, input: "0x4040404035"},
   101  		{size: 1024, input: "0x4040bf3ba2b3f684402d353234373438373934409fe5b1e7ada94ebfd7d0505e27be4035"},
   102  		{size: 1024, input: "0x404040bf3ba2b3f6844035"},
   103  		{size: 1024, input: "0x40402d35323437343837393440bfd7d0505e27be4035"},
   104  	}
   105  	for i, tt := range tests {
   106  		data := hexutil.MustDecode(tt.input)
   107  
   108  		orig, err := bitsetDecodeBytes(data, tt.size)
   109  		if err != tt.fail {
   110  			t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.fail)
   111  		}
   112  		if err != nil {
   113  			continue
   114  		}
   115  		if comp := bitsetEncodeBytes(orig); !bytes.Equal(comp, data) {
   116  			t.Errorf("test %d: decompress/compress mismatch: have %x, want %x", i, comp, data)
   117  		}
   118  	}
   119  }
   120  
   121  // TestCompression tests that compression works by returning either the bitset
   122  // encoded input, or the actual input if the bitset version is longer.
   123  func TestCompression(t *testing.T) {
   124  	// Check the compression returns the bitset encoding is shorter
   125  	in := hexutil.MustDecode("0x4912385c0e7b64000000")
   126  	out := hexutil.MustDecode("0x80fe4912385c0e7b64")
   127  
   128  	if data := CompressBytes(in); !bytes.Equal(data, out) {
   129  		t.Errorf("encoding mismatch for sparse data: have %x, want %x", data, out)
   130  	}
   131  	if data, err := DecompressBytes(out, len(in)); err != nil || !bytes.Equal(data, in) {
   132  		t.Errorf("decoding mismatch for sparse data: have %x, want %x, error %v", data, in, err)
   133  	}
   134  	// Check the compression returns the input if the bitset encoding is longer
   135  	in = hexutil.MustDecode("0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb")
   136  	out = hexutil.MustDecode("0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb")
   137  
   138  	if data := CompressBytes(in); !bytes.Equal(data, out) {
   139  		t.Errorf("encoding mismatch for dense data: have %x, want %x", data, out)
   140  	}
   141  	if data, err := DecompressBytes(out, len(in)); err != nil || !bytes.Equal(data, in) {
   142  		t.Errorf("decoding mismatch for dense data: have %x, want %x, error %v", data, in, err)
   143  	}
   144  	// Check that decompressing a longer input than the target fails
   145  	if _, err := DecompressBytes([]byte{0xc0, 0x01, 0x01}, 2); err != errExceededTarget {
   146  		t.Errorf("decoding error mismatch for long data: have %v, want %v", err, errExceededTarget)
   147  	}
   148  }
   149  
   150  // Crude benchmark for compressing random slices of bytes.
   151  func BenchmarkEncoding1KBVerySparse(b *testing.B) { benchmarkEncoding(b, 1024, 0.0001) }
   152  func BenchmarkEncoding2KBVerySparse(b *testing.B) { benchmarkEncoding(b, 2048, 0.0001) }
   153  func BenchmarkEncoding4KBVerySparse(b *testing.B) { benchmarkEncoding(b, 4096, 0.0001) }
   154  
   155  func BenchmarkEncoding1KBSparse(b *testing.B) { benchmarkEncoding(b, 1024, 0.001) }
   156  func BenchmarkEncoding2KBSparse(b *testing.B) { benchmarkEncoding(b, 2048, 0.001) }
   157  func BenchmarkEncoding4KBSparse(b *testing.B) { benchmarkEncoding(b, 4096, 0.001) }
   158  
   159  func BenchmarkEncoding1KBDense(b *testing.B) { benchmarkEncoding(b, 1024, 0.1) }
   160  func BenchmarkEncoding2KBDense(b *testing.B) { benchmarkEncoding(b, 2048, 0.1) }
   161  func BenchmarkEncoding4KBDense(b *testing.B) { benchmarkEncoding(b, 4096, 0.1) }
   162  
   163  func BenchmarkEncoding1KBSaturated(b *testing.B) { benchmarkEncoding(b, 1024, 0.5) }
   164  func BenchmarkEncoding2KBSaturated(b *testing.B) { benchmarkEncoding(b, 2048, 0.5) }
   165  func BenchmarkEncoding4KBSaturated(b *testing.B) { benchmarkEncoding(b, 4096, 0.5) }
   166  
   167  func benchmarkEncoding(b *testing.B, bytes int, fill float64) {
   168  	// Generate a random slice of bytes to compress
   169  	random := rand.NewSource(0) // reproducible and comparable
   170  
   171  	data := make([]byte, bytes)
   172  	bits := int(float64(bytes) * 8 * fill)
   173  
   174  	for i := 0; i < bits; i++ {
   175  		idx := random.Int63() % int64(len(data))
   176  		bit := uint(random.Int63() % 8)
   177  		data[idx] |= 1 << bit
   178  	}
   179  	// Reset the benchmark and measure encoding/decoding
   180  	b.ResetTimer()
   181  	b.ReportAllocs()
   182  	for i := 0; i < b.N; i++ {
   183  		bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
   184  	}
   185  }