-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_test.go
184 lines (142 loc) · 3.63 KB
/
array_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
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
package native
import (
"reflect"
"testing"
)
func TestNewArray(t *testing.T) {
array := NewArray(4, 16)
defer array.Free()
}
func TestNewArrayExt(t *testing.T) {
var (
dim = []uint{16}
itemSize uint = 4
)
pool := NewPool(func(pool IPool) interface{} {
return NewArrayExt(pool, itemSize, dim...)
})
defer pool.FreeData()
array := NewArrayExt(pool, itemSize, dim...)
array.Free()
}
func TestArray_Data(t *testing.T) {
var (
dim = []uint{16, 37}
itemSize uint = 4
)
array := NewArray(itemSize)
defer array.Free()
if array.Pointer() == nil {
t.Error("failed to allocate data")
}
if array.IsEmpty() {
t.Error("failed to check allocated data")
}
if existDim := array.Dim(); reflect.DeepEqual(existDim, dim) {
t.Error("failed to get dimention of allocated data. Got\n", existDim, "\n but expected is\n", dim)
}
if existItemSize := array.ItemSize(); existItemSize != itemSize {
t.Error("failed to get item size of allocated data. Got ", existItemSize, ", but expected is ", itemSize)
}
}
func sliceTestArray(array *Array) []int8 {
sz := array.Size() * array.itemSize
return (*[1 << 30]int8)(array.Pointer())[:sz:sz]
}
func generateTestDataArray(slice []int8) []int8 {
for i := range slice {
slice[i] = int8(int32(i+3) % 256)
}
return slice
}
func sumTestArray(slice []int8) int32 {
var (
sum int32 = 0
)
for _, v := range slice {
sum += int32(v)
}
return sum
}
func TestArray_Clear(t *testing.T) {
var (
expectedSum = []int32{3, 7, 18, 168, 738, 2208, -256}
len = uint(1)
)
for i, itemSize := range []uint{1, 2, 4, 16, 36, 64, 512} {
array := NewArray(itemSize, len)
slice := sliceTestArray(array)
sum := sumTestArray(generateTestDataArray(slice))
if sum != expectedSum[i] {
t.Error("failed to set some values. Sum is", sum, ", but expected is", expectedSum[i])
}
array.ClearData()
if sum = sumTestArray(slice); sum != 0 {
t.Error("failed to clear data. Sum is", sum)
}
if sum = sumTestArray(sliceTestArray(array)); sum != 0 {
t.Error("failed to clear data (with recreated slice). Sum is", sum)
}
array.Free()
}
}
func TestArray_Free(t *testing.T) {
var (
dim = []uint{16, 37, 2}
itemSize uint = 4
)
array := NewArray(itemSize, dim...)
array.Free()
array.Free()
if array.Pointer() != nil {
t.Error("failed to free data")
}
if !array.IsEmpty() {
t.Error("failed to check empty data")
}
if array.Size() != 0 {
t.Error("failed to clear length while freed data")
}
if array.ItemSize() != 0 {
t.Error("failed to clear item size while freed data")
}
// Pooling
pool := NewPool(func(pool IPool) interface{} {
return NewArrayExt(pool, itemSize, dim...)
})
array = NewArrayExt(pool, itemSize, dim...)
array.Free()
if array.Pointer() == nil {
t.Error("data shouldn't free for pooling")
}
if array.IsEmpty() {
t.Error("data shouldn't empty for pooling")
}
if array.Size() == 0 {
t.Error("data size shouldn't clear for pooling")
}
if array.ItemSize() == 0 {
t.Error("data item size shouldn't clear for pooling")
}
}
func TestArray_WithPool(t *testing.T) {
var (
dim = []uint{16}
itemSize uint = 4
)
pool := NewPool(func(pool IPool) interface{} {
return NewArrayExt(pool, itemSize, dim...)
})
defer pool.FreeData()
array := pool.Get().(*Array)
if sum := sumTestArray(generateTestDataArray(sliceTestArray(array))); sum == 0 {
t.Error("failed to generate data. Got ", sum)
}
array.Free()
if array.IsEmpty() {
t.Error("data isn't needing to free for arraypool version")
}
if sum := sumTestArray(sliceTestArray(array)); sum != 0 {
t.Error("data is empty, but should empty")
}
}