-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_test.go
352 lines (302 loc) · 10.5 KB
/
buffer_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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//go:build darwin
// +build darwin
package metal
import (
"fmt"
"math"
"math/rand"
"reflect"
"sort"
"sync"
"testing"
"github.com/stretchr/testify/require"
)
// Test_Globals tests the global variables and constants for buffers.
func Test_Globals(t *testing.T) {
t.Run("vars", func(t *testing.T) {
require.EqualError(t, ErrInvalidBufferId, "Invalid buffer Id")
})
}
// Test_BufferId_Valid tests that BufferId's Valid method correctly identifies a valid buffer Id.
func Test_BufferId_Valid(t *testing.T) {
// A valid buffer Id has a positive value. Let's run through a bunch of numbers and test that
// Valid always report the correct status.
for i := -100_00; i <= 100_000; i++ {
bufferId := BufferId(i)
if i > 0 {
require.True(t, bufferId.Valid())
} else {
require.False(t, bufferId.Valid())
}
}
}
// Test_NewBuffer_invalid tests that NewBuffer handles invalid arguments correctly.
func Test_NewBuffer_invalid(t *testing.T) {
t.Run("no width", func(t *testing.T) {
bufferId, buffer, err := NewBuffer[int32](0)
require.EqualError(t, err, "Invalid width")
require.Equal(t, BufferId(0), bufferId)
require.Nil(t, buffer)
})
t.Run("negative width", func(t *testing.T) {
bufferId, buffer, err := NewBuffer[float32](-1)
require.EqualError(t, err, "Invalid width")
require.Equal(t, BufferId(0), bufferId)
require.Nil(t, buffer)
})
t.Run("too many bytes", func(t *testing.T) {
bufferId, buffer, err := NewBuffer[float32](math.MaxInt32 + 1)
require.EqualError(t, err, "Exceeded maximum number of bytes")
require.Equal(t, BufferId(0), bufferId)
require.Nil(t, buffer)
})
}
// Test_NewBuffer tests that NewBuffer creates a new metal buffer with the expected underlying type
// and data shape.
func Test_NewBuffer(t *testing.T) {
// Test the primitive types that satisfy the BufferType constraint.
testNewBuffer(t, func(i int) byte { return byte(i) })
testNewBuffer(t, func(i int) rune { return rune(i) })
testNewBuffer(t, func(i int) uint8 { return uint8(i) })
testNewBuffer(t, func(i int) uint16 { return uint16(i) })
testNewBuffer(t, func(i int) uint32 { return uint32(i) })
testNewBuffer(t, func(i int) uint64 { return uint64(i) })
testNewBuffer(t, func(i int) int8 { return int8(-i) })
testNewBuffer(t, func(i int) int16 { return int16(-i) })
testNewBuffer(t, func(i int) int32 { return int32(-i) })
testNewBuffer(t, func(i int) int64 { return int64(-i) })
testNewBuffer(t, func(i int) float32 { return float32(i) * 1.1 })
testNewBuffer(t, func(i int) float64 { return float64(i) * 1.1 })
// Test custom types that satisfy the BufferType constraint.
type MyByte byte
testNewBuffer(t, func(i int) MyByte { return MyByte(i) })
type MyRune rune
testNewBuffer(t, func(i int) MyRune { return MyRune(i) })
type MyUint8 uint8
testNewBuffer(t, func(i int) MyUint8 { return MyUint8(i) })
type MyUint16 uint16
testNewBuffer(t, func(i int) MyUint16 { return MyUint16(i) })
type MyUint32 uint32
testNewBuffer(t, func(i int) MyUint32 { return MyUint32(i) })
type MyUint64 uint64
testNewBuffer(t, func(i int) MyUint64 { return MyUint64(i) })
type MyInt8 int8
testNewBuffer(t, func(i int) MyInt8 { return MyInt8(-i) })
type MyInt16 int16
testNewBuffer(t, func(i int) MyInt16 { return MyInt16(-i) })
type MyInt32 int32
testNewBuffer(t, func(i int) MyInt32 { return MyInt32(-i) })
type MyInt64 int64
testNewBuffer(t, func(i int) MyInt64 { return MyInt64(-i) })
type MyFloat32 float32
testNewBuffer(t, func(i int) MyFloat32 { return MyFloat32(i) * 1.1 })
type MyFloat64 float64
testNewBuffer(t, func(i int) MyFloat64 { return MyFloat64(i) * 1.1 })
t.Run("max size", func(t *testing.T) {
bufferId, buffer, err := NewBuffer[byte](math.MaxInt32)
require.NoError(t, err, "Unable to create metal buffer: %s", err)
require.True(t, validId(bufferId))
require.Len(t, buffer, math.MaxInt32)
require.Equal(t, cap(buffer), math.MaxInt32)
})
}
// testNewBuffer is a helper to test buffer creation for a variety of types.
func testNewBuffer[T BufferType](t *testing.T, converter func(int) T) {
var a T
t.Run(fmt.Sprintf("%T one dimension", a), func(t *testing.T) {
width := rand.Intn(20) + 1
bufferId, buffer, err := NewBuffer[T](width)
require.NoError(t, err, "Unable to create metal buffer: %s", err)
require.True(t, validId(bufferId))
require.Len(t, buffer, width)
require.Equal(t, cap(buffer), width)
// Test that every item in the buffer has its zero value.
for i := range buffer {
require.True(t, reflect.ValueOf(buffer[i]).IsZero())
}
// Test that we can write to every item in the buffer.
require.NotPanics(t, func() {
for i := range buffer {
buffer[i] = converter(i)
}
})
// Test that every item retained its value.
for i := range buffer {
require.Equal(t, converter(i), buffer[i])
}
})
t.Run(fmt.Sprintf("%T two dimensions", a), func(t *testing.T) {
width := rand.Intn(20) + 1
height := rand.Intn(20) + 1
bufferId, buffer1D, err := NewBuffer[T](width * height)
require.NoError(t, err, "Unable to create metal buffer: %s", err)
require.True(t, validId(bufferId))
require.Len(t, buffer1D, width*height)
require.Equal(t, width*height, cap(buffer1D))
buffer2D := Fold(buffer1D, width)
require.Len(t, buffer2D, width)
require.Equal(t, width, cap(buffer2D))
for _, y := range buffer2D {
require.Equal(t, height, len(y))
require.Equal(t, height, cap(y))
}
// Test that every item in the buffer has its zero value.
for i := range buffer2D {
for j := range buffer2D[i] {
require.True(t, reflect.ValueOf(buffer2D[i][j]).IsZero())
}
}
// Test that we can write to every item in the buffer.
require.NotPanics(t, func() {
for i := range buffer2D {
for j := range buffer2D[i] {
buffer2D[i][j] = converter(i * j)
}
}
})
// Test that every item retained its value.
for i := range buffer2D {
for j := range buffer2D[i] {
require.Equal(t, converter(i*j), buffer2D[i][j])
}
}
})
t.Run(fmt.Sprintf("%T three dimensions", a), func(t *testing.T) {
width := rand.Intn(20) + 1
height := rand.Intn(20) + 1
depth := rand.Intn(20) + 1
bufferId, buffer1D, err := NewBuffer[T](width * height * depth)
require.NoError(t, err, "Unable to create metal buffer: %s", err)
require.True(t, validId(bufferId))
require.Equal(t, width*height*depth, len(buffer1D))
require.Equal(t, width*height*depth, cap(buffer1D))
buffer3D := Fold(Fold(buffer1D, width*height), width)
require.Len(t, buffer3D, width)
require.Equal(t, width, cap(buffer3D))
for _, y := range buffer3D {
require.Equal(t, height, len(y))
require.Equal(t, height, cap(y))
for _, z := range y {
require.Equal(t, depth, len(z))
require.Equal(t, depth, cap(z))
}
}
// Test that every item in the buffer has its zero value.
for i := range buffer3D {
for j := range buffer3D[i] {
for k := range buffer3D[i][j] {
require.True(t, reflect.ValueOf(buffer3D[i][j][k]).IsZero())
}
}
}
// Test that we can write to every item in the buffer.
require.NotPanics(t, func() {
for i := range buffer3D {
for j := range buffer3D[i] {
for k := range buffer3D[i][j] {
buffer3D[i][j][k] = converter(i * j * k)
}
}
}
})
// Test that every item retained its value.
for i := range buffer3D {
for j := range buffer3D[i] {
for k := range buffer3D[i][j] {
require.Equal(t, converter(i*j*k), buffer3D[i][j][k])
}
}
}
})
}
// Test_NewBuffer_threadSafe tests that NewBuffer can handle multiple parallel invocations and still
// return the correct Id.
func Test_NewBuffer_threadSafe(t *testing.T) {
// We're going to use a wait group to block each goroutine after it's prepared until they're all
// ready to fire.
numIter := 100
var wg sync.WaitGroup
wg.Add(numIter)
dataCh := make(chan BufferId)
// Prepare one goroutine to create a new buffer for each iteration.
for i := 0; i < numIter; i++ {
// Calculate the dimensions for this buffer.
width := rand.Intn(20) + 1
// Spin up a new goroutine. This will wait until all goroutines are ready to fire, then
// create a new metal buffer and send its Id back to the main thread.
go func() {
wg.Wait()
bufferId, _, err := NewBuffer[int32](width)
require.NoError(t, err, "Unable to create metal buffer: %s", err)
dataCh <- bufferId
}()
// Mark that this goroutine is ready.
wg.Done()
}
// Test that each buffer's Id is unique.
idMap := make(map[BufferId]struct{})
for i := 0; i < numIter; i++ {
bufferId := <-dataCh
_, ok := idMap[bufferId]
require.False(t, ok)
idMap[bufferId] = struct{}{}
addId()
}
// Test that we received every Id in the sequence.
idList := make([]BufferId, 0, len(idMap))
for bufferId := range idMap {
idList = append(idList, bufferId)
}
sort.Slice(idList, func(i, j int) bool { return idList[i] < idList[j] })
require.Len(t, idList, numIter)
for i := 0; i < numIter; i++ {
require.Equal(t, nextMetalId-numIter+i, int(idList[i]))
}
}
// Test_NewBufferWith tests that NewBufferWith creates a new metal buffer with the expected
// underlying data.
func Test_NewBufferWith(t *testing.T) {
t.Run("int32", func(t *testing.T) {
input := []int32{1, 2, 3, 4, 5}
want := []int32{1, 2, 3, 4, 5}
bufferId, buffer, err := NewBufferWith(input)
require.NoError(t, err)
require.True(t, validId(bufferId))
require.Len(t, buffer, len(want))
require.Equal(t, cap(want), cap(buffer))
require.Equal(t, want, buffer)
})
t.Run("float32", func(t *testing.T) {
input := []float32{1.1, 2.2, 3.3, 4.4, 5.5}
want := []float32{1.1, 2.2, 3.3, 4.4, 5.5}
bufferId, buffer, err := NewBufferWith(input)
require.NoError(t, err)
require.True(t, validId(bufferId))
require.Len(t, buffer, len(want))
require.Equal(t, cap(want), cap(buffer))
require.Equal(t, want, buffer)
})
}
// Test_BufferId_Close tests the Close method of the BufferId type.
func Test_BufferId_Close(t *testing.T) {
t.Run("invalid buffer id", func(t *testing.T) {
var nilPtr *BufferId
require.ErrorIs(t, nilPtr.Close(), ErrInvalidBufferId)
var zeroId BufferId
require.ErrorIs(t, zeroId.Close(), ErrInvalidBufferId)
})
t.Run("invalid cache id", func(t *testing.T) {
bufferId := BufferId(2000)
require.EqualError(t, bufferId.Close(), "Unable to free buffer: Failed to retrieve buffer: Invalid cache Id: 2000")
})
t.Run("valid buffer id", func(t *testing.T) {
bufferId, buffer, err := NewBuffer[int32](10)
require.NoError(t, err)
require.True(t, validId(bufferId))
for i := range buffer {
buffer[i] = int32(i + 1)
}
require.NoError(t, bufferId.Close())
require.False(t, bufferId.Valid())
})
}