github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/lib/readers/pattern_reader_test.go (about) 1 package readers 2 3 import ( 4 "io" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestPatternReader(t *testing.T) { 12 b2 := make([]byte, 1) 13 14 r := NewPatternReader(0) 15 b, err := io.ReadAll(r) 16 require.NoError(t, err) 17 assert.Equal(t, []byte{}, b) 18 n, err := r.Read(b2) 19 require.Equal(t, io.EOF, err) 20 require.Equal(t, 0, n) 21 22 r = NewPatternReader(10) 23 b, err = io.ReadAll(r) 24 require.NoError(t, err) 25 assert.Equal(t, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, b) 26 n, err = r.Read(b2) 27 require.Equal(t, io.EOF, err) 28 require.Equal(t, 0, n) 29 } 30 31 func TestPatternReaderSeek(t *testing.T) { 32 r := NewPatternReader(1024) 33 b, err := io.ReadAll(r) 34 require.NoError(t, err) 35 36 for i := range b { 37 assert.Equal(t, byte(i%251), b[i]) 38 } 39 40 n, err := r.Seek(1, io.SeekStart) 41 require.NoError(t, err) 42 assert.Equal(t, int64(1), n) 43 44 // pos 1 45 46 b2 := make([]byte, 10) 47 nn, err := r.Read(b2) 48 require.NoError(t, err) 49 assert.Equal(t, 10, nn) 50 assert.Equal(t, b[1:11], b2) 51 52 // pos 11 53 54 n, err = r.Seek(9, io.SeekCurrent) 55 require.NoError(t, err) 56 assert.Equal(t, int64(20), n) 57 58 // pos 20 59 60 nn, err = r.Read(b2) 61 require.NoError(t, err) 62 assert.Equal(t, 10, nn) 63 assert.Equal(t, b[20:30], b2) 64 65 n, err = r.Seek(-24, io.SeekEnd) 66 require.NoError(t, err) 67 assert.Equal(t, int64(1000), n) 68 69 // pos 1000 70 71 nn, err = r.Read(b2) 72 require.NoError(t, err) 73 assert.Equal(t, 10, nn) 74 assert.Equal(t, b[1000:1010], b2) 75 76 // Now test errors 77 78 n, err = r.Seek(1, 400) 79 require.Error(t, err) 80 assert.Contains(t, err.Error(), "invalid whence") 81 assert.Equal(t, int64(0), n) 82 83 n, err = r.Seek(-1, io.SeekStart) 84 require.Error(t, err) 85 assert.Contains(t, err.Error(), "negative position") 86 assert.Equal(t, int64(0), n) 87 }