-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrw_test.go
69 lines (64 loc) · 1.52 KB
/
rw_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package bites
import (
"io"
"testing"
)
func slowCopy(w io.Writer, r io.Reader) (int, error) {
buf := make([]byte, 2)
tot := 0
for {
n, err := r.Read(buf)
if err == io.EOF {
return tot, nil
}
tot += n
w.Write(buf[:n])
}
}
func byteCopy(w io.ByteWriter, r io.ByteReader) (int, error) {
tot := 0
for {
c, err := r.ReadByte()
if err == io.EOF {
return tot, nil
}
tot++
w.WriteByte(c)
}
}
func TestReaderWriterCopy(t *testing.T) {
b1 := New().PutString("Hello, world!").Get()
b2 := New().PutString("PFX ")
r := b1.NewReader()
w := b2.NewWriter()
n, err := slowCopy(w, r)
b2 = w.Bites()
if n != len("Hello, world!") {
t.Fatalf("FAIL! Wrong copy length %d", n)
}
if n != r.Total() || n != w.Total() {
t.Fatalf("FAIL! Totals don't match. Expected %d, got reader(%d) and writer(%d)", n, r.Total(), w.Total())
}
if err != nil {
t.Fatalf("FAIL! Unexpected error %v", err)
}
if b2.String() != "PFX Hello, world!" {
t.Fatalf("FAIL! Wrong string after copy append: '%s'", b2.String())
}
r = b2.Get().NewReader()
w = b1.Put().NewWriter()
n, err = byteCopy(w, r)
b := w.Bites()
if n != len("PFX Hello, world!") {
t.Fatalf("FAIL! Wrong copy length %d", n)
}
if n != r.Total() || n != w.Total() {
t.Fatalf("FAIL! Totals don't match. Expected %d, got reader(%d) and writer(%d)", n, r.Total(), w.Total())
}
if err != nil {
t.Fatalf("FAIL! Unexpected error %v", err)
}
if b.String() != "Hello, world!PFX Hello, world!" {
t.Fatalf("FAIL! Wrong string after copy append: '%s'", b1.String())
}
}