forked from xushiwei/bytes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytes_test.go
60 lines (49 loc) · 1.34 KB
/
bytes_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
package bytes
import (
"io"
"testing"
)
// ---------------------------------------------------
func TestBuffer(t *testing.T) {
b := NewBuffer()
n, err := b.WriteStringAt("Hello", 4)
if n != 5 || err != nil {
t.Fatal("WriteStringAt failed:", n, err)
}
if b.Len() != 9 {
t.Fatal("Buffer.Len invalid (9 is required):", b.Len())
}
buf := make([]byte, 10)
n, err = b.ReadAt(buf, 50)
if n != 0 || err != io.EOF {
t.Fatal("ReadAt failed:", n, err)
}
n, err = b.ReadAt(buf, 6)
if n != 3 || err != io.EOF || string(buf[:n]) != "llo" {
t.Fatal("ReadAt failed:", n, err, string(buf[:n]))
}
n, err = b.WriteAt([]byte("Hi h"), 1)
if n != 4 || err != nil {
t.Fatal("WriteAt failed:", n, err)
}
if b.Len() != 9 {
t.Fatal("Buffer.Len invalid (9 is required):", b.Len())
}
n, err = b.ReadAt(buf, 0)
if n != 9 || err != io.EOF || string(buf[:n]) != "\x00Hi hello" {
t.Fatal("ReadAt failed:", n, err)
}
n, err = b.WriteStringAt("LO world!", 7)
if n != 9 || err != nil {
t.Fatal("WriteStringAt failed:", n, err)
}
if b.Len() != 16 {
t.Fatal("Buffer.Len invalid (16 is required):", b.Len())
}
buf = make([]byte, 17)
n, err = b.ReadAt(buf, 0)
if n != 16 || err != io.EOF || string(buf[:n]) != "\x00Hi helLO world!" {
t.Fatal("ReadAt failed:", n, err, string(buf[:n]))
}
}
// ---------------------------------------------------