package cursor_test import ( "bytes" "io" "testing" "git.codemonkeysoftware.net/b/gigaparsec/cursor" "github.com/shoenig/test" "github.com/shoenig/test/must" "pgregory.net/rapid" ) func Todo(t *testing.T) { t.Errorf("TODO") } func SliceOfNZero[T any](minLen, maxLen int) *rapid.Generator[[]T] { return rapid.Map(rapid.IntRange(minLen, maxLen), func(n int) []T { return make([]T, n) }) } func testCursor[C cursor.Cursor[byte]](t *testing.T, makeCursor func([]byte) C) { t.Helper() t.Run("cursor reads the same position every time", rapid.MakeCheck(func(t *rapid.T) { data := rapid.SliceOfN(rapid.Byte(), 1, 100).Draw(t, "data") dst := SliceOfNZero[byte](0, len(data)-1).Draw(t, "dst") expected := data[:len(dst)] c := makeCursor(data) _, next, err := c.Read(dst) must.NoError(t, err) must.SliceEqOp(t, expected, dst) next.Read(dst) _, _, err = c.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 := SliceOfNZero[byte](0, 200).Draw(t, "dst") c := makeCursor(data) n, _, err := c.Read(dst) t.Logf("n=%d", n) must.EqOp(t, min(len(data), len(dst)), int(n)) if len(dst) > len(data) || c.Pos() == uint64(len(data)) { must.ErrorIs(t, err, io.EOF) } else { must.NoError(t, err) } })) t.Run("next cursor 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, "data") c := makeCursor(data) _, next, err := c.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 an error if n is less than requested", rapid.MakeCheck(func(t *rapid.T) { data := rapid.SliceOfN(rapid.Byte(), 0, 100).Draw(t, "data") c := makeCursor(data) n, _, err := c.Read(make([]byte, len(data)+1)) test.ErrorIs(t, err, io.EOF) test.EqOp(t, len(data), int(n)) })) t.Run("At", Todo) } func TestSliceCursor(t *testing.T) { testCursor(t, cursor.NewSlice[byte]) } func TestReaderAtCursor(t *testing.T) { testCursor(t, func(b []byte) cursor.ReaderAtCursor { return cursor.NewReaderAt(bytes.NewReader(b)) }) }