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 io_test | 5 package io_test |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 . "io" | 9 . "io" |
10 "os" | 10 "os" |
11 "strings" | 11 "strings" |
12 "testing" | 12 "testing" |
13 ) | 13 ) |
14 | 14 |
15 // An version of bytes.Buffer without ReadFrom and WriteTo | 15 // An version of bytes.Buffer without ReadFrom and WriteTo |
16 type Buffer struct { | 16 type Buffer struct { |
17 bytes.Buffer | 17 bytes.Buffer |
18 ReaderFrom // conflicts with and hides bytes.Buffer's ReaderFrom. | 18 ReaderFrom // conflicts with and hides bytes.Buffer's ReaderFrom. |
19 WriterTo // conflicts with and hides bytes.Buffer's WriterTo. | 19 WriterTo // conflicts with and hides bytes.Buffer's WriterTo. |
20 } | 20 } |
21 | 21 |
22 // Simple tests, primarily to verify the ReadFrom and WriteTo callouts inside Co
py and Copyn. | 22 // Simple tests, primarily to verify the ReadFrom and WriteTo callouts inside Co
py and CopyN. |
23 | 23 |
24 func TestCopy(t *testing.T) { | 24 func TestCopy(t *testing.T) { |
25 rb := new(Buffer) | 25 rb := new(Buffer) |
26 wb := new(Buffer) | 26 wb := new(Buffer) |
27 rb.WriteString("hello, world.") | 27 rb.WriteString("hello, world.") |
28 Copy(wb, rb) | 28 Copy(wb, rb) |
29 if wb.String() != "hello, world." { | 29 if wb.String() != "hello, world." { |
30 t.Errorf("Copy did not work properly") | 30 t.Errorf("Copy did not work properly") |
31 } | 31 } |
32 } | 32 } |
(...skipping 11 matching lines...) Expand all Loading... |
44 func TestCopyWriteTo(t *testing.T) { | 44 func TestCopyWriteTo(t *testing.T) { |
45 rb := new(bytes.Buffer) // implements WriteTo. | 45 rb := new(bytes.Buffer) // implements WriteTo. |
46 wb := new(Buffer) | 46 wb := new(Buffer) |
47 rb.WriteString("hello, world.") | 47 rb.WriteString("hello, world.") |
48 Copy(wb, rb) | 48 Copy(wb, rb) |
49 if wb.String() != "hello, world." { | 49 if wb.String() != "hello, world." { |
50 t.Errorf("Copy did not work properly") | 50 t.Errorf("Copy did not work properly") |
51 } | 51 } |
52 } | 52 } |
53 | 53 |
54 func TestCopyn(t *testing.T) { | 54 func TestCopyN(t *testing.T) { |
55 » rb := new(Buffer) | 55 » rb := new(Buffer) |
56 » wb := new(Buffer) | 56 » wb := new(Buffer) |
57 » rb.WriteString("hello, world.") | 57 » rb.WriteString("hello, world.") |
58 » Copyn(wb, rb, 5) | 58 » CopyN(wb, rb, 5) |
59 if wb.String() != "hello" { | 59 if wb.String() != "hello" { |
60 » » t.Errorf("Copyn did not work properly") | 60 » » t.Errorf("CopyN did not work properly") |
61 » } | 61 » } |
62 } | 62 } |
63 | 63 |
64 func TestCopynReadFrom(t *testing.T) { | 64 func TestCopyNReadFrom(t *testing.T) { |
65 rb := new(Buffer) | 65 rb := new(Buffer) |
66 wb := new(bytes.Buffer) // implements ReadFrom. | 66 wb := new(bytes.Buffer) // implements ReadFrom. |
67 rb.WriteString("hello") | 67 rb.WriteString("hello") |
68 » Copyn(wb, rb, 5) | 68 » CopyN(wb, rb, 5) |
69 if wb.String() != "hello" { | 69 if wb.String() != "hello" { |
70 » » t.Errorf("Copyn did not work properly") | 70 » » t.Errorf("CopyN did not work properly") |
71 » } | 71 » } |
72 } | 72 } |
73 | 73 |
74 func TestCopynWriteTo(t *testing.T) { | 74 func TestCopyNWriteTo(t *testing.T) { |
75 rb := new(bytes.Buffer) // implements WriteTo. | 75 rb := new(bytes.Buffer) // implements WriteTo. |
76 wb := new(Buffer) | 76 wb := new(Buffer) |
77 rb.WriteString("hello, world.") | 77 rb.WriteString("hello, world.") |
78 » Copyn(wb, rb, 5) | 78 » CopyN(wb, rb, 5) |
79 if wb.String() != "hello" { | 79 if wb.String() != "hello" { |
80 » » t.Errorf("Copyn did not work properly") | 80 » » t.Errorf("CopyN did not work properly") |
81 } | 81 } |
82 } | 82 } |
83 | 83 |
84 type noReadFrom struct { | 84 type noReadFrom struct { |
85 w Writer | 85 w Writer |
86 } | 86 } |
87 | 87 |
88 func (w *noReadFrom) Write(p []byte) (n int, err os.Error) { | 88 func (w *noReadFrom) Write(p []byte) (n int, err os.Error) { |
89 return w.w.Write(p) | 89 return w.w.Write(p) |
90 } | 90 } |
91 | 91 |
92 func TestCopynEOF(t *testing.T) { | 92 func TestCopyNEOF(t *testing.T) { |
93 // Test that EOF behavior is the same regardless of whether | 93 // Test that EOF behavior is the same regardless of whether |
94 » // argument to Copyn has ReadFrom. | 94 » // argument to CopyN has ReadFrom. |
95 | 95 |
96 b := new(bytes.Buffer) | 96 b := new(bytes.Buffer) |
97 | 97 |
98 » n, err := Copyn(&noReadFrom{b}, strings.NewReader("foo"), 3) | 98 » n, err := CopyN(&noReadFrom{b}, strings.NewReader("foo"), 3) |
99 if n != 3 || err != nil { | 99 if n != 3 || err != nil { |
100 » » t.Errorf("Copyn(noReadFrom, foo, 3) = %d, %v; want 3, nil", n, e
rr) | 100 » » t.Errorf("CopyN(noReadFrom, foo, 3) = %d, %v; want 3, nil", n, e
rr) |
101 » } | 101 » } |
102 | 102 |
103 » n, err = Copyn(&noReadFrom{b}, strings.NewReader("foo"), 4) | 103 » n, err = CopyN(&noReadFrom{b}, strings.NewReader("foo"), 4) |
104 if n != 3 || err != os.EOF { | 104 if n != 3 || err != os.EOF { |
105 » » t.Errorf("Copyn(noReadFrom, foo, 4) = %d, %v; want 3, EOF", n, e
rr) | 105 » » t.Errorf("CopyN(noReadFrom, foo, 4) = %d, %v; want 3, EOF", n, e
rr) |
106 » } | 106 » } |
107 | 107 |
108 » n, err = Copyn(b, strings.NewReader("foo"), 3) // b has read from | 108 » n, err = CopyN(b, strings.NewReader("foo"), 3) // b has read from |
109 if n != 3 || err != nil { | 109 if n != 3 || err != nil { |
110 » » t.Errorf("Copyn(bytes.Buffer, foo, 3) = %d, %v; want 3, nil", n,
err) | 110 » » t.Errorf("CopyN(bytes.Buffer, foo, 3) = %d, %v; want 3, nil", n,
err) |
111 » } | 111 » } |
112 | 112 |
113 » n, err = Copyn(b, strings.NewReader("foo"), 4) // b has read from | 113 » n, err = CopyN(b, strings.NewReader("foo"), 4) // b has read from |
114 if n != 3 || err != os.EOF { | 114 if n != 3 || err != os.EOF { |
115 » » t.Errorf("Copyn(bytes.Buffer, foo, 4) = %d, %v; want 3, EOF", n,
err) | 115 » » t.Errorf("CopyN(bytes.Buffer, foo, 4) = %d, %v; want 3, EOF", n,
err) |
116 } | 116 } |
117 } | 117 } |
118 | 118 |
119 func TestReadAtLeast(t *testing.T) { | 119 func TestReadAtLeast(t *testing.T) { |
120 var rb bytes.Buffer | 120 var rb bytes.Buffer |
121 testReadAtLeast(t, &rb) | 121 testReadAtLeast(t, &rb) |
122 } | 122 } |
123 | 123 |
124 // A version of bytes.Buffer that returns n > 0, os.EOF on Read | 124 // A version of bytes.Buffer that returns n > 0, os.EOF on Read |
125 // when the input is exhausted. | 125 // when the input is exhausted. |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 t.Errorf("r.Read at EOF = %d, %v want 0, EOF", n, err) | 197 t.Errorf("r.Read at EOF = %d, %v want 0, EOF", n, err) |
198 } | 198 } |
199 rb = bytes.NewBuffer(src) | 199 rb = bytes.NewBuffer(src) |
200 pr, pw := Pipe() | 200 pr, pw := Pipe() |
201 pr.Close() | 201 pr.Close() |
202 r = TeeReader(rb, pw) | 202 r = TeeReader(rb, pw) |
203 if n, err := ReadFull(r, dst); n != 0 || err != os.EPIPE { | 203 if n, err := ReadFull(r, dst); n != 0 || err != os.EPIPE { |
204 t.Errorf("closed tee: ReadFull(r, dst) = %d, %v; want 0, EPIPE",
n, err) | 204 t.Errorf("closed tee: ReadFull(r, dst) = %d, %v; want 0, EPIPE",
n, err) |
205 } | 205 } |
206 } | 206 } |
LEFT | RIGHT |