-
Notifications
You must be signed in to change notification settings - Fork 0
/
drain_test.go
109 lines (94 loc) · 2.33 KB
/
drain_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package drain
import (
"bufio"
"bytes"
"strconv"
"strings"
"testing"
)
func TestReaderLinesToChan(t *testing.T) {
r := strings.NewReader("a b c d e\nf g h i j\n")
c := make(chan string, 2)
ReaderToChan(r, bufio.ScanLines, c)
expectedLines := []string{"a b c d e", "f g h i j"}
for i, expected := range expectedLines {
if actual := <-c; expected != actual {
t.Errorf("Expected '%s' at index %d, got '%s'", expected, i, actual)
}
}
if l := len(c); l != 0 {
t.Errorf("Expected empty channel, got length %d", l)
}
}
func TestReaderWordsToChan(t *testing.T) {
r := strings.NewReader("a b c\nd e f \n g h i j")
c := make(chan string, 10)
ReaderToChan(r, bufio.ScanWords, c)
expectedWords := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
for i, expected := range expectedWords {
if actual := <-c; expected != actual {
t.Errorf("Expected '%s' at index %d, got '%s'", expected, i, actual)
}
}
if l := len(c); l != 0 {
t.Errorf("Expected empty channel, got length %d", l)
}
}
func TestChanToWriter(t *testing.T) {
c := make(chan string, 10)
go func() {
for _, s := range []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"} {
c <- s
}
close(c)
}()
w := new(bytes.Buffer)
ChanToWriter(c, ".", w)
expected := "a.b.c.d.e.f.g.h.i.j."
if actual := w.String(); expected != actual {
t.Errorf("Expected '%s', got '%s'", expected, actual)
}
}
func TestChanToBufioWriter(t *testing.T) {
c := make(chan string, 10)
go func() {
for _, s := range []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"} {
c <- s
}
close(c)
}()
w := new(bytes.Buffer)
bw := bufio.NewWriterSize(w, 10)
ChanToBufioWriter(c, ".", bw)
expected := "a.b.c.d.e.f.g.h.i.j."
if actual := w.String(); expected != actual {
t.Errorf("Expected '%s', got '%s'", expected, actual)
}
}
func BenchmarkChanToWriter(b *testing.B) {
c := make(chan string, 1024)
go func() {
for i := 0; i < b.N*1024; i++ {
c <- strconv.Itoa(i)
}
close(c)
}()
w := new(bytes.Buffer)
b.StartTimer()
ChanToWriter(c, ".", w)
b.StopTimer()
}
func BenchmarkChanToBufioWriter(b *testing.B) {
c := make(chan string, 1024)
go func() {
for i := 0; i < b.N*1024; i++ {
c <- strconv.Itoa(i)
}
close(c)
}()
w := new(bytes.Buffer)
bw := bufio.NewWriterSize(w, 64*1024)
b.StartTimer()
ChanToBufioWriter(c, ".", bw)
b.StopTimer()
}