You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: ring_buffer_test.go
    path of file: ./repos/gnet/pkg/buffer/ring
    
    the code of the file until where you have to start completion: package ring

import (
	"bytes"
	"math/rand"
	"strings"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestRingBuffer_Write(t *testing.T) {
	rb := New(64)

	_, err := rb.ReadByte()
	assert.ErrorIs(t, err, ErrIsEmpty, "expect nil err, but got nil")
	// check empty or full
	assert.True(t, rb.IsEmpty(), "expect IsEmpty is true but got false")
	assert.False(t, rb.IsFull(), "expect IsFull is false but got true")
	assert.EqualValuesf(t, 0, rb.Buffered(), "expect len 0 bytes but got %d. r.w=%d, r.r=%d", rb.Buffered(), rb.w, rb.r)
	assert.EqualValuesf(t, 64, rb.Available(), "expect free 64 bytes but got %d. r.w=%d, r.r=%d", rb.Available(), rb.w, rb.r)

	// write 4 * 4 = 16 bytes
	data := []byte(strings.Repeat("abcd", 4))
	n, _ := rb.Write(data)
	assert.EqualValuesf(t, 16, n, "expect write 16 bytes but got %d", n)
	assert.EqualValuesf(t, 16, rb.Buffered(), "expect len 16 bytes but got %d. r.w=%d, r.r=%d", rb.Buffered(), rb.w, rb.r)
	assert.EqualValuesf(t, 48, rb.Available(), "expect free 48 bytes but got %d. r.w=%d, r.r=%d", rb.Available(), rb.w, rb.r)
	assert.EqualValuesf(t, data, rb.Bytes(), "expect 4 abcd but got %s. r.w=%d, r.r=%d", rb.Bytes(), rb.w, rb.r)

	// check empty or full
	assert.False(t, rb.IsEmpty(), "expect IsEmpty is false but got true")
	assert.False(t, rb.IsFull(), "expect IsFull is false but got true")

	// write 48 bytes, should full
	data = []byte(strings.Repeat("abcd", 12))
	n, _ = rb.Write(data)
	assert.EqualValuesf(t, 48, n, "expect write 48 bytes but got %d", n)
	assert.EqualValuesf(t, 64, rb.Buffered(), "expect len 64 bytes but got %d. r.w=%d, r.r=%d", rb.Buffered(), rb.w, rb.r)
	assert.EqualValuesf(t, 0, rb.Available(), "expect free 0 bytes but got %d. r.w=%d, r.r=%d", rb.Available(), rb.w, rb.r)
	assert.EqualValuesf(t, 0, rb.w, "expect r.w=0 but got %d. r.r=%d", rb.w, rb.r)
	assert.EqualValuesf(t, []byte(strings.Repeat("abcd", 16)), rb.Bytes(), "expect 16 abcd but got %s. r.w=%d, r.r=%d", rb.Bytes(), rb.w, rb.r)

	// check empty or full
	assert.False(t, rb.IsEmpty(), "expect IsEmpty is false but got true")
	assert.True(t, rb.IsFull(), "expect IsFull is true but got false")
	assert.True(t, rb.IsFull(), "expect IsFull is true but got false
    
func TestRingBuffer_Read(t *testing.T) {
	rb := New(64)

	// check empty or full
	assert.True(t, rb.IsEmpty(), "expect IsEmpty is true but got false")
	assert.False(t, rb.IsFull(), "expect isfull is false but got true")
	assert.EqualValuesf(t, 0, rb.Buffered(), "expect len 0 bytes but got %d. r.w=%d, r.r=%d", rb.Buffered(), rb.w, rb.r)
	assert.EqualValuesf(t, 64, rb.Available(), "expect free 64 bytes but got %d. r.w=%d, r.r=%d", rb.Available(), rb.w, rb.r)

	// read empty
	buf := make([]byte, 1024)
	n, err := rb.Read(buf)
	assert.ErrorIs(t, err, ErrIsEmpty, "expect ErrIsEmpty but got nil")
	assert.EqualValuesf(t, 0, n, "expect read 0 bytes but got %d", n)
	assert.EqualValuesf(t, 0, rb.Buffered(), "expect len 0 bytes but got %d. r.w=%d, r.r=%d", rb.Buffered(), rb.w, rb.r)
	assert.EqualValuesf(t, 64, rb.Available(), "expect free 64 bytes but got %d. r.w=%d, r.r=%d", rb.Available(), rb.w, rb.r)
	assert.EqualValuesf(t, 0, rb.r, "expect r.r=0 but got %d. r.w=%d", rb.r, rb.w)

	// write 16 bytes to read
	_, _ = rb.Write([]byte(strings.Repeat("abcd", 4)))
	// read all data from buffer, it will be shrunk from 64 to 32.
	n, err = rb.Read(buf)
	assert.NoErrorf(t, err, "read failed: %v", err)
	assert.EqualValuesf(t, 16, n, "expect read 16 bytes but got %d", n)
	assert.EqualValuesf(t, 0, rb.Buffered(), "expect len 0 bytes but got %d. r.w=%d, r.r=%d", rb.Buffered(), rb.w, rb.r)
	assert.EqualValuesf(t, 64, rb.Available(), "expect free 64 bytes but got %d. r.w=%d, r.r=%d", rb.Available(), rb.w, rb.r)
	assert.EqualValuesf(t, 0, rb.r, "expect r.r=0 but got %d. r.w=%d", rb.r, rb.w)

	// write long slice to read, it will scale from 32 to 128 bytes.
	_, _ = rb.Write([]byte(strings.Repeat("abcd", 20)))
	// read all data from buffer, it will be shrunk from 128 to 64.
	n, err = rb.Read(buf)
	assert.NoErrorf(t, err, "read failed: %v", err)
	assert.EqualValuesf(t, 80, n, "expect read 80 bytes but got %d", n)
	assert.EqualValuesf(t, 0, rb.Buffered(), "expect len 0 bytes but got %d. r.w=%d, r.r=%d", rb.Buffered(), rb.w, rb.r)
	assert.EqualValuesf(t, 128, rb.Available(), "expect free 128 bytes but got %d. r.w=%d, r.r=%d", rb.Available(), rb.w, rb.r)
	assert.EqualValuesf(t, 0, rb.r, "expect r.r=0 but got %d. r.w=%d", rb.r, rb.w)

	rb.Reset()
	_, _ = rb.Write([]byte(strings.Repeat("1234", 32)))
	assert.True(t, rb.IsFull(), "ring buffer should be full")
	assert.EqualValuesf(t, 0, rb.Available(), "expect free 0 bytes but got %d. r.w=%d, r.r=%d", rb.Available(), rb.w, rb.r)
	assert.EqualValuesf(t, 0, rb.w, "expect r.2=0 but got %d. r.r=%d", rb.w, rb.r)
	head, tail := rb.Peek(64)
	assert.Truef(t, len(head) == 64 && tail == nil, "expect len(head)=64 and tail=nil, yet len(head)=%d and tail != nil", len(head))
	assert.EqualValuesf(t, 0, rb.r, "expect r.r=0 but got %d", rb.r)
	assert.EqualValues(t, []byte(strings.Repeat("1234", 16)), head)
	_, _ = rb.Discard(64)
	assert.EqualValuesf(t, 64, rb.r, "expect r.r=64 but got %d", rb.r)
	_, _ = rb.Write([]byte(strings.Repeat("1234", 4)))
	assert.EqualValuesf(t, 16, rb.w, "expect r.w=16 but got %d", rb.w)
	head, tail = rb.Peek(128)
	assert.Truef(t, len(head) == 64 && len(tail) == 16, "expect len(head)=64 and len(tail)=16, yet len(head)=%d and len(tail)=%d", len(head), len(tail))
	assert.EqualValues(t, []byte(strings.Repeat("1234", 16)), head)
	assert.EqualValues(t, []byte(strings.Repeat("1234", 4)), tail)

	head, tail = rb.Peek(-1)
	assert.Truef(t, len(head) == 64 && len(tail) == 16, "expect len(head)=64 and len(tail)=16, yet len(head)=%d and len(tail)=%d", len(head), len(tail))
	assert.EqualValues(t, []byte(strings.Repeat("1234", 16)), head)
	assert.EqualValues(t, []byte(strings.Repeat("1234", 4)), tail)

	_, _ = rb.Discard(64)
	_, _ = rb.Discard(16)
	assert.True(t, rb.IsEmpty(), "should be empty")
}