1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

more test cases

This commit is contained in:
Darien Raymond 2018-10-24 13:16:08 +02:00
parent c8e11595f2
commit 2621305413
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 35 additions and 3 deletions

View File

@ -65,11 +65,11 @@ func (b *Buffer) Bytes() []byte {
// Reset resets the content of the Buffer with a supplier.
func (b *Buffer) Reset(writer Supplier) error {
nBytes, err := writer(b.v)
if nBytes > len(b.v) {
return newError("too many bytes written: ", nBytes, " > ", len(b.v))
}
b.start = 0
b.end = int32(nBytes)
if b.end > int32(len(b.v)) {
b.end = int32(len(b.v))
}
return err
}

View File

@ -3,6 +3,9 @@ package buf_test
import (
"testing"
"v2ray.com/core/common"
"v2ray.com/core/common/compare"
. "v2ray.com/core/common/buf"
"v2ray.com/core/common/serial"
. "v2ray.com/ext/assert"
@ -41,6 +44,35 @@ func TestBufferString(t *testing.T) {
assert(buffer.String(), Equals, "Test String")
}
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)
}
}
}
func BenchmarkNewBuffer(b *testing.B) {
for i := 0; i < b.N; i++ {
buffer := New()