2016-12-09 12:17:34 +00:00
|
|
|
package buf_test
|
2016-05-11 17:54:20 +00:00
|
|
|
|
|
|
|
import (
|
2017-04-18 10:32:24 +00:00
|
|
|
"io"
|
2018-10-24 11:51:04 +00:00
|
|
|
"strings"
|
2016-05-11 17:54:20 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-10-24 11:51:04 +00:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 12:17:34 +00:00
|
|
|
. "v2ray.com/core/common/buf"
|
2018-04-16 22:40:27 +00:00
|
|
|
"v2ray.com/core/transport/pipe"
|
2017-10-24 14:15:35 +00:00
|
|
|
. "v2ray.com/ext/assert"
|
2016-05-11 17:54:20 +00:00
|
|
|
)
|
|
|
|
|
2017-04-18 10:32:24 +00:00
|
|
|
func TestBytesReaderWriteTo(t *testing.T) {
|
2017-10-24 14:15:35 +00:00
|
|
|
assert := With(t)
|
2017-04-18 10:32:24 +00:00
|
|
|
|
2018-05-25 10:50:33 +00:00
|
|
|
pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024))
|
2018-04-20 22:54:53 +00:00
|
|
|
reader := &BufferedReader{Reader: pReader}
|
2017-04-18 10:32:24 +00:00
|
|
|
b1 := New()
|
2018-11-14 21:55:20 +00:00
|
|
|
b1.WriteString("abc")
|
2017-04-18 10:32:24 +00:00
|
|
|
b2 := New()
|
2018-11-14 21:55:20 +00:00
|
|
|
b2.WriteString("efg")
|
2018-11-16 10:08:12 +00:00
|
|
|
assert(pWriter.WriteMultiBuffer(MultiBuffer{b1, b2}), IsNil)
|
2018-04-16 22:40:27 +00:00
|
|
|
pWriter.Close()
|
2017-04-18 10:32:24 +00:00
|
|
|
|
2018-05-25 10:50:33 +00:00
|
|
|
pReader2, pWriter2 := pipe.New(pipe.WithSizeLimit(1024))
|
2018-04-16 22:40:27 +00:00
|
|
|
writer := NewBufferedWriter(pWriter2)
|
2017-11-09 21:33:15 +00:00
|
|
|
writer.SetBuffered(false)
|
2017-04-18 10:32:24 +00:00
|
|
|
|
|
|
|
nBytes, err := io.Copy(writer, reader)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(nBytes, Equals, int64(6))
|
2017-04-18 10:32:24 +00:00
|
|
|
|
2018-04-16 22:40:27 +00:00
|
|
|
mb, err := pReader2.ReadMultiBuffer()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(len(mb), Equals, 2)
|
|
|
|
assert(mb[0].String(), Equals, "abc")
|
|
|
|
assert(mb[1].String(), Equals, "efg")
|
2017-04-18 10:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBytesReaderMultiBuffer(t *testing.T) {
|
2017-10-24 14:15:35 +00:00
|
|
|
assert := With(t)
|
2017-04-18 10:32:24 +00:00
|
|
|
|
2018-05-25 10:50:33 +00:00
|
|
|
pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024))
|
2018-04-20 22:54:53 +00:00
|
|
|
reader := &BufferedReader{Reader: pReader}
|
2017-04-18 10:32:24 +00:00
|
|
|
b1 := New()
|
2018-11-14 21:55:20 +00:00
|
|
|
b1.WriteString("abc")
|
2017-04-18 10:32:24 +00:00
|
|
|
b2 := New()
|
2018-11-14 21:55:20 +00:00
|
|
|
b2.WriteString("efg")
|
2018-11-16 10:08:12 +00:00
|
|
|
assert(pWriter.WriteMultiBuffer(MultiBuffer{b1, b2}), IsNil)
|
2018-04-16 22:40:27 +00:00
|
|
|
pWriter.Close()
|
2017-04-18 10:32:24 +00:00
|
|
|
|
|
|
|
mbReader := NewReader(reader)
|
2017-11-09 21:33:15 +00:00
|
|
|
mb, err := mbReader.ReadMultiBuffer()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(len(mb), Equals, 2)
|
|
|
|
assert(mb[0].String(), Equals, "abc")
|
|
|
|
assert(mb[1].String(), Equals, "efg")
|
2017-04-18 10:32:24 +00:00
|
|
|
}
|
2017-12-03 22:11:29 +00:00
|
|
|
|
2018-10-24 11:51:04 +00:00
|
|
|
func TestReadByte(t *testing.T) {
|
|
|
|
sr := strings.NewReader("abcd")
|
|
|
|
reader := &BufferedReader{
|
|
|
|
Reader: NewReader(sr),
|
|
|
|
}
|
|
|
|
b, err := reader.ReadByte()
|
|
|
|
common.Must(err)
|
|
|
|
if b != 'a' {
|
|
|
|
t.Error("unexpected byte: ", b, " want a")
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:36:36 +00:00
|
|
|
nBytes, err := reader.WriteTo(DiscardBytes)
|
2018-10-24 11:51:04 +00:00
|
|
|
common.Must(err)
|
|
|
|
if nBytes != 3 {
|
|
|
|
t.Error("unexpect bytes written: ", nBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 22:11:29 +00:00
|
|
|
func TestReaderInterface(t *testing.T) {
|
2018-10-24 11:09:07 +00:00
|
|
|
_ = (io.Reader)(new(ReadVReader))
|
|
|
|
_ = (Reader)(new(ReadVReader))
|
2017-12-03 22:11:29 +00:00
|
|
|
|
2018-10-24 11:09:07 +00:00
|
|
|
_ = (Reader)(new(BufferedReader))
|
|
|
|
_ = (io.Reader)(new(BufferedReader))
|
|
|
|
_ = (io.ByteReader)(new(BufferedReader))
|
|
|
|
_ = (io.WriterTo)(new(BufferedReader))
|
2017-12-03 22:11:29 +00:00
|
|
|
}
|