Left: | ||
Right: |
OLD | NEW |
---|---|
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package bufio_test | 5 package bufio_test |
6 | 6 |
7 import ( | 7 import ( |
8 . "bufio" | 8 . "bufio" |
9 "bytes" | 9 "bytes" |
10 "fmt" | 10 "fmt" |
(...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
756 t.Errorf("%q call %d, isPrefix == %v, want %v", input, i , isPrefix, e.isPrefix) | 756 t.Errorf("%q call %d, isPrefix == %v, want %v", input, i , isPrefix, e.isPrefix) |
757 return | 757 return |
758 } | 758 } |
759 if err != e.err { | 759 if err != e.err { |
760 t.Errorf("%q call %d, err == %v, want %v", input, i, err , e.err) | 760 t.Errorf("%q call %d, err == %v, want %v", input, i, err , e.err) |
761 return | 761 return |
762 } | 762 } |
763 } | 763 } |
764 } | 764 } |
765 | 765 |
766 func TestReaderWriteTo(t *testing.T) { | 766 func createTestInput(n int) []byte { |
767 » input := make([]byte, 8192) | 767 » input := make([]byte, n) |
768 for i := range input { | 768 for i := range input { |
769 // 101 and 251 are arbitrary prime numbers. | 769 // 101 and 251 are arbitrary prime numbers. |
770 // The idea is to create an input sequence | 770 // The idea is to create an input sequence |
771 // which doesn't repeat too frequently. | 771 // which doesn't repeat too frequently. |
772 input[i] = byte(i % 251) | 772 input[i] = byte(i % 251) |
773 if i%101 == 0 { | 773 if i%101 == 0 { |
774 input[i] ^= byte(i / 101) | 774 input[i] ^= byte(i / 101) |
775 } | 775 } |
776 } | 776 } |
777 » r := NewReader(bytes.NewBuffer(input)) | 777 » return input |
778 } | |
779 | |
780 func TestReaderWriteTo(t *testing.T) { | |
781 » input := createTestInput(8192) | |
782 » r := NewReader(&onlyReader{bytes.NewBuffer(input)}) | |
chaten
2012/09/28 06:15:32
I made this change to make sure we are testing the
| |
778 w := new(bytes.Buffer) | 783 w := new(bytes.Buffer) |
779 if n, err := r.WriteTo(w); err != nil || n != int64(len(input)) { | 784 if n, err := r.WriteTo(w); err != nil || n != int64(len(input)) { |
780 t.Fatalf("r.WriteTo(w) = %d, %v, want %d, nil", n, err, len(inpu t)) | 785 t.Fatalf("r.WriteTo(w) = %d, %v, want %d, nil", n, err, len(inpu t)) |
781 } | 786 } |
782 | 787 |
783 for i, val := range w.Bytes() { | 788 for i, val := range w.Bytes() { |
784 if val != input[i] { | 789 if val != input[i] { |
785 t.Errorf("after write: out[%d] = %#x, want %#x", i, val, input[i]) | 790 t.Errorf("after write: out[%d] = %#x, want %#x", i, val, input[i]) |
786 } | 791 } |
787 } | 792 } |
(...skipping 22 matching lines...) Expand all Loading... | |
810 | 815 |
811 func TestReaderWriteToErrors(t *testing.T) { | 816 func TestReaderWriteToErrors(t *testing.T) { |
812 for i, rw := range errorWriterToTests { | 817 for i, rw := range errorWriterToTests { |
813 r := NewReader(rw) | 818 r := NewReader(rw) |
814 if _, err := r.WriteTo(rw); err != rw.expected { | 819 if _, err := r.WriteTo(rw); err != rw.expected { |
815 t.Errorf("r.WriteTo(errorWriterToTests[%d]) = _, %v, wan t _,%v", i, err, rw.expected) | 820 t.Errorf("r.WriteTo(errorWriterToTests[%d]) = _, %v, wan t _,%v", i, err, rw.expected) |
816 } | 821 } |
817 } | 822 } |
818 } | 823 } |
819 | 824 |
825 func TestWriterReadFrom(t *testing.T) { | |
826 ws := []func(io.Writer) io.Writer{ | |
827 func(w io.Writer) io.Writer { return &onlyWriter{w} }, | |
828 func(w io.Writer) io.Writer { return w }, | |
829 } | |
830 | |
831 rs := []func(io.Reader) io.Reader{ | |
832 iotest.DataErrReader, | |
833 func(r io.Reader) io.Reader { return r }, | |
834 } | |
835 | |
836 for ri, rfunc := range rs { | |
837 for wi, wfunc := range ws { | |
838 input := createTestInput(8192) | |
839 b := new(bytes.Buffer) | |
840 w := NewWriter(wfunc(b)) | |
841 r := rfunc(bytes.NewBuffer(input)) | |
842 if n, err := w.ReadFrom(r); err != nil || n != int64(len (input)) { | |
843 t.Errorf("ws[%d],rs[%d]: w.ReadFrom(r) = %d, %v, want %d, nil", wi, ri, n, err, len(input)) | |
844 continue //Don't want to spit out a large amount of errors below | |
nigeltao
2012/10/04 12:34:46
I don't think that the comment is necessary.
| |
845 } | |
846 for i, val := range b.Bytes() { | |
nigeltao
2012/10/04 12:34:46
I'd say
if got, want := b.String(), string(input);
| |
847 if val != input[i] { | |
848 t.Errorf("ws[%d], rs[%d]: after read: ou t[%d] = %#x, want %#x", wi, ri, i, val, input[i]) | |
849 } | |
850 } | |
851 | |
852 } | |
853 } | |
854 } | |
855 | |
856 type errorReaderFromTest struct { | |
857 rn, wn int | |
858 rerr, werr error | |
859 expected error | |
860 } | |
861 | |
862 func (r errorReaderFromTest) Read(p []byte) (int, error) { | |
863 return len(p) * r.rn, r.rerr | |
864 } | |
865 | |
866 func (w errorReaderFromTest) Write(p []byte) (int, error) { | |
867 return len(p) * w.wn, w.werr | |
868 } | |
869 | |
870 var errorReaderFromTests = []errorReaderFromTest{ | |
871 {0, 1, io.EOF, nil, nil}, | |
872 {1, 1, io.EOF, nil, nil}, | |
873 {0, 1, io.ErrClosedPipe, nil, io.ErrClosedPipe}, | |
874 {0, 0, io.ErrClosedPipe, io.ErrShortWrite, io.ErrClosedPipe}, | |
875 {1, 0, nil, io.ErrShortWrite, io.ErrShortWrite}, | |
876 } | |
877 | |
878 func TestWriterReadFromErrors(t *testing.T) { | |
879 for i, rw := range errorReaderFromTests { | |
880 w := NewWriter(rw) | |
881 if _, err := w.ReadFrom(rw); err != rw.expected { | |
882 t.Errorf("w.ReadFrom(errorReaderFromTests[%d]) = _, %v, want _,%v", i, err, rw.expected) | |
883 } | |
884 } | |
885 } | |
886 | |
820 // An onlyReader only implements io.Reader, no matter what other methods the und erlying implementation may have. | 887 // An onlyReader only implements io.Reader, no matter what other methods the und erlying implementation may have. |
821 type onlyReader struct { | 888 type onlyReader struct { |
822 r io.Reader | 889 r io.Reader |
823 } | 890 } |
824 | 891 |
825 func (r *onlyReader) Read(b []byte) (int, error) { | 892 func (r *onlyReader) Read(b []byte) (int, error) { |
826 return r.r.Read(b) | 893 return r.r.Read(b) |
827 } | 894 } |
828 | 895 |
829 // An onlyWriter only implements io.Writer, no matter what other methods the und erlying implementation may have. | 896 // An onlyWriter only implements io.Writer, no matter what other methods the und erlying implementation may have. |
(...skipping 29 matching lines...) Expand all Loading... | |
859 | 926 |
860 func BenchmarkReaderCopyNoWriteTo(b *testing.B) { | 927 func BenchmarkReaderCopyNoWriteTo(b *testing.B) { |
861 for i := 0; i < b.N; i++ { | 928 for i := 0; i < b.N; i++ { |
862 b.StopTimer() | 929 b.StopTimer() |
863 src := &onlyReader{NewReader(bytes.NewBuffer(make([]byte, 8192)) )} | 930 src := &onlyReader{NewReader(bytes.NewBuffer(make([]byte, 8192)) )} |
864 dst := &onlyWriter{new(bytes.Buffer)} | 931 dst := &onlyWriter{new(bytes.Buffer)} |
865 b.StartTimer() | 932 b.StartTimer() |
866 io.Copy(dst, src) | 933 io.Copy(dst, src) |
867 } | 934 } |
868 } | 935 } |
936 | |
937 func BenchmarkWriterCopyOptimal(b *testing.B) { | |
938 // Optimal case is where the underlying writer implements io.ReaderFrom | |
939 for i := 0; i < b.N; i++ { | |
940 b.StopTimer() | |
941 src := &onlyReader{bytes.NewBuffer(make([]byte, 8192))} | |
942 dst := NewWriter(new(bytes.Buffer)) | |
943 b.StartTimer() | |
944 io.Copy(dst, src) | |
945 } | |
946 } | |
947 | |
948 func BenchmarkWriterCopyUnoptimal(b *testing.B) { | |
949 for i := 0; i < b.N; i++ { | |
950 b.StopTimer() | |
951 src := &onlyReader{bytes.NewBuffer(make([]byte, 8192))} | |
952 dst := NewWriter(&onlyWriter{new(bytes.Buffer)}) | |
953 b.StartTimer() | |
954 io.Copy(dst, src) | |
955 } | |
956 } | |
957 | |
958 func BenchmarkWriterCopyNoReadFrom(b *testing.B) { | |
959 for i := 0; i < b.N; i++ { | |
960 b.StopTimer() | |
961 src := &onlyReader{bytes.NewBuffer(make([]byte, 8192))} | |
962 dst := &onlyWriter{NewWriter(new(bytes.Buffer))} | |
963 b.StartTimer() | |
964 io.Copy(dst, src) | |
965 } | |
966 } | |
OLD | NEW |