Added cursor tests

This commit is contained in:
2024-09-03 13:55:32 -06:00
parent b06454a4bc
commit 49bfcb7462
5 changed files with 126 additions and 5 deletions

View File

@ -17,8 +17,9 @@ type Cursor[Datum any] interface {
// source. It returns the number of data it read and a new Cursor for
// the position at which the read ended, or an error if the read failed.
// All calls to a given Cursor will return data from the same position.
// If n < len(dst), Read will return an error explaining why it read fewer
// bytes than requested. If Read tried to read past the end of the source,
// If n < len(dst) or if the cursor's position is at the end of the data source,
// Read will return an error explaining why it read fewer bytes than requested.
// If the error was due to the cursor reaching the end of the data source,
// err will be io.EOF.
Read(dst []Datum) (n uint64, next Cursor[Datum], err error)
@ -31,9 +32,17 @@ type SliceCursor[Datum any] struct {
offset uint64
}
func NewSlice[Datum any]([]Datum) SliceCursor[Datum] { panic("not implemented") }
func NewSlice[Datum any](data []Datum) SliceCursor[Datum] {
return SliceCursor[Datum]{
data: data,
offset: 0,
}
}
func (sc SliceCursor[Datum]) Read(dst []Datum) (n uint64, next Cursor[Datum], err error) {
if sc.offset == uint64(len(sc.data)) {
return 0, sc, io.EOF
}
copied := copy(dst, sc.data[sc.offset:])
if copied < len(dst) {
err = io.EOF
@ -81,6 +90,9 @@ func NewString(s string) StringCursor {
}
func (sc StringCursor) Read(dst []byte) (n uint64, next Cursor[byte], err error) {
if sc.offset == uint64(len(sc.source)) {
return 0, sc, io.EOF
}
copied := copy(dst, sc.source[sc.offset:])
if copied < len(dst) {
err = io.EOF