github.com/theQRL/go-zond@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 got := hexToCompactInPlace(hexBytes) 90 if !bytes.Equal(exp, got) { 91 t.Fatalf("test %d: encoding err\ninp %v\ngot %x\nexp %x\n", i, key, 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 crand.Read(key) 101 hexBytes := keybytesToHex(key) 102 hexOrig := []byte(string(hexBytes)) 103 exp := hexToCompact(hexBytes) 104 got := hexToCompactInPlace(hexBytes) 105 106 if !bytes.Equal(exp, got) { 107 t.Fatalf("encoding err \ncpt %x\nhex %x\ngot %x\nexp %x\n", 108 key, hexOrig, got, exp) 109 } 110 } 111 } 112 113 func BenchmarkHexToCompact(b *testing.B) { 114 testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/} 115 for i := 0; i < b.N; i++ { 116 hexToCompact(testBytes) 117 } 118 } 119 120 func BenchmarkHexToCompactInPlace(b *testing.B) { 121 testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/} 122 for i := 0; i < b.N; i++ { 123 hexToCompactInPlace(testBytes) 124 } 125 } 126 127 func BenchmarkCompactToHex(b *testing.B) { 128 testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/} 129 for i := 0; i < b.N; i++ { 130 compactToHex(testBytes) 131 } 132 } 133 134 func BenchmarkKeybytesToHex(b *testing.B) { 135 testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16} 136 for i := 0; i < b.N; i++ { 137 keybytesToHex(testBytes) 138 } 139 } 140 141 func BenchmarkHexToKeybytes(b *testing.B) { 142 testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16} 143 for i := 0; i < b.N; i++ { 144 hexToKeybytes(testBytes) 145 } 146 }