github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/util/ioutil/size_test.go (about) 1 package ioutil 2 3 import ( 4 "io" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 ) 10 11 func TestNewSizeReaderAt(t *testing.T) { 12 f, err := os.Create(filepath.Join(t.TempDir(), "abc.txt")) 13 if err != nil { 14 t.Fatal(err) 15 } 16 defer f.Close() 17 18 n, err := NewSizeReaderAt(f).Size() 19 if err != nil { 20 t.Fatal(err) 21 } 22 if n != 0 { 23 t.Errorf("got %d", n) 24 } 25 26 reader := strings.NewReader("abc") 27 28 n, err = NewSizeReaderAt(reader).Size() 29 if err != nil { 30 t.Fatal(err) 31 } 32 if n != 3 { 33 t.Errorf("got %d", n) 34 } 35 36 n, err = NewSizeReaderAt(readlener{reader, reader.Len()}).Size() 37 if err != nil { 38 t.Fatal(err) 39 } 40 if n != 3 { 41 t.Errorf("got %d", n) 42 } 43 44 n, err = NewSizeReaderAt(readsizer{reader, reader.Size()}).Size() 45 if err != nil { 46 t.Fatal(err) 47 } 48 if n != 3 { 49 t.Errorf("got %d", n) 50 } 51 52 n, err = NewSizeReaderAt(readseeker{reader, reader}).Size() 53 if err != nil { 54 t.Fatal(err) 55 } 56 if n != 3 { 57 t.Errorf("got %d", n) 58 } 59 60 _, err = NewSizeReaderAt(readerat{reader}).Size() 61 if err == nil { 62 t.Error("want error") 63 } 64 } 65 66 type readlener struct { 67 io.ReaderAt 68 len int 69 } 70 71 func (l readlener) Len() int { return l.len } 72 73 type readsizer struct { 74 io.ReaderAt 75 size int64 76 } 77 78 func (l readsizer) Size() (int64, error) { return l.size, nil } 79 80 type readseeker struct { 81 io.ReaderAt 82 io.Seeker 83 } 84 85 type readerat struct { 86 io.ReaderAt 87 }