Skip to content

Commit

Permalink
fix RingBuffer.IsFull
Browse files Browse the repository at this point in the history
  • Loading branch information
flowbehappy committed Aug 16, 2024
1 parent 44a2349 commit be4f25c
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 1 deletion.
8 changes: 7 additions & 1 deletion utils/ringbuffer/ring_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ func (rb *RingBuffer[T]) PushBack(item T) {

// Push to front
func (rb *RingBuffer[T]) PushFront(item T) {
// equivalent to
// if rb.front == 0 {
// rb.front = rb.capacity
// } else {
// rb.front--
// }
rb.front = (rb.front + rb.capacity) % (rb.capacity + 1)
rb.buffer[rb.front] = item

Expand Down Expand Up @@ -117,7 +123,7 @@ func (rb *RingBuffer[T]) PopBack() (T, bool) {
}

func (rb *RingBuffer[T]) IsFull() bool {
return (rb.back+1)%rb.capacity == rb.front
return (rb.back+1)%(rb.capacity+1) == rb.front
}

func (rb *RingBuffer[T]) IsEmpty() bool {
Expand Down
179 changes: 179 additions & 0 deletions utils/ringbuffer/ring_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,182 @@ func TestRingBuffer(t *testing.T) {
assert.Equal(t, []int{3, 6, 7}, items)
}
}

func TestRingBufferReverse(t *testing.T) {
rb := NewRingBuffer[int](3)

rb.PushFront(1)
rb.PushFront(2)
rb.PushFront(3)

{
item, ok := rb.Back()
assert.Equal(t, true, ok)
assert.Equal(t, 1, item)
}

rb.PushFront(4)

{
item, ok := rb.Back()
assert.Equal(t, true, ok)
assert.Equal(t, 2, item)
}

{
item, ok := rb.Front()
assert.Equal(t, true, ok)
assert.Equal(t, 4, item)
}

rb.PushFront(5)
rb.PushFront(6)
rb.PushFront(7)
rb.PushFront(8)

assert.Equal(t, true, rb.IsFull())
{
item, ok := rb.Back()
assert.Equal(t, true, ok)
assert.Equal(t, 6, item)
}

{
item, ok := rb.Front()
assert.Equal(t, true, ok)
assert.Equal(t, 8, item)
}

{
item, ok := rb.PopBack()
assert.Equal(t, true, ok)
assert.Equal(t, 6, item)
}
{
item, ok := rb.PopBack()
assert.Equal(t, true, ok)
assert.Equal(t, 7, item)
}
{
item, ok := rb.PopBack()
assert.Equal(t, true, ok)
assert.Equal(t, 8, item)
}
{
item, ok := rb.PopBack()
assert.Equal(t, false, ok)
assert.Equal(t, 0, item)
}
assert.Equal(t, true, rb.IsEmpty())

rb.PushFront(1)
rb.PushFront(2)
assert.Equal(t, 2, rb.Length())

rb.PushFront(3)

assert.Equal(t, 3, rb.Length())

{
item, ok := rb.Back()
assert.Equal(t, true, ok)
assert.Equal(t, 1, item)
}

rb.PushFront(4)

{
item, ok := rb.Back()
assert.Equal(t, true, ok)
assert.Equal(t, 2, item)
}

{
item, ok := rb.Front()
assert.Equal(t, true, ok)
assert.Equal(t, 4, item)
}

{
itr := rb.BackwardIterator()
items := make([]int, 0)
for item, ok := itr.Next(); ok; item, ok = itr.Next() {
items = append(items, item)
}
assert.Equal(t, []int{2, 3, 4}, items)
}

{
itr := rb.ForwardIterator()
items := make([]int, 0)
for item, ok := itr.Next(); ok; item, ok = itr.Next() {
items = append(items, item)
}
assert.Equal(t, []int{4, 3, 2}, items)
}

{
item, ok := rb.PopFront()
assert.Equal(t, true, ok)
assert.Equal(t, 4, item)
item, ok = rb.PopFront()
assert.Equal(t, true, ok)
assert.Equal(t, 3, item)
item, ok = rb.PopFront()
assert.Equal(t, true, ok)
assert.Equal(t, 2, item)
_, ok = rb.PopFront()
assert.Equal(t, false, ok)
}

{
rb.PushFront(1)
rb.PushFront(2)

rb.PushFront(3)
rb.PushFront(4)
rb.PushFront(5)

item, ok := rb.PopFront()
assert.Equal(t, true, ok)
assert.Equal(t, 5, item)
item, ok = rb.PopFront()
assert.Equal(t, true, ok)
assert.Equal(t, 4, item)
item, ok = rb.PopFront()
assert.Equal(t, true, ok)
assert.Equal(t, 3, item)
_, ok = rb.PopFront()
assert.Equal(t, false, ok)
}

{
rb.PushFront(1)
rb.PushFront(2)

rb.PushFront(3)
rb.PushFront(4)
rb.PushFront(5)

rb.PushBack(6)
rb.PushBack(7)
}

{
itr := rb.BackwardIterator()
items := make([]int, 0)
for item, ok := itr.Next(); ok; item, ok = itr.Next() {
items = append(items, item)
}
assert.Equal(t, []int{7, 6, 3}, items)
}

{
itr := rb.ForwardIterator()
items := make([]int, 0)
for item, ok := itr.Next(); ok; item, ok = itr.Next() {
items = append(items, item)
}
assert.Equal(t, []int{3, 6, 7}, items)
}
}

0 comments on commit be4f25c

Please sign in to comment.