Skip to content

Commit

Permalink
add uint32 read test
Browse files Browse the repository at this point in the history
  • Loading branch information
suyashkumar committed Aug 9, 2024
1 parent e598a0e commit a667e8e
Showing 1 changed file with 56 additions and 21 deletions.
77 changes: 56 additions & 21 deletions read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,23 +221,24 @@ func TestReadNativeFrames(t *testing.T) {
cases := []struct {
Name string
existingData Dataset
data []uint16
uint16Data []uint16
dataBytes []byte
uint32Data []uint32
expectedPixelData *PixelDataInfo
expectedError error
pixelVLOverride uint32
parseOptSet parseOptSet
}{
{
Name: "5x5, 1 frame, 1 samples/pixel",
Name: "5x5, 1 frame, 1 samples/pixel, bitsAllocated=16",
existingData: Dataset{Elements: []*Element{
mustNewElement(tag.Rows, []int{5}),
mustNewElement(tag.Columns, []int{5}),
mustNewElement(tag.NumberOfFrames, []string{"1"}),
mustNewElement(tag.BitsAllocated, []int{16}),
mustNewElement(tag.SamplesPerPixel, []int{1}),
}},
data: []uint16{1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
uint16Data: []uint16{1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
expectedPixelData: &PixelDataInfo{
IsEncapsulated: false,
Frames: []*frame.Frame{
Expand All @@ -256,15 +257,15 @@ func TestReadNativeFrames(t *testing.T) {
expectedError: nil,
},
{
Name: "2x2, 3 frames, 1 samples/pixel",
Name: "2x2, 3 frames, 1 samples/pixel, bitsAllocated=16",
existingData: Dataset{Elements: []*Element{
mustNewElement(tag.Rows, []int{2}),
mustNewElement(tag.Columns, []int{2}),
mustNewElement(tag.NumberOfFrames, []string{"3"}),
mustNewElement(tag.BitsAllocated, []int{16}),
mustNewElement(tag.SamplesPerPixel, []int{1}),
}},
data: []uint16{1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 0},
uint16Data: []uint16{1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 0},
expectedPixelData: &PixelDataInfo{
IsEncapsulated: false,
Frames: []*frame.Frame{
Expand Down Expand Up @@ -303,15 +304,15 @@ func TestReadNativeFrames(t *testing.T) {
expectedError: nil,
},
{
Name: "2x2, 2 frames, 2 samples/pixel",
Name: "2x2, 2 frames, 2 samples/pixel, bitsAllocated=16",
existingData: Dataset{Elements: []*Element{
mustNewElement(tag.Rows, []int{2}),
mustNewElement(tag.Columns, []int{2}),
mustNewElement(tag.NumberOfFrames, []string{"2"}),
mustNewElement(tag.BitsAllocated, []int{16}),
mustNewElement(tag.SamplesPerPixel, []int{2}),
}},
data: []uint16{1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 5},
uint16Data: []uint16{1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 5},
expectedPixelData: &PixelDataInfo{
IsEncapsulated: false,
Frames: []*frame.Frame{
Expand Down Expand Up @@ -339,6 +340,33 @@ func TestReadNativeFrames(t *testing.T) {
},
expectedError: nil,
},
{
Name: "bitsAllocated=32",
existingData: Dataset{Elements: []*Element{
mustNewElement(tag.Rows, []int{5}),
mustNewElement(tag.Columns, []int{5}),
mustNewElement(tag.NumberOfFrames, []string{"1"}),
mustNewElement(tag.BitsAllocated, []int{32}),
mustNewElement(tag.SamplesPerPixel, []int{1}),
}},
uint32Data: []uint32{1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
expectedPixelData: &PixelDataInfo{
IsEncapsulated: false,
Frames: []*frame.Frame{
{
Encapsulated: false,
NativeData: &frame.NativeFrame[uint32]{
InternalBitsPerSample: 32,
InternalRows: 5,
InternalCols: 5,
InternalSamplesPerPixel: 1,
RawData: []uint32{1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
},
},
},
},
expectedError: nil,
},
{
Name: "insufficient bytes, uint32",
existingData: Dataset{Elements: []*Element{
Expand All @@ -348,7 +376,7 @@ func TestReadNativeFrames(t *testing.T) {
mustNewElement(tag.BitsAllocated, []int{32}),
mustNewElement(tag.SamplesPerPixel, []int{2}),
}},
data: []uint16{1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3},
uint16Data: []uint16{1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3},
expectedPixelData: nil,
expectedError: ErrorMismatchPixelDataLength,
},
Expand All @@ -361,7 +389,7 @@ func TestReadNativeFrames(t *testing.T) {
mustNewElement(tag.BitsAllocated, []int{32}),
mustNewElement(tag.SamplesPerPixel, []int{2}),
}},
data: []uint16{1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 2},
uint16Data: []uint16{1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 2},
expectedPixelData: nil,
expectedError: ErrorMismatchPixelDataLength,
},
Expand All @@ -374,7 +402,7 @@ func TestReadNativeFrames(t *testing.T) {
mustNewElement(tag.BitsAllocated, []int{32}),
mustNewElement(tag.SamplesPerPixel, []int{2}),
}},
data: []uint16{1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 2},
uint16Data: []uint16{1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 2},
expectedPixelData: &PixelDataInfo{
ParseErr: ErrorMismatchPixelDataLength,
Frames: []*frame.Frame{
Expand All @@ -396,7 +424,7 @@ func TestReadNativeFrames(t *testing.T) {
mustNewElement(tag.BitsAllocated, []int{16}),
mustNewElement(tag.SamplesPerPixel, []int{1}),
}},
data: []uint16{1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
uint16Data: []uint16{1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
expectedPixelData: nil,
expectedError: ErrorElementNotFound,
},
Expand All @@ -409,12 +437,12 @@ func TestReadNativeFrames(t *testing.T) {
mustNewElement(tag.BitsAllocated, []int{24}),
mustNewElement(tag.SamplesPerPixel, []int{1}),
}},
data: []uint16{1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
uint16Data: []uint16{1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
expectedPixelData: nil,
expectedError: ErrorUnsupportedBitsAllocated,
},
{
Name: "3x3, 3 frames, 1 samples/pixel, data bytes with padded 0",
Name: "3x3, 3 frames, 1 samples/pixel, bytes data (uint8) with padded 0",
existingData: Dataset{Elements: []*Element{
mustNewElement(tag.Rows, []int{3}),
mustNewElement(tag.Columns, []int{3}),
Expand Down Expand Up @@ -462,7 +490,7 @@ func TestReadNativeFrames(t *testing.T) {
expectedError: nil,
},
{
Name: "1x1, 3 frames, 3 samples/pixel, data bytes with padded 0",
Name: "1x1, 3 frames, 3 samples/pixel, bytes data (uint8) with padded 0",
existingData: Dataset{Elements: []*Element{
mustNewElement(tag.Rows, []int{1}),
mustNewElement(tag.Columns, []int{1}),
Expand Down Expand Up @@ -530,18 +558,25 @@ func TestReadNativeFrames(t *testing.T) {
dcmdata := bytes.Buffer{}
var expectedBytes int

if len(tc.data) == 0 {
if len(tc.dataBytes) != 0 {
// writing byte-by-byte
expectedBytes = len(tc.dataBytes)
for _, item := range tc.dataBytes {
if err := binary.Write(&dcmdata, binary.LittleEndian, item); err != nil {
t.Errorf("TestReadNativeFrames: Unable to setup test buffer")
}
}
} else {
} else if len(tc.uint16Data) != 0 {
// writing 2 bytes (uint16) at a time
expectedBytes = len(tc.data) * 2
for _, item := range tc.data {
expectedBytes = len(tc.uint16Data) * 2
for _, item := range tc.uint16Data {
if err := binary.Write(&dcmdata, binary.LittleEndian, item); err != nil {
t.Errorf("TestReadNativeFrames: Unable to setup test buffer")
}
}
} else if len(tc.uint32Data) != 0 {
expectedBytes = len(tc.uint32Data) * 4
for _, item := range tc.uint32Data {
if err := binary.Write(&dcmdata, binary.LittleEndian, item); err != nil {
t.Errorf("TestReadNativeFrames: Unable to setup test buffer")
}
Expand All @@ -562,14 +597,14 @@ func TestReadNativeFrames(t *testing.T) {

pixelData, bytesRead, err := r.readNativeFrames(&tc.existingData, nil, vl)
if !errors.Is(err, tc.expectedError) {
t.Errorf("TestReadNativeFrames(%v): did not get expected error. got: %v, want: %v", tc.data, err, tc.expectedError)
t.Errorf("TestReadNativeFrames(%+v): did not get expected error. got: %v, want: %v", tc, err, tc.expectedError)
}
if err == nil && bytesRead != expectedBytes {
t.Errorf("TestReadNativeFrames(%v): did not read expected number of bytes. got: %d, want: %d", tc.data, bytesRead, expectedBytes)
t.Errorf("TestReadNativeFrames(%+v): did not read expected number of bytes. got: %d, want: %d", tc, bytesRead, expectedBytes)
}

if diff := cmp.Diff(tc.expectedPixelData, pixelData, cmpopts.EquateErrors()); diff != "" {
t.Errorf("TestReadNativeFrames(%v): unexpected diff: %v", tc.data, diff)
t.Errorf("TestReadNativeFrames(%+v): unexpected diff: %v", tc, diff)
}
})
}
Expand Down

0 comments on commit a667e8e

Please sign in to comment.