1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 func TestReader(t
*testing
.T
) {
17 r
:= NewReader([]byte("0123456789"))
26 {seek
: os
.SEEK_SET
, off
: 0, n
: 20, want
: "0123456789"},
27 {seek
: os
.SEEK_SET
, off
: 1, n
: 1, want
: "1"},
28 {seek
: os
.SEEK_CUR
, off
: 1, wantpos
: 3, n
: 2, want
: "34"},
29 {seek
: os
.SEEK_SET
, off
: -1, seekerr
: "bytes: negative position"},
30 {seek
: os
.SEEK_SET
, off
: 1<<31 - 1},
31 {seek
: os
.SEEK_CUR
, off
: 1, seekerr
: "bytes: position out of range"},
32 {seek
: os
.SEEK_SET
, n
: 5, want
: "01234"},
33 {seek
: os
.SEEK_CUR
, n
: 5, want
: "56789"},
34 {seek
: os
.SEEK_END
, off
: -1, n
: 1, wantpos
: 9, want
: "9"},
37 for i
, tt
:= range tests
{
38 pos
, err
:= r
.Seek(tt
.off
, tt
.seek
)
39 if err
== nil && tt
.seekerr
!= "" {
40 t
.Errorf("%d. want seek error %q", i
, tt
.seekerr
)
43 if err
!= nil && err
.Error() != tt
.seekerr
{
44 t
.Errorf("%d. seek error = %q; want %q", i
, err
.Error(), tt
.seekerr
)
47 if tt
.wantpos
!= 0 && tt
.wantpos
!= pos
{
48 t
.Errorf("%d. pos = %d, want %d", i
, pos
, tt
.wantpos
)
50 buf
:= make([]byte, tt
.n
)
53 t
.Errorf("%d. read = %v", i
, err
)
56 got
:= string(buf
[:n
])
58 t
.Errorf("%d. got %q; want %q", i
, got
, tt
.want
)
63 func TestReaderAt(t
*testing
.T
) {
64 r
:= NewReader([]byte("0123456789"))
71 {0, 10, "0123456789", nil},
72 {1, 10, "123456789", io
.EOF
},
73 {1, 9, "123456789", nil},
76 {-1, 0, "", "bytes: invalid offset"},
78 for i
, tt
:= range tests
{
79 b
:= make([]byte, tt
.n
)
80 rn
, err
:= r
.ReadAt(b
, tt
.off
)
83 t
.Errorf("%d. got %q; want %q", i
, got
, tt
.want
)
85 if fmt
.Sprintf("%v", err
) != fmt
.Sprintf("%v", tt
.wanterr
) {
86 t
.Errorf("%d. got error = %v; want %v", i
, err
, tt
.wanterr
)
91 func TestReaderWriteTo(t
*testing
.T
) {
92 for i
:= 0; i
< 30; i
+= 3 {
98 r
:= NewReader(testBytes
[:l
])
100 n
, err
:= r
.WriteTo(&b
)
101 if expect
:= int64(len(s
)); n
!= expect
{
102 t
.Errorf("got %v; want %v", n
, expect
)
105 t
.Errorf("for length %d: got error = %v; want nil", l
, err
)
108 t
.Errorf("got string %q; want %q", b
.String(), s
)
111 t
.Errorf("reader contains %v bytes; want 0", r
.Len())
116 func TestReaderLen(t
*testing
.T
) {
117 const data
= "hello world"
118 r
:= NewReader([]byte(data
))
119 if got
, want
:= r
.Len(), 11; got
!= want
{
120 t
.Errorf("r.Len(): got %d, want %d", got
, want
)
122 if n
, err
:= r
.Read(make([]byte, 10)); err
!= nil || n
!= 10 {
123 t
.Errorf("Read failed: read %d %v", n
, err
)
125 if got
, want
:= r
.Len(), 1; got
!= want
{
126 t
.Errorf("r.Len(): got %d, want %d", got
, want
)
128 if n
, err
:= r
.Read(make([]byte, 1)); err
!= nil || n
!= 1 {
129 t
.Errorf("Read failed: read %d %v", n
, err
)
131 if got
, want
:= r
.Len(), 0; got
!= want
{
132 t
.Errorf("r.Len(): got %d, want %d", got
, want
)
136 func TestReaderDoubleUnreadRune(t
*testing
.T
) {
137 buf
:= NewBuffer([]byte("groucho"))
138 if _
, _
, err
:= buf
.ReadRune(); err
!= nil {
142 if err
:= buf
.UnreadByte(); err
!= nil {
146 if err
:= buf
.UnreadByte(); err
== nil {
147 t
.Fatal("UnreadByte: expected error, got nil")
151 // verify that copying from an empty reader always has the same results,
152 // regardless of the presence of a WriteTo method.
153 func TestReaderCopyNothing(t
*testing
.T
) {
158 type justReader
struct {
161 type justWriter
struct {
164 discard
:= justWriter
{ioutil
.Discard
} // hide ReadFrom
166 var with
, withOut nErr
167 with
.n
, with
.err
= io
.Copy(discard
, NewReader(nil))
168 withOut
.n
, withOut
.err
= io
.Copy(discard
, justReader
{NewReader(nil)})
170 t
.Errorf("behavior differs: with = %#v; without: %#v", with
, withOut
)