-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f3042d
commit 2659fb1
Showing
5 changed files
with
323 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package stream_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/iotaledger/hive.go/serializer/v2/stream" | ||
) | ||
|
||
func TestByteReaderBytesRead(t *testing.T) { | ||
testCases := []struct { | ||
input []byte | ||
expected int | ||
}{ | ||
{[]byte{1, 2, 3, 4, 5}, 0}, // No bytes read initially | ||
{[]byte{1, 2, 3, 4, 5}, 3}, // 3 bytes read after reading 3 bytes | ||
{[]byte{1, 2, 3, 4, 5}, 5}, // All 5 bytes read after reading all bytes | ||
} | ||
|
||
for _, tc := range testCases { | ||
bytes := make([]byte, tc.expected) | ||
|
||
reader := stream.NewByteReader(tc.input) | ||
|
||
nBytes, err := reader.Read(bytes) | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expected, nBytes) | ||
|
||
require.Equal(t, tc.input[:tc.expected], bytes[:tc.expected]) | ||
|
||
require.Equal(t, tc.expected, reader.BytesRead()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package stream_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/iotaledger/hive.go/serializer/v2/stream" | ||
) | ||
|
||
func TestOffset(t *testing.T) { | ||
buffer := stream.NewByteReader([]byte{1, 2, 3, 4, 5}) | ||
|
||
offset, err := stream.Offset(buffer) | ||
require.NoError(t, err) | ||
require.EqualValues(t, 0, offset) | ||
|
||
{ | ||
_, err = buffer.Read([]byte{1}) | ||
require.NoError(t, err) | ||
|
||
offset, err = stream.Offset(buffer) | ||
require.NoError(t, err) | ||
require.EqualValues(t, 1, offset) | ||
} | ||
|
||
{ | ||
newOffset, err := stream.Skip(buffer, 3) | ||
require.NoError(t, err) | ||
require.EqualValues(t, 4, newOffset) | ||
|
||
offset, err = stream.Offset(buffer) | ||
require.NoError(t, err) | ||
require.EqualValues(t, 4, offset) | ||
} | ||
|
||
{ | ||
newOffset, err := stream.GoTo(buffer, 2) | ||
require.NoError(t, err) | ||
require.EqualValues(t, 2, newOffset) | ||
|
||
offset, err = stream.Offset(buffer) | ||
require.NoError(t, err) | ||
require.EqualValues(t, 2, offset) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package stream_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/iotaledger/hive.go/serializer/v2" | ||
"github.com/iotaledger/hive.go/serializer/v2/stream" | ||
) | ||
|
||
func TestRead(t *testing.T) { | ||
buffer := bytes.NewReader([]byte{42, 0, 0, 0, 0, 0, 0, 0}) | ||
|
||
result, err := stream.Read[uint64](buffer) | ||
|
||
require.NoError(t, err) | ||
require.EqualValues(t, 42, result) | ||
} | ||
|
||
func TestReadBytes(t *testing.T) { | ||
initialBytes := []byte{1, 2, 3, 4, 5} | ||
buffer := bytes.NewReader(initialBytes) | ||
|
||
readBytes, err := stream.ReadBytes(buffer, 5) | ||
require.NoError(t, err) | ||
require.EqualValues(t, initialBytes, readBytes) | ||
} | ||
|
||
func TestReadBytesWithSize(t *testing.T) { | ||
initialBytes := []byte{5, 0, 1, 2, 3, 4, 5} | ||
buffer := bytes.NewReader(initialBytes) | ||
|
||
readBytes, err := stream.ReadBytesWithSize(buffer, serializer.SeriLengthPrefixTypeAsUint16) | ||
require.NoError(t, err) | ||
|
||
require.EqualValues(t, []byte{1, 2, 3, 4, 5}, readBytes) | ||
} | ||
|
||
func TestReadObject(t *testing.T) { | ||
buffer := bytes.NewReader([]byte{42, 0, 57, 5, 0, 0, 0, 0, 0, 0}) | ||
|
||
result, err := stream.ReadObject(buffer, 10, sampleStructFromBytes) | ||
require.NoError(t, err) | ||
|
||
expected := sampleStruct{42, 1337} | ||
require.EqualValues(t, expected, result) | ||
} | ||
|
||
func TestReadObjectWithSize(t *testing.T) { | ||
buffer := bytes.NewReader([]byte{10, 0, 42, 0, 57, 5, 0, 0, 0, 0, 0, 0}) | ||
|
||
result, err := stream.ReadObjectWithSize(buffer, serializer.SeriLengthPrefixTypeAsUint16, sampleStructFromBytes) | ||
require.NoError(t, err) | ||
|
||
expected := sampleStruct{42, 1337} | ||
require.EqualValues(t, expected, result) | ||
} | ||
|
||
func TestReadObjectFromReader(t *testing.T) { | ||
buffer := bytes.NewReader([]byte{42, 0, 57, 5, 0, 0, 0, 0, 0, 0}) | ||
|
||
result, err := stream.ReadObjectFromReader(buffer, sampleStructFromReader) | ||
require.NoError(t, err) | ||
|
||
expected := sampleStruct{42, 1337} | ||
require.EqualValues(t, expected, result) | ||
} | ||
|
||
func TestPeek(t *testing.T) { | ||
buffer := bytes.NewReader([]byte{3, 0, 0, 0, 1, 0, 2, 0, 3, 0}) | ||
|
||
elementsCount, err := stream.Peek(buffer, serializer.SeriLengthPrefixTypeAsUint32) | ||
require.NoError(t, err) | ||
require.EqualValues(t, 3, elementsCount) | ||
} | ||
|
||
func TestReadCollection(t *testing.T) { | ||
buffer := bytes.NewReader([]byte{3, 0, 0, 0, 1, 0, 2, 0, 3, 0}) | ||
|
||
count, err := stream.Peek(buffer, serializer.SeriLengthPrefixTypeAsUint32) | ||
require.NoError(t, err) | ||
require.EqualValues(t, 3, count) | ||
|
||
results := make([]uint16, count) | ||
|
||
err = stream.ReadCollection(buffer, serializer.SeriLengthPrefixTypeAsUint32, func(i int) error { | ||
result, err := stream.Read[uint16](buffer) | ||
require.NoError(t, err) | ||
|
||
results[i] = result | ||
|
||
return nil | ||
}) | ||
require.NoError(t, err) | ||
|
||
expected := []uint16{1, 2, 3} | ||
require.EqualValues(t, expected, results) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package stream_test | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/iotaledger/hive.go/lo" | ||
"github.com/iotaledger/hive.go/serializer/v2" | ||
"github.com/iotaledger/hive.go/serializer/v2/byteutils" | ||
"github.com/iotaledger/hive.go/serializer/v2/stream" | ||
) | ||
|
||
func requireBufferBytes(t *testing.T, buffer *stream.ByteBuffer, expected []byte) { | ||
bytesInBuffer, err := buffer.Bytes() | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, expected, bytesInBuffer) | ||
} | ||
|
||
type sampleStruct struct { | ||
Value0 uint16 | ||
Value1 uint64 | ||
} | ||
|
||
func (s sampleStruct) Bytes() ([]byte, error) { | ||
bytes := make([]byte, 10) | ||
binary.LittleEndian.PutUint16(bytes[:2], s.Value0) | ||
binary.LittleEndian.PutUint64(bytes[2:], s.Value1) | ||
|
||
return bytes, nil | ||
} | ||
|
||
func sampleStructFromBytes(bytes []byte) (sampleStruct, int, error) { | ||
value0 := binary.LittleEndian.Uint16(bytes[:2]) | ||
value1 := binary.LittleEndian.Uint64(bytes[2:]) | ||
|
||
return sampleStruct{value0, value1}, 10, nil | ||
} | ||
|
||
func sampleStructFromReader(reader io.ReadSeeker) (sampleStruct, error) { | ||
bytes := make([]byte, 10) | ||
_, err := reader.Read(bytes) | ||
if err != nil { | ||
return sampleStruct{}, err | ||
} | ||
|
||
value0 := binary.LittleEndian.Uint16(bytes[:2]) | ||
value1 := binary.LittleEndian.Uint64(bytes[2:]) | ||
|
||
return sampleStruct{value0, value1}, nil | ||
} | ||
|
||
func TestWrite(t *testing.T) { | ||
buffer := stream.NewByteBuffer() | ||
|
||
err := stream.Write(buffer, uint64(42)) | ||
require.NoError(t, err) | ||
|
||
expected := []byte{42, 0, 0, 0, 0, 0, 0, 0} | ||
requireBufferBytes(t, buffer, expected) | ||
} | ||
|
||
func TestWriteBytes(t *testing.T) { | ||
buffer := stream.NewByteBuffer() | ||
bytesToWrite := []byte{1, 2, 3, 4, 5} | ||
|
||
err := stream.WriteBytes(buffer, bytesToWrite) | ||
require.NoError(t, err) | ||
|
||
requireBufferBytes(t, buffer, bytesToWrite) | ||
} | ||
|
||
func TestWriteBytesWithSize(t *testing.T) { | ||
buffer := stream.NewByteBuffer() | ||
bytesToWrite := []byte{1, 2, 3, 4, 5} | ||
|
||
err := stream.WriteBytesWithSize(buffer, bytesToWrite, serializer.SeriLengthPrefixTypeAsUint16) | ||
require.NoError(t, err) | ||
|
||
expected := []byte{5, 0, 1, 2, 3, 4, 5} | ||
requireBufferBytes(t, buffer, expected) | ||
} | ||
|
||
func TestWriteObject(t *testing.T) { | ||
buffer := stream.NewByteBuffer() | ||
|
||
s := sampleStruct{42, 1337} | ||
err := stream.WriteObject(buffer, s, sampleStruct.Bytes) | ||
require.NoError(t, err) | ||
|
||
expected := lo.PanicOnErr(s.Bytes()) | ||
requireBufferBytes(t, buffer, expected) | ||
} | ||
|
||
func TestWriteObjectWithSize(t *testing.T) { | ||
buffer := stream.NewByteBuffer() | ||
|
||
s := sampleStruct{42, 1337} | ||
err := stream.WriteObjectWithSize(buffer, s, serializer.SeriLengthPrefixTypeAsUint16, sampleStruct.Bytes) | ||
require.NoError(t, err) | ||
|
||
expected := byteutils.ConcatBytes([]byte{10, 0}, lo.PanicOnErr(s.Bytes())) | ||
requireBufferBytes(t, buffer, expected) | ||
} | ||
|
||
func TestWriteCollection(t *testing.T) { | ||
buffer := stream.NewByteBuffer() | ||
|
||
elementsCount := 3 | ||
err := stream.WriteCollection(buffer, serializer.SeriLengthPrefixTypeAsUint32, func() (int, error) { | ||
for i := 0; i < elementsCount; i++ { | ||
if err := stream.Write(buffer, uint16(i+1)); err != nil { | ||
return 0, err | ||
} | ||
} | ||
return elementsCount, nil | ||
}) | ||
require.NoError(t, err) | ||
|
||
expected := []byte{3, 0, 0, 0, 1, 0, 2, 0, 3, 0} | ||
requireBufferBytes(t, buffer, expected) | ||
} |