-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbytes_pool_test.go
More file actions
101 lines (87 loc) · 2.19 KB
/
bytes_pool_test.go
File metadata and controls
101 lines (87 loc) · 2.19 KB
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
package pulse
import (
"testing"
)
func TestGetBytes(t *testing.T) {
tests := []struct {
name string
size int
expected int
}{
{"small size", 100, 1024}, // 1KB
{"medium size", 2000, 2048}, // 2KB
{"large size", 5000, 5120}, // 5KB
{"very large size", 1024 * 1024, 1024 * 1024}, // 1MB
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := getBytes(tt.size)
if buf == nil {
t.Fatal("getBytes returned nil")
}
if cap(*buf) < tt.expected {
t.Errorf("getBytes(%d) = %d, want >= %d", tt.size, cap(*buf), tt.expected)
}
putBytes(buf)
})
}
}
func TestPutBytes(t *testing.T) {
// Test putting back a small buffer
smallBuf := getBytes(100)
putBytes(smallBuf)
// Test putting back a large buffer
largeBuf := getBytes(1024 * 1024)
putBytes(largeBuf)
// Test putting back a nil buffer
putBytes(nil)
// Test putting back an empty buffer
emptyBuf := &[]byte{}
putBytes(emptyBuf)
}
func TestGetBigBytes(t *testing.T) {
tests := []struct {
name string
size int
expected int
}{
{"512KB", 512 * 1024, 512 * 1024},
{"1MB", 1024 * 1024, 1024 * 1024},
{"2MB", 2 * 1024 * 1024, 2 * 1024 * 1024},
{"32MB", 32 * 1024 * 1024, 32 * 1024 * 1024},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := getBigBytes(tt.size)
if buf == nil {
t.Fatal("getBigBytes returned nil")
}
if cap(*buf) < tt.expected {
t.Errorf("getBigBytes(%d) = %d, want >= %d", tt.size, cap(*buf), tt.expected)
}
putBigBytes(buf)
})
}
}
func TestPutBigBytes(t *testing.T) {
// Test putting back a big buffer
bigBuf := getBigBytes(1024 * 1024)
putBigBytes(bigBuf)
// Test putting back a nil buffer
putBigBytes(nil)
// Test putting back an empty buffer
emptyBuf := &[]byte{}
putBigBytes(emptyBuf)
}
func TestBytesPoolReuse(t *testing.T) {
// Test that we can reuse the same buffer multiple times
buf1 := getBytes(100)
putBytes(buf1)
buf2 := getBytes(100)
putBytes(buf2)
// Test that we can reuse the same big buffer multiple times
bigBuf1 := getBigBytes(1024 * 1024)
putBigBytes(bigBuf1)
bigBuf2 := getBigBytes(1024 * 1024)
putBigBytes(bigBuf2)
}