diff options
author | Felix Lange <fjl@twurst.com> | 2014-11-25 02:01:25 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2014-11-25 02:03:11 +0800 |
commit | 5a5560f1051b51fae34e799ee8d2dfd8d1094e09 (patch) | |
tree | 7b05772ffcbcf36aef9f8957aba586ead7d6c048 /rlp/decode_test.go | |
parent | 59b63caf5e4de64ceb7dcdf01551a080f53b1672 (diff) | |
download | go-tangerine-5a5560f1051b51fae34e799ee8d2dfd8d1094e09.tar.gz go-tangerine-5a5560f1051b51fae34e799ee8d2dfd8d1094e09.tar.zst go-tangerine-5a5560f1051b51fae34e799ee8d2dfd8d1094e09.zip |
rlp: add Stream.Reset and accept any reader (for p2p)
Diffstat (limited to 'rlp/decode_test.go')
-rw-r--r-- | rlp/decode_test.go | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/rlp/decode_test.go b/rlp/decode_test.go index eb1618299..9d320564b 100644 --- a/rlp/decode_test.go +++ b/rlp/decode_test.go @@ -286,14 +286,14 @@ var decodeTests = []decodeTest{ func intp(i int) *int { return &i } -func TestDecode(t *testing.T) { +func runTests(t *testing.T, decode func([]byte, interface{}) error) { for i, test := range decodeTests { input, err := hex.DecodeString(test.input) if err != nil { t.Errorf("test %d: invalid hex input %q", i, test.input) continue } - err = Decode(bytes.NewReader(input), test.ptr) + err = decode(input, test.ptr) if err != nil && test.error == nil { t.Errorf("test %d: unexpected Decode error: %v\ndecoding into %T\ninput %q", i, err, test.ptr, test.input) @@ -312,6 +312,40 @@ func TestDecode(t *testing.T) { } } +func TestDecodeWithByteReader(t *testing.T) { + runTests(t, func(input []byte, into interface{}) error { + return Decode(bytes.NewReader(input), into) + }) +} + +// dumbReader reads from a byte slice but does not +// implement ReadByte. +type dumbReader []byte + +func (r *dumbReader) Read(buf []byte) (n int, err error) { + if len(*r) == 0 { + return 0, io.EOF + } + n = copy(buf, *r) + *r = (*r)[n:] + return n, nil +} + +func TestDecodeWithNonByteReader(t *testing.T) { + runTests(t, func(input []byte, into interface{}) error { + r := dumbReader(input) + return Decode(&r, into) + }) +} + +func TestDecodeStreamReset(t *testing.T) { + s := NewStream(nil) + runTests(t, func(input []byte, into interface{}) error { + s.Reset(bytes.NewReader(input)) + return s.Decode(into) + }) +} + type testDecoder struct{ called bool } func (t *testDecoder) DecodeRLP(s *Stream) error { |