You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/common/buf/multi_buffer_test.go

97 lines
1.8 KiB

8 years ago
package buf_test
import (
7 years ago
"crypto/rand"
7 years ago
"io"
8 years ago
"testing"
6 years ago
"github.com/google/go-cmp/cmp"
7 years ago
"v2ray.com/core/common"
8 years ago
. "v2ray.com/core/common/buf"
. "v2ray.com/ext/assert"
8 years ago
)
func TestMultiBufferRead(t *testing.T) {
assert := With(t)
8 years ago
b1 := New()
b1.WriteString("ab")
8 years ago
b2 := New()
b2.WriteString("cd")
mb := MultiBuffer{b1, b2}
8 years ago
bs := make([]byte, 32)
_, nBytes := SplitBytes(mb, bs)
assert(nBytes, Equals, 4)
assert(bs[:nBytes], Equals, []byte("abcd"))
8 years ago
}
func TestMultiBufferAppend(t *testing.T) {
assert := With(t)
var mb MultiBuffer
b := New()
b.WriteString("ab")
mb = append(mb, b)
assert(mb.Len(), Equals, int32(2))
}
7 years ago
func TestMultiBufferSliceBySizeLarge(t *testing.T) {
lb := make([]byte, 8*1024)
common.Must2(io.ReadFull(rand.Reader, lb))
7 years ago
mb := MergeBytes(nil, lb)
7 years ago
mb, mb2 := SplitSize(mb, 1024)
6 years ago
if mb2.Len() != 1024 {
t.Error("expect length 1024, but got ", mb2.Len())
}
if mb.Len() != 7*1024 {
t.Error("expect length 7*1024, but got ", mb.Len())
}
mb, mb3 := SplitSize(mb, 7*1024)
if mb3.Len() != 7*1024 {
t.Error("expect length 7*1024, but got", mb.Len())
}
if !mb.IsEmpty() {
t.Error("expect empty buffer, but got ", mb.Len())
}
}
func TestMultiBufferSplitFirst(t *testing.T) {
b1 := New()
b1.WriteString("b1")
b2 := New()
b2.WriteString("b2")
b3 := New()
b3.WriteString("b3")
var mb MultiBuffer
mb = append(mb, b1, b2, b3)
mb, c1 := SplitFirst(mb)
if diff := cmp.Diff(b1.String(), c1.String()); diff != "" {
t.Error(diff)
}
mb, c2 := SplitFirst(mb)
if diff := cmp.Diff(b2.String(), c2.String()); diff != "" {
t.Error(diff)
}
mb, c3 := SplitFirst(mb)
if diff := cmp.Diff(b3.String(), c3.String()); diff != "" {
t.Error(diff)
}
if !mb.IsEmpty() {
t.Error("expect empty buffer, but got ", mb.String())
}
7 years ago
}