forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_test.go
88 lines (66 loc) · 1.67 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
package alloc_test
import (
"testing"
. "v2ray.com/core/common/alloc"
"v2ray.com/core/testing/assert"
)
func TestBufferClear(t *testing.T) {
assert := assert.On(t)
buffer := NewBuffer().Clear()
defer buffer.Release()
payload := "Bytes"
buffer.Append([]byte(payload))
assert.Int(buffer.Len()).Equals(len(payload))
buffer.Clear()
assert.Int(buffer.Len()).Equals(0)
}
func TestBufferIsFull(t *testing.T) {
assert := assert.On(t)
buffer := NewBuffer()
defer buffer.Release()
assert.Bool(buffer.IsFull()).IsTrue()
buffer.Clear()
assert.Bool(buffer.IsFull()).IsFalse()
}
func TestBufferPrepend(t *testing.T) {
assert := assert.On(t)
buffer := NewBuffer().Clear()
defer buffer.Release()
buffer.Append([]byte{'a', 'b', 'c'})
buffer.Prepend([]byte{'x', 'y', 'z'})
assert.Int(buffer.Len()).Equals(6)
assert.Bytes(buffer.Value).Equals([]byte("xyzabc"))
buffer.Prepend([]byte{'u', 'v', 'w'})
assert.Bytes(buffer.Value).Equals([]byte("uvwxyzabc"))
}
func TestBufferString(t *testing.T) {
assert := assert.On(t)
buffer := NewBuffer().Clear()
defer buffer.Release()
buffer.AppendString("Test String")
assert.String(buffer.String()).Equals("Test String")
}
func BenchmarkNewBuffer8192(b *testing.B) {
for i := 0; i < b.N; i++ {
buffer := NewBuffer()
buffer.Release()
}
}
func BenchmarkNewLocalBuffer8192(b *testing.B) {
for i := 0; i < b.N; i++ {
buffer := NewLocalBuffer(8192)
buffer.Release()
}
}
func BenchmarkNewBuffer2048(b *testing.B) {
for i := 0; i < b.N; i++ {
buffer := NewSmallBuffer()
buffer.Release()
}
}
func BenchmarkNewLocalBuffer2048(b *testing.B) {
for i := 0; i < b.N; i++ {
buffer := NewLocalBuffer(2048)
buffer.Release()
}
}