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

96 lines
2.2 KiB

package buf_test
import (
"bufio"
"bytes"
"crypto/rand"
"io"
7 years ago
"testing"
7 years ago
"v2ray.com/core/common"
. "v2ray.com/core/common/buf"
7 years ago
"v2ray.com/core/transport/pipe"
7 years ago
. "v2ray.com/ext/assert"
)
func TestWriter(t *testing.T) {
assert := With(t)
lb := New()
common.Must2(lb.ReadFrom(rand.Reader))
expectedBytes := append([]byte(nil), lb.Bytes()...)
writeBuffer := bytes.NewBuffer(make([]byte, 0, 1024*1024))
writer := NewBufferedWriter(NewWriter(writeBuffer))
writer.SetBuffered(false)
err := writer.WriteMultiBuffer(MultiBuffer{lb})
assert(err, IsNil)
assert(writer.Flush(), IsNil)
assert(expectedBytes, Equals, writeBuffer.Bytes())
}
func TestBytesWriterReadFrom(t *testing.T) {
assert := With(t)
const size = 50000
pReader, pWriter := pipe.New(pipe.WithSizeLimit(size))
reader := bufio.NewReader(io.LimitReader(rand.Reader, size))
7 years ago
writer := NewBufferedWriter(pWriter)
writer.SetBuffered(false)
nBytes, err := reader.WriteTo(writer)
assert(nBytes, Equals, int64(size))
if err != nil {
t.Fatal("expect success, but actually error: ", err.Error())
}
7 years ago
mb, err := pReader.ReadMultiBuffer()
assert(err, IsNil)
assert(mb.Len(), Equals, int32(size))
}
7 years ago
func TestDiscardBytes(t *testing.T) {
assert := With(t)
b := New()
common.Must2(b.ReadFullFrom(rand.Reader, Size))
7 years ago
nBytes, err := io.Copy(DiscardBytes, b)
assert(nBytes, Equals, int64(Size))
assert(err, IsNil)
}
func TestDiscardBytesMultiBuffer(t *testing.T) {
assert := With(t)
const size = 10240*1024 + 1
buffer := bytes.NewBuffer(make([]byte, 0, size))
common.Must2(buffer.ReadFrom(io.LimitReader(rand.Reader, size)))
r := NewReader(buffer)
nBytes, err := io.Copy(DiscardBytes, &BufferedReader{Reader: r})
7 years ago
assert(nBytes, Equals, int64(size))
assert(err, IsNil)
}
func TestWriterInterface(t *testing.T) {
{
var writer interface{} = (*BufferToBytesWriter)(nil)
switch writer.(type) {
case Writer, io.Writer, io.ReaderFrom:
default:
t.Error("BufferToBytesWriter is not Writer, io.Writer or io.ReaderFrom")
}
}
{
var writer interface{} = (*BufferedWriter)(nil)
switch writer.(type) {
case Writer, io.Writer, io.ReaderFrom, io.ByteWriter:
default:
t.Error("BufferedWriter is not Writer, io.Writer, io.ReaderFrom or io.ByteWriter")
}
}
}