github.com/ipld/go-ipld-prime@v0.21.0/testutil/multibytenode_test.go (about) 1 package testutil_test 2 3 import ( 4 "io" 5 "testing" 6 7 qt "github.com/frankban/quicktest" 8 9 "github.com/ipld/go-ipld-prime/testutil" 10 ) 11 12 func TestMultiByteNode(t *testing.T) { 13 mbn := testutil.NewMultiByteNode( 14 []byte("foo"), 15 []byte("bar"), 16 []byte("baz"), 17 []byte("!"), 18 ) 19 // Sanity check that the readseeker works. 20 // (This is a test of the test, not the code under test.) 21 22 for _, rl := range []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} { 23 t.Run("readseeker works with read length "+qt.Format(rl), func(t *testing.T) { 24 rs, err := mbn.AsLargeBytes() 25 qt.Assert(t, err, qt.IsNil) 26 acc := make([]byte, 0, mbn.TotalLength()) 27 buf := make([]byte, rl) 28 for { 29 n, err := rs.Read(buf) 30 if err == io.EOF { 31 qt.Check(t, n, qt.Equals, 0) 32 break 33 } 34 qt.Assert(t, err, qt.IsNil) 35 acc = append(acc, buf[0:n]...) 36 } 37 qt.Assert(t, string(acc), qt.DeepEquals, "foobarbaz!") 38 }) 39 } 40 41 t.Run("readseeker can seek and read middle bytes", func(t *testing.T) { 42 rs, err := mbn.AsLargeBytes() 43 qt.Assert(t, err, qt.IsNil) 44 _, err = rs.Seek(2, io.SeekStart) 45 qt.Assert(t, err, qt.IsNil) 46 buf := make([]byte, 2) 47 acc := make([]byte, 0, 5) 48 for len(acc) < 5 { 49 n, err := rs.Read(buf) 50 qt.Assert(t, err, qt.IsNil) 51 acc = append(acc, buf[0:n]...) 52 } 53 qt.Assert(t, string(acc), qt.DeepEquals, "obarba") 54 }) 55 56 t.Run("readseeker can seek and read last byte", func(t *testing.T) { 57 rs, err := mbn.AsLargeBytes() 58 qt.Assert(t, err, qt.IsNil) 59 _, err = rs.Seek(-1, io.SeekEnd) 60 qt.Assert(t, err, qt.IsNil) 61 buf := make([]byte, 1) 62 n, err := rs.Read(buf) 63 qt.Assert(t, err, qt.IsNil) 64 qt.Check(t, n, qt.Equals, 1) 65 qt.Check(t, string(buf[0]), qt.Equals, "!") 66 }) 67 }