2016-12-09 10:35:27 +00:00
|
|
|
package buf_test
|
2015-10-10 20:15:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-12-09 10:35:27 +00:00
|
|
|
. "v2ray.com/core/common/buf"
|
2016-12-06 10:03:42 +00:00
|
|
|
"v2ray.com/core/common/serial"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/testing/assert"
|
2015-10-10 20:15:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBufferClear(t *testing.T) {
|
2016-05-24 19:55:46 +00:00
|
|
|
assert := assert.On(t)
|
2015-10-10 20:15:10 +00:00
|
|
|
|
2016-12-09 11:08:25 +00:00
|
|
|
buffer := New()
|
2015-10-10 20:15:10 +00:00
|
|
|
defer buffer.Release()
|
|
|
|
|
|
|
|
payload := "Bytes"
|
|
|
|
buffer.Append([]byte(payload))
|
|
|
|
assert.Int(buffer.Len()).Equals(len(payload))
|
|
|
|
|
|
|
|
buffer.Clear()
|
|
|
|
assert.Int(buffer.Len()).Equals(0)
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:03:42 +00:00
|
|
|
func TestBufferIsEmpty(t *testing.T) {
|
2016-05-24 19:55:46 +00:00
|
|
|
assert := assert.On(t)
|
2015-10-10 20:15:10 +00:00
|
|
|
|
2016-12-09 11:08:25 +00:00
|
|
|
buffer := New()
|
2015-10-10 20:15:10 +00:00
|
|
|
defer buffer.Release()
|
|
|
|
|
2016-12-06 10:03:42 +00:00
|
|
|
assert.Bool(buffer.IsEmpty()).IsTrue()
|
2015-10-10 20:15:10 +00:00
|
|
|
}
|
2016-01-31 20:24:40 +00:00
|
|
|
|
2016-04-29 21:47:42 +00:00
|
|
|
func TestBufferString(t *testing.T) {
|
2016-05-24 19:55:46 +00:00
|
|
|
assert := assert.On(t)
|
2016-04-29 21:47:42 +00:00
|
|
|
|
2016-12-09 11:08:25 +00:00
|
|
|
buffer := New()
|
2016-04-29 21:47:42 +00:00
|
|
|
defer buffer.Release()
|
|
|
|
|
2016-12-21 14:37:16 +00:00
|
|
|
assert.Error(buffer.AppendSupplier(serial.WriteString("Test String"))).IsNil()
|
2016-05-24 19:55:46 +00:00
|
|
|
assert.String(buffer.String()).Equals("Test String")
|
2016-04-29 21:47:42 +00:00
|
|
|
}
|
2016-11-21 21:08:34 +00:00
|
|
|
|
2016-12-02 13:35:28 +00:00
|
|
|
func TestBufferWrite(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
2016-12-09 11:08:25 +00:00
|
|
|
buffer := NewLocal(8)
|
2016-12-02 13:35:28 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:08:34 +00:00
|
|
|
func BenchmarkNewBuffer8192(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-12-09 11:08:25 +00:00
|
|
|
buffer := New()
|
2016-11-21 21:08:34 +00:00
|
|
|
buffer.Release()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkNewLocalBuffer8192(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-12-09 11:08:25 +00:00
|
|
|
buffer := NewLocal(8192)
|
2016-11-21 21:08:34 +00:00
|
|
|
buffer.Release()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkNewBuffer2048(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-12-09 11:08:25 +00:00
|
|
|
buffer := NewSmall()
|
2016-11-21 21:08:34 +00:00
|
|
|
buffer.Release()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkNewLocalBuffer2048(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-12-09 11:08:25 +00:00
|
|
|
buffer := NewLocal(2048)
|
2016-11-21 21:08:34 +00:00
|
|
|
buffer.Release()
|
|
|
|
}
|
|
|
|
}
|
2016-12-08 16:39:15 +00:00
|
|
|
|
|
|
|
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) {
|
2016-12-09 11:08:25 +00:00
|
|
|
x := NewSmall()
|
2016-12-08 16:39:15 +00:00
|
|
|
doSomething := func(a *Buffer) {
|
|
|
|
_ = a.Len()
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
doSomething(x)
|
|
|
|
}
|
|
|
|
}
|