Skip to content

Commit

Permalink
Fix crash is linebuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
evanphx committed Aug 12, 2016
1 parent c74d02f commit 7cd942b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions linebuffer/linebuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (lb *LineBuffer) Append(line string) error {
} else {
lb.lines[lb.cur] = line
lb.cur++

if lb.cur == len(lb.lines) {
lb.cur = 0
}
}

return nil
Expand Down
27 changes: 27 additions & 0 deletions linebuffer/linebuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,32 @@ func TestLineBuffer(t *testing.T) {
assert.Equal(t, "hello4", lines[2])
})

n.It("wraps around automatically multiple times", func() {
var lb LineBuffer

lb.Size = 3

lb.Append("hello1")
lb.Append("hello2")
lb.Append("hello3")
lb.Append("hello4")
lb.Append("hello5")
lb.Append("hello6")
lb.Append("hello7")

var lines []string

lb.Do(func(x string) error {
lines = append(lines, x)
return nil
})

assert.Equal(t, 3, len(lb.lines))

assert.Equal(t, "hello5", lines[0])
assert.Equal(t, "hello6", lines[1])
assert.Equal(t, "hello7", lines[2])
})

n.Meow()
}

0 comments on commit 7cd942b

Please sign in to comment.