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

84 lines
1.5 KiB

package buf_test
import (
"crypto/rand"
"testing"
. "v2ray.com/core/common/buf"
"v2ray.com/core/common/serial"
. "v2ray.com/ext/assert"
)
func TestBufferClear(t *testing.T) {
assert := With(t)
8 years ago
buffer := New()
defer buffer.Release()
payload := "Bytes"
buffer.Append([]byte(payload))
assert(buffer.Len(), Equals, len(payload))
buffer.Clear()
assert(buffer.Len(), Equals, 0)
}
func TestBufferIsEmpty(t *testing.T) {
assert := With(t)
8 years ago
buffer := New()
defer buffer.Release()
assert(buffer.IsEmpty(), IsTrue)
}
9 years ago
func TestBufferString(t *testing.T) {
assert := With(t)
8 years ago
buffer := New()
defer buffer.Release()
assert(buffer.AppendSupplier(serial.WriteString("Test String")), IsNil)
assert(buffer.String(), Equals, "Test String")
}
8 years ago
func TestBufferWrite(t *testing.T) {
assert := With(t)
8 years ago
buffer := NewLocal(8)
nBytes, err := buffer.Write([]byte("abcd"))
assert(err, IsNil)
assert(nBytes, Equals, 4)
nBytes, err = buffer.Write([]byte("abcde"))
assert(err, IsNil)
assert(nBytes, Equals, 4)
assert(buffer.String(), Equals, "abcdabcd")
}
func TestSyncPool(t *testing.T) {
assert := With(t)
p := NewSyncPool(32)
b := p.Allocate()
assert(b.Len(), Equals, 0)
assert(b.AppendSupplier(ReadFrom(rand.Reader)), IsNil)
assert(b.Len(), Equals, 32)
b.Release()
}
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()
}
}
func BenchmarkNewLocalBuffer(b *testing.B) {
8 years ago
for i := 0; i < b.N; i++ {
buffer := NewLocal(Size)
8 years ago
buffer.Release()
}
}