From be4f25c2b8152dd80ab6e51863e9ca345164d22b Mon Sep 17 00:00:00 2001 From: flow Date: Fri, 16 Aug 2024 16:50:47 +0800 Subject: [PATCH] fix RingBuffer.IsFull --- utils/ringbuffer/ring_buffer.go | 8 +- utils/ringbuffer/ring_buffer_test.go | 179 +++++++++++++++++++++++++++ 2 files changed, 186 insertions(+), 1 deletion(-) diff --git a/utils/ringbuffer/ring_buffer.go b/utils/ringbuffer/ring_buffer.go index ea3d8867a..7187635a9 100644 --- a/utils/ringbuffer/ring_buffer.go +++ b/utils/ringbuffer/ring_buffer.go @@ -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 @@ -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 { diff --git a/utils/ringbuffer/ring_buffer_test.go b/utils/ringbuffer/ring_buffer_test.go index b663bd93c..a565e7cf9 100644 --- a/utils/ringbuffer/ring_buffer_test.go +++ b/utils/ringbuffer/ring_buffer_test.go @@ -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) + } +}