github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/common/bitutil/compress_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2017 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package bitutil
    26  
    27  import (
    28  	"bytes"
    29  	"math/rand"
    30  	"testing"
    31  
    32  	"github.com/ethereum/go-ethereum/common/hexutil"
    33  )
    34  
    35  //
    36  func TestEncodingCycle(t *testing.T) {
    37  	tests := []string{
    38  //Go-Fuzz生成的测试以最大限度地提高代码覆盖率
    39  		"0x000000000000000000",
    40  		"0xef0400",
    41  		"0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb",
    42  		"0x7b64000000",
    43  		"0x000034000000000000",
    44  		"0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f0000000000000000000",
    45  		"0x4912385c0e7b64000000",
    46  		"0x000034000000000000000000000000000000",
    47  		"0x00",
    48  		"0x000003e834ff7f0000",
    49  		"0x0000",
    50  		"0x0000000000000000000000000000000000000000000000000000000000ff00",
    51  		"0x895f0c6a020f850c6a020f85f88df88d",
    52  		"0xdf7070533534333636313639343638373432313536346c1bc3315aac2f65fefb",
    53  		"0x0000000000",
    54  		"0xdf70706336346c65fefb",
    55  		"0x00006d643634000000",
    56  		"0xdf7070533534333636313639343638373532313536346c1bc333393438373130707063363430353639343638373532313536346c1bc333393438336336346c65fe",
    57  	}
    58  	for i, tt := range tests {
    59  		data := hexutil.MustDecode(tt)
    60  
    61  		proc, err := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
    62  		if err != nil {
    63  			t.Errorf("test %d: failed to decompress compressed data: %v", i, err)
    64  			continue
    65  		}
    66  		if !bytes.Equal(data, proc) {
    67  			t.Errorf("test %d: compress/decompress mismatch: have %x, want %x", i, proc, data)
    68  		}
    69  	}
    70  }
    71  
    72  //测试数据位集解码和重新编码是否工作并且是双目标的。
    73  func TestDecodingCycle(t *testing.T) {
    74  	tests := []struct {
    75  		size  int
    76  		input string
    77  		fail  error
    78  	}{
    79  		{size: 0, input: "0x"},
    80  
    81  //由Go-Fuzz产生的碰撞器
    82  		{size: 0, input: "0x0020", fail: errUnreferencedData},
    83  		{size: 0, input: "0x30", fail: errUnreferencedData},
    84  		{size: 1, input: "0x00", fail: errUnreferencedData},
    85  		{size: 2, input: "0x07", fail: errMissingData},
    86  		{size: 1024, input: "0x8000", fail: errZeroContent},
    87  
    88  //Go-Fuzz生成的测试以最大限度地提高代码覆盖率
    89  		{size: 29490, input: "0x343137343733323134333839373334323073333930783e3078333930783e70706336346c65303e", fail: errMissingData},
    90  		{size: 59395, input: "0x00", fail: errUnreferencedData},
    91  		{size: 52574, input: "0x70706336346c65c0de", fail: errExceededTarget},
    92  		{size: 42264, input: "0x07", fail: errMissingData},
    93  		{size: 52, input: "0xa5045bad48f4", fail: errExceededTarget},
    94  		{size: 52574, input: "0xc0de", fail: errMissingData},
    95  		{size: 52574, input: "0x"},
    96  		{size: 29490, input: "0x34313734373332313433383937333432307333393078073034333839373334323073333930783e3078333937333432307333393078073061333930783e70706336346c65303e", fail: errMissingData},
    97  		{size: 29491, input: "0x3973333930783e30783e", fail: errMissingData},
    98  
    99  		{size: 1024, input: "0x808080608080"},
   100  		{size: 1024, input: "0x808470705e3632383337363033313434303137393130306c6580ef46806380635a80"},
   101  		{size: 1024, input: "0x8080808070"},
   102  		{size: 1024, input: "0x808070705e36346c6580ef46806380635a80"},
   103  		{size: 1024, input: "0x80808046802680"},
   104  		{size: 1024, input: "0x4040404035"},
   105  		{size: 1024, input: "0x4040bf3ba2b3f684402d353234373438373934409fe5b1e7ada94ebfd7d0505e27be4035"},
   106  		{size: 1024, input: "0x404040bf3ba2b3f6844035"},
   107  		{size: 1024, input: "0x40402d35323437343837393440bfd7d0505e27be4035"},
   108  	}
   109  	for i, tt := range tests {
   110  		data := hexutil.MustDecode(tt.input)
   111  
   112  		orig, err := bitsetDecodeBytes(data, tt.size)
   113  		if err != tt.fail {
   114  			t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.fail)
   115  		}
   116  		if err != nil {
   117  			continue
   118  		}
   119  		if comp := bitsetEncodeBytes(orig); !bytes.Equal(comp, data) {
   120  			t.Errorf("test %d: decompress/compress mismatch: have %x, want %x", i, comp, data)
   121  		}
   122  	}
   123  }
   124  
   125  //testcompression通过返回位集来测试压缩是否有效
   126  //编码输入,或实际输入(如果位集版本较长)。
   127  func TestCompression(t *testing.T) {
   128  //检查压缩返回的位集编码是否更短
   129  	in := hexutil.MustDecode("0x4912385c0e7b64000000")
   130  	out := hexutil.MustDecode("0x80fe4912385c0e7b64")
   131  
   132  	if data := CompressBytes(in); !bytes.Equal(data, out) {
   133  		t.Errorf("encoding mismatch for sparse data: have %x, want %x", data, out)
   134  	}
   135  	if data, err := DecompressBytes(out, len(in)); err != nil || !bytes.Equal(data, in) {
   136  		t.Errorf("decoding mismatch for sparse data: have %x, want %x, error %v", data, in, err)
   137  	}
   138  //如果位集编码较长,则检查压缩是否返回输入
   139  	in = hexutil.MustDecode("0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb")
   140  	out = hexutil.MustDecode("0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb")
   141  
   142  	if data := CompressBytes(in); !bytes.Equal(data, out) {
   143  		t.Errorf("encoding mismatch for dense data: have %x, want %x", data, out)
   144  	}
   145  	if data, err := DecompressBytes(out, len(in)); err != nil || !bytes.Equal(data, in) {
   146  		t.Errorf("decoding mismatch for dense data: have %x, want %x, error %v", data, in, err)
   147  	}
   148  //检查是否解压缩比目标长的输入失败
   149  	if _, err := DecompressBytes([]byte{0xc0, 0x01, 0x01}, 2); err != errExceededTarget {
   150  		t.Errorf("decoding error mismatch for long data: have %v, want %v", err, errExceededTarget)
   151  	}
   152  }
   153  
   154  //压缩随机字节片的原始基准。
   155  func BenchmarkEncoding1KBVerySparse(b *testing.B) { benchmarkEncoding(b, 1024, 0.0001) }
   156  func BenchmarkEncoding2KBVerySparse(b *testing.B) { benchmarkEncoding(b, 2048, 0.0001) }
   157  func BenchmarkEncoding4KBVerySparse(b *testing.B) { benchmarkEncoding(b, 4096, 0.0001) }
   158  
   159  func BenchmarkEncoding1KBSparse(b *testing.B) { benchmarkEncoding(b, 1024, 0.001) }
   160  func BenchmarkEncoding2KBSparse(b *testing.B) { benchmarkEncoding(b, 2048, 0.001) }
   161  func BenchmarkEncoding4KBSparse(b *testing.B) { benchmarkEncoding(b, 4096, 0.001) }
   162  
   163  func BenchmarkEncoding1KBDense(b *testing.B) { benchmarkEncoding(b, 1024, 0.1) }
   164  func BenchmarkEncoding2KBDense(b *testing.B) { benchmarkEncoding(b, 2048, 0.1) }
   165  func BenchmarkEncoding4KBDense(b *testing.B) { benchmarkEncoding(b, 4096, 0.1) }
   166  
   167  func BenchmarkEncoding1KBSaturated(b *testing.B) { benchmarkEncoding(b, 1024, 0.5) }
   168  func BenchmarkEncoding2KBSaturated(b *testing.B) { benchmarkEncoding(b, 2048, 0.5) }
   169  func BenchmarkEncoding4KBSaturated(b *testing.B) { benchmarkEncoding(b, 4096, 0.5) }
   170  
   171  func benchmarkEncoding(b *testing.B, bytes int, fill float64) {
   172  //生成要压缩的随机字节片
   173  random := rand.NewSource(0) //可重复性和可比性
   174  
   175  	data := make([]byte, bytes)
   176  	bits := int(float64(bytes) * 8 * fill)
   177  
   178  	for i := 0; i < bits; i++ {
   179  		idx := random.Int63() % int64(len(data))
   180  		bit := uint(random.Int63() % 8)
   181  		data[idx] |= 1 << bit
   182  	}
   183  //重新设置基准并测量编码/解码
   184  	b.ResetTimer()
   185  	b.ReportAllocs()
   186  	for i := 0; i < b.N; i++ {
   187  		bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
   188  	}
   189  }