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

84 lines
1.5 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 (
2017-04-21 08:32:29 -04:00
"crypto/rand"
2015-10-10 16:15:10 -04:00
"testing"
2016-12-09 05:35:27 -05:00
. "v2ray.com/core/common/buf"
2016-12-06 05:03:42 -05:00
"v2ray.com/core/common/serial"
2017-10-24 10:15:35 -04:00
. "v2ray.com/ext/assert"
2015-10-10 16:15:10 -04:00
)
func TestBufferClear(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2015-10-10 16:15:10 -04:00
2016-12-09 06:08:25 -05:00
buffer := New()
2015-10-10 16:15:10 -04:00
defer buffer.Release()
payload := "Bytes"
buffer.Append([]byte(payload))
2017-10-24 10:15:35 -04:00
assert(buffer.Len(), Equals, len(payload))
2015-10-10 16:15:10 -04:00
buffer.Clear()
2017-10-24 10:15:35 -04:00
assert(buffer.Len(), Equals, 0)
2015-10-10 16:15:10 -04:00
}
2016-12-06 05:03:42 -05:00
func TestBufferIsEmpty(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2015-10-10 16:15:10 -04:00
2016-12-09 06:08:25 -05:00
buffer := New()
2015-10-10 16:15:10 -04:00
defer buffer.Release()
2017-10-24 10:15:35 -04:00
assert(buffer.IsEmpty(), IsTrue)
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) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-04-29 17:47:42 -04:00
2016-12-09 06:08:25 -05:00
buffer := New()
2016-04-29 17:47:42 -04:00
defer buffer.Release()
2017-10-24 10:15:35 -04:00
assert(buffer.AppendSupplier(serial.WriteString("Test String")), IsNil)
assert(buffer.String(), Equals, "Test String")
2016-04-29 17:47:42 -04:00
}
2016-11-21 16:08:34 -05:00
2016-12-02 08:35:28 -05:00
func TestBufferWrite(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-12-02 08:35:28 -05:00
2016-12-09 06:08:25 -05:00
buffer := NewLocal(8)
2016-12-02 08:35:28 -05:00
nBytes, err := buffer.Write([]byte("abcd"))
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(nBytes, Equals, 4)
2016-12-02 08:35:28 -05:00
nBytes, err = buffer.Write([]byte("abcde"))
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(nBytes, Equals, 4)
assert(buffer.String(), Equals, "abcdabcd")
2016-12-02 08:35:28 -05:00
}
2017-04-21 08:32:29 -04:00
func TestSyncPool(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-04-21 08:32:29 -04:00
p := NewSyncPool(32)
b := p.Allocate()
2017-10-24 10:15:35 -04:00
assert(b.Len(), Equals, 0)
2017-04-21 08:32:29 -04:00
2017-10-24 10:15:35 -04:00
assert(b.AppendSupplier(ReadFrom(rand.Reader)), IsNil)
assert(b.Len(), Equals, 32)
2017-04-21 08:32:29 -04:00
b.Release()
}
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()
}
}
2017-04-21 08:32:29 -04:00
func BenchmarkNewLocalBuffer(b *testing.B) {
2016-11-21 16:08:34 -05:00
for i := 0; i < b.N; i++ {
2017-04-21 08:32:29 -04:00
buffer := NewLocal(Size)
2016-11-21 16:08:34 -05:00
buffer.Release()
}
}