github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/trie/encoding_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package trie
    13  
    14  import (
    15  	"bytes"
    16  	"testing"
    17  )
    18  
    19  func TestHexCompact(t *testing.T) {
    20  	tests := []struct{ hex, compact []byte }{
    21  		// empty keys, with and without terminator.
    22  		{hex: []byte{}, compact: []byte{0x00}},
    23  		{hex: []byte{16}, compact: []byte{0x20}},
    24  		// odd length, no terminator
    25  		{hex: []byte{1, 2, 3, 4, 5}, compact: []byte{0x11, 0x23, 0x45}},
    26  		// even length, no terminator
    27  		{hex: []byte{0, 1, 2, 3, 4, 5}, compact: []byte{0x00, 0x01, 0x23, 0x45}},
    28  		// odd length, terminator
    29  		{hex: []byte{15, 1, 12, 11, 8, 16 /*term*/}, compact: []byte{0x3f, 0x1c, 0xb8}},
    30  		// even length, terminator
    31  		{hex: []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}, compact: []byte{0x20, 0x0f, 0x1c, 0xb8}},
    32  	}
    33  	for _, test := range tests {
    34  		if c := hexToCompact(test.hex); !bytes.Equal(c, test.compact) {
    35  			t.Errorf("hexToCompact(%x) -> %x, want %x", test.hex, c, test.compact)
    36  		}
    37  		if h := compactToHex(test.compact); !bytes.Equal(h, test.hex) {
    38  			t.Errorf("compactToHex(%x) -> %x, want %x", test.compact, h, test.hex)
    39  		}
    40  	}
    41  }
    42  
    43  func TestHexKeybytes(t *testing.T) {
    44  	tests := []struct{ key, hexIn, hexOut []byte }{
    45  		{key: []byte{}, hexIn: []byte{16}, hexOut: []byte{16}},
    46  		{key: []byte{}, hexIn: []byte{}, hexOut: []byte{16}},
    47  		{
    48  			key:    []byte{0x12, 0x34, 0x56},
    49  			hexIn:  []byte{1, 2, 3, 4, 5, 6, 16},
    50  			hexOut: []byte{1, 2, 3, 4, 5, 6, 16},
    51  		},
    52  		{
    53  			key:    []byte{0x12, 0x34, 0x5},
    54  			hexIn:  []byte{1, 2, 3, 4, 0, 5, 16},
    55  			hexOut: []byte{1, 2, 3, 4, 0, 5, 16},
    56  		},
    57  		{
    58  			key:    []byte{0x12, 0x34, 0x56},
    59  			hexIn:  []byte{1, 2, 3, 4, 5, 6},
    60  			hexOut: []byte{1, 2, 3, 4, 5, 6, 16},
    61  		},
    62  	}
    63  	for _, test := range tests {
    64  		if h := keybytesToHex(test.key); !bytes.Equal(h, test.hexOut) {
    65  			t.Errorf("keybytesToHex(%x) -> %x, want %x", test.key, h, test.hexOut)
    66  		}
    67  		if k := hexToKeybytes(test.hexIn); !bytes.Equal(k, test.key) {
    68  			t.Errorf("hexToKeybytes(%x) -> %x, want %x", test.hexIn, k, test.key)
    69  		}
    70  	}
    71  }
    72  
    73  func BenchmarkHexToCompact(b *testing.B) {
    74  	testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}
    75  	for i := 0; i < b.N; i++ {
    76  		hexToCompact(testBytes)
    77  	}
    78  }
    79  
    80  func BenchmarkCompactToHex(b *testing.B) {
    81  	testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}
    82  	for i := 0; i < b.N; i++ {
    83  		compactToHex(testBytes)
    84  	}
    85  }
    86  
    87  func BenchmarkKeybytesToHex(b *testing.B) {
    88  	testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
    89  	for i := 0; i < b.N; i++ {
    90  		keybytesToHex(testBytes)
    91  	}
    92  }
    93  
    94  func BenchmarkHexToKeybytes(b *testing.B) {
    95  	testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
    96  	for i := 0; i < b.N; i++ {
    97  		hexToKeybytes(testBytes)
    98  	}
    99  }