github.com/daeuniverse/quic-go@v0.0.0-20240413031024-943f218e0810/internal/utils/ringbuffer/ringbuffer_test.go (about)

     1  package ringbuffer
     2  
     3  import (
     4  	. "github.com/onsi/ginkgo/v2"
     5  	. "github.com/onsi/gomega"
     6  )
     7  
     8  var _ = Describe("RingBuffer", func() {
     9  	It("push, peek and pop", func() {
    10  		r := RingBuffer[int]{}
    11  		Expect(len(r.ring)).To(Equal(0))
    12  		Expect(func() { r.PopFront() }).To(Panic())
    13  		r.PushBack(1)
    14  		r.PushBack(2)
    15  		r.PushBack(3)
    16  		Expect(r.PeekFront()).To(Equal(1))
    17  		Expect(r.PeekFront()).To(Equal(1))
    18  		Expect(r.PopFront()).To(Equal(1))
    19  		Expect(r.PeekFront()).To(Equal(2))
    20  		Expect(r.PopFront()).To(Equal(2))
    21  		r.PushBack(4)
    22  		r.PushBack(5)
    23  		Expect(r.Len()).To(Equal(3))
    24  		r.PushBack(6)
    25  		Expect(r.Len()).To(Equal(4))
    26  		Expect(r.PopFront()).To(Equal(3))
    27  		Expect(r.PopFront()).To(Equal(4))
    28  		Expect(r.PopFront()).To(Equal(5))
    29  		Expect(r.PopFront()).To(Equal(6))
    30  	})
    31  
    32  	It("panics when Peek or Pop are called on an empty buffer", func() {
    33  		r := RingBuffer[string]{}
    34  		Expect(r.Empty()).To(BeTrue())
    35  		Expect(r.Len()).To(BeZero())
    36  		Expect(func() { r.PeekFront() }).To(Panic())
    37  		Expect(func() { r.PopFront() }).To(Panic())
    38  	})
    39  
    40  	It("clearing", func() {
    41  		r := RingBuffer[int]{}
    42  		r.Init(2)
    43  		r.PushBack(1)
    44  		r.PushBack(2)
    45  		Expect(r.full).To(BeTrue())
    46  		r.Clear()
    47  		Expect(r.full).To(BeFalse())
    48  		Expect(r.Len()).To(Equal(0))
    49  	})
    50  })