-
Notifications
You must be signed in to change notification settings - Fork 7
/
buffer_test.go
97 lines (84 loc) · 1.75 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
package tiledb
import (
"fmt"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func ExampleNewBuffer() {
// Create context with default config
context, err := NewContext(nil)
if err != nil {
// Handle error
return
}
// Create Buffer
buffer, err := NewBuffer(context)
if err != nil {
// Handle error
return
}
// Get data slice
bytes, err := buffer.dataCopy()
if err != nil {
// Handle error
return
}
fmt.Println(bytes)
// Output: []
}
// TestNewBuffer tests creating a new buffer
func TestNewBuffer(t *testing.T) {
context, err := NewContext(nil)
require.NoError(t, err)
buffer, err := NewBuffer(context)
require.NoError(t, err)
assert.NotNil(t, buffer)
bytes, err := buffer.dataCopy()
require.NoError(t, err)
assert.Nil(t, bytes)
datatype, err := buffer.Type()
require.NoError(t, err)
assert.Equal(t, datatype, TILEDB_UINT8)
}
func TestBufferSafety(t *testing.T) {
context, err := NewContext(nil)
require.NoError(t, err)
buffer, err := NewBuffer(context)
require.NoError(t, err)
require.NoError(t, buffer.SetBuffer([]byte{8, 6, 7, 5, 3, 0, 9}))
churn := func() {
churners := make([][]byte, 1024*128)
for i := range churners {
churners[i] = make([]byte, 7)
for j := range churners[i] {
churners[i][j] = ^byte(j)
}
}
for i := range churners {
churners[i] = nil
}
}
verify := func() {
got, err := buffer.Serialize(TILEDB_CAPNP)
require.NoError(t, err)
assert.Equal(t, []byte{8, 6, 7, 5, 3, 0, 9}, got)
}
t.Log("pre churn")
churn()
t.Log("post churn")
verify()
t.Log("pre gc")
runtime.GC()
t.Log("post gc")
verify()
t.Log("pre churn 2")
churn()
t.Log("post churn 2")
verify()
t.Log("pre gc 2")
runtime.GC()
t.Log("post gc 2")
verify()
}