go4.org@v0.0.0-20230225012048-214862532bf5/readerutil/readersize_test.go (about) 1 /* 2 Copyright 2012 The Go4 Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package readerutil 18 19 import ( 20 "bytes" 21 "io" 22 "io/ioutil" 23 "os" 24 "testing" 25 ) 26 27 const text = "HelloWorld" 28 29 type testSrc struct { 30 name string 31 src io.Reader 32 want int64 33 } 34 35 func (tsrc *testSrc) run(t *testing.T) { 36 n, ok := Size(tsrc.src) 37 if !ok { 38 t.Fatalf("failed to read size for %q", tsrc.name) 39 } 40 if n != tsrc.want { 41 t.Fatalf("wanted %v, got %v", tsrc.want, n) 42 } 43 } 44 45 func TestBytesBuffer(t *testing.T) { 46 buf := bytes.NewBuffer([]byte(text)) 47 tsrc := &testSrc{"buffer", buf, int64(len(text))} 48 tsrc.run(t) 49 } 50 51 func TestSeeker(t *testing.T) { 52 f, err := ioutil.TempFile("", "camliTestReaderSize") 53 if err != nil { 54 t.Fatal(err) 55 } 56 defer os.Remove(f.Name()) 57 defer f.Close() 58 size, err := f.Write([]byte(text)) 59 if err != nil { 60 t.Fatal(err) 61 } 62 pos, err := f.Seek(5, 0) 63 if err != nil { 64 t.Fatal(err) 65 } 66 tsrc := &testSrc{"seeker", f, int64(size) - pos} 67 tsrc.run(t) 68 }