github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/ethutil/rlp_test.go (about)

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