v2ray-core/common/buf/buffer_test.go

82 lines
1.5 KiB
Go
Raw Normal View History

2016-12-09 11:35:27 +01:00
package buf_test
2015-10-10 22:15:10 +02:00
import (
"testing"
2018-10-24 13:16:08 +02:00
"v2ray.com/core/common"
"v2ray.com/core/common/compare"
2016-12-09 11:35:27 +01:00
. "v2ray.com/core/common/buf"
2016-12-06 11:03:42 +01:00
"v2ray.com/core/common/serial"
2017-10-24 16:15:35 +02:00
. "v2ray.com/ext/assert"
2015-10-10 22:15:10 +02:00
)
func TestBufferClear(t *testing.T) {
2017-10-24 16:15:35 +02:00
assert := With(t)
2015-10-10 22:15:10 +02:00
2016-12-09 12:08:25 +01:00
buffer := New()
2015-10-10 22:15:10 +02:00
defer buffer.Release()
payload := "Bytes"
2018-04-19 22:56:55 +02:00
buffer.Write([]byte(payload))
2018-04-02 20:00:50 +02:00
assert(buffer.Len(), Equals, int32(len(payload)))
2015-10-10 22:15:10 +02:00
buffer.Clear()
2018-04-02 20:00:50 +02:00
assert(buffer.Len(), Equals, int32(0))
2015-10-10 22:15:10 +02:00
}
2016-12-06 11:03:42 +01:00
func TestBufferIsEmpty(t *testing.T) {
2017-10-24 16:15:35 +02:00
assert := With(t)
2015-10-10 22:15:10 +02:00
2016-12-09 12:08:25 +01:00
buffer := New()
2015-10-10 22:15:10 +02:00
defer buffer.Release()
2017-10-24 16:15:35 +02:00
assert(buffer.IsEmpty(), IsTrue)
2015-10-10 22:15:10 +02:00
}
2016-01-31 21:24:40 +01:00
2016-04-29 23:47:42 +02:00
func TestBufferString(t *testing.T) {
2017-10-24 16:15:35 +02:00
assert := With(t)
2016-04-29 23:47:42 +02:00
2016-12-09 12:08:25 +01:00
buffer := New()
2016-04-29 23:47:42 +02:00
defer buffer.Release()
2017-10-24 16:15:35 +02:00
assert(buffer.AppendSupplier(serial.WriteString("Test String")), IsNil)
assert(buffer.String(), Equals, "Test String")
2016-04-29 23:47:42 +02:00
}
2016-11-21 22:08:34 +01:00
2018-10-24 13:16:08 +02:00
func TestBufferSlice(t *testing.T) {
{
b := New()
common.Must2(b.Write([]byte("abcd")))
bytes := b.BytesFrom(-2)
if err := compare.BytesEqualWithDetail(bytes, []byte{'c', 'd'}); err != nil {
t.Error(err)
}
}
{
b := New()
common.Must2(b.Write([]byte("abcd")))
bytes := b.BytesTo(-2)
if err := compare.BytesEqualWithDetail(bytes, []byte{'a', 'b'}); err != nil {
t.Error(err)
}
}
{
b := New()
common.Must2(b.Write([]byte("abcd")))
bytes := b.BytesRange(-3, -1)
if err := compare.BytesEqualWithDetail(bytes, []byte{'b', 'c'}); err != nil {
t.Error(err)
}
}
}
2017-04-21 14:32:29 +02:00
func BenchmarkNewBuffer(b *testing.B) {
2016-11-21 22:08:34 +01:00
for i := 0; i < b.N; i++ {
2016-12-09 12:08:25 +01:00
buffer := New()
2016-11-21 22:08:34 +01:00
buffer.Release()
}
}