encoding_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package trie
  17. import (
  18. checker "gopkg.in/check.v1"
  19. )
  20. type TrieEncodingSuite struct{}
  21. var _ = checker.Suite(&TrieEncodingSuite{})
  22. func (s *TrieEncodingSuite) TestCompactEncode(c *checker.C) {
  23. // even compact encode
  24. test1 := []byte{1, 2, 3, 4, 5}
  25. res1 := CompactEncode(test1)
  26. c.Assert(res1, checker.Equals, "\x11\x23\x45")
  27. // odd compact encode
  28. test2 := []byte{0, 1, 2, 3, 4, 5}
  29. res2 := CompactEncode(test2)
  30. c.Assert(res2, checker.Equals, "\x00\x01\x23\x45")
  31. //odd terminated compact encode
  32. test3 := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
  33. res3 := CompactEncode(test3)
  34. c.Assert(res3, checker.Equals, "\x20\x0f\x1c\xb8")
  35. // even terminated compact encode
  36. test4 := []byte{15, 1, 12, 11, 8 /*term*/, 16}
  37. res4 := CompactEncode(test4)
  38. c.Assert(res4, checker.Equals, "\x3f\x1c\xb8")
  39. }
  40. func (s *TrieEncodingSuite) TestCompactHexDecode(c *checker.C) {
  41. exp := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
  42. res := CompactHexDecode("verb")
  43. c.Assert(res, checker.DeepEquals, exp)
  44. }
  45. func (s *TrieEncodingSuite) TestCompactDecode(c *checker.C) {
  46. // odd compact decode
  47. exp := []byte{1, 2, 3, 4, 5}
  48. res := CompactDecode("\x11\x23\x45")
  49. c.Assert(res, checker.DeepEquals, exp)
  50. // even compact decode
  51. exp = []byte{0, 1, 2, 3, 4, 5}
  52. res = CompactDecode("\x00\x01\x23\x45")
  53. c.Assert(res, checker.DeepEquals, exp)
  54. // even terminated compact decode
  55. exp = []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
  56. res = CompactDecode("\x20\x0f\x1c\xb8")
  57. c.Assert(res, checker.DeepEquals, exp)
  58. // even terminated compact decode
  59. exp = []byte{15, 1, 12, 11, 8 /*term*/, 16}
  60. res = CompactDecode("\x3f\x1c\xb8")
  61. c.Assert(res, checker.DeepEquals, exp)
  62. }