Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
338 lines (299 loc) · 7.11 KB
/
Copy pathmain_test.go
File metadata and controls
338 lines (299 loc) · 7.11 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package main
import (
"sync"
"testing"
)
func BenchmarkSlice(b *testing.B) {
b.Run("SliceAllocateCapacity", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
SliceAllocateCapacity()
}
})
b.Run("SliceLetCapacityGrow", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
SliceLetCapacityGrow()
}
})
b.Run("NoSliceCapacitySharing", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
SliceNoShareCapacity()
}
})
b.Run("SliceCapacitySharing", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
SliceShareCapacity()
}
})
}
func TestSliceCapacityBehavior(t *testing.T) {
t.Run("NoShareCapacity", func(t *testing.T) {
original := SliceNoShareCapacity()
if len(original) != 5 {
t.Errorf("Original slice length changed: got %d, want 5", len(original))
}
if original[3] != 0 { // Now this should pass
t.Errorf("Original slice was modified: got %d at index 3, want 0", original[3])
}
})
t.Run("ShareCapacity", func(t *testing.T) {
original := SliceShareCapacity()
if original[3] != 6 {
t.Errorf("Original slice was not modified: got %d at index 3, want 6", original[3])
}
})
}
// Map operations
func BenchmarkMap(b *testing.B) {
b.Run("SingleHash", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
MapSingleHashAllocation()
}
})
b.Run("MultipleHash", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
MapMultipleRehashings()
}
})
}
// Channel operations
func BenchmarkChannel(b *testing.B) {
b.Run("Sync", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
ChannelSync()
}
})
b.Run("Async", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
ChannelAsync()
}
})
b.Run("SyncMulti", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
ChannelSyncMulti()
}
})
b.Run("AsyncMulti", func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
ChannelAsyncMulti()
}
})
}
// Test functions to verify behavior
func TestChannel(t *testing.T) {
t.Run("Sync", func(t *testing.T) {
val := ChannelSync()
if val != 1 {
t.Errorf("Expected 1, got %d", val)
}
})
t.Run("Async", func(t *testing.T) {
val := ChannelAsync()
if val != 1 {
t.Errorf("Expected 1, got %d", val)
}
})
t.Run("SyncMulti", func(t *testing.T) {
result := ChannelSyncMulti()
if len(result) != 100 {
t.Errorf("Expected length 100, got %d", len(result))
}
// Verify sequence
for i := 0; i < 100; i++ {
if result[i] != i {
t.Errorf("At index %d: expected %d, got %d", i, i, result[i])
}
}
})
t.Run("AsyncMulti", func(t *testing.T) {
result := ChannelAsyncMulti()
if len(result) != 100 {
t.Errorf("Expected length 100, got %d", len(result))
}
// Verify sequence
for i := 0; i < 100; i++ {
if result[i] != i {
t.Errorf("At index %d: expected %d, got %d", i, i, result[i])
}
}
})
}
func TestBufferPools(t *testing.T) {
t.Run("FixedPool", func(t *testing.T) {
pool := NewFixedPool(64)
// Get a buffer and verify its capacity
buf := pool.Get()
if cap(buf) != 64 {
t.Errorf("Expected capacity 64, got %d", cap(buf))
}
// Try to grow the buffer
largerData := make([]byte, 128)
grown := append(buf, largerData...)
// Put back the grown buffer
pool.Put(grown)
// Get another buffer - should get a fresh one of original size
newBuf := pool.Get()
if cap(newBuf) != 64 {
t.Errorf("Expected capacity 64 after grow attempt, got %d", cap(newBuf))
}
})
t.Run("GrowablePool", func(t *testing.T) {
pool := NewGrowablePool()
// Get a buffer and grow it
buf := pool.Get()
largerData := make([]byte, 128)
grown := append(buf, largerData...)
// Put back the grown buffer
pool.Put(grown)
// Get another buffer - might get the grown one
newBuf := pool.Get()
if cap(newBuf) < 128 {
t.Logf("Note: Got a fresh buffer instead of reusing grown one")
}
})
}
// Benchmark functions
func BenchmarkFixedPool(b *testing.B) {
pool := NewFixedPool(64)
data := make([]byte, 32) // Data smaller than buffer
b.ResetTimer()
b.ReportAllocs()
b.Run("Normal Use", func(b *testing.B) {
for n := 0; n < b.N; n++ {
buf := pool.Get()
buf = append(buf, data...)
pool.Put(buf)
}
})
b.Run("Growth Attempt", func(b *testing.B) {
largeData := make([]byte, 128) // Data larger than buffer
for n := 0; n < b.N; n++ {
buf := pool.Get()
buf = append(buf, largeData...)
pool.Put(buf) // Will discard grown buffer
}
})
}
func BenchmarkGrowablePool(b *testing.B) {
pool := NewGrowablePool()
data := make([]byte, 32)
b.ResetTimer()
b.ReportAllocs()
b.Run("Normal Use", func(b *testing.B) {
for n := 0; n < b.N; n++ {
buf := pool.Get()
buf = append(buf, data...)
pool.Put(buf)
}
})
b.Run("Growth Allowed", func(b *testing.B) {
largeData := make([]byte, 128)
for n := 0; n < b.N; n++ {
buf := pool.Get()
buf = append(buf, largeData...)
pool.Put(buf) // Will retain grown buffer
}
})
}
// Concurrent usage test
func TestConcurrentUsage(t *testing.T) {
t.Run("FixedPool", func(t *testing.T) {
pool := NewFixedPool(64)
var wg sync.WaitGroup
workers := 100
iterations := 1000
wg.Add(workers)
for i := 0; i < workers; i++ {
go func() {
defer wg.Done()
data := make([]byte, 32)
for j := 0; j < iterations; j++ {
buf := pool.Get()
buf = append(buf, data...)
pool.Put(buf)
}
}()
}
wg.Wait()
})
t.Run("GrowablePool", func(t *testing.T) {
pool := NewGrowablePool()
var wg sync.WaitGroup
workers := 100
iterations := 1000
wg.Add(workers)
for i := 0; i < workers; i++ {
go func() {
defer wg.Done()
data := make([]byte, 32)
for j := 0; j < iterations; j++ {
buf := pool.Get()
buf = append(buf, data...)
pool.Put(buf)
}
}()
}
wg.Wait()
})
}
func TestReverseStringWorks(t *testing.T) {
testString := "This is a test string"
outString := ReverseString(testString)
expected := "gnirts tset a si sihT"
if outString != expected {
t.Errorf("String has not been reversed")
}
}
func TestReverse(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"hello", "olleh"},
{"world", "dlrow"},
}
for _, tt := range tests {
got := ReverseString(tt.input)
if tt.expected != got {
t.Errorf("Strings not equal, calling function Reverse(%q)", tt.input)
}
}
}
// Property-based test
func FuzzReverse(f *testing.F) {
seeds := []string{"", "a", "hello", "12345", "!@#$%"}
for _, seed := range seeds {
f.Add(seed)
}
f.Fuzz(func(t *testing.T, input string) {
// Property 1: reversing twice should return the original string
if twice := ReverseString(ReverseString(input)); twice != input {
t.Errorf("Double reverse failed: got %q, want %q", twice, input)
}
// Property 2: byte length should be preserved
reversed := ReverseString(input)
if len(reversed) != len(input) {
t.Errorf("Length not preserved: got %d bytes, want %d bytes",
len(reversed), len(input))
}
})
}