Skip to content

Commit

Permalink
test: more Chunk unit tests added
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Ege <[email protected]>
  • Loading branch information
graugans committed Dec 25, 2023
1 parent fa59aa7 commit 4752a8f
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 1 deletion.
55 changes: 54 additions & 1 deletion pkg/pcic/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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),
Expand All @@ -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
}

Expand All @@ -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")
Expand Down
78 changes: 78 additions & 0 deletions pkg/pcic/chunk_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pcic_test

import (
"math/rand"
"testing"
"time"

Expand Down Expand Up @@ -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",
)
}

0 comments on commit 4752a8f

Please sign in to comment.