1 // Copyright 2009 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.
14 // An version of bytes.Buffer without ReadFrom and WriteTo
17 ReaderFrom // conflicts with and hides bytes.Buffer's ReaderFrom.
18 WriterTo // conflicts with and hides bytes.Buffer's WriterTo.
21 // Simple tests, primarily to verify the ReadFrom and WriteTo callouts inside Copy and CopyN.
23 func TestCopy(t *testing.T) {
26 rb.WriteString("hello, world.")
28 if wb.String() != "hello, world." {
29 t.Errorf("Copy did not work properly")
33 func TestCopyReadFrom(t *testing.T) {
35 wb := new(bytes.Buffer) // implements ReadFrom.
36 rb.WriteString("hello, world.")
38 if wb.String() != "hello, world." {
39 t.Errorf("Copy did not work properly")
43 func TestCopyWriteTo(t *testing.T) {
44 rb := new(bytes.Buffer) // implements WriteTo.
46 rb.WriteString("hello, world.")
48 if wb.String() != "hello, world." {
49 t.Errorf("Copy did not work properly")
53 func TestCopyN(t *testing.T) {
56 rb.WriteString("hello, world.")
58 if wb.String() != "hello" {
59 t.Errorf("CopyN did not work properly")
63 func TestCopyNReadFrom(t *testing.T) {
65 wb := new(bytes.Buffer) // implements ReadFrom.
66 rb.WriteString("hello")
68 if wb.String() != "hello" {
69 t.Errorf("CopyN did not work properly")
73 func TestCopyNWriteTo(t *testing.T) {
74 rb := new(bytes.Buffer) // implements WriteTo.
76 rb.WriteString("hello, world.")
78 if wb.String() != "hello" {
79 t.Errorf("CopyN did not work properly")
83 type noReadFrom struct {
87 func (w *noReadFrom) Write(p []byte) (n int, err error) {
91 func TestCopyNEOF(t *testing.T) {
92 // Test that EOF behavior is the same regardless of whether
93 // argument to CopyN has ReadFrom.
95 b := new(bytes.Buffer)
97 n, err := CopyN(&noReadFrom{b}, strings.NewReader("foo"), 3)
98 if n != 3 || err != nil {
99 t.Errorf("CopyN(noReadFrom, foo, 3) = %d, %v; want 3, nil", n, err)
102 n, err = CopyN(&noReadFrom{b}, strings.NewReader("foo"), 4)
103 if n != 3 || err != EOF {
104 t.Errorf("CopyN(noReadFrom, foo, 4) = %d, %v; want 3, EOF", n, err)
107 n, err = CopyN(b, strings.NewReader("foo"), 3) // b has read from
108 if n != 3 || err != nil {
109 t.Errorf("CopyN(bytes.Buffer, foo, 3) = %d, %v; want 3, nil", n, err)
112 n, err = CopyN(b, strings.NewReader("foo"), 4) // b has read from
113 if n != 3 || err != EOF {
114 t.Errorf("CopyN(bytes.Buffer, foo, 4) = %d, %v; want 3, EOF", n, err)
118 func TestReadAtLeast(t *testing.T) {
120 testReadAtLeast(t, &rb)
123 // A version of bytes.Buffer that returns n > 0, EOF on Read
124 // when the input is exhausted.
125 type dataAndEOFBuffer struct {
129 func (r *dataAndEOFBuffer) Read(p []byte) (n int, err error) {
130 n, err = r.Buffer.Read(p)
131 if n > 0 && r.Buffer.Len() == 0 && err == nil {
137 func TestReadAtLeastWithDataAndEOF(t *testing.T) {
138 var rb dataAndEOFBuffer
139 testReadAtLeast(t, &rb)
142 func testReadAtLeast(t *testing.T, rb ReadWriter) {
143 rb.Write([]byte("0123"))
144 buf := make([]byte, 2)
145 n, err := ReadAtLeast(rb, buf, 2)
149 n, err = ReadAtLeast(rb, buf, 4)
150 if err != ErrShortBuffer {
151 t.Errorf("expected ErrShortBuffer got %v", err)
154 t.Errorf("expected to have read 0 bytes, got %v", n)
156 n, err = ReadAtLeast(rb, buf, 1)
161 t.Errorf("expected to have read 2 bytes, got %v", n)
163 n, err = ReadAtLeast(rb, buf, 2)
165 t.Errorf("expected EOF, got %v", err)
168 t.Errorf("expected to have read 0 bytes, got %v", n)
170 rb.Write([]byte("4"))
171 n, err = ReadAtLeast(rb, buf, 2)
172 if err != ErrUnexpectedEOF {
173 t.Errorf("expected ErrUnexpectedEOF, got %v", err)
176 t.Errorf("expected to have read 1 bytes, got %v", n)
180 func TestTeeReader(t *testing.T) {
181 src := []byte("hello, world")
182 dst := make([]byte, len(src))
183 rb := bytes.NewBuffer(src)
184 wb := new(bytes.Buffer)
185 r := TeeReader(rb, wb)
186 if n, err := ReadFull(r, dst); err != nil || n != len(src) {
187 t.Fatalf("ReadFull(r, dst) = %d, %v; want %d, nil", n, err, len(src))
189 if !bytes.Equal(dst, src) {
190 t.Errorf("bytes read = %q want %q", dst, src)
192 if !bytes.Equal(wb.Bytes(), src) {
193 t.Errorf("bytes written = %q want %q", wb.Bytes(), src)
195 if n, err := r.Read(dst); n != 0 || err != EOF {
196 t.Errorf("r.Read at EOF = %d, %v want 0, EOF", n, err)
198 rb = bytes.NewBuffer(src)
201 r = TeeReader(rb, pw)
202 if n, err := ReadFull(r, dst); n != 0 || err != ErrClosedPipe {
203 t.Errorf("closed tee: ReadFull(r, dst) = %d, %v; want 0, EPIPE", n, err)