github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/container/ringbuf/ringbuf_test.go (about)

     1  package ringbuf
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestRingBuf(t *testing.T) {
     8  	rb := NewRingBuf(16, 0)
     9  	rb.Write([]byte("fghibbbbccccddde"))
    10  	rb.Write([]byte("fghibbbbc"))
    11  	rb.Resize(16)
    12  	off := rb.Evacuate(9, 3)
    13  	t.Log(string(rb.Dump()))
    14  	if off != rb.End()-3 {
    15  		t.Log(string(rb.Dump()), rb.End())
    16  		t.Fatalf("off got %v", off)
    17  	}
    18  	off = rb.Evacuate(15, 5)
    19  	t.Log(string(rb.Dump()))
    20  	if off != rb.End()-5 {
    21  		t.Fatalf("off got %v", off)
    22  	}
    23  	rb.Resize(64)
    24  	rb.Resize(32)
    25  	data := make([]byte, 5)
    26  	rb.ReadAt(data, off)
    27  	if string(data) != "efghi" {
    28  		t.Fatalf("read at should be efghi, got %v", string(data))
    29  	}
    30  
    31  	off = rb.Evacuate(0, 10)
    32  	if off != -1 {
    33  		t.Fatal("evacutate out of range offset should return error")
    34  	}
    35  }