encoding_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "encoding/hex"
  19. "testing"
  20. checker "gopkg.in/check.v1"
  21. )
  22. func TestEncoding(t *testing.T) { checker.TestingT(t) }
  23. type TrieEncodingSuite struct{}
  24. var _ = checker.Suite(&TrieEncodingSuite{})
  25. func (s *TrieEncodingSuite) TestCompactEncode(c *checker.C) {
  26. // even compact encode
  27. test1 := []byte{1, 2, 3, 4, 5}
  28. res1 := compactEncode(test1)
  29. c.Assert(res1, checker.DeepEquals, []byte("\x11\x23\x45"))
  30. // odd compact encode
  31. test2 := []byte{0, 1, 2, 3, 4, 5}
  32. res2 := compactEncode(test2)
  33. c.Assert(res2, checker.DeepEquals, []byte("\x00\x01\x23\x45"))
  34. //odd terminated compact encode
  35. test3 := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
  36. res3 := compactEncode(test3)
  37. c.Assert(res3, checker.DeepEquals, []byte("\x20\x0f\x1c\xb8"))
  38. // even terminated compact encode
  39. test4 := []byte{15, 1, 12, 11, 8 /*term*/, 16}
  40. res4 := compactEncode(test4)
  41. c.Assert(res4, checker.DeepEquals, []byte("\x3f\x1c\xb8"))
  42. }
  43. func (s *TrieEncodingSuite) TestCompactHexDecode(c *checker.C) {
  44. exp := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
  45. res := compactHexDecode([]byte("verb"))
  46. c.Assert(res, checker.DeepEquals, exp)
  47. }
  48. func (s *TrieEncodingSuite) TestCompactHexEncode(c *checker.C) {
  49. exp := []byte("verb")
  50. res := compactHexEncode([]byte{7, 6, 6, 5, 7, 2, 6, 2, 16})
  51. c.Assert(res, checker.DeepEquals, exp)
  52. }
  53. func (s *TrieEncodingSuite) TestCompactDecode(c *checker.C) {
  54. // odd compact decode
  55. exp := []byte{1, 2, 3, 4, 5}
  56. res := compactDecode([]byte("\x11\x23\x45"))
  57. c.Assert(res, checker.DeepEquals, exp)
  58. // even compact decode
  59. exp = []byte{0, 1, 2, 3, 4, 5}
  60. res = compactDecode([]byte("\x00\x01\x23\x45"))
  61. c.Assert(res, checker.DeepEquals, exp)
  62. // even terminated compact decode
  63. exp = []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
  64. res = compactDecode([]byte("\x20\x0f\x1c\xb8"))
  65. c.Assert(res, checker.DeepEquals, exp)
  66. // even terminated compact decode
  67. exp = []byte{15, 1, 12, 11, 8 /*term*/, 16}
  68. res = compactDecode([]byte("\x3f\x1c\xb8"))
  69. c.Assert(res, checker.DeepEquals, exp)
  70. }
  71. func (s *TrieEncodingSuite) TestDecodeCompact(c *checker.C) {
  72. exp, _ := hex.DecodeString("012345")
  73. res := decodeCompact([]byte{0, 1, 2, 3, 4, 5})
  74. c.Assert(res, checker.DeepEquals, exp)
  75. exp, _ = hex.DecodeString("012345")
  76. res = decodeCompact([]byte{0, 1, 2, 3, 4, 5, 16})
  77. c.Assert(res, checker.DeepEquals, exp)
  78. exp, _ = hex.DecodeString("abcdef")
  79. res = decodeCompact([]byte{10, 11, 12, 13, 14, 15})
  80. c.Assert(res, checker.DeepEquals, exp)
  81. }
  82. func BenchmarkCompactEncode(b *testing.B) {
  83. testBytes := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
  84. for i := 0; i < b.N; i++ {
  85. compactEncode(testBytes)
  86. }
  87. }
  88. func BenchmarkCompactDecode(b *testing.B) {
  89. testBytes := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
  90. for i := 0; i < b.N; i++ {
  91. compactDecode(testBytes)
  92. }
  93. }
  94. func BenchmarkCompactHexDecode(b *testing.B) {
  95. testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
  96. for i := 0; i < b.N; i++ {
  97. compactHexDecode(testBytes)
  98. }
  99. }
  100. func BenchmarkDecodeCompact(b *testing.B) {
  101. testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
  102. for i := 0; i < b.N; i++ {
  103. decodeCompact(testBytes)
  104. }
  105. }