github.com/klaytn/klaytn@v1.12.1/storage/statedb/encoding_test.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2014 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 trie/encoding_test.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package statedb
    22  
    23  import (
    24  	"bytes"
    25  	"testing"
    26  )
    27  
    28  func TestHexCompact(t *testing.T) {
    29  	tests := []struct{ hex, compact []byte }{
    30  		// empty keys, with and without terminator.
    31  		{hex: []byte{}, compact: []byte{0x00}},
    32  		{hex: []byte{16}, compact: []byte{0x20}},
    33  		// odd length, no terminator
    34  		{hex: []byte{1, 2, 3, 4, 5}, compact: []byte{0x11, 0x23, 0x45}},
    35  		// even length, no terminator
    36  		{hex: []byte{0, 1, 2, 3, 4, 5}, compact: []byte{0x00, 0x01, 0x23, 0x45}},
    37  		// odd length, terminator
    38  		{hex: []byte{15, 1, 12, 11, 8, 16 /*term*/}, compact: []byte{0x3f, 0x1c, 0xb8}},
    39  		// even length, terminator
    40  		{hex: []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}, compact: []byte{0x20, 0x0f, 0x1c, 0xb8}},
    41  	}
    42  	for _, test := range tests {
    43  		if c := hexToCompact(test.hex); !bytes.Equal(c, test.compact) {
    44  			t.Errorf("hexToCompact(%x) -> %x, want %x", test.hex, c, test.compact)
    45  		}
    46  		if h := compactToHex(test.compact); !bytes.Equal(h, test.hex) {
    47  			t.Errorf("compactToHex(%x) -> %x, want %x", test.compact, h, test.hex)
    48  		}
    49  	}
    50  }
    51  
    52  func TestHexKeybytes(t *testing.T) {
    53  	tests := []struct{ key, hexIn, hexOut []byte }{
    54  		{key: []byte{}, hexIn: []byte{16}, hexOut: []byte{16}},
    55  		{key: []byte{}, hexIn: []byte{}, hexOut: []byte{16}},
    56  		{
    57  			key:    []byte{0x12, 0x34, 0x56},
    58  			hexIn:  []byte{1, 2, 3, 4, 5, 6, 16},
    59  			hexOut: []byte{1, 2, 3, 4, 5, 6, 16},
    60  		},
    61  		{
    62  			key:    []byte{0x12, 0x34, 0x5},
    63  			hexIn:  []byte{1, 2, 3, 4, 0, 5, 16},
    64  			hexOut: []byte{1, 2, 3, 4, 0, 5, 16},
    65  		},
    66  		{
    67  			key:    []byte{0x12, 0x34, 0x56},
    68  			hexIn:  []byte{1, 2, 3, 4, 5, 6},
    69  			hexOut: []byte{1, 2, 3, 4, 5, 6, 16},
    70  		},
    71  	}
    72  	for _, test := range tests {
    73  		if h := keybytesToHex(test.key); !bytes.Equal(h, test.hexOut) {
    74  			t.Errorf("keybytesToHex(%x) -> %x, want %x", test.key, h, test.hexOut)
    75  		}
    76  		if k := hexToKeybytes(test.hexIn); !bytes.Equal(k, test.key) {
    77  			t.Errorf("hexToKeybytes(%x) -> %x, want %x", test.hexIn, k, test.key)
    78  		}
    79  	}
    80  }
    81  
    82  func BenchmarkHexToCompact(b *testing.B) {
    83  	testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}
    84  	for i := 0; i < b.N; i++ {
    85  		hexToCompact(testBytes)
    86  	}
    87  }
    88  
    89  func BenchmarkCompactToHex(b *testing.B) {
    90  	testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}
    91  	for i := 0; i < b.N; i++ {
    92  		compactToHex(testBytes)
    93  	}
    94  }
    95  
    96  func BenchmarkKeybytesToHex(b *testing.B) {
    97  	testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
    98  	for i := 0; i < b.N; i++ {
    99  		keybytesToHex(testBytes)
   100  	}
   101  }
   102  
   103  func BenchmarkHexToKeybytes(b *testing.B) {
   104  	testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
   105  	for i := 0; i < b.N; i++ {
   106  		hexToKeybytes(testBytes)
   107  	}
   108  }