Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pc/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ L_HEADER:
pp.Data[dataOffset:dataOffset+4], b,
)
case "U":
v, _ := strconv.ParseUint(pointData[lineOffset+j], 10, 32)
v, err := strconv.ParseUint(pointData[lineOffset+j], 10, 32)
if err != nil {
return nil, err
}
Expand Down
114 changes: 114 additions & 0 deletions pc/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package pc

import (
"bytes"
"errors"
"io"
"reflect"
"strconv"
"testing"

lzf "github.com/zhuyie/golzf"

"github.com/seqsense/pcgol/mat"
)

Expand All @@ -17,6 +22,7 @@ func TestUnmarshal(t *testing.T) {
testCases := map[string]struct {
pcd []byte
expected []point
err error
}{
"Ascii": {
pcd: []byte(`# .PCD v0.7 - Point Cloud Data file format
Expand Down Expand Up @@ -103,12 +109,120 @@ DATA ascii
{0.968, 0.512, -0.998, 1},
},
},
"ErrorVersion": {
pcd: []byte("VERSION X"),
err: strconv.ErrSyntax,
},
"ErrorSize": {
pcd: []byte("SIZE X"),
err: strconv.ErrSyntax,
},
"ErrorCount": {
pcd: []byte("COUNT X"),
err: strconv.ErrSyntax,
},
"ErrorWidth": {
pcd: []byte("WIDTH X"),
err: strconv.ErrSyntax,
},
"ErrorHeight": {
pcd: []byte("HEIGHT X"),
err: strconv.ErrSyntax,
},
"ErrorViewpoint": {
pcd: []byte("VIEWPOINT X"),
err: strconv.ErrSyntax,
},
"ErrorPoints": {
pcd: []byte("POINTS X"),
err: strconv.ErrSyntax,
},
"ErrorBrokenAsciiFloat": {
pcd: []byte(`VERSION 0.7
FIELDS x
SIZE 4
TYPE F
COUNT 1
WIDTH 1
HEIGHT 1
POINTS 5
DATA ascii
X
`),
err: strconv.ErrSyntax,
},
"ErrorBrokenAsciiUint": {
pcd: []byte(`VERSION 0.7
FIELDS x
SIZE 4
TYPE U
COUNT 1
WIDTH 1
HEIGHT 1
POINTS 5
DATA ascii
&
`),
err: strconv.ErrSyntax,
},
"ErrorBinaryCompressedNCompressedEOF": {
pcd: []byte{
0x56, 0x45, 0x52, 0x53, 0x49, 0x4f, 0x4e, 0x20, 0x30, 0x2e, 0x37, 0x0a, 0x46, 0x49, 0x45, 0x4c,
0x44, 0x53, 0x20, 0x78, 0x0a, 0x53, 0x49, 0x5a, 0x45, 0x20, 0x34, 0x0a, 0x54, 0x59, 0x50, 0x45,
0x20, 0x46, 0x0a, 0x43, 0x4f, 0x55, 0x4e, 0x54, 0x20, 0x31, 0x0a, 0x57, 0x49, 0x44, 0x54, 0x48,
0x20, 0x31, 0x0a, 0x48, 0x45, 0x49, 0x47, 0x48, 0x54, 0x20, 0x31, 0x0a, 0x50, 0x4f, 0x49, 0x4e,
0x54, 0x53, 0x20, 0x31, 0x0a, 0x44, 0x41, 0x54, 0x41, 0x20, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79,
0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x65, 0x64, 0x0a,
},
err: io.EOF,
},
"ErrorBinaryCompressedNUncompressedEOF": {
pcd: []byte{
0x56, 0x45, 0x52, 0x53, 0x49, 0x4f, 0x4e, 0x20, 0x30, 0x2e, 0x37, 0x0a, 0x46, 0x49, 0x45, 0x4c,
0x44, 0x53, 0x20, 0x78, 0x0a, 0x53, 0x49, 0x5a, 0x45, 0x20, 0x34, 0x0a, 0x54, 0x59, 0x50, 0x45,
0x20, 0x46, 0x0a, 0x43, 0x4f, 0x55, 0x4e, 0x54, 0x20, 0x31, 0x0a, 0x57, 0x49, 0x44, 0x54, 0x48,
0x20, 0x31, 0x0a, 0x48, 0x45, 0x49, 0x47, 0x48, 0x54, 0x20, 0x31, 0x0a, 0x50, 0x4f, 0x49, 0x4e,
0x54, 0x53, 0x20, 0x31, 0x0a, 0x44, 0x41, 0x54, 0x41, 0x20, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79,
0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x65, 0x64, 0x0a, 0x01, 0x00, 0x00, 0x00,
},
err: io.EOF,
},
"ErrorBinaryCompressedDataEOF": {
pcd: []byte{
0x56, 0x45, 0x52, 0x53, 0x49, 0x4f, 0x4e, 0x20, 0x30, 0x2e, 0x37, 0x0a, 0x46, 0x49, 0x45, 0x4c,
0x44, 0x53, 0x20, 0x78, 0x0a, 0x53, 0x49, 0x5a, 0x45, 0x20, 0x34, 0x0a, 0x54, 0x59, 0x50, 0x45,
0x20, 0x46, 0x0a, 0x43, 0x4f, 0x55, 0x4e, 0x54, 0x20, 0x31, 0x0a, 0x57, 0x49, 0x44, 0x54, 0x48,
0x20, 0x31, 0x0a, 0x48, 0x45, 0x49, 0x47, 0x48, 0x54, 0x20, 0x31, 0x0a, 0x50, 0x4f, 0x49, 0x4e,
0x54, 0x53, 0x20, 0x31, 0x0a, 0x44, 0x41, 0x54, 0x41, 0x20, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79,
0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x65, 0x64, 0x0a, 0x01, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00,
},
err: io.EOF,
},
"ErrorBinaryCompressedInvalidData": {
pcd: []byte{
0x56, 0x45, 0x52, 0x53, 0x49, 0x4f, 0x4e, 0x20, 0x30, 0x2e, 0x37, 0x0a, 0x46, 0x49, 0x45, 0x4c,
0x44, 0x53, 0x20, 0x78, 0x0a, 0x53, 0x49, 0x5a, 0x45, 0x20, 0x34, 0x0a, 0x54, 0x59, 0x50, 0x45,
0x20, 0x46, 0x0a, 0x43, 0x4f, 0x55, 0x4e, 0x54, 0x20, 0x31, 0x0a, 0x57, 0x49, 0x44, 0x54, 0x48,
0x20, 0x31, 0x0a, 0x48, 0x45, 0x49, 0x47, 0x48, 0x54, 0x20, 0x31, 0x0a, 0x50, 0x4f, 0x49, 0x4e,
0x54, 0x53, 0x20, 0x31, 0x0a, 0x44, 0x41, 0x54, 0x41, 0x20, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79,
0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x65, 0x64, 0x0a, 0x01, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00,
},
err: lzf.ErrDataCorruption,
},
}

for name, tt := range testCases {
tt := tt
t.Run(name, func(t *testing.T) {
pp, err := Unmarshal(bytes.NewReader(tt.pcd))
if tt.err != nil {
if !errors.Is(err, tt.err) {
t.Fatalf("Expected error: '%v', got: '%v'", tt.err, err)
}
return
}
if err != nil {
t.Fatal(err)
}
Expand Down