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
:= strings
.NewReader("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
: "strings: negative position"},
30 {seek
: os
.SEEK_SET
, off
: 1<<31 - 1},
31 {seek
: os
.SEEK_CUR
, off
: 1, seekerr
: "strings: 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
:= strings
.NewReader("0123456789")
71 {0, 10, "0123456789", nil},
72 {1, 10, "123456789", io
.EOF
},
73 {1, 9, "123456789", nil},
76 {-1, 0, "", "strings: 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 TestWriteTo(t
*testing
.T
) {
92 const str
= "0123456789"
93 for i
:= 0; i
<= len(str
); i
++ {
95 r
:= strings
.NewReader(s
)
97 n
, err
:= r
.WriteTo(&b
)
98 if expect
:= int64(len(s
)); n
!= expect
{
99 t
.Errorf("got %v; want %v", n
, expect
)
102 t
.Errorf("for length %d: got error = %v; want nil", len(s
), err
)
105 t
.Errorf("got string %q; want %q", b
.String(), s
)
108 t
.Errorf("reader contains %v bytes; want 0", r
.Len())