|
| 1 | +// Copyright 2025 the libevm authors. |
| 2 | +// |
| 3 | +// The libevm additions to go-ethereum are free software: you can redistribute |
| 4 | +// them and/or modify them under the terms of the GNU Lesser General Public License |
| 5 | +// as published by the Free Software Foundation, either version 3 of the License, |
| 6 | +// or (at your option) any later version. |
| 7 | +// |
| 8 | +// The libevm additions are distributed in the hope that they will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser |
| 11 | +// General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU Lesser General Public License |
| 14 | +// along with the go-ethereum library. If not, see |
| 15 | +// <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package rlp |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | +) |
| 26 | + |
| 27 | +func TestEncodeListToBuffer(t *testing.T) { |
| 28 | + vals := []uint{1, 2, 3, 4, 5} |
| 29 | + |
| 30 | + want, err := EncodeToBytes(vals) |
| 31 | + require.NoErrorf(t, err, "EncodeToBytes(%T{%[1]v})", vals) |
| 32 | + |
| 33 | + var got bytes.Buffer |
| 34 | + buf := NewEncoderBuffer(&got) |
| 35 | + err = EncodeListToBuffer(buf, vals) |
| 36 | + require.NoErrorf(t, err, "EncodeListToBuffer(..., %T{%[1]v})", vals) |
| 37 | + require.NoErrorf(t, buf.Flush(), "%T.Flush()", buf) |
| 38 | + |
| 39 | + assert.Equal(t, want, got.Bytes(), "EncodeListToBuffer(..., %T{%[1]v})", vals) |
| 40 | +} |
| 41 | + |
| 42 | +func TestDecodeList(t *testing.T) { |
| 43 | + vals := []uint{0, 1, 42, 314159} |
| 44 | + |
| 45 | + rlp, err := EncodeToBytes(vals) |
| 46 | + require.NoErrorf(t, err, "EncodeToBytes(%T{%[1]v})", vals) |
| 47 | + |
| 48 | + s := NewStream(bytes.NewReader(rlp), 0) |
| 49 | + got, err := DecodeList[uint](s) |
| 50 | + require.NoErrorf(t, err, "DecodeList[%T]()", vals[0]) |
| 51 | + |
| 52 | + require.Equal(t, len(vals), len(got), "number of values returned by DecodeList()") |
| 53 | + for i, gotPtr := range got { |
| 54 | + assert.Equalf(t, vals[i], *gotPtr, "DecodeList()[%d]", i) |
| 55 | + } |
| 56 | +} |
0 commit comments