github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/common/bitutil/compress_test.go (about)

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