LEFT | RIGHT |
(no file at all) | |
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 "errors" |
10 "fmt" | 11 "fmt" |
11 "io" | 12 "io" |
12 "io/ioutil" | 13 "io/ioutil" |
13 "strings" | 14 "strings" |
14 "testing" | 15 "testing" |
15 "testing/iotest" | 16 "testing/iotest" |
16 "unicode/utf8" | 17 "unicode/utf8" |
17 ) | 18 ) |
18 | 19 |
19 // Reads from a reader and rot13s the result. | 20 // Reads from a reader and rot13s the result. |
(...skipping 903 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
923 io.Copy(b1, onlyReader{strings.NewReader(strings.Repeat("x", 600))}) | 924 io.Copy(b1, onlyReader{strings.NewReader(strings.Repeat("x", 600))}) |
924 if w1 != 2 { | 925 if w1 != 2 { |
925 t.Fatalf("write 1200 + 1389 'x's: got %d writes, want 2", w1) | 926 t.Fatalf("write 1200 + 1389 'x's: got %d writes, want 2", w1) |
926 } | 927 } |
927 b1.Flush() | 928 b1.Flush() |
928 if w1 != 3 { | 929 if w1 != 3 { |
929 t.Fatalf("flush 1200 + 1389 'x's: got %d writes, want 3", w1) | 930 t.Fatalf("flush 1200 + 1389 'x's: got %d writes, want 3", w1) |
930 } | 931 } |
931 } | 932 } |
932 | 933 |
| 934 type readLinesTest struct { |
| 935 in string |
| 936 want string |
| 937 wanterr string |
| 938 } |
| 939 |
| 940 var readLinesTests = []readLinesTest{ |
| 941 { |
| 942 in: "foo\nbar\n", |
| 943 want: "1[foo\n]2[bar\n]", |
| 944 }, |
| 945 { |
| 946 in: "foo\nbar\npartial", |
| 947 want: "1[foo\n]2[bar\n]3[partial]", |
| 948 }, |
| 949 { |
| 950 in: "foo\nERR:myerr\nlast\n", |
| 951 want: "1[foo\n]2[ERR:myerr\n]", |
| 952 wanterr: "myerr", |
| 953 }, |
| 954 } |
| 955 |
| 956 func TestReadLines(t *testing.T) { |
| 957 for _, tt := range readLinesTests { |
| 958 var buf bytes.Buffer |
| 959 var n = 0 |
| 960 err := ReadLines(strings.NewReader(tt.in), func(line string) err
or { |
| 961 n++ |
| 962 fmt.Fprintf(&buf, "%d[%s]", n, line) |
| 963 if strings.HasPrefix(line, "ERR:") { |
| 964 return errors.New(strings.TrimSpace(line[len("ER
R:"):])) |
| 965 } |
| 966 return nil |
| 967 }) |
| 968 if got := buf.String(); got != tt.want { |
| 969 t.Errorf("for %q got %q; want %q", tt.in, got, tt.want) |
| 970 } |
| 971 var errstr string |
| 972 if err != nil { |
| 973 errstr = err.Error() |
| 974 } |
| 975 if errstr != tt.wanterr { |
| 976 t.Errorf("for %q got error %q; want error %q", tt.in, er
rstr, tt.wanterr) |
| 977 } |
| 978 } |
| 979 } |
| 980 |
933 // A writeCountingDiscard is like ioutil.Discard and counts the number of times | 981 // A writeCountingDiscard is like ioutil.Discard and counts the number of times |
934 // Write is called on it. | 982 // Write is called on it. |
935 type writeCountingDiscard int | 983 type writeCountingDiscard int |
936 | 984 |
937 func (w *writeCountingDiscard) Write(p []byte) (int, error) { | 985 func (w *writeCountingDiscard) Write(p []byte) (int, error) { |
938 *w++ | 986 *w++ |
939 return len(p), nil | 987 return len(p), nil |
940 } | 988 } |
941 | 989 |
942 // An onlyReader only implements io.Reader, no matter what other methods the und
erlying implementation may have. | 990 // An onlyReader only implements io.Reader, no matter what other methods the und
erlying implementation may have. |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1012 | 1060 |
1013 func BenchmarkWriterCopyNoReadFrom(b *testing.B) { | 1061 func BenchmarkWriterCopyNoReadFrom(b *testing.B) { |
1014 for i := 0; i < b.N; i++ { | 1062 for i := 0; i < b.N; i++ { |
1015 b.StopTimer() | 1063 b.StopTimer() |
1016 src := onlyReader{bytes.NewBuffer(make([]byte, 8192))} | 1064 src := onlyReader{bytes.NewBuffer(make([]byte, 8192))} |
1017 dst := onlyWriter{NewWriter(new(bytes.Buffer))} | 1065 dst := onlyWriter{NewWriter(new(bytes.Buffer))} |
1018 b.StartTimer() | 1066 b.StartTimer() |
1019 io.Copy(dst, src) | 1067 io.Copy(dst, src) |
1020 } | 1068 } |
1021 } | 1069 } |
LEFT | RIGHT |