-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathslicewriteseek_test.go
142 lines (135 loc) · 3.05 KB
/
slicewriteseek_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
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
package slicewriteseek
import (
"io"
"testing"
)
func TestWrite(t *testing.T) {
s := New()
if _, err := s.Write([]byte{1, 2, 4}); err != nil {
t.Error(err)
}
}
func TestWriteMany(t *testing.T) {
s := New()
if _, err := s.Write([]byte{1, 2, 3}); err != nil {
t.Error(err)
}
if _, err := s.Write([]byte{4, 5, 6}); err != nil {
t.Error(err)
}
if s.Len() != 6 {
t.Errorf("Expecting Len == 6, got %d", s.Len())
}
}
func TestLen(t *testing.T) {
s := New()
if _, err := s.Write([]byte{1, 2, 4}); err != nil {
t.Error(err)
}
if s.Len() != 3 {
t.Errorf("Expecting Len == 3, got %d", s.Len())
}
}
func TestSeek(t *testing.T) {
s := New()
if off, err := s.Seek(0, io.SeekEnd); err != nil || off != 0 {
t.Error("Expecting offset to be zero at the end of an empty slice")
}
if _, err := s.Write([]byte{1, 2, 4}); err != nil {
t.Error(err)
}
if off, err := s.Seek(0, io.SeekCurrent); err != nil || off != 3 {
t.Error("Unexpected seek")
}
if off, err := s.Seek(0, io.SeekStart); err != nil || off != 0 {
t.Error("Unexpected seek")
}
if s.Buffer[s.Index] != 1 {
t.Errorf("Expecting first item to be 1, got %v", s.Buffer[s.Index])
}
if off, err := s.Seek(0, io.SeekEnd); err != nil || off != 3 {
t.Error("Unexpected seek")
}
if s.Buffer[s.Index-1] != 4 {
t.Errorf("Expecting last item to be 4, got %v", s.Buffer[s.Index-1])
}
s.Index = 1
if off, err := s.Seek(0, io.SeekCurrent); err != nil || off != 1 {
t.Error("Unexpected seek")
}
if s.Buffer[s.Index] != 2 {
t.Errorf("Expecting item to be 2, got %v", s.Buffer[s.Index])
}
}
func TestRead(t *testing.T) {
s := New()
if _, err := s.Write([]byte{1, 2, 4}); err != nil {
t.Error(err)
}
if _, err := s.Seek(0, io.SeekEnd); err != nil {
t.Error(err)
}
p := make([]byte, 1)
n, err := s.Read(p)
if err != nil {
t.Error(err)
}
if n != 0 {
t.Errorf("Expecting to get back an empty slice, got %d", n)
}
if _, err = s.Seek(0, io.SeekStart); err != nil {
t.Error(err)
}
p = make([]byte, 2)
n, err = s.Read(p)
if err != nil {
t.Error(err)
}
if n != 2 {
t.Errorf("Expecting to get back an len 2 slice, got %d", n)
}
if _, err = s.Seek(0, io.SeekStart); err != nil {
t.Error(err)
}
p = make([]byte, 4)
n, err = s.Read(p)
if err != nil {
t.Error(err)
}
if n != 3 {
t.Errorf("Expecting to get back an len 3 slice, got: %d", n)
}
}
func TestWriteAt(t *testing.T) {
s := New()
if _, err := s.Write([]byte{1, 2, 4}); err != nil {
t.Error(err)
}
if _, err := s.Seek(1, io.SeekStart); err != nil {
t.Error(err)
}
s.Write([]byte{5})
if s.Len() != 3 {
t.Errorf("Expecting Len == 3, got %d", s.Len())
}
if s.Buffer[1] != 5 {
t.Errorf("Expecting 5, got %v", s.Buffer)
}
if _, err := s.Seek(0, io.SeekEnd); err != nil {
t.Error(err)
}
s.Write([]byte{1})
if s.Len() != 4 {
t.Errorf("Expecting Len == 4, got %d", s.Len())
}
if _, err := s.Seek(1, io.SeekStart); err != nil {
t.Error(err)
}
s.Write([]byte{2, 3, 4, 5})
if s.Len() != 5 {
t.Errorf("Expecting Len == 5, got %d", s.Len())
}
if s.Buffer[4] != 5 {
t.Errorf("Expecting 5, got %v", s.Buffer[4])
}
}