2017-04-15 15:07:23 -04:00
|
|
|
package buf_test
|
|
|
|
|
|
|
|
import (
|
2018-03-28 08:33:34 -04:00
|
|
|
"crypto/rand"
|
2018-04-13 07:54:36 -04:00
|
|
|
"io"
|
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()
|
2018-07-30 16:45:06 -04:00
|
|
|
b1.WriteBytes('a', 'b')
|
2017-04-15 15:07:23 -04:00
|
|
|
|
|
|
|
b2 := New()
|
2018-07-30 16:45:06 -04:00
|
|
|
b2.WriteBytes('c', 'd')
|
2017-04-15 15:07:23 -04:00
|
|
|
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
|
|
|
}
|
2017-04-20 09:02:35 -04:00
|
|
|
|
|
|
|
func TestMultiBufferAppend(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2017-04-20 09:02:35 -04:00
|
|
|
|
|
|
|
var mb MultiBuffer
|
|
|
|
b := New()
|
2018-07-30 16:45:06 -04:00
|
|
|
b.WriteBytes('a', 'b')
|
2017-04-20 09:02:35 -04:00
|
|
|
mb.Append(b)
|
2018-04-02 14:00:50 -04:00
|
|
|
assert(mb.Len(), Equals, int32(2))
|
2017-04-20 09:02:35 -04:00
|
|
|
}
|
2018-03-28 08:33:34 -04:00
|
|
|
|
|
|
|
func TestMultiBufferSliceBySizeLarge(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
2018-08-16 06:05:33 -04:00
|
|
|
lb := make([]byte, 8*1024)
|
|
|
|
common.Must2(io.ReadFull(rand.Reader, lb))
|
2018-03-28 08:33:34 -04:00
|
|
|
|
|
|
|
var mb MultiBuffer
|
2018-08-16 06:05:33 -04:00
|
|
|
common.Must2(mb.Write(lb))
|
2018-03-28 08:33:34 -04:00
|
|
|
|
2018-08-16 06:05:33 -04:00
|
|
|
mb2 := mb.SliceBySize(1024)
|
|
|
|
assert(mb2.Len(), Equals, int32(1024))
|
2018-03-28 08:33:34 -04:00
|
|
|
}
|
2018-04-13 07:54:36 -04:00
|
|
|
|
|
|
|
func TestInterface(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
|
|
|
assert((*MultiBuffer)(nil), Implements, (*io.WriterTo)(nil))
|
|
|
|
assert((*MultiBuffer)(nil), Implements, (*io.ReaderFrom)(nil))
|
|
|
|
}
|