github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/trie/encoding_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:45</date>
    10  //</624450122809348096>
    11  
    12  
    13  package trie
    14  
    15  import (
    16  	"bytes"
    17  	"testing"
    18  )
    19  
    20  func TestHexCompact(t *testing.T) {
    21  	tests := []struct{ hex, compact []byte }{
    22  //空键,带或不带终止符。
    23  		{hex: []byte{}, compact: []byte{0x00}},
    24  		{hex: []byte{16}, compact: []byte{0x20}},
    25  //奇数长度,无终止符
    26  		{hex: []byte{1, 2, 3, 4, 5}, compact: []byte{0x11, 0x23, 0x45}},
    27  //长度均匀,无终止符
    28  		{hex: []byte{0, 1, 2, 3, 4, 5}, compact: []byte{0x00, 0x01, 0x23, 0x45}},
    29  //奇数长度,终止符
    30    /*x:[]字节15、1、12、11、8、16/*术语*/,压缩:[]字节0x3f、0x1c、0xb8,
    31    //偶数长度,终止符
    32    十六进制:[]字节0、15、1、12、11、8、16/*ter*/}, compact: []byte{0x20, 0x0f, 0x1c, 0xb8}},
    33  
    34  	}
    35  	for _, test := range tests {
    36  		if c := hexToCompact(test.hex); !bytes.Equal(c, test.compact) {
    37  			t.Errorf("hexToCompact(%x) -> %x, want %x", test.hex, c, test.compact)
    38  		}
    39  		if h := compactToHex(test.compact); !bytes.Equal(h, test.hex) {
    40  			t.Errorf("compactToHex(%x) -> %x, want %x", test.compact, h, test.hex)
    41  		}
    42  	}
    43  }
    44  
    45  func TestHexKeybytes(t *testing.T) {
    46  	tests := []struct{ key, hexIn, hexOut []byte }{
    47  		{key: []byte{}, hexIn: []byte{16}, hexOut: []byte{16}},
    48  		{key: []byte{}, hexIn: []byte{}, hexOut: []byte{16}},
    49  		{
    50  			key:    []byte{0x12, 0x34, 0x56},
    51  			hexIn:  []byte{1, 2, 3, 4, 5, 6, 16},
    52  			hexOut: []byte{1, 2, 3, 4, 5, 6, 16},
    53  		},
    54  		{
    55  			key:    []byte{0x12, 0x34, 0x5},
    56  			hexIn:  []byte{1, 2, 3, 4, 0, 5, 16},
    57  			hexOut: []byte{1, 2, 3, 4, 0, 5, 16},
    58  		},
    59  		{
    60  			key:    []byte{0x12, 0x34, 0x56},
    61  			hexIn:  []byte{1, 2, 3, 4, 5, 6},
    62  			hexOut: []byte{1, 2, 3, 4, 5, 6, 16},
    63  		},
    64  	}
    65  	for _, test := range tests {
    66  		if h := keybytesToHex(test.key); !bytes.Equal(h, test.hexOut) {
    67  			t.Errorf("keybytesToHex(%x) -> %x, want %x", test.key, h, test.hexOut)
    68  		}
    69  		if k := hexToKeybytes(test.hexIn); !bytes.Equal(k, test.key) {
    70  			t.Errorf("hexToKeybytes(%x) -> %x, want %x", test.hexIn, k, test.key)
    71  		}
    72  	}
    73  }
    74  
    75  func BenchmarkHexToCompact(b *testing.B) {
    76   /*tbytes:=[]字节0、15、1、12、11、8、16/*术语*/
    77   对于i:=0;i<b.n;i++
    78    HextoCompact(测试字节)
    79   }
    80  }
    81  
    82  func基准压缩到hex(b*testing.b)
    83   测试字节:=[]字节0、15、1、12、11、8、16/*ter*/}
    84  
    85  	for i := 0; i < b.N; i++ {
    86  		compactToHex(testBytes)
    87  	}
    88  }
    89  
    90  func BenchmarkKeybytesToHex(b *testing.B) {
    91  	testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
    92  	for i := 0; i < b.N; i++ {
    93  		keybytesToHex(testBytes)
    94  	}
    95  }
    96  
    97  func BenchmarkHexToKeybytes(b *testing.B) {
    98  	testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
    99  	for i := 0; i < b.N; i++ {
   100  		hexToKeybytes(testBytes)
   101  	}
   102  }
   103