forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_test.go
108 lines (86 loc) · 1.96 KB
/
buffer_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
package protocol
import (
"bytes"
"io"
"io/ioutil"
"testing"
)
func TestPageBufferWriteReadSeek(t *testing.T) {
buffer := newPageBuffer()
defer buffer.unref()
io.WriteString(buffer, "Hello World!")
if n := buffer.Size(); n != 12 {
t.Fatal("invalid size:", n)
}
for i := 0; i < 3; i++ {
if n := buffer.Len(); n != 12 {
t.Fatal("invalid length before read:", n)
}
b, err := ioutil.ReadAll(buffer)
if err != nil {
t.Fatal(err)
}
if n := buffer.Len(); n != 0 {
t.Fatal("invalid length after read:", n)
}
if string(b) != "Hello World!" {
t.Fatalf("invalid content after read #%d: %q", i, b)
}
offset, err := buffer.Seek(0, io.SeekStart)
if err != nil {
t.Fatal(err)
}
if offset != 0 {
t.Fatalf("invalid offset after seek #%d: %d", i, offset)
}
}
}
func TestPageRefWriteReadSeek(t *testing.T) {
buffer := newPageBuffer()
defer buffer.unref()
io.WriteString(buffer, "Hello World!")
ref := buffer.ref(1, 11)
defer ref.unref()
if n := ref.Size(); n != 10 {
t.Fatal("invalid size:", n)
}
for i := 0; i < 3; i++ {
if n := ref.Len(); n != 10 {
t.Fatal("invalid length before read:", n)
}
b, err := ioutil.ReadAll(ref)
if err != nil {
t.Fatal(err)
}
if n := ref.Len(); n != 0 {
t.Fatal("invalid length after read:", n)
}
if string(b) != "ello World" {
t.Fatalf("invalid content after read #%d: %q", i, b)
}
offset, err := ref.Seek(0, io.SeekStart)
if err != nil {
t.Fatal(err)
}
if offset != 0 {
t.Fatalf("invalid offset after seek #%d: %d", i, offset)
}
}
}
func TestPageRefReadByte(t *testing.T) {
buffer := newPageBuffer()
defer buffer.unref()
content := bytes.Repeat([]byte("1234567890"), 10e3)
buffer.Write(content)
ref := buffer.ref(0, buffer.Size())
defer ref.unref()
for i, c := range content {
b, err := ref.ReadByte()
if err != nil {
t.Fatal(err)
}
if b != c {
t.Fatalf("byte at offset %d mismatch, expected '%c' but got '%c'", i, c, b)
}
}
}