Re-added SliceReaderAt

This commit is contained in:
2024-09-30 13:02:57 -06:00
parent 5e6eafef64
commit c29be1a7b6
3 changed files with 58 additions and 0 deletions

View File

@ -2,11 +2,13 @@ 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"
@ -102,3 +104,42 @@ func TestState(t *testing.T) {
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)
}))
}