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/reader_test.go

70 lines
1.8 KiB

package buf_test
import (
8 years ago
"io"
"testing"
. "v2ray.com/core/common/buf"
7 years ago
"v2ray.com/core/transport/pipe"
. "v2ray.com/ext/assert"
)
8 years ago
func TestBytesReaderWriteTo(t *testing.T) {
assert := With(t)
8 years ago
pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024))
reader := &BufferedReader{Reader: pReader}
8 years ago
b1 := New()
b1.WriteBytes('a', 'b', 'c')
8 years ago
b2 := New()
b2.WriteBytes('e', 'f', 'g')
7 years ago
assert(pWriter.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil)
pWriter.Close()
8 years ago
pReader2, pWriter2 := pipe.New(pipe.WithSizeLimit(1024))
7 years ago
writer := NewBufferedWriter(pWriter2)
writer.SetBuffered(false)
8 years ago
nBytes, err := io.Copy(writer, reader)
assert(err, IsNil)
assert(nBytes, Equals, int64(6))
8 years ago
7 years ago
mb, err := pReader2.ReadMultiBuffer()
assert(err, IsNil)
assert(len(mb), Equals, 2)
assert(mb[0].String(), Equals, "abc")
assert(mb[1].String(), Equals, "efg")
8 years ago
}
func TestBytesReaderMultiBuffer(t *testing.T) {
assert := With(t)
8 years ago
pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024))
reader := &BufferedReader{Reader: pReader}
8 years ago
b1 := New()
b1.WriteBytes('a', 'b', 'c')
8 years ago
b2 := New()
b2.WriteBytes('e', 'f', 'g')
7 years ago
assert(pWriter.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil)
pWriter.Close()
8 years ago
mbReader := NewReader(reader)
mb, err := mbReader.ReadMultiBuffer()
assert(err, IsNil)
assert(len(mb), Equals, 2)
assert(mb[0].String(), Equals, "abc")
assert(mb[1].String(), Equals, "efg")
8 years ago
}
func TestReaderInterface(t *testing.T) {
assert := With(t)
assert((*ReadVReader)(nil), Implements, (*io.Reader)(nil))
assert((*ReadVReader)(nil), Implements, (*Reader)(nil))
assert((*BufferedReader)(nil), Implements, (*Reader)(nil))
assert((*BufferedReader)(nil), Implements, (*io.Reader)(nil))
assert((*BufferedReader)(nil), Implements, (*io.ByteReader)(nil))
assert((*BufferedReader)(nil), Implements, (*io.WriterTo)(nil))
}