github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/common/bitutil/compress_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:33</date> 10 //</624450072972627968> 11 12 13 package bitutil 14 15 import ( 16 "bytes" 17 "math/rand" 18 "testing" 19 20 "github.com/ethereum/go-ethereum/common/hexutil" 21 ) 22 23 //测试数据位集编码和解码是否工作并且是双目标的。 24 func TestEncodingCycle(t *testing.T) { 25 tests := []string{ 26 //Go-Fuzz生成的测试以最大限度地提高代码覆盖率 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 49 proc, err := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data)) 50 if err != nil { 51 t.Errorf("test %d: failed to decompress compressed data: %v", i, err) 52 continue 53 } 54 if !bytes.Equal(data, proc) { 55 t.Errorf("test %d: compress/decompress mismatch: have %x, want %x", i, proc, data) 56 } 57 } 58 } 59 60 //测试数据位集解码和重新编码是否工作并且是双目标的。 61 func TestDecodingCycle(t *testing.T) { 62 tests := []struct { 63 size int 64 input string 65 fail error 66 }{ 67 {size: 0, input: "0x"}, 68 69 //由Go-Fuzz产生的碰撞器 70 {size: 0, input: "0x0020", fail: errUnreferencedData}, 71 {size: 0, input: "0x30", fail: errUnreferencedData}, 72 {size: 1, input: "0x00", fail: errUnreferencedData}, 73 {size: 2, input: "0x07", fail: errMissingData}, 74 {size: 1024, input: "0x8000", fail: errZeroContent}, 75 76 //Go-Fuzz生成的测试以最大限度地提高代码覆盖率 77 {size: 29490, input: "0x343137343733323134333839373334323073333930783e3078333930783e70706336346c65303e", fail: errMissingData}, 78 {size: 59395, input: "0x00", fail: errUnreferencedData}, 79 {size: 52574, input: "0x70706336346c65c0de", fail: errExceededTarget}, 80 {size: 42264, input: "0x07", fail: errMissingData}, 81 {size: 52, input: "0xa5045bad48f4", fail: errExceededTarget}, 82 {size: 52574, input: "0xc0de", fail: errMissingData}, 83 {size: 52574, input: "0x"}, 84 {size: 29490, input: "0x34313734373332313433383937333432307333393078073034333839373334323073333930783e3078333937333432307333393078073061333930783e70706336346c65303e", fail: errMissingData}, 85 {size: 29491, input: "0x3973333930783e30783e", fail: errMissingData}, 86 87 {size: 1024, input: "0x808080608080"}, 88 {size: 1024, input: "0x808470705e3632383337363033313434303137393130306c6580ef46806380635a80"}, 89 {size: 1024, input: "0x8080808070"}, 90 {size: 1024, input: "0x808070705e36346c6580ef46806380635a80"}, 91 {size: 1024, input: "0x80808046802680"}, 92 {size: 1024, input: "0x4040404035"}, 93 {size: 1024, input: "0x4040bf3ba2b3f684402d353234373438373934409fe5b1e7ada94ebfd7d0505e27be4035"}, 94 {size: 1024, input: "0x404040bf3ba2b3f6844035"}, 95 {size: 1024, input: "0x40402d35323437343837393440bfd7d0505e27be4035"}, 96 } 97 for i, tt := range tests { 98 data := hexutil.MustDecode(tt.input) 99 100 orig, err := bitsetDecodeBytes(data, tt.size) 101 if err != tt.fail { 102 t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.fail) 103 } 104 if err != nil { 105 continue 106 } 107 if comp := bitsetEncodeBytes(orig); !bytes.Equal(comp, data) { 108 t.Errorf("test %d: decompress/compress mismatch: have %x, want %x", i, comp, data) 109 } 110 } 111 } 112 113 //testcompression通过返回位集来测试压缩是否有效 114 //编码输入,或实际输入(如果位集版本较长)。 115 func TestCompression(t *testing.T) { 116 //检查压缩返回的位集编码较短 117 in := hexutil.MustDecode("0x4912385c0e7b64000000") 118 out := hexutil.MustDecode("0x80fe4912385c0e7b64") 119 120 if data := CompressBytes(in); !bytes.Equal(data, out) { 121 t.Errorf("encoding mismatch for sparse data: have %x, want %x", data, out) 122 } 123 if data, err := DecompressBytes(out, len(in)); err != nil || !bytes.Equal(data, in) { 124 t.Errorf("decoding mismatch for sparse data: have %x, want %x, error %v", data, in, err) 125 } 126 //如果位集编码较长,则检查压缩返回输入 127 in = hexutil.MustDecode("0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb") 128 out = hexutil.MustDecode("0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb") 129 130 if data := CompressBytes(in); !bytes.Equal(data, out) { 131 t.Errorf("encoding mismatch for dense data: have %x, want %x", data, out) 132 } 133 if data, err := DecompressBytes(out, len(in)); err != nil || !bytes.Equal(data, in) { 134 t.Errorf("decoding mismatch for dense data: have %x, want %x, error %v", data, in, err) 135 } 136 //检查是否解压缩比目标长的输入失败 137 if _, err := DecompressBytes([]byte{0xc0, 0x01, 0x01}, 2); err != errExceededTarget { 138 t.Errorf("decoding error mismatch for long data: have %v, want %v", err, errExceededTarget) 139 } 140 } 141 142 //压缩随机字节片的原始基准。 143 func BenchmarkEncoding1KBVerySparse(b *testing.B) { benchmarkEncoding(b, 1024, 0.0001) } 144 func BenchmarkEncoding2KBVerySparse(b *testing.B) { benchmarkEncoding(b, 2048, 0.0001) } 145 func BenchmarkEncoding4KBVerySparse(b *testing.B) { benchmarkEncoding(b, 4096, 0.0001) } 146 147 func BenchmarkEncoding1KBSparse(b *testing.B) { benchmarkEncoding(b, 1024, 0.001) } 148 func BenchmarkEncoding2KBSparse(b *testing.B) { benchmarkEncoding(b, 2048, 0.001) } 149 func BenchmarkEncoding4KBSparse(b *testing.B) { benchmarkEncoding(b, 4096, 0.001) } 150 151 func BenchmarkEncoding1KBDense(b *testing.B) { benchmarkEncoding(b, 1024, 0.1) } 152 func BenchmarkEncoding2KBDense(b *testing.B) { benchmarkEncoding(b, 2048, 0.1) } 153 func BenchmarkEncoding4KBDense(b *testing.B) { benchmarkEncoding(b, 4096, 0.1) } 154 155 func BenchmarkEncoding1KBSaturated(b *testing.B) { benchmarkEncoding(b, 1024, 0.5) } 156 func BenchmarkEncoding2KBSaturated(b *testing.B) { benchmarkEncoding(b, 2048, 0.5) } 157 func BenchmarkEncoding4KBSaturated(b *testing.B) { benchmarkEncoding(b, 4096, 0.5) } 158 159 func benchmarkEncoding(b *testing.B, bytes int, fill float64) { 160 //生成要压缩的随机字节片 161 random := rand.NewSource(0) //可重复性和可比性 162 163 data := make([]byte, bytes) 164 bits := int(float64(bytes) * 8 * fill) 165 166 for i := 0; i < bits; i++ { 167 idx := random.Int63() % int64(len(data)) 168 bit := uint(random.Int63() % 8) 169 data[idx] |= 1 << bit 170 } 171 //重新设置基准并测量编码/解码 172 b.ResetTimer() 173 b.ReportAllocs() 174 for i := 0; i < b.N; i++ { 175 bitsetDecodeBytes(bitsetEncodeBytes(data), len(data)) 176 } 177 } 178