github.com/calmw/ethereum@v0.1.1/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 crand "crypto/rand" 22 "encoding/hex" 23 "math/rand" 24 "testing" 25 ) 26 27 func TestHexCompact(t *testing.T) { 28 tests := []struct{ hex, compact []byte }{ 29 // empty keys, with and without terminator. 30 {hex: []byte{}, compact: []byte{0x00}}, 31 {hex: []byte{16}, compact: []byte{0x20}}, 32 // odd length, no terminator 33 {hex: []byte{1, 2, 3, 4, 5}, compact: []byte{0x11, 0x23, 0x45}}, 34 // even length, no terminator 35 {hex: []byte{0, 1, 2, 3, 4, 5}, compact: []byte{0x00, 0x01, 0x23, 0x45}}, 36 // odd length, terminator 37 {hex: []byte{15, 1, 12, 11, 8, 16 /*term*/}, compact: []byte{0x3f, 0x1c, 0xb8}}, 38 // even length, terminator 39 {hex: []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}, compact: []byte{0x20, 0x0f, 0x1c, 0xb8}}, 40 } 41 for _, test := range tests { 42 if c := hexToCompact(test.hex); !bytes.Equal(c, test.compact) { 43 t.Errorf("hexToCompact(%x) -> %x, want %x", test.hex, c, test.compact) 44 } 45 if h := compactToHex(test.compact); !bytes.Equal(h, test.hex) { 46 t.Errorf("compactToHex(%x) -> %x, want %x", test.compact, h, test.hex) 47 } 48 } 49 } 50 51 func TestHexKeybytes(t *testing.T) { 52 tests := []struct{ key, hexIn, hexOut []byte }{ 53 {key: []byte{}, hexIn: []byte{16}, hexOut: []byte{16}}, 54 {key: []byte{}, hexIn: []byte{}, hexOut: []byte{16}}, 55 { 56 key: []byte{0x12, 0x34, 0x56}, 57 hexIn: []byte{1, 2, 3, 4, 5, 6, 16}, 58 hexOut: []byte{1, 2, 3, 4, 5, 6, 16}, 59 }, 60 { 61 key: []byte{0x12, 0x34, 0x5}, 62 hexIn: []byte{1, 2, 3, 4, 0, 5, 16}, 63 hexOut: []byte{1, 2, 3, 4, 0, 5, 16}, 64 }, 65 { 66 key: []byte{0x12, 0x34, 0x56}, 67 hexIn: []byte{1, 2, 3, 4, 5, 6}, 68 hexOut: []byte{1, 2, 3, 4, 5, 6, 16}, 69 }, 70 } 71 for _, test := range tests { 72 if h := keybytesToHex(test.key); !bytes.Equal(h, test.hexOut) { 73 t.Errorf("keybytesToHex(%x) -> %x, want %x", test.key, h, test.hexOut) 74 } 75 if k := hexToKeybytes(test.hexIn); !bytes.Equal(k, test.key) { 76 t.Errorf("hexToKeybytes(%x) -> %x, want %x", test.hexIn, k, test.key) 77 } 78 } 79 } 80 81 func TestHexToCompactInPlace(t *testing.T) { 82 for i, key := range []string{ 83 "00", 84 "060a040c0f000a090b040803010801010900080d090a0a0d0903000b10", 85 "10", 86 } { 87 hexBytes, _ := hex.DecodeString(key) 88 exp := hexToCompact(hexBytes) 89 sz := hexToCompactInPlace(hexBytes) 90 got := hexBytes[:sz] 91 if !bytes.Equal(exp, got) { 92 t.Fatalf("test %d: encoding err\ninp %v\ngot %x\nexp %x\n", i, key, got, exp) 93 } 94 } 95 } 96 97 func TestHexToCompactInPlaceRandom(t *testing.T) { 98 for i := 0; i < 10000; i++ { 99 l := rand.Intn(128) 100 key := make([]byte, l) 101 crand.Read(key) 102 hexBytes := keybytesToHex(key) 103 hexOrig := []byte(string(hexBytes)) 104 exp := hexToCompact(hexBytes) 105 sz := hexToCompactInPlace(hexBytes) 106 got := hexBytes[:sz] 107 108 if !bytes.Equal(exp, got) { 109 t.Fatalf("encoding err \ncpt %x\nhex %x\ngot %x\nexp %x\n", 110 key, hexOrig, got, exp) 111 } 112 } 113 } 114 115 func BenchmarkHexToCompact(b *testing.B) { 116 testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/} 117 for i := 0; i < b.N; i++ { 118 hexToCompact(testBytes) 119 } 120 } 121 122 func BenchmarkCompactToHex(b *testing.B) { 123 testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/} 124 for i := 0; i < b.N; i++ { 125 compactToHex(testBytes) 126 } 127 } 128 129 func BenchmarkKeybytesToHex(b *testing.B) { 130 testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16} 131 for i := 0; i < b.N; i++ { 132 keybytesToHex(testBytes) 133 } 134 } 135 136 func BenchmarkHexToKeybytes(b *testing.B) { 137 testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16} 138 for i := 0; i < b.N; i++ { 139 hexToKeybytes(testBytes) 140 } 141 }