encoding_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. package trie
  17. import (
  18. "bytes"
  19. "testing"
  20. )
  21. func TestHexCompact(t *testing.T) {
  22. tests := []struct{ hex, compact []byte }{
  23. // empty keys, with and without terminator.
  24. {hex: []byte{}, compact: []byte{0x00}},
  25. {hex: []byte{16}, compact: []byte{0x20}},
  26. // odd length, no terminator
  27. {hex: []byte{1, 2, 3, 4, 5}, compact: []byte{0x11, 0x23, 0x45}},
  28. // even length, no terminator
  29. {hex: []byte{0, 1, 2, 3, 4, 5}, compact: []byte{0x00, 0x01, 0x23, 0x45}},
  30. // odd length, terminator
  31. {hex: []byte{15, 1, 12, 11, 8, 16 /*term*/}, compact: []byte{0x3f, 0x1c, 0xb8}},
  32. // even length, terminator
  33. {hex: []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}, compact: []byte{0x20, 0x0f, 0x1c, 0xb8}},
  34. }
  35. for _, test := range tests {
  36. if c := hexToCompact(test.hex); !bytes.Equal(c, test.compact) {
  37. t.Errorf("hexToCompact(%x) -> %x, want %x", test.hex, c, test.compact)
  38. }
  39. if h := compactToHex(test.compact); !bytes.Equal(h, test.hex) {
  40. t.Errorf("compactToHex(%x) -> %x, want %x", test.compact, h, test.hex)
  41. }
  42. }
  43. }
  44. func TestHexKeybytes(t *testing.T) {
  45. tests := []struct{ key, hexIn, hexOut []byte }{
  46. {key: []byte{}, hexIn: []byte{16}, hexOut: []byte{16}},
  47. {key: []byte{}, hexIn: []byte{}, hexOut: []byte{16}},
  48. {
  49. key: []byte{0x12, 0x34, 0x56},
  50. hexIn: []byte{1, 2, 3, 4, 5, 6, 16},
  51. hexOut: []byte{1, 2, 3, 4, 5, 6, 16},
  52. },
  53. {
  54. key: []byte{0x12, 0x34, 0x5},
  55. hexIn: []byte{1, 2, 3, 4, 0, 5, 16},
  56. hexOut: []byte{1, 2, 3, 4, 0, 5, 16},
  57. },
  58. {
  59. key: []byte{0x12, 0x34, 0x56},
  60. hexIn: []byte{1, 2, 3, 4, 5, 6},
  61. hexOut: []byte{1, 2, 3, 4, 5, 6, 16},
  62. },
  63. }
  64. for _, test := range tests {
  65. if h := keybytesToHex(test.key); !bytes.Equal(h, test.hexOut) {
  66. t.Errorf("keybytesToHex(%x) -> %x, want %x", test.key, h, test.hexOut)
  67. }
  68. if k := hexToKeybytes(test.hexIn); !bytes.Equal(k, test.key) {
  69. t.Errorf("hexToKeybytes(%x) -> %x, want %x", test.hexIn, k, test.key)
  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. func BenchmarkCompactToHex(b *testing.B) {
  80. testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}
  81. for i := 0; i < b.N; i++ {
  82. compactToHex(testBytes)
  83. }
  84. }
  85. func BenchmarkKeybytesToHex(b *testing.B) {
  86. testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
  87. for i := 0; i < b.N; i++ {
  88. keybytesToHex(testBytes)
  89. }
  90. }
  91. func BenchmarkHexToKeybytes(b *testing.B) {
  92. testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
  93. for i := 0; i < b.N; i++ {
  94. hexToKeybytes(testBytes)
  95. }
  96. }