You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/common/buf/buffer_test.go

168 lines
3.1 KiB

package buf_test
import (
"bytes"
"crypto/rand"
"testing"
6 years ago
"github.com/google/go-cmp/cmp"
6 years ago
"v2ray.com/core/common"
. "v2ray.com/core/common/buf"
)
func TestBufferClear(t *testing.T) {
8 years ago
buffer := New()
defer buffer.Release()
payload := "Bytes"
buffer.Write([]byte(payload))
6 years ago
if diff := cmp.Diff(buffer.Bytes(), []byte(payload)); diff != "" {
6 years ago
t.Error(diff)
}
buffer.Clear()
6 years ago
if buffer.Len() != 0 {
t.Error("expect 0 lenght, but got ", buffer.Len())
}
}
func TestBufferIsEmpty(t *testing.T) {
8 years ago
buffer := New()
defer buffer.Release()
6 years ago
if buffer.IsEmpty() != true {
t.Error("expect empty buffer, but not")
}
}
9 years ago
func TestBufferString(t *testing.T) {
8 years ago
buffer := New()
defer buffer.Release()
6 years ago
const payload = "Test String"
common.Must2(buffer.WriteString(payload))
if buffer.String() != payload {
t.Error("expect buffer content as ", payload, " but actually ", buffer.String())
}
}
8 years ago
6 years ago
func TestBufferSlice(t *testing.T) {
{
b := New()
common.Must2(b.Write([]byte("abcd")))
bytes := b.BytesFrom(-2)
6 years ago
if diff := cmp.Diff(bytes, []byte{'c', 'd'}); diff != "" {
t.Error(diff)
6 years ago
}
}
{
b := New()
common.Must2(b.Write([]byte("abcd")))
bytes := b.BytesTo(-2)
6 years ago
if diff := cmp.Diff(bytes, []byte{'a', 'b'}); diff != "" {
t.Error(diff)
6 years ago
}
}
{
b := New()
common.Must2(b.Write([]byte("abcd")))
bytes := b.BytesRange(-3, -1)
6 years ago
if diff := cmp.Diff(bytes, []byte{'b', 'c'}); diff != "" {
t.Error(diff)
6 years ago
}
}
}
func TestBufferReadFullFrom(t *testing.T) {
payload := make([]byte, 1024)
common.Must2(rand.Read(payload))
reader := bytes.NewReader(payload)
b := New()
n, err := b.ReadFullFrom(reader, 1024)
common.Must(err)
if n != 1024 {
t.Error("expect reading 1024 bytes, but actually ", n)
}
6 years ago
if diff := cmp.Diff(payload, b.Bytes()); diff != "" {
t.Error(diff)
}
}
func BenchmarkNewBuffer(b *testing.B) {
8 years ago
for i := 0; i < b.N; i++ {
8 years ago
buffer := New()
8 years ago
buffer.Release()
}
}
6 years ago
func BenchmarkNewBufferStack(b *testing.B) {
for i := 0; i < b.N; i++ {
buffer := StackNew()
buffer.Release()
}
}
func BenchmarkWrite2(b *testing.B) {
buffer := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = buffer.Write([]byte{'a', 'b'})
buffer.Clear()
}
}
func BenchmarkWrite8(b *testing.B) {
buffer := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = buffer.Write([]byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'})
buffer.Clear()
}
}
func BenchmarkWrite32(b *testing.B) {
buffer := New()
payload := make([]byte, 32)
rand.Read(payload)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = buffer.Write(payload)
buffer.Clear()
}
}
func BenchmarkWriteByte2(b *testing.B) {
buffer := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = buffer.WriteByte('a')
_ = buffer.WriteByte('b')
buffer.Clear()
}
}
func BenchmarkWriteByte8(b *testing.B) {
buffer := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = buffer.WriteByte('a')
_ = buffer.WriteByte('b')
_ = buffer.WriteByte('c')
_ = buffer.WriteByte('d')
_ = buffer.WriteByte('e')
_ = buffer.WriteByte('f')
_ = buffer.WriteByte('g')
_ = buffer.WriteByte('h')
buffer.Clear()
}
}