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

106 lines
1.9 KiB

package buf_test
import (
"testing"
. "v2ray.com/core/common/buf"
"v2ray.com/core/common/serial"
"v2ray.com/core/testing/assert"
)
func TestBufferClear(t *testing.T) {
9 years ago
assert := assert.On(t)
8 years ago
buffer := New()
defer buffer.Release()
payload := "Bytes"
buffer.Append([]byte(payload))
assert.Int(buffer.Len()).Equals(len(payload))
buffer.Clear()
assert.Int(buffer.Len()).Equals(0)
}
func TestBufferIsEmpty(t *testing.T) {
9 years ago
assert := assert.On(t)
8 years ago
buffer := New()
defer buffer.Release()
assert.Bool(buffer.IsEmpty()).IsTrue()
}
9 years ago
func TestBufferString(t *testing.T) {
9 years ago
assert := assert.On(t)
8 years ago
buffer := New()
defer buffer.Release()
8 years ago
buffer.AppendSupplier(serial.WriteString("Test String"))
9 years ago
assert.String(buffer.String()).Equals("Test String")
}
8 years ago
func TestBufferWrite(t *testing.T) {
assert := assert.On(t)
8 years ago
buffer := NewLocal(8)
nBytes, err := buffer.Write([]byte("abcd"))
assert.Error(err).IsNil()
assert.Int(nBytes).Equals(4)
nBytes, err = buffer.Write([]byte("abcde"))
assert.Error(err).IsNil()
assert.Int(nBytes).Equals(4)
assert.String(buffer.String()).Equals("abcdabcd")
}
8 years ago
func BenchmarkNewBuffer8192(b *testing.B) {
for i := 0; i < b.N; i++ {
8 years ago
buffer := New()
8 years ago
buffer.Release()
}
}
func BenchmarkNewLocalBuffer8192(b *testing.B) {
for i := 0; i < b.N; i++ {
8 years ago
buffer := NewLocal(8192)
8 years ago
buffer.Release()
}
}
func BenchmarkNewBuffer2048(b *testing.B) {
for i := 0; i < b.N; i++ {
8 years ago
buffer := NewSmall()
8 years ago
buffer.Release()
}
}
func BenchmarkNewLocalBuffer2048(b *testing.B) {
for i := 0; i < b.N; i++ {
8 years ago
buffer := NewLocal(2048)
8 years ago
buffer.Release()
}
}
func BenchmarkBufferValue(b *testing.B) {
x := Buffer{}
doSomething := func(a Buffer) {
_ = a.Len()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
doSomething(x)
}
}
func BenchmarkBufferPointer(b *testing.B) {
8 years ago
x := NewSmall()
doSomething := func(a *Buffer) {
_ = a.Len()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
doSomething(x)
}
}