-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer_pool_test.go
More file actions
166 lines (140 loc) · 3.53 KB
/
buffer_pool_test.go
File metadata and controls
166 lines (140 loc) · 3.53 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package twig
import (
"strings"
"testing"
)
func TestBufferPool(t *testing.T) {
// Test getting a buffer from the pool
buf := GetBuffer()
if buf == nil {
t.Fatal("GetBuffer() returned nil")
}
// Test writing to the buffer
str := "Hello, world!"
n, err := buf.WriteString(str)
if err != nil {
t.Fatalf("WriteString failed: %v", err)
}
if n != len(str) {
t.Fatalf("WriteString returned wrong length: got %d, want %d", n, len(str))
}
// Test getting the string back
if buf.String() != str {
t.Fatalf("String() returned wrong value: got %q, want %q", buf.String(), str)
}
// Test resetting the buffer
buf.Reset()
if buf.Len() != 0 {
t.Fatalf("Reset() didn't clear the buffer: length = %d", buf.Len())
}
// Test writing a different string after reset
str2 := "Another string"
buf.WriteString(str2)
if buf.String() != str2 {
t.Fatalf("String() after reset returned wrong value: got %q, want %q", buf.String(), str2)
}
// Test releasing the buffer
buf.Release()
// Getting a new buffer should not have any content
buf2 := GetBuffer()
if buf2.Len() != 0 {
t.Fatalf("New buffer from pool has content: %q", buf2.String())
}
buf2.Release()
}
func TestWriteValue(t *testing.T) {
buf := GetBuffer()
defer buf.Release()
tests := []struct {
value interface{}
expected string
}{
{nil, ""},
{"test", "test"},
{123, "123"},
{-456, "-456"},
{3.14159, "3.14159"},
{true, "true"},
{false, "false"},
{[]byte("bytes"), "bytes"},
}
for _, test := range tests {
buf.Reset()
_, err := WriteValue(buf, test.value)
if err != nil {
t.Errorf("WriteValue(%v) error: %v", test.value, err)
continue
}
if buf.String() != test.expected {
t.Errorf("WriteValue(%v) = %q, want %q", test.value, buf.String(), test.expected)
}
}
}
func TestWriteInt(t *testing.T) {
buf := GetBuffer()
defer buf.Release()
tests := []struct {
value int
expected string
}{
{0, "0"},
{5, "5"},
{-5, "-5"},
{123, "123"},
{-123, "-123"},
{9999, "9999"},
{-9999, "-9999"},
{123456789, "123456789"},
{-123456789, "-123456789"},
}
for _, test := range tests {
buf.Reset()
_, err := buf.WriteInt(test.value)
if err != nil {
t.Errorf("WriteInt(%d) error: %v", test.value, err)
continue
}
if buf.String() != test.expected {
t.Errorf("WriteInt(%d) = %q, want %q", test.value, buf.String(), test.expected)
}
}
}
func TestBufferWriteTo(t *testing.T) {
buf := GetBuffer()
defer buf.Release()
testStr := "This is a test string"
buf.WriteString(testStr)
// Create a destination buffer to write to
var dest strings.Builder
n, err := buf.WriteTo(&dest)
if err != nil {
t.Fatalf("WriteTo failed: %v", err)
}
if n != int64(len(testStr)) {
t.Fatalf("WriteTo returned wrong length: got %d, want %d", n, len(testStr))
}
if dest.String() != testStr {
t.Fatalf("WriteTo output mismatch: got %q, want %q", dest.String(), testStr)
}
}
func TestBufferGrowCapacity(t *testing.T) {
buf := GetBuffer()
defer buf.Release()
// Start with small string
initialStr := "small"
buf.WriteString(initialStr)
initialCapacity := cap(buf.buf)
// Write a larger string that should cause a grow
largeStr := strings.Repeat("abcdefghijklmnopqrstuvwxyz", 100) // 2600 bytes
buf.WriteString(largeStr)
// Verify capacity increased
if cap(buf.buf) <= initialCapacity {
t.Fatalf("Buffer didn't grow capacity: initial=%d, after=%d",
initialCapacity, cap(buf.buf))
}
// Verify content is correct
expected := initialStr + largeStr
if buf.String() != expected {
t.Fatalf("Buffer content incorrect after growth")
}
}