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
|
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
package ioutil
import (
diff "0dev.org/diff"
"bytes"
"errors"
"io"
"testing"
)
func TestWriter(t *testing.T) {
func TestWriterFunc(t *testing.T) {
var (
input []byte = []byte{0, 1, 2, 3, 4, 5, 6, 7}
output []byte
reader *bytes.Reader = bytes.NewReader(input)
buffer bytes.Buffer
)
reader.WriteTo(WriterFunc(buffer.Write))
output = buffer.Bytes()
// Diff the result against the initial input
delta := diff.Diff(diff.D{len(input), len(output),
func(i, j int) bool { return input[i] == output[j] }})
if len(delta.Added) > 0 || len(delta.Removed) > 0 {
t.Error("Differences detected ", delta)
}
}
func TestReader(t *testing.T) {
func TestReaderFunc(t *testing.T) {
var (
input []byte = []byte{0, 1, 2, 3, 4, 5, 6, 7}
output []byte
reader *bytes.Reader = bytes.NewReader(input)
buffer bytes.Buffer
)
buffer.ReadFrom(ReaderFunc(reader.Read))
output = buffer.Bytes()
// Diff the result against the initial input
delta := diff.Diff(diff.D{len(input), len(output),
func(i, j int) bool { return input[i] == output[j] }})
if len(delta.Added) > 0 || len(delta.Removed) > 0 {
t.Error("Differences detected ", delta)
}
}
func TestSizedWriter(t *testing.T) {
var (
buffer bytes.Buffer
writer io.Writer = SizedWriter(&buffer, 4)
)
count, err := writer.Write([]byte("12"))
if count != 2 {
t.Error("Unexpected write count from SizedWriter", count)
}
if err != nil {
t.Error("Unexpected error from SizedWriter", err)
}
count, err = writer.Write([]byte("3456"))
if count != 2 {
t.Error("Unexpected write count from SizedWriter", count)
}
if err != nil {
t.Error("Unexpected error from SizedWriter", err)
}
if buffer.String() != "1234" {
t.Error("Unexpected value in wrapped writer", buffer.String())
}
// Flush the buffer
count, err = writer.Write(nil)
if count != 0 {
t.Error("Unexpected write count from SizedWriter", count)
}
if err != nil {
t.Error("Unexpected error from SizedWriter", err)
}
if buffer.String() != "123456" {
t.Error("Unexpected value in wrapped writer", buffer.String())
}
count, err = writer.Write([]byte("7890"))
if count != 4 {
t.Error("Unexpected write count from SizedWriter", count)
}
if err != nil {
t.Error("Unexpected error from SizedWriter", err)
}
if buffer.String() != "1234567890" {
t.Error("Unexpected value in wrapped writer", buffer.String())
}
}
func TestSizedWriterError(t *testing.T) {
var (
errorWriter io.Writer = WriterFunc(func([]byte) (int, error) {
return 1, errors.New("Invalid write")
})
writer io.Writer = SizedWriter(errorWriter, 2)
)
count, err := writer.Write([]byte("1"))
if count != 1 {
t.Error("Unexpected write count from SizedWriter", count)
}
if err != nil {
t.Error("Unexpected error from SizedWriter", err)
}
count, err = writer.Write([]byte("2"))
if count != 1 {
t.Error("Unexpected write count from SizedWriter", count)
}
if err == nil {
t.Error("Unexpected lack of error from SizedWriter")
}
}
func TestBlockReader(t *testing.T) {
func TestSizedReader(t *testing.T) {
var (
input []byte = []byte{0, 1, 2, 3, 4, 5, 6, 7}
output []byte = make([]byte, 16)
reader *bytes.Reader = bytes.NewReader(input)
min io.Reader = SizedReader(reader, 4)
)
// Expecting a read count of 2
count, err := min.Read(output[:2])
if count != 2 {
t.Error("Invalid read count from MinReader", count)
t.Error("Invalid read count from SizedReader", count)
}
if err != nil {
t.Error("Unexpected error from MinReader", err)
t.Error("Unexpected error from SizedReader", err)
}
// Expecting a read count of 2 as it should have 2 bytes in its buffer
count, err = min.Read(output[:3])
if count != 2 {
t.Error("Invalid read count from MinReader", count)
t.Error("Invalid read count from SizedReader", count)
}
if err != nil {
t.Error("Unexpected error from MinReader", err)
t.Error("Unexpected error from SizedReader", err)
}
// Expecting a read count of 4 as the buffer should be empty
count, err = min.Read(output[:4])
if count != 4 {
t.Error("Invalid read count from MinReader", count)
t.Error("Invalid read count from SizedReader", count)
}
if err != nil {
t.Error("Unexpected error from MinReader", err)
t.Error("Unexpected error from SizedReader", err)
}
// Expecting a read count of 0 with an EOF as the buffer should be empty
count, err = min.Read(output[:1])
if count != 0 {
t.Error("Invalid read count from MinReader", count)
t.Error("Invalid read count from SizedReader", count)
}
if err != io.EOF {
t.Error("Unexpected error from MinReader", err)
t.Error("Unexpected error from SizedReader", err)
}
}
|