read_write_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 rle
  17. import (
  18. "testing"
  19. checker "gopkg.in/check.v1"
  20. )
  21. func Test(t *testing.T) { checker.TestingT(t) }
  22. type CompressionRleSuite struct{}
  23. var _ = checker.Suite(&CompressionRleSuite{})
  24. func (s *CompressionRleSuite) TestDecompressSimple(c *checker.C) {
  25. exp := []byte{0xc5, 0xd2, 0x46, 0x1, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x3, 0xc0, 0xe5, 0x0, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x4, 0x5d, 0x85, 0xa4, 0x70}
  26. res, err := Decompress([]byte{token, 0xfd})
  27. c.Assert(err, checker.IsNil)
  28. c.Assert(res, checker.DeepEquals, exp)
  29. // if bytes.Compare(res, exp) != 0 {
  30. // t.Error("empty sha3", res)
  31. // }
  32. exp = []byte{0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x1, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21}
  33. res, err = Decompress([]byte{token, 0xfe})
  34. c.Assert(err, checker.IsNil)
  35. c.Assert(res, checker.DeepEquals, exp)
  36. // if bytes.Compare(res, exp) != 0 {
  37. // t.Error("0x80 sha3", res)
  38. // }
  39. res, err = Decompress([]byte{token, 0xff})
  40. c.Assert(err, checker.IsNil)
  41. c.Assert(res, checker.DeepEquals, []byte{token})
  42. // if bytes.Compare(res, []byte{token}) != 0 {
  43. // t.Error("token", res)
  44. // }
  45. res, err = Decompress([]byte{token, 12})
  46. c.Assert(err, checker.IsNil)
  47. c.Assert(res, checker.DeepEquals, make([]byte, 10))
  48. // if bytes.Compare(res, make([]byte, 10)) != 0 {
  49. // t.Error("10 * zero", res)
  50. // }
  51. }
  52. // func TestDecompressMulti(t *testing.T) {
  53. // res, err := Decompress([]byte{token, 0xfd, token, 0xfe, token, 12})
  54. // if err != nil {
  55. // t.Error(err)
  56. // }
  57. // var exp []byte
  58. // exp = append(exp, crypto.Sha3([]byte(""))...)
  59. // exp = append(exp, crypto.Sha3([]byte{0x80})...)
  60. // exp = append(exp, make([]byte, 10)...)
  61. // if bytes.Compare(res, res) != 0 {
  62. // t.Error("Expected", exp, "result", res)
  63. // }
  64. // }
  65. // func TestCompressSimple(t *testing.T) {
  66. // res := Compress([]byte{0, 0, 0, 0, 0})
  67. // if bytes.Compare(res, []byte{token, 7}) != 0 {
  68. // t.Error("5 * zero", res)
  69. // }
  70. // res = Compress(crypto.Sha3([]byte("")))
  71. // if bytes.Compare(res, []byte{token, emptyShaToken}) != 0 {
  72. // t.Error("empty sha", res)
  73. // }
  74. // res = Compress(crypto.Sha3([]byte{0x80}))
  75. // if bytes.Compare(res, []byte{token, emptyListShaToken}) != 0 {
  76. // t.Error("empty list sha", res)
  77. // }
  78. // res = Compress([]byte{token})
  79. // if bytes.Compare(res, []byte{token, tokenToken}) != 0 {
  80. // t.Error("token", res)
  81. // }
  82. // }
  83. // func TestCompressMulti(t *testing.T) {
  84. // in := []byte{0, 0, 0, 0, 0}
  85. // in = append(in, crypto.Sha3([]byte(""))...)
  86. // in = append(in, crypto.Sha3([]byte{0x80})...)
  87. // in = append(in, token)
  88. // res := Compress(in)
  89. // exp := []byte{token, 7, token, emptyShaToken, token, emptyListShaToken, token, tokenToken}
  90. // if bytes.Compare(res, exp) != 0 {
  91. // t.Error("expected", exp, "got", res)
  92. // }
  93. // }
  94. // func TestCompressDecompress(t *testing.T) {
  95. // var in []byte
  96. // for i := 0; i < 20; i++ {
  97. // in = append(in, []byte{0, 0, 0, 0, 0}...)
  98. // in = append(in, crypto.Sha3([]byte(""))...)
  99. // in = append(in, crypto.Sha3([]byte{0x80})...)
  100. // in = append(in, []byte{123, 2, 19, 89, 245, 254, 255, token, 98, 233}...)
  101. // in = append(in, token)
  102. // }
  103. // c := Compress(in)
  104. // d, err := Decompress(c)
  105. // if err != nil {
  106. // t.Error(err)
  107. // }
  108. // if bytes.Compare(d, in) != 0 {
  109. // t.Error("multi failed\n", d, "\n", in)
  110. // }
  111. // }