146 lines
4.8 KiB
Go
146 lines
4.8 KiB
Go
package gigaparsec_test
|
|
|
|
import (
|
|
"bytes"
|
|
"cmp"
|
|
"io"
|
|
"testing"
|
|
|
|
"git.codemonkeysoftware.net/b/gigaparsec"
|
|
ptest "git.codemonkeysoftware.net/b/gigaparsec/test"
|
|
"git.codemonkeysoftware.net/b/gigaparsec/test/generator"
|
|
pgen "git.codemonkeysoftware.net/b/gigaparsec/test/generator"
|
|
"github.com/shoenig/test"
|
|
"github.com/shoenig/test/must"
|
|
"pgregory.net/rapid"
|
|
)
|
|
|
|
func TestState(t *testing.T) {
|
|
t.Run("state reads the same position every time", rapid.MakeCheck(func(t *rapid.T) {
|
|
data := rapid.SliceOfN(rapid.Byte(), 1, 100).Draw(t, "data")
|
|
dst := pgen.SliceOfNZero[byte](0, len(data)-1).Draw(t, "dst")
|
|
expected := data[:len(dst)]
|
|
st := gigaparsec.MakeState(bytes.NewReader(data))
|
|
|
|
_, next, err := st.Read(dst)
|
|
must.NoError(t, err)
|
|
must.SliceEqOp(t, expected, dst)
|
|
|
|
next.Read(dst)
|
|
_, _, err = st.Read(dst)
|
|
must.NoError(t, err)
|
|
must.SliceEqOp(t, expected, dst)
|
|
}))
|
|
t.Run("Read returns io.EOF iff it overruns source", rapid.MakeCheck(func(t *rapid.T) {
|
|
data := rapid.SliceOfN(rapid.Byte(), 0, 100).Draw(t, "data")
|
|
dst := pgen.SliceOfNZero[byte](0, 200).Draw(t, "dst")
|
|
st := gigaparsec.MakeState(bytes.NewReader(data))
|
|
|
|
n, _, err := st.Read(dst)
|
|
t.Logf("n=%d", n)
|
|
must.EqOp(t, min(len(data), len(dst)), int(n))
|
|
if len(dst) > len(data) || st.Pos() == uint64(len(data)) {
|
|
must.ErrorIs(t, err, io.EOF)
|
|
} else {
|
|
must.NoError(t, err)
|
|
}
|
|
}))
|
|
t.Run("next state reads next input", rapid.MakeCheck(func(t *rapid.T) {
|
|
const maxLen = 100
|
|
data := rapid.SliceOfN(rapid.Byte(), 1, maxLen).Draw(t, "data")
|
|
skip := rapid.IntRange(0, len(data)-1).Draw(t, "skip")
|
|
st := gigaparsec.MakeState(bytes.NewReader(data))
|
|
|
|
_, next, err := st.Read(make([]byte, skip))
|
|
must.NoError(t, err)
|
|
must.EqOp(t, skip, int(next.Pos()))
|
|
dst := make([]byte, maxLen)
|
|
n, _, _ := next.Read(dst)
|
|
must.SliceEqOp(t, data[skip:skip+int(n)], dst[:n])
|
|
}))
|
|
t.Run("Read returns io.EOF if n is less than requested", rapid.MakeCheck(func(t *rapid.T) {
|
|
data := rapid.SliceOfN(rapid.Byte(), 0, 100).Draw(t, "data")
|
|
st := gigaparsec.MakeState(bytes.NewReader(data))
|
|
|
|
n, _, err := st.Read(make([]byte, len(data)+1))
|
|
test.ErrorIs(t, err, io.EOF)
|
|
test.EqOp(t, len(data), int(n))
|
|
}))
|
|
t.Run("At sets state position", rapid.MakeCheck(func(t *rapid.T) {
|
|
data := rapid.SliceOfN(rapid.Byte(), 1, 100).Draw(t, "data")
|
|
pos := rapid.Uint64Range(0, uint64(len(data)-1)).Draw(t, "pos")
|
|
st := gigaparsec.MakeState(bytes.NewReader(data)).At(pos)
|
|
|
|
dst := make([]byte, 1)
|
|
n, _, err := st.Read(dst)
|
|
test.EqOp(t, 1, n)
|
|
test.NoError(t, err)
|
|
test.EqOp(t, data[pos], dst[0])
|
|
}))
|
|
t.Run("Pos returns correct position after At", rapid.MakeCheck(func(t *rapid.T) {
|
|
var data []byte
|
|
pos := rapid.Uint64().Draw(t, "pos")
|
|
st := gigaparsec.MakeState(bytes.NewReader(data)).At(pos)
|
|
test.EqOp(t, pos, st.Pos())
|
|
}))
|
|
t.Run("Pos returns correct position after Read", rapid.MakeCheck(func(t *rapid.T) {
|
|
const maxLen = 100
|
|
data := rapid.SliceOfN(rapid.Byte(), 1, maxLen).Draw(t, "data")
|
|
skip := rapid.Uint64Range(0, uint64(len(data)-1)).Draw(t, "skip")
|
|
st := gigaparsec.MakeState(bytes.NewReader(data))
|
|
|
|
_, next, err := st.Read(make([]byte, skip))
|
|
must.NoError(t, err)
|
|
test.EqOp(t, skip, next.Pos())
|
|
}))
|
|
t.Run("Read returns an error if the ReaderAt fails", rapid.MakeCheck(func(t *rapid.T) {
|
|
expectedErr := pgen.Error().Draw(t, "expectedErr")
|
|
startPos := rapid.Uint64().Draw(t, "startPos")
|
|
dst := pgen.SliceOfNZero[byte](0, 100).Draw(t, "dst")
|
|
st := gigaparsec.MakeState(ptest.ErrReaderAt(expectedErr)).At(startPos)
|
|
n, next, err := st.Read(dst)
|
|
test.ErrorIs(t, err, expectedErr)
|
|
test.EqOp(t, startPos, next.Pos())
|
|
test.Zero(t, n)
|
|
}))
|
|
}
|
|
|
|
func TestSliceReaderAt(t *testing.T) {
|
|
const maxLen = 100
|
|
t.Run("offset ≥ 0", rapid.MakeCheck(func(t *rapid.T) {
|
|
src := rapid.SliceOfN(rapid.Byte(), 0, maxLen).Draw(t, "src")
|
|
dst := generator.SliceOfNZero[byte](0, maxLen).Draw(t, "dst")
|
|
offset := rapid.Int64Range(0, int64(len(src))+10).Draw(t, "offset")
|
|
|
|
n, err := gigaparsec.SliceReaderAt[byte](src).ReadAt(dst, offset)
|
|
|
|
switch cmp.Compare(len(src), int(offset)+len(dst)) {
|
|
case -1:
|
|
// Read overruns src.
|
|
test.ErrorIs(t, err, io.EOF)
|
|
test.EqOp(t, max(0, len(src)-int(offset)), n)
|
|
case 0:
|
|
// Read exactly reaches end of source.
|
|
// io.ReaderAt spec allows error to be either io.EOF or nil.
|
|
test.EqOp(t, len(dst), n)
|
|
case 1:
|
|
// Read ends before end of source.
|
|
test.NoError(t, err)
|
|
test.EqOp(t, len(dst), n)
|
|
}
|
|
|
|
if offset < int64(len(src)) {
|
|
test.SliceEqOp(t, src[offset:offset+int64(n)], dst[:n])
|
|
}
|
|
}))
|
|
t.Run("offset < 0", rapid.MakeCheck(func(t *rapid.T) {
|
|
src := rapid.SliceOfN(rapid.Byte(), 0, maxLen).Draw(t, "src")
|
|
dst := generator.SliceOfNZero[byte](0, maxLen).Draw(t, "dst")
|
|
offset := rapid.Int64Max(-1).Draw(t, "offset")
|
|
|
|
n, err := gigaparsec.SliceReaderAt[byte](src).ReadAt(dst, offset)
|
|
test.Error(t, err)
|
|
test.EqOp(t, 0, n)
|
|
}))
|
|
}
|