Skip to content

Commit

Permalink
test: add a round trip test for Chunks
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 61a3c13 commit 35537fd
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 7 deletions.
29 changes: 23 additions & 6 deletions pkg/pcic/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ const (

func NewChunk(options ...ChunkOption) *Chunk {
chunk := &Chunk{
metadata: "{}",
data: []byte{},
chunkSize: offsetOfData,
headerSize: offsetOfData,
headerVersion: 2,
metadata: "{}",
data: []byte{},
}
// Apply options
for _, opt := range options {
Expand Down Expand Up @@ -120,7 +123,24 @@ func (c *Chunk) Bytes() []byte {
//
// The binary representation is encoded in the byte slice
func (c *Chunk) MarshalBinary() (data []byte, err error) {
return []byte{}, nil
blob := make([]byte, offsetOfData)
binary.LittleEndian.PutUint32(
blob,
uint32(c.chunkType),
)
binary.LittleEndian.PutUint32(
blob[offsetOfSize:offsetOfHeaderSize],
c.chunkSize,
)
binary.LittleEndian.PutUint32(
blob[offsetOfHeaderSize:offsetOfHeaderVersion],
c.headerSize,
)
binary.LittleEndian.PutUint32(
blob[offsetOfHeaderVersion:offsetOfWidth],
c.headerVersion,
)
return blob, nil
}

// UnmarshalBinary creates a ChunkData from a byte slice
Expand Down Expand Up @@ -216,9 +236,6 @@ func (c *Chunk) UnmarshalBinary(data []byte) error {
src := data[offsetOfData : offsetOfData+(c.chunkSize-c.headerSize)]
c.data = make([]byte, len(src))
copy(c.data, src)
if src == nil {
c.data = nil
}

if (c.dataWidth * c.dataHeight * byteSizeLUT[c.dataFormat]) != uint32(len(c.data)) {
return fmt.Errorf(
Expand Down
75 changes: 74 additions & 1 deletion pkg/pcic/chunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,81 @@ func TestUnmarshalBinary(t *testing.T) {
c.Bytes(),
"A data size mismatch occurred",
)

assert.Error(t,
c.UnmarshalBinary([]byte{
0x69, 0x00, 0x00, 0x00, /* CHUNK_TYPE */
0x34, 0x00, 0x00, 0x00, /* CHUNK_SIZE */
0x30, 0x00, 0x00, 0x00, /* HEADER_SIZE */
0x02, 0x00, 0x00, 0x00, /* HEADER_VERSION */
0x04, 0x00, 0x00, 0x00, /* IMAGE_WIDTH */
0x01, 0x00, 0x00, 0x00, /* IMAGE_HEIGHT */
0x04, 0x00, 0x00, 0x00, /* DATA_FORMAT */
0x00, 0x00, 0x00, 0x00, /* TIME_STAMP */
0x00, 0x00, 0x00, 0x00, /* FRAME_COUNT */
0x00, 0x00, 0x00, 0x00, /* STATUS_CODE */
0x00, 0x01, 0x00, 0x00, /* TIME_STAMP_SEC */
0x01, 0x01, 0x00, 0x00, /* TIME_STAMP_NSEC */
0xFF, 0xFF, 0xFF, 0xBB, /* DATA */
0x69, 0x00, 0x00, 0x00, /* CHUNK_TYPE of second frame*/
}),
"The (width * height * data format) does not match the data size",
)

assert.Error(t,
c.UnmarshalBinary([]byte{
0x69, 0x00, 0x00, 0x00, /* CHUNK_TYPE */
0x28, 0x00, 0x00, 0x00, /* CHUNK_SIZE */
0x30, 0x00, 0x00, 0x00, /* HEADER_SIZE */
0x02, 0x00, 0x00, 0x00, /* HEADER_VERSION */
0x04, 0x00, 0x00, 0x00, /* IMAGE_WIDTH */
0x01, 0x00, 0x00, 0x00, /* IMAGE_HEIGHT */
0x04, 0x00, 0x00, 0x00, /* DATA_FORMAT */
0x00, 0x00, 0x00, 0x00, /* TIME_STAMP */
0x00, 0x00, 0x00, 0x00, /* FRAME_COUNT */
0x00, 0x00, 0x00, 0x00, /* STATUS_CODE */
0x00, 0x01, 0x00, 0x00, /* TIME_STAMP_SEC */
0x01, 0x01, 0x00, 0x00, /* TIME_STAMP_NSEC */
0xFF, 0xFF, 0xFF, 0xBB, /* DATA */
0x69, 0x00, 0x00, 0x00, /* CHUNK_TYPE of second frame*/
}),
"The Chunk size is smaller than the expected size",
)

assert.Error(t,
c.UnmarshalBinary([]byte{
0x69, 0x00, 0x00, 0x00, /* CHUNK_TYPE */
0x00, 0x01, 0x00, 0x00, /* CHUNK_SIZE */
0x30, 0x00, 0x00, 0x00, /* HEADER_SIZE */
0x02, 0x00, 0x00, 0x00, /* HEADER_VERSION */
0x04, 0x00, 0x00, 0x00, /* IMAGE_WIDTH */
0x01, 0x00, 0x00, 0x00, /* IMAGE_HEIGHT */
0x04, 0x00, 0x00, 0x00, /* DATA_FORMAT */
0x00, 0x00, 0x00, 0x00, /* TIME_STAMP */
0x00, 0x00, 0x00, 0x00, /* FRAME_COUNT */
0x00, 0x00, 0x00, 0x00, /* STATUS_CODE */
0x00, 0x01, 0x00, 0x00, /* TIME_STAMP_SEC */
0x01, 0x01, 0x00, 0x00, /* TIME_STAMP_NSEC */
0xFF, 0xFF, 0xFF, 0xBB, /* DATA */
0x69, 0x00, 0x00, 0x00, /* CHUNK_TYPE of second frame*/
}),
"The Chunk size is bigger than the data size",
)

}

func TestRoundtrip(t *testing.T) {

chunk := pcic.NewChunk(pcic.WithChunkType(pcic.RADIAL_DISTANCE_NOISE))
chunkData, err := chunk.MarshalBinary()
assert.NoError(t, err, "No error expected when marshalling to binary")
roundTripChunk := pcic.NewChunk()
assert.NoError(t,
roundTripChunk.UnmarshalBinary(chunkData),
"No Error expected when unmarshalling from binary",
)
assert.Equal(t,
chunk.Type(),
roundTripChunk.Type(),
"We expect the type to be the same after the round trip",
)
}

0 comments on commit 35537fd

Please sign in to comment.