From 4752a8f1d6aa241425c42f99be8f3fb9f20b34fa Mon Sep 17 00:00:00 2001 From: Christian Ege Date: Mon, 25 Dec 2023 21:55:11 +0100 Subject: [PATCH] test: more Chunk unit tests added Signed-off-by: Christian Ege --- pkg/pcic/chunk.go | 55 ++++++++++++++++++++++++++++- pkg/pcic/chunk_test.go | 78 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 1 deletion(-) diff --git a/pkg/pcic/chunk.go b/pkg/pcic/chunk.go index 993abaa..d66ce7d 100644 --- a/pkg/pcic/chunk.go +++ b/pkg/pcic/chunk.go @@ -89,32 +89,62 @@ func NewChunk(options ...ChunkOption) *Chunk { return chunk } +// WithChunkType sets the chunk type of the new Chunk object func WithChunkType(cType ChunkType) ChunkOption { return func(c *Chunk) { c.chunkType = cType } } +// WithDimension set the chunk dimension and the given data format +func WithDimension(width, height int, format DataFormat) ChunkOption { + return func(c *Chunk) { + c.dataWidth = uint32(width) + c.dataHeight = uint32(height) + c.dataFormat = format + c.data = make([]byte, c.dataWidth*c.dataHeight*byteSizeLUT[format]) + c.chunkSize = c.headerSize + uint32(len(c.data)) + } +} + +// Type returns the given ChunkType func (c *Chunk) Type() ChunkType { return c.chunkType } +// Size returns the Size of the whole Chunk +// +// This is the size the Chunk is marshalled to. func (c *Chunk) Size() int { return int(c.chunkSize) } +// FrameCount returns the frame count of the Chunk func (c *Chunk) FrameCount() uint32 { return c.frameCount } +// SetFrameCount sets the frame count of the Chunk +func (c *Chunk) SetFrameCount(num uint32) { + c.frameCount = num +} + +// Status returns the status of the given Chunk func (c *Chunk) Status() uint32 { return c.statusCode } +// SetStatus sets the status of the Chunk +func (c *Chunk) SetStatus(status uint32) { + c.statusCode = status +} + +// TimeStamp returns the time stamp of the given Chunk func (c *Chunk) TimeStamp() time.Time { return time.Unix(int64(c.timestampSec), int64(c.timestampNSec)) } +// Bytes return the data the current Chunk is holding func (c *Chunk) Bytes() []byte { return c.data } @@ -123,7 +153,7 @@ func (c *Chunk) Bytes() []byte { // // The binary representation is encoded in the byte slice func (c *Chunk) MarshalBinary() (data []byte, err error) { - blob := make([]byte, offsetOfData) + blob := make([]byte, offsetOfData+len(c.data)) binary.LittleEndian.PutUint32( blob, uint32(c.chunkType), @@ -140,6 +170,28 @@ func (c *Chunk) MarshalBinary() (data []byte, err error) { blob[offsetOfHeaderVersion:offsetOfWidth], c.headerVersion, ) + binary.LittleEndian.PutUint32( + blob[offsetOfWidth:offsetOfHeight], + c.dataWidth, + ) + binary.LittleEndian.PutUint32( + blob[offsetOfHeight:offsetOfFormat], + c.dataHeight, + ) + binary.LittleEndian.PutUint32( + blob[offsetOfFormat:offsetOfTimeStamp], + uint32(c.dataFormat), + ) + // We skip the timestamp for now, it is deprecated + binary.LittleEndian.PutUint32( + blob[offsetOfFrameCount:offsetOfStatusCode], + c.frameCount, + ) + binary.LittleEndian.PutUint32( + blob[offsetOfStatusCode:offsetOfTimeStampSec], + c.statusCode, + ) + return blob, nil } @@ -148,6 +200,7 @@ func (c *Chunk) MarshalBinary() (data []byte, err error) { // It copies the data from the input slice to comply with the BinaryUnmarshaler // interface. func (c *Chunk) UnmarshalBinary(data []byte) error { + dataLen := uint32(len(data)) if dataLen < offsetOfData { return errors.New("unable to parse an empty input") diff --git a/pkg/pcic/chunk_test.go b/pkg/pcic/chunk_test.go index 0df0d7c..b873722 100644 --- a/pkg/pcic/chunk_test.go +++ b/pkg/pcic/chunk_test.go @@ -1,6 +1,7 @@ package pcic_test import ( + "math/rand" "testing" "time" @@ -339,3 +340,80 @@ func TestInvalidHeaderVersion(t *testing.T) { "An error expected, due to wrong header version", ) } + +func TestWithNil(t *testing.T) { + vector := pcic.NewChunk() + assert.Error( + t, + vector.UnmarshalBinary(nil), + "An error is expected when providing nil as input", + ) +} +func TestWithDimension(t *testing.T) { + vector := make([]*pcic.Chunk, 2) + vector[0] = pcic.NewChunk(pcic.WithDimension(640, 480, pcic.FORMAT_32F)) + vector[1] = pcic.NewChunk() + data, err := vector[0].MarshalBinary() + assert.NoError(t, err, "No Error expected during MarshalBinary") + assert.NoError( + t, + vector[1].UnmarshalBinary(data), + "No error expected while UnmarshalBinary", + ) +} + +func TestFrameCount(t *testing.T) { + count := rand.Uint32() + chunk := pcic.NewChunk() + chunk.SetFrameCount(count) + assert.Equal( + t, + count, + chunk.FrameCount(), + "An error is expected when providing nil as input", + ) + clone := pcic.NewChunk() + data, err := chunk.MarshalBinary() + assert.NoError(t, + err, + "No error expected when creating a binary clone", + ) + assert.NoError(t, + clone.UnmarshalBinary(data), + "No error expected when creating a clone from the bytes", + ) + assert.Equal( + t, + chunk.FrameCount(), + clone.FrameCount(), + "The frame count of the original and the clone do not match", + ) +} + +func TestFrameStatus(t *testing.T) { + status := rand.Uint32() + chunk := pcic.NewChunk() + chunk.SetStatus(status) + assert.Equal( + t, + status, + chunk.Status(), + "No error expected when setting the staus", + ) + clone := pcic.NewChunk() + data, err := chunk.MarshalBinary() + assert.NoError(t, + err, + "No error expected when creating a binary clone", + ) + assert.NoError(t, + clone.UnmarshalBinary(data), + "No error expected when creating a clone from the bytes", + ) + assert.Equal( + t, + chunk.Status(), + clone.Status(), + "The status of the original and the clone do not match", + ) +}