-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseekable_buffer_test.go
More file actions
108 lines (101 loc) · 2.64 KB
/
seekable_buffer_test.go
File metadata and controls
108 lines (101 loc) · 2.64 KB
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
package byte_utils
import (
"io"
"testing"
)
func TestSeekableBuffer(t *testing.T) {
b := NewSeekableBuffer()
tmp := make([]byte, 10)
_, e := b.Read(tmp)
if e == nil {
t.Logf("Didn't get expected error for reading empty buffer.\n")
t.Fail()
} else {
t.Logf("Got expected error for reading empty buffer: %s\n", e)
}
// Test that seeking past the buffer end expands the buffer without an
// error.
offset, e := b.Seek(10, io.SeekCurrent)
if e != nil {
t.Logf("Error seeking to offset 10: %s\n", e)
t.FailNow()
}
if offset != 10 {
t.Logf("Expected an offset of 10 after seeking: %s\n", e)
t.FailNow()
}
// Test writing 10 bytes of data, expanding the data.
for i := range tmp {
tmp[i] = byte(i)
}
n, e := b.Write(tmp)
if e != nil {
t.Logf("Error writing data to buffer: %s\n", e)
t.FailNow()
}
if n != len(tmp) {
t.Logf("Wrote %d bytes, expected to write %d.\n", n, len(tmp))
t.Fail()
}
// Make sure that:
// - The early part of the buffer is still 0s.
// - The data was written starting at the correct offset.
_, e = b.Seek(8, io.SeekStart)
if e != nil {
t.Logf("Failed seeking to offset 5: %s\n", e)
t.FailNow()
}
_, e = b.Read(tmp[0:5])
if e != nil {
t.Logf("Failed reading 5 bytes at offset 5: %s\n", e)
t.FailNow()
}
expectedData := []byte{0, 0, 0, 1, 2}
for i := range expectedData {
if tmp[i] != expectedData[i] {
t.Logf("Didn't get expected read contents: %v vs %v\n",
tmp, expectedData)
t.FailNow()
}
}
// Make sure that we fail setting the offset to something negative.
// (Required by the Seek interface.)
_, e = b.Seek(-12, io.SeekStart)
if e == nil {
t.Logf("Didn't get expected error when seeking to negative offset.\n")
t.FailNow()
}
t.Logf("Got expected error when seeking to negative offset: %s\n", e)
// Make sure that writing something that doesn't require expanding the
// buffer won't add to its length.
currentLength, e := b.Seek(0, io.SeekEnd)
if e != nil {
t.Logf("Failed getting length of buffer: %s\n", e)
t.FailNow()
}
if currentLength != 20 {
t.Logf("Got incorrect length of buffer. Expected 20, got %d.\n",
currentLength)
t.FailNow()
}
_, e = b.Seek(2, io.SeekStart)
if e != nil {
t.Logf("Failed setting offset in buffer to byte 2: %s\n", e)
t.FailNow()
}
_, e = b.Write([]byte{123, 211})
if e != nil {
t.Logf("Failed writing arbitrary 2 bytes to buffer: %s\n", e)
t.FailNow()
}
currentLength, e = b.Seek(0, io.SeekEnd)
if e != nil {
t.Logf("Failed getting length of buffer (2nd time): %s\n", e)
t.FailNow()
}
if currentLength != 20 {
t.Logf("Buffer expanded to %d bytes when it shouldn't have.\n",
currentLength)
t.FailNow()
}
}