1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 02:26:49 -05:00
v2fly/common/buf/buffer_test.go

168 lines
3.1 KiB
Go
Raw Normal View History

2016-12-09 05:35:27 -05:00
package buf_test
2015-10-10 16:15:10 -04:00
import (
2018-11-02 10:01:33 -04:00
"bytes"
"crypto/rand"
2015-10-10 16:15:10 -04:00
"testing"
2018-11-15 04:30:03 -05:00
"github.com/google/go-cmp/cmp"
2018-10-24 07:16:08 -04:00
"v2ray.com/core/common"
2016-12-09 05:35:27 -05:00
. "v2ray.com/core/common/buf"
2015-10-10 16:15:10 -04:00
)
func TestBufferClear(t *testing.T) {
2016-12-09 06:08:25 -05:00
buffer := New()
2015-10-10 16:15:10 -04:00
defer buffer.Release()
payload := "Bytes"
2018-04-19 16:56:55 -04:00
buffer.Write([]byte(payload))
2018-11-15 10:32:38 -05:00
if diff := cmp.Diff(buffer.Bytes(), []byte(payload)); diff != "" {
2018-11-15 04:30:03 -05:00
t.Error(diff)
}
2015-10-10 16:15:10 -04:00
buffer.Clear()
2018-11-15 04:30:03 -05:00
if buffer.Len() != 0 {
t.Error("expect 0 lenght, but got ", buffer.Len())
}
2015-10-10 16:15:10 -04:00
}
2016-12-06 05:03:42 -05:00
func TestBufferIsEmpty(t *testing.T) {
2016-12-09 06:08:25 -05:00
buffer := New()
2015-10-10 16:15:10 -04:00
defer buffer.Release()
2018-11-15 04:30:03 -05:00
if buffer.IsEmpty() != true {
t.Error("expect empty buffer, but not")
}
2015-10-10 16:15:10 -04:00
}
2016-01-31 15:24:40 -05:00
2016-04-29 17:47:42 -04:00
func TestBufferString(t *testing.T) {
2016-12-09 06:08:25 -05:00
buffer := New()
2016-04-29 17:47:42 -04:00
defer buffer.Release()
2018-11-15 04:30:03 -05:00
const payload = "Test String"
common.Must2(buffer.WriteString(payload))
if buffer.String() != payload {
t.Error("expect buffer content as ", payload, " but actually ", buffer.String())
}
2016-04-29 17:47:42 -04:00
}
2016-11-21 16:08:34 -05:00
2018-10-24 07:16:08 -04:00
func TestBufferSlice(t *testing.T) {
{
b := New()
common.Must2(b.Write([]byte("abcd")))
bytes := b.BytesFrom(-2)
2018-11-15 04:30:03 -05:00
if diff := cmp.Diff(bytes, []byte{'c', 'd'}); diff != "" {
t.Error(diff)
2018-10-24 07:16:08 -04:00
}
}
{
b := New()
common.Must2(b.Write([]byte("abcd")))
bytes := b.BytesTo(-2)
2018-11-15 04:30:03 -05:00
if diff := cmp.Diff(bytes, []byte{'a', 'b'}); diff != "" {
t.Error(diff)
2018-10-24 07:16:08 -04:00
}
}
{
b := New()
common.Must2(b.Write([]byte("abcd")))
bytes := b.BytesRange(-3, -1)
2018-11-15 04:30:03 -05:00
if diff := cmp.Diff(bytes, []byte{'b', 'c'}); diff != "" {
t.Error(diff)
2018-10-24 07:16:08 -04:00
}
}
}
2018-11-02 10:01:33 -04:00
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)
}
2018-11-15 04:30:03 -05:00
if diff := cmp.Diff(payload, b.Bytes()); diff != "" {
t.Error(diff)
2018-11-02 10:01:33 -04:00
}
}
2017-04-21 08:32:29 -04:00
func BenchmarkNewBuffer(b *testing.B) {
2016-11-21 16:08:34 -05:00
for i := 0; i < b.N; i++ {
2016-12-09 06:08:25 -05:00
buffer := New()
2016-11-21 16:08:34 -05:00
buffer.Release()
}
}
2018-11-14 16:55:20 -05:00
2018-11-15 16:06:54 -05:00
func BenchmarkNewBufferStack(b *testing.B) {
for i := 0; i < b.N; i++ {
buffer := StackNew()
buffer.Release()
}
}
2018-11-14 16:55:20 -05:00
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()
}
}