encoding_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 Test(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) TestCompactDecode(c *checker.C) {
  49. // odd compact decode
  50. exp := []byte{1, 2, 3, 4, 5}
  51. res := CompactDecode([]byte("\x11\x23\x45"))
  52. c.Assert(res, checker.DeepEquals, exp)
  53. // even compact decode
  54. exp = []byte{0, 1, 2, 3, 4, 5}
  55. res = CompactDecode([]byte("\x00\x01\x23\x45"))
  56. c.Assert(res, checker.DeepEquals, exp)
  57. // even terminated compact decode
  58. exp = []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
  59. res = CompactDecode([]byte("\x20\x0f\x1c\xb8"))
  60. c.Assert(res, checker.DeepEquals, exp)
  61. // even terminated compact decode
  62. exp = []byte{15, 1, 12, 11, 8 /*term*/, 16}
  63. res = CompactDecode([]byte("\x3f\x1c\xb8"))
  64. c.Assert(res, checker.DeepEquals, exp)
  65. }
  66. func (s *TrieEncodingSuite) TestDecodeCompact(c *checker.C) {
  67. exp, _ := hex.DecodeString("012345")
  68. res := DecodeCompact([]byte{0, 1, 2, 3, 4, 5})
  69. c.Assert(res, checker.DeepEquals, exp)
  70. exp, _ = hex.DecodeString("012345")
  71. res = DecodeCompact([]byte{0, 1, 2, 3, 4, 5, 16})
  72. c.Assert(res, checker.DeepEquals, exp)
  73. exp, _ = hex.DecodeString("abcdef")
  74. res = DecodeCompact([]byte{10, 11, 12, 13, 14, 15})
  75. c.Assert(res, checker.DeepEquals, exp)
  76. }
  77. func BenchmarkCompactEncode(b *testing.B) {
  78. testBytes := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
  79. for i := 0; i < b.N; i++ {
  80. CompactEncode(testBytes)
  81. }
  82. }
  83. func BenchmarkCompactDecode(b *testing.B) {
  84. testBytes := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
  85. for i := 0; i < b.N; i++ {
  86. CompactDecode(testBytes)
  87. }
  88. }
  89. func BenchmarkCompactHexDecode(b *testing.B) {
  90. testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
  91. for i := 0; i < b.N; i++ {
  92. CompactHexDecode(testBytes)
  93. }
  94. }
  95. func BenchmarkDecodeCompact(b *testing.B) {
  96. testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
  97. for i := 0; i < b.N; i++ {
  98. DecodeCompact(testBytes)
  99. }
  100. }