encoding_test.go 4.1 KB

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