github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/trie/encoding_test.go (about)

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