github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/io/io_test.go (about)

     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.
     4  
     5  package io_test
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"fmt"
    11  	. "io"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  // An version of bytes.Buffer without ReadFrom and WriteTo
    17  type Buffer struct {
    18  	bytes.Buffer
    19  	ReaderFrom // conflicts with and hides bytes.Buffer's ReaderFrom.
    20  	WriterTo   // conflicts with and hides bytes.Buffer's WriterTo.
    21  }
    22  
    23  // Simple tests, primarily to verify the ReadFrom and WriteTo callouts inside Copy and CopyN.
    24  
    25  func TestCopy(t *testing.T) {
    26  	rb := new(Buffer)
    27  	wb := new(Buffer)
    28  	rb.WriteString("hello, world.")
    29  	Copy(wb, rb)
    30  	if wb.String() != "hello, world." {
    31  		t.Errorf("Copy did not work properly")
    32  	}
    33  }
    34  
    35  func TestCopyReadFrom(t *testing.T) {
    36  	rb := new(Buffer)
    37  	wb := new(bytes.Buffer) // implements ReadFrom.
    38  	rb.WriteString("hello, world.")
    39  	Copy(wb, rb)
    40  	if wb.String() != "hello, world." {
    41  		t.Errorf("Copy did not work properly")
    42  	}
    43  }
    44  
    45  func TestCopyWriteTo(t *testing.T) {
    46  	rb := new(bytes.Buffer) // implements WriteTo.
    47  	wb := new(Buffer)
    48  	rb.WriteString("hello, world.")
    49  	Copy(wb, rb)
    50  	if wb.String() != "hello, world." {
    51  		t.Errorf("Copy did not work properly")
    52  	}
    53  }
    54  
    55  func TestCopyN(t *testing.T) {
    56  	rb := new(Buffer)
    57  	wb := new(Buffer)
    58  	rb.WriteString("hello, world.")
    59  	CopyN(wb, rb, 5)
    60  	if wb.String() != "hello" {
    61  		t.Errorf("CopyN did not work properly")
    62  	}
    63  }
    64  
    65  func TestCopyNReadFrom(t *testing.T) {
    66  	rb := new(Buffer)
    67  	wb := new(bytes.Buffer) // implements ReadFrom.
    68  	rb.WriteString("hello")
    69  	CopyN(wb, rb, 5)
    70  	if wb.String() != "hello" {
    71  		t.Errorf("CopyN did not work properly")
    72  	}
    73  }
    74  
    75  func TestCopyNWriteTo(t *testing.T) {
    76  	rb := new(bytes.Buffer) // implements WriteTo.
    77  	wb := new(Buffer)
    78  	rb.WriteString("hello, world.")
    79  	CopyN(wb, rb, 5)
    80  	if wb.String() != "hello" {
    81  		t.Errorf("CopyN did not work properly")
    82  	}
    83  }
    84  
    85  type noReadFrom struct {
    86  	w Writer
    87  }
    88  
    89  func (w *noReadFrom) Write(p []byte) (n int, err error) {
    90  	return w.w.Write(p)
    91  }
    92  
    93  type wantedAndErrReader struct{}
    94  
    95  func (wantedAndErrReader) Read(p []byte) (int, error) {
    96  	return len(p), errors.New("wantedAndErrReader error")
    97  }
    98  
    99  func TestCopyNEOF(t *testing.T) {
   100  	// Test that EOF behavior is the same regardless of whether
   101  	// argument to CopyN has ReadFrom.
   102  
   103  	b := new(bytes.Buffer)
   104  
   105  	n, err := CopyN(&noReadFrom{b}, strings.NewReader("foo"), 3)
   106  	if n != 3 || err != nil {
   107  		t.Errorf("CopyN(noReadFrom, foo, 3) = %d, %v; want 3, nil", n, err)
   108  	}
   109  
   110  	n, err = CopyN(&noReadFrom{b}, strings.NewReader("foo"), 4)
   111  	if n != 3 || err != EOF {
   112  		t.Errorf("CopyN(noReadFrom, foo, 4) = %d, %v; want 3, EOF", n, err)
   113  	}
   114  
   115  	n, err = CopyN(b, strings.NewReader("foo"), 3) // b has read from
   116  	if n != 3 || err != nil {
   117  		t.Errorf("CopyN(bytes.Buffer, foo, 3) = %d, %v; want 3, nil", n, err)
   118  	}
   119  
   120  	n, err = CopyN(b, strings.NewReader("foo"), 4) // b has read from
   121  	if n != 3 || err != EOF {
   122  		t.Errorf("CopyN(bytes.Buffer, foo, 4) = %d, %v; want 3, EOF", n, err)
   123  	}
   124  
   125  	n, err = CopyN(b, wantedAndErrReader{}, 5)
   126  	if n != 5 || err != nil {
   127  		t.Errorf("CopyN(bytes.Buffer, wantedAndErrReader, 5) = %d, %v; want 5, nil", n, err)
   128  	}
   129  
   130  	n, err = CopyN(&noReadFrom{b}, wantedAndErrReader{}, 5)
   131  	if n != 5 || err != nil {
   132  		t.Errorf("CopyN(noReadFrom, wantedAndErrReader, 5) = %d, %v; want 5, nil", n, err)
   133  	}
   134  }
   135  
   136  func TestReadAtLeast(t *testing.T) {
   137  	var rb bytes.Buffer
   138  	testReadAtLeast(t, &rb)
   139  }
   140  
   141  // A version of bytes.Buffer that returns n > 0, err on Read
   142  // when the input is exhausted.
   143  type dataAndErrorBuffer struct {
   144  	err error
   145  	bytes.Buffer
   146  }
   147  
   148  func (r *dataAndErrorBuffer) Read(p []byte) (n int, err error) {
   149  	n, err = r.Buffer.Read(p)
   150  	if n > 0 && r.Buffer.Len() == 0 && err == nil {
   151  		err = r.err
   152  	}
   153  	return
   154  }
   155  
   156  func TestReadAtLeastWithDataAndEOF(t *testing.T) {
   157  	var rb dataAndErrorBuffer
   158  	rb.err = EOF
   159  	testReadAtLeast(t, &rb)
   160  }
   161  
   162  func TestReadAtLeastWithDataAndError(t *testing.T) {
   163  	var rb dataAndErrorBuffer
   164  	rb.err = fmt.Errorf("fake error")
   165  	testReadAtLeast(t, &rb)
   166  }
   167  
   168  func testReadAtLeast(t *testing.T, rb ReadWriter) {
   169  	rb.Write([]byte("0123"))
   170  	buf := make([]byte, 2)
   171  	n, err := ReadAtLeast(rb, buf, 2)
   172  	if err != nil {
   173  		t.Error(err)
   174  	}
   175  	n, err = ReadAtLeast(rb, buf, 4)
   176  	if err != ErrShortBuffer {
   177  		t.Errorf("expected ErrShortBuffer got %v", err)
   178  	}
   179  	if n != 0 {
   180  		t.Errorf("expected to have read 0 bytes, got %v", n)
   181  	}
   182  	n, err = ReadAtLeast(rb, buf, 1)
   183  	if err != nil {
   184  		t.Error(err)
   185  	}
   186  	if n != 2 {
   187  		t.Errorf("expected to have read 2 bytes, got %v", n)
   188  	}
   189  	n, err = ReadAtLeast(rb, buf, 2)
   190  	if err != EOF {
   191  		t.Errorf("expected EOF, got %v", err)
   192  	}
   193  	if n != 0 {
   194  		t.Errorf("expected to have read 0 bytes, got %v", n)
   195  	}
   196  	rb.Write([]byte("4"))
   197  	n, err = ReadAtLeast(rb, buf, 2)
   198  	want := ErrUnexpectedEOF
   199  	if rb, ok := rb.(*dataAndErrorBuffer); ok && rb.err != EOF {
   200  		want = rb.err
   201  	}
   202  	if err != want {
   203  		t.Errorf("expected %v, got %v", want, err)
   204  	}
   205  	if n != 1 {
   206  		t.Errorf("expected to have read 1 bytes, got %v", n)
   207  	}
   208  }
   209  
   210  func TestTeeReader(t *testing.T) {
   211  	src := []byte("hello, world")
   212  	dst := make([]byte, len(src))
   213  	rb := bytes.NewBuffer(src)
   214  	wb := new(bytes.Buffer)
   215  	r := TeeReader(rb, wb)
   216  	if n, err := ReadFull(r, dst); err != nil || n != len(src) {
   217  		t.Fatalf("ReadFull(r, dst) = %d, %v; want %d, nil", n, err, len(src))
   218  	}
   219  	if !bytes.Equal(dst, src) {
   220  		t.Errorf("bytes read = %q want %q", dst, src)
   221  	}
   222  	if !bytes.Equal(wb.Bytes(), src) {
   223  		t.Errorf("bytes written = %q want %q", wb.Bytes(), src)
   224  	}
   225  	if n, err := r.Read(dst); n != 0 || err != EOF {
   226  		t.Errorf("r.Read at EOF = %d, %v want 0, EOF", n, err)
   227  	}
   228  	rb = bytes.NewBuffer(src)
   229  	pr, pw := Pipe()
   230  	pr.Close()
   231  	r = TeeReader(rb, pw)
   232  	if n, err := ReadFull(r, dst); n != 0 || err != ErrClosedPipe {
   233  		t.Errorf("closed tee: ReadFull(r, dst) = %d, %v; want 0, EPIPE", n, err)
   234  	}
   235  }
   236  
   237  func TestSectionReader_ReadAt(tst *testing.T) {
   238  	dat := "a long sample data, 1234567890"
   239  	tests := []struct {
   240  		data   string
   241  		off    int
   242  		n      int
   243  		bufLen int
   244  		at     int
   245  		exp    string
   246  		err    error
   247  	}{
   248  		{data: "", off: 0, n: 10, bufLen: 2, at: 0, exp: "", err: EOF},
   249  		{data: dat, off: 0, n: len(dat), bufLen: 0, at: 0, exp: "", err: nil},
   250  		{data: dat, off: len(dat), n: 1, bufLen: 1, at: 0, exp: "", err: EOF},
   251  		{data: dat, off: 0, n: len(dat) + 2, bufLen: len(dat), at: 0, exp: dat, err: nil},
   252  		{data: dat, off: 0, n: len(dat), bufLen: len(dat) / 2, at: 0, exp: dat[:len(dat)/2], err: nil},
   253  		{data: dat, off: 0, n: len(dat), bufLen: len(dat), at: 0, exp: dat, err: nil},
   254  		{data: dat, off: 0, n: len(dat), bufLen: len(dat) / 2, at: 2, exp: dat[2 : 2+len(dat)/2], err: nil},
   255  		{data: dat, off: 3, n: len(dat), bufLen: len(dat) / 2, at: 2, exp: dat[5 : 5+len(dat)/2], err: nil},
   256  		{data: dat, off: 3, n: len(dat) / 2, bufLen: len(dat)/2 - 2, at: 2, exp: dat[5 : 5+len(dat)/2-2], err: nil},
   257  		{data: dat, off: 3, n: len(dat) / 2, bufLen: len(dat)/2 + 2, at: 2, exp: dat[5 : 5+len(dat)/2-2], err: EOF},
   258  	}
   259  	for i, t := range tests {
   260  		r := strings.NewReader(t.data)
   261  		s := NewSectionReader(r, int64(t.off), int64(t.n))
   262  		buf := make([]byte, t.bufLen)
   263  		if n, err := s.ReadAt(buf, int64(t.at)); n != len(t.exp) || string(buf[:n]) != t.exp || err != t.err {
   264  			tst.Fatalf("%d: ReadAt(%d) = %q, %v; expected %q, %v", i, t.at, buf[:n], err, t.exp, t.err)
   265  		}
   266  	}
   267  }