rlp_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package common
  2. import (
  3. "bytes"
  4. "math/big"
  5. "reflect"
  6. "testing"
  7. )
  8. func TestNonInterfaceSlice(t *testing.T) {
  9. vala := []string{"value1", "value2", "value3"}
  10. valb := []interface{}{"value1", "value2", "value3"}
  11. resa := Encode(vala)
  12. resb := Encode(valb)
  13. if !bytes.Equal(resa, resb) {
  14. t.Errorf("expected []string & []interface{} to be equal")
  15. }
  16. }
  17. func TestRlpValueEncoding(t *testing.T) {
  18. val := EmptyValue()
  19. val.AppendList().Append(1).Append(2).Append(3)
  20. val.Append("4").AppendList().Append(5)
  21. res := val.Encode()
  22. exp := Encode([]interface{}{[]interface{}{1, 2, 3}, "4", []interface{}{5}})
  23. if bytes.Compare(res, exp) != 0 {
  24. t.Errorf("expected %q, got %q", res, exp)
  25. }
  26. }
  27. func TestValueSlice(t *testing.T) {
  28. val := []interface{}{
  29. "value1",
  30. "valeu2",
  31. "value3",
  32. }
  33. value := NewValue(val)
  34. splitVal := value.SliceFrom(1)
  35. if splitVal.Len() != 2 {
  36. t.Error("SliceFrom: Expected len", 2, "got", splitVal.Len())
  37. }
  38. splitVal = value.SliceTo(2)
  39. if splitVal.Len() != 2 {
  40. t.Error("SliceTo: Expected len", 2, "got", splitVal.Len())
  41. }
  42. splitVal = value.SliceFromTo(1, 3)
  43. if splitVal.Len() != 2 {
  44. t.Error("SliceFromTo: Expected len", 2, "got", splitVal.Len())
  45. }
  46. }
  47. func TestLargeData(t *testing.T) {
  48. data := make([]byte, 100000)
  49. enc := Encode(data)
  50. value := NewValue(enc)
  51. value.Decode()
  52. if value.Len() != len(data) {
  53. t.Error("Expected data to be", len(data), "got", value.Len())
  54. }
  55. }
  56. func TestValue(t *testing.T) {
  57. value := NewValueFromBytes([]byte("\xcd\x83dog\x83god\x83cat\x01"))
  58. if value.Get(0).Str() != "dog" {
  59. t.Errorf("expected '%v', got '%v'", value.Get(0).Str(), "dog")
  60. }
  61. if value.Get(3).Uint() != 1 {
  62. t.Errorf("expected '%v', got '%v'", value.Get(3).Uint(), 1)
  63. }
  64. }
  65. func TestEncode(t *testing.T) {
  66. strRes := "\x83dog"
  67. bytes := Encode("dog")
  68. str := string(bytes)
  69. if str != strRes {
  70. t.Errorf("Expected %q, got %q", strRes, str)
  71. }
  72. sliceRes := "\xcc\x83dog\x83god\x83cat"
  73. strs := []interface{}{"dog", "god", "cat"}
  74. bytes = Encode(strs)
  75. slice := string(bytes)
  76. if slice != sliceRes {
  77. t.Error("Expected %q, got %q", sliceRes, slice)
  78. }
  79. intRes := "\x82\x04\x00"
  80. bytes = Encode(1024)
  81. if string(bytes) != intRes {
  82. t.Errorf("Expected %q, got %q", intRes, bytes)
  83. }
  84. }
  85. func TestDecode(t *testing.T) {
  86. single := []byte("\x01")
  87. b, _ := Decode(single, 0)
  88. if b.(uint8) != 1 {
  89. t.Errorf("Expected 1, got %q", b)
  90. }
  91. str := []byte("\x83dog")
  92. b, _ = Decode(str, 0)
  93. if bytes.Compare(b.([]byte), []byte("dog")) != 0 {
  94. t.Errorf("Expected dog, got %q", b)
  95. }
  96. slice := []byte("\xcc\x83dog\x83god\x83cat")
  97. res := []interface{}{"dog", "god", "cat"}
  98. b, _ = Decode(slice, 0)
  99. if reflect.DeepEqual(b, res) {
  100. t.Errorf("Expected %q, got %q", res, b)
  101. }
  102. }
  103. func TestEncodeDecodeBigInt(t *testing.T) {
  104. bigInt := big.NewInt(1391787038)
  105. encoded := Encode(bigInt)
  106. value := NewValueFromBytes(encoded)
  107. if value.BigInt().Cmp(bigInt) != 0 {
  108. t.Errorf("Expected %v, got %v", bigInt, value.BigInt())
  109. }
  110. }
  111. func TestEncodeDecodeBytes(t *testing.T) {
  112. b := NewValue([]interface{}{[]byte{1, 2, 3, 4, 5}, byte(6)})
  113. val := NewValueFromBytes(b.Encode())
  114. if !b.Cmp(val) {
  115. t.Errorf("Expected %v, got %v", val, b)
  116. }
  117. }
  118. func TestEncodeZero(t *testing.T) {
  119. b := NewValue(0).Encode()
  120. exp := []byte{0xc0}
  121. if bytes.Compare(b, exp) == 0 {
  122. t.Error("Expected", exp, "got", b)
  123. }
  124. }
  125. func BenchmarkEncodeDecode(b *testing.B) {
  126. for i := 0; i < b.N; i++ {
  127. bytes := Encode([]interface{}{"dog", "god", "cat"})
  128. Decode(bytes, 0)
  129. }
  130. }