This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathbytecounter_test.go
112 lines (95 loc) · 2.84 KB
/
bytecounter_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
110
111
112
package main
import (
"bytes"
"io"
"testing"
)
func TestInitializationOfByteCountingReader(t *testing.T) {
byteReader := bytes.NewReader(make([]byte, 6))
bcr := byteCountingReader{
R: byteReader,
}
if bcr.bytesRead != 0 {
t.Fatalf("Expected an initialization value of 0 of the byteCountReader, but got: %v",
bcr.bytesRead)
}
if bcr.R != byteReader {
t.Fatal("Initialization of ByteCountingReader struct failure")
}
}
func TestMultipleReadsAccumlateBytesCorrectly(t *testing.T) {
// Create a reader, wrap it with a ByteCountingReader,
// read some stuff, and verify that the number of bytes
// read the number of bytes that should have been read.
sixBytes := []byte{'a', 'b', 'c', 'd', 'e', 'f'}
byteReader := bytes.NewReader(sixBytes)
oneByte := make([]byte, 1)
bcr := byteCountingReader{
R: byteReader,
}
for read, err := bcr.Read(oneByte); read > 0; {
if err != nil {
t.Fatalf("Expected no error, but got: %v", err)
}
if read != 1 {
t.Fatalf("Expected to read 1 byte, but instead read %d byte[s].", read)
}
read, err = bcr.Read(oneByte)
}
expectedByteCount := int64(6)
if bcr.bytesRead != expectedByteCount {
t.Fatalf("The byte counting reader should have accumulated %d bytes, but accumulated %d",
expectedByteCount, bcr.bytesRead)
}
}
func TestSingleReadAccumulatesBytesCorrectly(t *testing.T) {
sixBytes := []byte{'a', 'b', 'c', 'd', 'e', 'f'}
byteReader := bytes.NewReader(sixBytes)
tenBytes := make([]byte, 10)
bcr := byteCountingReader{
R: byteReader,
}
read, err := bcr.Read(tenBytes)
if err != nil && io.EOF != err {
t.Fatalf("Expected that if error is not nil, then it is of type io.EOF. Here it was of type %T", err)
}
if read != 6 {
t.Fatalf("Expected to have read 6 bytes, but read %d byte[s]", read)
}
if bcr.bytesRead != 6 {
t.Fatalf("Expected to have accumulated 6 bytes, but accumulated %d byte[s]",
bcr.bytesRead)
}
}
func TestByteCountingReaderCanAccumulateFromDifferentReaders(t *testing.T) {
sixBytes, eightBytes := make([]byte, 6), make([]byte, 8)
sixByteReader, eightByteReader := bytes.NewReader(sixBytes), bytes.NewReader(eightBytes)
buffer := make([]byte, 3)
bcr := byteCountingReader{
R: sixByteReader,
}
read := 1
for read > 0 {
read, _ = bcr.Read(buffer)
}
bcr.R = eightByteReader
read = 1
for read > 0 {
read, _ = bcr.Read(buffer)
}
expectedAccumulatedBytes := int64(14)
if expectedAccumulatedBytes != bcr.bytesRead {
t.Fatalf("Expected ByteCountingReader to have read %d byte[s], but read %d byte[s]",
expectedAccumulatedBytes, bcr.bytesRead)
}
}
func TestByteCountingReaderImplementsIoReader(t *testing.T) {
byteReader := bytes.NewReader(make([]byte, 6))
bcr := &byteCountingReader{
R: byteReader,
}
_, ok := interface{}(bcr).(io.Reader)
if !ok {
t.Fatal("Expected ByteCountingReader to implement io.Reader interface, but it did not.")
}
}