mirror of
https://github.com/datarhei/core.git
synced 2025-10-05 16:07:07 +08:00
48 lines
828 B
Go
48 lines
828 B
Go
package mem
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/datarhei/core/v16/math/rand"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestBufferReadChunks(t *testing.T) {
|
|
data := []byte(rand.StringAlphanumeric(1024 * 1024))
|
|
|
|
r := bytes.NewReader(data)
|
|
buf := &Buffer{}
|
|
|
|
buf.ReadFrom(r)
|
|
|
|
res := bytes.Compare(data, buf.Bytes())
|
|
require.Equal(t, 0, res)
|
|
}
|
|
|
|
func BenchmarkBufferReadFrom(b *testing.B) {
|
|
data := []byte(rand.StringAlphanumeric(1024 * 1024))
|
|
|
|
r := bytes.NewReader(data)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
r.Seek(0, io.SeekStart)
|
|
buf := &Buffer{}
|
|
buf.ReadFrom(r)
|
|
}
|
|
}
|
|
|
|
func BenchmarkBytesBufferReadFrom(b *testing.B) {
|
|
data := []byte(rand.StringAlphanumeric(1024 * 1024))
|
|
|
|
r := bytes.NewReader(data)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
r.Seek(0, io.SeekStart)
|
|
buf := &bytes.Buffer{}
|
|
buf.ReadFrom(r)
|
|
}
|
|
}
|