github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/recordio/deprecated/range_test.go (about)

     1  // Copyright 2017 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache-2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package deprecated_test
     6  
     7  import (
     8  	"bytes"
     9  	"io"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/Schaudge/grailbase/recordio/deprecated"
    14  	"github.com/grailbio/testutil"
    15  )
    16  
    17  func TestBounded(t *testing.T) {
    18  	failIf := func(err error) {
    19  		if err != nil {
    20  			t.Fatalf("%v: %v", testutil.Caller(1), err)
    21  		}
    22  	}
    23  
    24  	expectError := func(err error, msg string) {
    25  		if err == nil || !strings.Contains(err.Error(), msg) {
    26  			t.Fatalf("%v: expected an error about %v: got: %v", testutil.Caller(1), msg, err)
    27  		}
    28  	}
    29  
    30  	expectLen := func(got, want int) {
    31  		if got != want {
    32  			t.Errorf("%v: got %v, want %v", testutil.Caller(1), got, want)
    33  		}
    34  	}
    35  
    36  	expectBuf := func(got []byte, want ...byte) {
    37  		if !bytes.Equal(got, want) {
    38  			t.Errorf("%v: got %v, want %v", testutil.Caller(1), got, want)
    39  		}
    40  	}
    41  
    42  	raw := make([]byte, 255)
    43  	for i := 0; i < 255; i++ {
    44  		raw[i] = byte(i)
    45  	}
    46  
    47  	rs := bytes.NewReader(raw)
    48  
    49  	// Negative offset will fail.
    50  	_, err := deprecated.NewRangeReader(rs, -1, 5)
    51  	expectError(err, "negative position")
    52  
    53  	// Seeking past the end of the file is the same as seeking to the end.
    54  	br, err := deprecated.NewRangeReader(rs, 512, 5)
    55  	failIf(err)
    56  
    57  	buf := make([]byte, 3)
    58  	n, err := br.Read(buf)
    59  	expectError(err, "EOF")
    60  	expectLen(n, 0)
    61  
    62  	br, err = deprecated.NewRangeReader(rs, 48, 5)
    63  	failIf(err)
    64  
    65  	n, err = br.Read(buf)
    66  	failIf(err)
    67  	expectLen(n, 3)
    68  	expectBuf(buf, '0', '1', '2')
    69  
    70  	buf = make([]byte, 10)
    71  	n, err = br.Read(buf)
    72  	expectError(err, "EOF")
    73  	expectLen(n, 2)
    74  	expectBuf(buf[:n], '3', '4')
    75  
    76  	p, err := br.Seek(0, io.SeekStart)
    77  	failIf(err)
    78  	if got, want := p, int64(0); got != want {
    79  		t.Errorf("got %v, want %v", got, want)
    80  	}
    81  
    82  	n, err = br.Read(buf)
    83  	expectError(err, "EOF")
    84  	expectBuf(buf[:n], '0', '1', '2', '3', '4')
    85  
    86  	_, err = br.Seek(2, io.SeekStart)
    87  	failIf(err)
    88  	n, err = br.Read(buf)
    89  	expectError(err, "EOF")
    90  	if got, want := buf[:n], []byte{'2', '3', '4'}; !bytes.Equal(got, want) {
    91  		t.Errorf("got %v, want %v", got, want)
    92  	}
    93  
    94  	_, err = br.Seek(-2, io.SeekEnd)
    95  	failIf(err)
    96  	n, err = br.Read(buf)
    97  	expectError(err, "EOF")
    98  	expectBuf(buf[:n], '3', '4')
    99  	_, err = br.Seek(1, io.SeekStart)
   100  	failIf(err)
   101  	_, err = br.Seek(1, io.SeekCurrent)
   102  	failIf(err)
   103  	n, err = br.Read(buf[:2])
   104  	failIf(err)
   105  	expectBuf(buf[:n], '2', '3')
   106  
   107  	_, err = br.Seek(100, io.SeekCurrent)
   108  	failIf(err)
   109  	n, err = br.Read(buf[:2])
   110  	expectError(err, "EOF")
   111  	if got, want := n, 0; got != want {
   112  		t.Errorf("got %v, want %v", got, want)
   113  	}
   114  	_, err = br.Seek(-1, io.SeekEnd)
   115  	failIf(err)
   116  	n, err = br.Read(buf[:1])
   117  	expectError(err, "EOF")
   118  	expectBuf(buf[:n], '4')
   119  
   120  	// Seeking past the end of the stream is the same as seeking to the end.
   121  	_, err = br.Seek(100, io.SeekEnd)
   122  	failIf(err)
   123  	n, err = br.Read(buf[:1])
   124  	expectError(err, "EOF")
   125  	expectLen(n, 0)
   126  }