-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreelist_test.go
118 lines (104 loc) · 2.39 KB
/
freelist_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
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
package gofl
import (
"reflect"
"testing"
)
type testType []int
func (t testType) Reset() {
for i := range t {
t[i] = 0
}
}
func TestFreeList_Get(t *testing.T) {
newFn := func() testType {
return make(testType, 4)
}
type args struct {
fl FreeList[testType]
}
tests := []struct {
name string
args args
expectedT testType
expectedFromFreeList bool
}{
{
name: "with valid new function",
args: args{fl: NewFreeList(1, newFn)},
expectedT: testType{0, 0, 0, 0},
expectedFromFreeList: false,
},
{
name: "without new function",
args: args{fl: NewFreeList[testType](1, nil)},
expectedT: nil,
expectedFromFreeList: false,
},
{
name: "with existing T on free list",
args: args{
fl: func() FreeList[testType] {
fl := NewFreeList(1, newFn)
fl.put(testType{1, 2, 3, 4})
return fl
}(),
},
expectedT: testType{0, 0, 0, 0},
expectedFromFreeList: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualT, actualFromFreeList := tt.args.fl.get()
if actualFromFreeList != tt.expectedFromFreeList {
t.Errorf("get() actualFromFreeList = %v, expectedFromFreeList %v", actualFromFreeList, tt.expectedFromFreeList)
}
if !reflect.DeepEqual(actualT, tt.expectedT) {
t.Errorf("get() actualT = %v, expectedT %v", actualT, tt.expectedT)
}
})
}
}
func TestFreeList_Put(t *testing.T) {
newFn := func() testType {
return make(testType, 4)
}
type args struct {
fl FreeList[testType]
t testType
}
tests := []struct {
name string
args args
expectedAdded bool
}{
{
name: "with no space available free list",
args: args{
fl: func() FreeList[testType] {
fl := NewFreeList(1, newFn)
fl.put(newFn())
return fl
}(),
t: testType{0, 0, 0, 0},
},
expectedAdded: false,
},
{
name: "with space available on free list",
args: args{
fl: NewFreeList(1, newFn),
t: testType{0, 0, 0, 0},
},
expectedAdded: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualAdded := tt.args.fl.put(tt.args.t)
if !reflect.DeepEqual(actualAdded, tt.expectedAdded) {
t.Errorf("Get() actualAdded = %v, expectedAdded %v", actualAdded, tt.expectedAdded)
}
})
}
}