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.
17 func TestReader(t
*testing
.T
) {
18 r
:= strings
.NewReader("0123456789")
28 {seek
: io
.SeekStart
, off
: 0, n
: 20, want
: "0123456789"},
29 {seek
: io
.SeekStart
, off
: 1, n
: 1, want
: "1"},
30 {seek
: io
.SeekCurrent
, off
: 1, wantpos
: 3, n
: 2, want
: "34"},
31 {seek
: io
.SeekStart
, off
: -1, seekerr
: "strings.Reader.Seek: negative position"},
32 {seek
: io
.SeekStart
, off
: 1 << 33, wantpos
: 1 << 33, readerr
: io
.EOF
},
33 {seek
: io
.SeekCurrent
, off
: 1, wantpos
: 1<<33 + 1, readerr
: io
.EOF
},
34 {seek
: io
.SeekStart
, n
: 5, want
: "01234"},
35 {seek
: io
.SeekCurrent
, n
: 5, want
: "56789"},
36 {seek
: io
.SeekEnd
, off
: -1, n
: 1, wantpos
: 9, want
: "9"},
39 for i
, tt
:= range tests
{
40 pos
, err
:= r
.Seek(tt
.off
, tt
.seek
)
41 if err
== nil && tt
.seekerr
!= "" {
42 t
.Errorf("%d. want seek error %q", i
, tt
.seekerr
)
45 if err
!= nil && err
.Error() != tt
.seekerr
{
46 t
.Errorf("%d. seek error = %q; want %q", i
, err
.Error(), tt
.seekerr
)
49 if tt
.wantpos
!= 0 && tt
.wantpos
!= pos
{
50 t
.Errorf("%d. pos = %d, want %d", i
, pos
, tt
.wantpos
)
52 buf
:= make([]byte, tt
.n
)
54 if err
!= tt
.readerr
{
55 t
.Errorf("%d. read = %v; want %v", i
, err
, tt
.readerr
)
58 got
:= string(buf
[:n
])
60 t
.Errorf("%d. got %q; want %q", i
, got
, tt
.want
)
65 func TestReadAfterBigSeek(t
*testing
.T
) {
66 r
:= strings
.NewReader("0123456789")
67 if _
, err
:= r
.Seek(1<<31+5, io
.SeekStart
); err
!= nil {
70 if n
, err
:= r
.Read(make([]byte, 10)); n
!= 0 || err
!= io
.EOF
{
71 t
.Errorf("Read = %d, %v; want 0, EOF", n
, err
)
75 func TestReaderAt(t
*testing
.T
) {
76 r
:= strings
.NewReader("0123456789")
83 {0, 10, "0123456789", nil},
84 {1, 10, "123456789", io
.EOF
},
85 {1, 9, "123456789", nil},
88 {-1, 0, "", "strings.Reader.ReadAt: negative offset"},
90 for i
, tt
:= range tests
{
91 b
:= make([]byte, tt
.n
)
92 rn
, err
:= r
.ReadAt(b
, tt
.off
)
95 t
.Errorf("%d. got %q; want %q", i
, got
, tt
.want
)
97 if fmt
.Sprintf("%v", err
) != fmt
.Sprintf("%v", tt
.wanterr
) {
98 t
.Errorf("%d. got error = %v; want %v", i
, err
, tt
.wanterr
)
103 func TestReaderAtConcurrent(t
*testing
.T
) {
104 // Test for the race detector, to verify ReadAt doesn't mutate
106 r
:= strings
.NewReader("0123456789")
107 var wg sync
.WaitGroup
108 for i
:= 0; i
< 5; i
++ {
113 r
.ReadAt(buf
[:], int64(i
))
119 func TestEmptyReaderConcurrent(t
*testing
.T
) {
120 // Test for the race detector, to verify a Read that doesn't yield any bytes
121 // is okay to use from multiple goroutines. This was our historic behavior.
122 // See golang.org/issue/7856
123 r
:= strings
.NewReader("")
124 var wg sync
.WaitGroup
125 for i
:= 0; i
< 5; i
++ {
140 func TestWriteTo(t
*testing
.T
) {
141 const str
= "0123456789"
142 for i
:= 0; i
<= len(str
); i
++ {
144 r
:= strings
.NewReader(s
)
146 n
, err
:= r
.WriteTo(&b
)
147 if expect
:= int64(len(s
)); n
!= expect
{
148 t
.Errorf("got %v; want %v", n
, expect
)
151 t
.Errorf("for length %d: got error = %v; want nil", len(s
), err
)
154 t
.Errorf("got string %q; want %q", b
.String(), s
)
157 t
.Errorf("reader contains %v bytes; want 0", r
.Len())
162 // tests that Len is affected by reads, but Size is not.
163 func TestReaderLenSize(t
*testing
.T
) {
164 r
:= strings
.NewReader("abc")
165 io
.CopyN(ioutil
.Discard
, r
, 1)
167 t
.Errorf("Len = %d; want 2", r
.Len())
170 t
.Errorf("Size = %d; want 3", r
.Size())
174 func TestReaderReset(t
*testing
.T
) {
175 r
:= strings
.NewReader("世界")
176 if _
, _
, err
:= r
.ReadRune(); err
!= nil {
177 t
.Errorf("ReadRune: unexpected error: %v", err
)
180 const want
= "abcdef"
182 if err
:= r
.UnreadRune(); err
== nil {
183 t
.Errorf("UnreadRune: expected error, got nil")
185 buf
, err
:= ioutil
.ReadAll(r
)
187 t
.Errorf("ReadAll: unexpected error: %v", err
)
189 if got
:= string(buf
); got
!= want
{
190 t
.Errorf("ReadAll: got %q, want %q", got
, want
)