github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/checksum/checksum_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package checksum 15 16 import ( 17 "bytes" 18 "io" 19 "os" 20 "testing" 21 22 "github.com/whtcorpsinc/check" 23 ) 24 25 func TestT(t *testing.T) { 26 check.TestingT(t) 27 } 28 29 var _ = check.Suite(&testChecksumSuite{}) 30 31 type testChecksumSuite struct{} 32 33 func (s *testChecksumSuite) TestChecksumReadAt(c *check.C) { 34 path := "checksum" 35 f, err := os.Create(path) 36 c.Assert(err, check.IsNil) 37 defer func() { 38 err = f.Close() 39 c.Assert(err, check.IsNil) 40 err = os.Remove(path) 41 c.Assert(err, check.IsNil) 42 }() 43 44 writeString := "0123456789" 45 c.Assert(err, check.IsNil) 46 csw := NewWriter(NewWriter(NewWriter(NewWriter(f)))) 47 w := bytes.NewBuffer(nil) 48 for i := 0; i < 510; i++ { 49 w.WriteString(writeString) 50 } 51 n1, err := csw.Write(w.Bytes()) 52 c.Assert(err, check.IsNil) 53 n2, err := csw.Write(w.Bytes()) 54 c.Assert(err, check.IsNil) 55 err = csw.Close() 56 c.Assert(err, check.IsNil) 57 58 f, err = os.Open(path) 59 c.Assert(err, check.IsNil) 60 61 assertReadAt := func(off int64, assertErr interface{}, assertN int, assertString string) { 62 cs := NewReader(NewReader(NewReader(NewReader(f)))) 63 r := make([]byte, 10) 64 n, err := cs.ReadAt(r, off) 65 c.Assert(err, check.Equals, assertErr) 66 c.Assert(n, check.Equals, assertN) 67 c.Assert(string(r), check.Equals, assertString) 68 } 69 70 assertReadAt(0, nil, 10, "0123456789") 71 assertReadAt(5, nil, 10, "5678901234") 72 assertReadAt(int64(n1+n2)-5, io.EOF, 5, "56789\x00\x00\x00\x00\x00") 73 }