1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/common/buf/buffer_test.go

226 lines
4.4 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"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v5/common"
. "github.com/v2fly/v2ray-core/v5/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 length, but got ", buffer.Len())
2018-11-15 04:30:03 -05:00
}
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
2019-11-22 03:26:59 -05:00
func TestBufferByte(t *testing.T) {
{
buffer := New()
common.Must(buffer.WriteByte('m'))
if buffer.String() != "m" {
t.Error("expect buffer content as ", "m", " but actually ", buffer.String())
}
buffer.Release()
}
{
buffer := StackNew()
common.Must(buffer.WriteByte('n'))
if buffer.String() != "n" {
t.Error("expect buffer content as ", "n", " but actually ", buffer.String())
}
buffer.Release()
}
{
buffer := StackNew()
common.Must2(buffer.WriteString("HELLOWORLD"))
if b := buffer.Byte(5); b != 'W' {
t.Error("unexpected byte ", b)
}
buffer.SetByte(5, 'M')
if buffer.String() != "HELLOMORLD" {
t.Error("expect buffer content as ", "n", " but actually ", buffer.String())
}
buffer.Release()
}
}
2021-05-19 17:28:52 -04:00
2019-11-22 03:26:59 -05:00
func TestBufferResize(t *testing.T) {
buffer := New()
defer buffer.Release()
const payload = "Test String"
common.Must2(buffer.WriteString(payload))
if buffer.String() != payload {
t.Error("expect buffer content as ", payload, " but actually ", buffer.String())
}
buffer.Resize(-6, -3)
if l := buffer.Len(); int(l) != 3 {
t.Error("len error ", l)
}
if s := buffer.String(); s != "Str" {
t.Error("unexpect buffer ", s)
}
buffer.Resize(int32(len(payload)), 200)
if l := buffer.Len(); int(l) != 200-len(payload) {
t.Error("len error ", l)
}
}
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()
}
}