github.com/ethw3/go-ethereuma@v0.0.0-20221013053120-c14602a4c23c/trie/encoding_test.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package trie 18 19 import ( 20 "bytes" 21 "encoding/hex" 22 "math/rand" 23 "testing" 24 ) 25 26 func TestHexCompact(t *testing.T) { 27 tests := []struct{ hex, compact []byte }{ 28 // empty keys, with and without terminator. 29 {hex: []byte{}, compact: []byte{0x00}}, 30 {hex: []byte{16}, compact: []byte{0x20}}, 31 // odd length, no terminator 32 {hex: []byte{1, 2, 3, 4, 5}, compact: []byte{0x11, 0x23, 0x45}}, 33 // even length, no terminator 34 {hex: []byte{0, 1, 2, 3, 4, 5}, compact: []byte{0x00, 0x01, 0x23, 0x45}}, 35 // odd length, terminator 36 {hex: []byte{15, 1, 12, 11, 8, 16 /*term*/}, compact: []byte{0x3f, 0x1c, 0xb8}}, 37 // even length, terminator 38 {hex: []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}, compact: []byte{0x20, 0x0f, 0x1c, 0xb8}}, 39 } 40 for _, test := range tests { 41 if c := hexToCompact(test.hex); !bytes.Equal(c, test.compact) { 42 t.Errorf("hexToCompact(%x) -> %x, want %x", test.hex, c, test.compact) 43 } 44 if h := compactToHex(test.compact); !bytes.Equal(h, test.hex) { 45 t.Errorf("compactToHex(%x) -> %x, want %x", test.compact, h, test.hex) 46 } 47 } 48 } 49 50 func TestHexKeybytes(t *testing.T) { 51 tests := []struct{ key, hexIn, hexOut []byte }{ 52 {key: []byte{}, hexIn: []byte{16}, hexOut: []byte{16}}, 53 {key: []byte{}, hexIn: []byte{}, hexOut: []byte{16}}, 54 { 55 key: []byte{0x12, 0x34, 0x56}, 56 hexIn: []byte{1, 2, 3, 4, 5, 6, 16}, 57 hexOut: []byte{1, 2, 3, 4, 5, 6, 16}, 58 }, 59 { 60 key: []byte{0x12, 0x34, 0x5}, 61 hexIn: []byte{1, 2, 3, 4, 0, 5, 16}, 62 hexOut: []byte{1, 2, 3, 4, 0, 5, 16}, 63 }, 64 { 65 key: []byte{0x12, 0x34, 0x56}, 66 hexIn: []byte{1, 2, 3, 4, 5, 6}, 67 hexOut: []byte{1, 2, 3, 4, 5, 6, 16}, 68 }, 69 } 70 for _, test := range tests { 71 if h := keybytesToHex(test.key); !bytes.Equal(h, test.hexOut) { 72 t.Errorf("keybytesToHex(%x) -> %x, want %x", test.key, h, test.hexOut) 73 } 74 if k := hexToKeybytes(test.hexIn); !bytes.Equal(k, test.key) { 75 t.Errorf("hexToKeybytes(%x) -> %x, want %x", test.hexIn, k, test.key) 76 } 77 } 78 } 79 80 func TestHexToCompactInPlace(t *testing.T) { 81 for i, keyS := range []string{ 82 "00", 83 "060a040c0f000a090b040803010801010900080d090a0a0d0903000b10", 84 "10", 85 } { 86 hexBytes, _ := hex.DecodeString(keyS) 87 exp := hexToCompact(hexBytes) 88 sz := hexToCompactInPlace(hexBytes) 89 got := hexBytes[:sz] 90 if !bytes.Equal(exp, got) { 91 t.Fatalf("test %d: encoding err\ninp %v\ngot %x\nexp %x\n", i, keyS, got, exp) 92 } 93 } 94 } 95 96 func TestHexToCompactInPlaceRandom(t *testing.T) { 97 for i := 0; i < 10000; i++ { 98 l := rand.Intn(128) 99 key := make([]byte, l) 100 rand.Read(key) 101 hexBytes := keybytesToHex(key) 102 hexOrig := []byte(string(hexBytes)) 103 exp := hexToCompact(hexBytes) 104 sz := hexToCompactInPlace(hexBytes) 105 got := hexBytes[:sz] 106 107 if !bytes.Equal(exp, got) { 108 t.Fatalf("encoding err \ncpt %x\nhex %x\ngot %x\nexp %x\n", 109 key, hexOrig, got, exp) 110 } 111 } 112 } 113 114 func BenchmarkHexToCompact(b *testing.B) { 115 testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/} 116 for i := 0; i < b.N; i++ { 117 hexToCompact(testBytes) 118 } 119 } 120 121 func BenchmarkCompactToHex(b *testing.B) { 122 testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/} 123 for i := 0; i < b.N; i++ { 124 compactToHex(testBytes) 125 } 126 } 127 128 func BenchmarkKeybytesToHex(b *testing.B) { 129 testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16} 130 for i := 0; i < b.N; i++ { 131 keybytesToHex(testBytes) 132 } 133 } 134 135 func BenchmarkHexToKeybytes(b *testing.B) { 136 testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16} 137 for i := 0; i < b.N; i++ { 138 hexToKeybytes(testBytes) 139 } 140 }