1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-03 07:56:42 -05:00
v2fly/common/buf/multi_buffer_test.go

51 lines
879 B
Go
Raw Normal View History

2017-04-15 15:07:23 -04:00
package buf_test
import (
2018-03-28 08:33:34 -04:00
"crypto/rand"
2017-04-15 15:07:23 -04:00
"testing"
2018-03-28 08:33:34 -04:00
"v2ray.com/core/common"
2017-04-15 15:07:23 -04:00
. "v2ray.com/core/common/buf"
2017-10-24 10:15:35 -04:00
. "v2ray.com/ext/assert"
2017-04-15 15:07:23 -04:00
)
func TestMultiBufferRead(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-04-15 15:07:23 -04:00
b1 := New()
b1.AppendBytes('a', 'b')
b2 := New()
b2.AppendBytes('c', 'd')
mb := NewMultiBufferValue(b1, b2)
bs := make([]byte, 32)
nBytes, err := mb.Read(bs)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(nBytes, Equals, 4)
assert(bs[:nBytes], Equals, []byte("abcd"))
2017-04-15 15:07:23 -04:00
}
func TestMultiBufferAppend(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
var mb MultiBuffer
b := New()
b.AppendBytes('a', 'b')
mb.Append(b)
2017-10-24 10:15:35 -04:00
assert(mb.Len(), Equals, 2)
}
2018-03-28 08:33:34 -04:00
func TestMultiBufferSliceBySizeLarge(t *testing.T) {
assert := With(t)
lb := NewSize(8 * 1024)
common.Must(lb.Reset(ReadFrom(rand.Reader)))
var mb MultiBuffer
mb.Append(lb)
mb2 := mb.SliceBySize(4 * 1024)
assert(mb2.Len(), Equals, 4*1024)
}