-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraypool_test.go
More file actions
84 lines (67 loc) · 1.83 KB
/
arraypool_test.go
File metadata and controls
84 lines (67 loc) · 1.83 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
package arraypool
import (
"testing"
)
func TestArrayPoolBasic(t *testing.T) {
ap := NewPool[int]()
apbuffer := ap.Get()
if apbuffer == nil && len(apbuffer.Buffer) == defaultBufferCapacity {
t.Errorf("Result; ArrayPoolBuffer is nil or length does not match defaultCapacity")
}
for i := 0; i < 10; i++ {
apbuffer.Write(i)
}
if len(apbuffer.Buffer) != 10 {
t.Errorf("Result; Expected Buffer Length %d actually got %d", 10, len(apbuffer.Buffer))
}
apbuffer.ClearAll()
if len(apbuffer.Buffer) != 10 {
t.Errorf("Result; Expected Buffer Length %d actually got %d", 10, len(apbuffer.Buffer))
}
ap.Put(apbuffer, true)
if len(apbuffer.Buffer) != 0 {
t.Errorf("Result; Expected Buffer Resize to 0 got %d", len(apbuffer.Buffer))
}
t.Logf("Len: %d, Cap: %d", len(apbuffer.Buffer), cap(apbuffer.Buffer))
}
func TestArrayPoolGet(t *testing.T) {
ap := NewPool[int]()
apbuffer := ap.Get()
if apbuffer == nil {
t.Error("Result; ArrayPoolBuffer is nil")
}
apbuffer.Write(4)
ap.Put(apbuffer, false)
apbuffer2 := ap.Get()
if len(apbuffer.Buffer) != len(apbuffer2.Buffer) {
t.Errorf("Result; Mismatch Lengths Expected Buffers of Equal Size")
}
if apbuffer.Buffer[0] != apbuffer2.Buffer[0] {
t.Errorf("Result; Mismatch Values %d vs %d", apbuffer.Buffer[0], apbuffer2.Buffer[0])
}
}
// benchmark against regular buffers
var ap *ArrayPool[int] = NewPool[int]()
func ArrayPoolBenchTest() {
apBuffer := ap.Get()
for i := 0; i < defaultBufferCapacity+2; i++ {
apBuffer.Write(i)
}
ap.Put(apBuffer, true)
}
func SliceBenchTest() {
sl := make([]int, 0, defaultBufferCapacity)
for i := 0; i < defaultBufferCapacity+2; i++ {
sl = append(sl, i)
}
}
func BenchmarkArrayPool(b *testing.B) {
for i := 0; i < b.N; i++ {
ArrayPoolBenchTest()
}
}
func BenchmarkSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
SliceBenchTest()
}
}