Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set capacity, not length, for bytes buffer allocation #200

Merged
merged 4 commits into from
Apr 10, 2021
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
4 changes: 2 additions & 2 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ func readBytes(r dicomio.Reader, t tag.Tag, vr string, vl uint32) (Value, error)
if vl%2 != 0 {
return nil, ErrorOWRequiresEvenVL
}
data := make([]byte, vl)
buf := bytes.NewBuffer(data)

buf := bytes.NewBuffer(make([]byte, 0, vl))
numWords := int(vl / 2)
for i := 0; i < numWords; i++ {
word, err := r.ReadUInt16()
Expand Down
47 changes: 47 additions & 0 deletions read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,53 @@ func TestReadFloat_float32(t *testing.T) {
}
}

func TestReadOWBytes(t *testing.T) {
cases := []struct {
name string
bytes []byte
VR string
want Value
expectedErr error
}{
{
name: "even-number bytes",
bytes: []byte{0x1, 0x2, 0x3, 0x4},
VR: vrraw.OtherWord,
want: &bytesValue{value: []byte{0x1, 0x2, 0x3, 0x4}},
expectedErr: nil,
},
{
name: "error on odd-number bytes",
bytes: []byte{0x1, 0x2, 0x3},
VR: vrraw.OtherWord,
want: nil,
expectedErr: ErrorOWRequiresEvenVL,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
data := bytes.Buffer{}
if err := binary.Write(&data, binary.LittleEndian, tc.bytes); err != nil {
t.Errorf("TestReadOWBytes: Unable to setup test buffer")
}

r, err := dicomio.NewReader(bufio.NewReader(&data), binary.LittleEndian, int64(data.Len()))
if err != nil {
t.Errorf("TestReadOWBytes: unable to create new dicomio.Reader")
}

got, err := readBytes(r, tag.Tag{}, tc.VR, uint32(data.Len()))
if err != tc.expectedErr {
t.Fatalf("readBytes(r, tg, %s, %d) got unexpected error: got: %v, want: %v", tc.VR, data.Len(), err, tc.expectedErr)
}
if diff := cmp.Diff(got, tc.want, cmp.AllowUnexported(bytesValue{})); diff != "" {
t.Errorf("readBytes(r, tg, %s, %d) unexpected diff: %s", tc.VR, data.Len(), diff)
}
})
}
}

func TestReadNativeFrames(t *testing.T) {
cases := []struct {
Name string
Expand Down
42 changes: 42 additions & 0 deletions write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestWrite(t *testing.T) {
mustNewElement(tag.Rows, []int{128}),
mustNewElement(tag.FloatingPointValue, []float64{128.10}),
mustNewElement(tag.DimensionIndexPointer, []int{32, 36950}),
mustNewElement(tag.RedPaletteColorLookupTableData, []byte{0x1, 0x2, 0x3, 0x4}),
}},
expectedError: nil,
},
Expand Down Expand Up @@ -587,6 +588,47 @@ func TestWriteFloats(t *testing.T) {

}

func TestWriteOtherWord(t *testing.T) {
// TODO: add additional cases
cases := []struct {
name string
value []byte
vr string
expectedData []byte
expectedErr error
}{
{
name: "OtherWord",
value: []byte{0x1, 0x2, 0x3, 0x4},
vr: "OW",
expectedData: []byte{0x1, 0x2, 0x3, 0x4},
expectedErr: nil,
},
{
name: "OtherBytes",
value: []byte{0x1, 0x2, 0x3, 0x4},
vr: "OB",
expectedData: []byte{0x1, 0x2, 0x3, 0x4},
expectedErr: nil,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
buf := bytes.Buffer{}
w := dicomio.NewWriter(&buf, binary.LittleEndian, false)
err := writeBytes(w, tc.value, tc.vr)
if err != tc.expectedErr {
t.Errorf("writeBytes(%v, %s) returned unexpected err. got: %v, want: %v", tc.value, tc.vr, err, tc.expectedErr)
}
if diff := cmp.Diff(tc.expectedData, buf.Bytes()); diff != "" {
t.Errorf("writeBytes(%v, %s) wrote unexpected data. diff: %s", tc.value, tc.vr, diff)
t.Errorf("% x", buf.Bytes())
}
})
}

}

func setUndefinedLength(e *Element) *Element {
e.ValueLength = tag.VLUndefinedLength
return e
Expand Down