Skip to content

Commit

Permalink
Merge 6b33958 into 3cbc1ae
Browse files Browse the repository at this point in the history
  • Loading branch information
peternewman committed Mar 19, 2021
2 parents 3cbc1ae + 6b33958 commit 150b712
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion osc/osc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ func writePaddedString(str string, buf *bytes.Buffer) (int, error) {
// padBytesNeeded determines how many bytes are needed to fill up to the next 4
// byte length.
func padBytesNeeded(elementLen int) int {
return 4*(elementLen/4+1) - elementLen
return -elementLen%4
}

////
Expand Down
19 changes: 12 additions & 7 deletions osc/osc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func TestReadPaddedString(t *testing.T) {
s string // resulting string
}{
{[]byte{'t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g', 0, 0}, 12, "teststring"},
{[]byte{'t', 'e', 's', 't', 0, 0, 0, 0}, 8, "test"},
{[]byte{'t', 'e', 's', 't'}, 4, "test"},
} {
buf := bytes.NewBuffer(tt.buf)
s, n, err := readPaddedString(bufio.NewReader(buf))
Expand Down Expand Up @@ -391,28 +391,33 @@ func TestWritePaddedString(t *testing.T) {
func TestPadBytesNeeded(t *testing.T) {
var n int
n = padBytesNeeded(4)
if n != 4 {
t.Errorf("Number of pad bytes should be 4 and is: %d", n)
if n != 0 {
t.Errorf("Number of pad bytes should be 0 and is: %d", n)
}

n = padBytesNeeded(3)
if n != 1 {
t.Errorf("Number of pad bytes should be 1 and is: %d", n)
}

n = padBytesNeeded(2)
if n != 2 {
t.Errorf("Number of pad bytes should be 2 and is: %d", n)
}

n = padBytesNeeded(1)
if n != 3 {
t.Errorf("Number of pad bytes should be 3 and is: %d", n)
}

n = padBytesNeeded(0)
if n != 4 {
t.Errorf("Number of pad bytes should be 4 and is: %d", n)
if n != 0 {
t.Errorf("Number of pad bytes should be 0 and is: %d", n)
}

n = padBytesNeeded(32)
if n != 4 {
t.Errorf("Number of pad bytes should be 4 and is: %d", n)
if n != 0 {
t.Errorf("Number of pad bytes should be 0 and is: %d", n)
}

n = padBytesNeeded(63)
Expand Down

0 comments on commit 150b712

Please sign in to comment.