forked from libgox/buffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer_length_prefixed_string_test.go
90 lines (69 loc) · 2.71 KB
/
buffer_length_prefixed_string_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
package buffer
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestWriteLengthPrefixedString(t *testing.T) {
b := NewBuffer(1024)
str := "hello world"
err := b.WriteLengthPrefixedString(str)
assert.NoError(t, err, "expected no error during WriteLengthPrefixedString")
assert.Equal(t, len(str)+4, b.ReadableSize(), "expected size to be string length + 4 for length prefix")
}
func TestReadLengthPrefixedString(t *testing.T) {
b := NewBuffer(1024)
str := "hello world"
// Write the string
err := b.WriteLengthPrefixedString(str)
assert.NoError(t, err, "expected no error during WriteLengthPrefixedString")
// Reset read cursor for reading
b.readCursor = 0
// Read the string
readStr, err := b.ReadLengthPrefixedString()
assert.NoError(t, err, "expected no error during ReadLengthPrefixedString")
assert.Equal(t, str, readStr, "expected read string to match written string")
}
func TestWriteAndReadLengthPrefixedString(t *testing.T) {
b := NewBuffer(1024)
str := "test string"
// Write the string
err := b.WriteLengthPrefixedString(str)
assert.NoError(t, err, "expected no error during WriteLengthPrefixedString")
// Reset read cursor for reading
b.readCursor = 0
// Read the string
readStr, err := b.ReadLengthPrefixedString()
assert.NoError(t, err, "expected no error during ReadLengthPrefixedString")
assert.Equal(t, str, readStr, "expected read string to match written string")
}
func TestWriteLengthPrefixedStringLe(t *testing.T) {
b := NewBuffer(1024)
str := "hello world"
err := b.WriteLengthPrefixedStringLe(str)
assert.NoError(t, err, "expected no error during WriteLengthPrefixedStringLe")
assert.Equal(t, len(str)+4, b.ReadableSize(), "expected size to be string length + 4 for length prefix")
}
func TestReadLengthPrefixedStringLe(t *testing.T) {
b := NewBuffer(1024)
str := "hello world"
// Write the string
err := b.WriteLengthPrefixedStringLe(str)
assert.NoError(t, err, "expected no error during WriteLengthPrefixedStringLe")
// Read the string
readStr, err := b.ReadLengthPrefixedStringLe()
assert.NoError(t, err, "expected no error during ReadLengthPrefixedStringLe")
assert.Equal(t, str, readStr, "expected read string to match written string")
}
func TestWriteAndReadLengthPrefixedStringLe(t *testing.T) {
b := NewBuffer(1024)
str := "test string"
// Write the string
err := b.WriteLengthPrefixedStringLe(str)
assert.NoError(t, err, "expected no error during WriteLengthPrefixedStringLe")
// Reset read cursor for reading
b.readCursor = 0
// Read the string
readStr, err := b.ReadLengthPrefixedStringLe()
assert.NoError(t, err, "expected no error during ReadLengthPrefixedStringLe")
assert.Equal(t, str, readStr, "expected read string to match written string")
}