forked from CannibalVox/cgoparam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc_test.go
115 lines (92 loc) · 3.32 KB
/
alloc_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
package cgoparam
import "testing"
import "github.com/stretchr/testify/require"
func TestBasicFunc(t *testing.T) {
alloc := GetAlloc()
require.NotNil(t, alloc)
require.NotNil(t, alloc.Malloc(1))
require.NotNil(t, alloc.Malloc(100))
require.NotNil(t, alloc.Malloc(1000))
require.Len(t, alloc.basePages, 1)
require.Equal(t, 1112, alloc.basePages[0].nextOffset)
require.Equal(t, 4096, alloc.basePages[0].size)
require.Equal(t, 2984, alloc.basePages[0].remainingSize)
ReturnAlloc(alloc)
require.Len(t, alloc.basePages, 1)
require.Equal(t, 0, alloc.basePages[0].nextOffset)
require.Equal(t, 4096, alloc.basePages[0].size)
require.Equal(t, 4096, alloc.basePages[0].remainingSize)
}
func TestMultiPage(t *testing.T) {
alloc := GetAlloc()
require.NotNil(t, alloc)
require.NotNil(t, alloc.Malloc(900))
require.NotNil(t, alloc.Malloc(900))
require.NotNil(t, alloc.Malloc(900))
require.NotNil(t, alloc.Malloc(900))
require.NotNil(t, alloc.Malloc(900))
require.Len(t, alloc.basePages, 2)
require.Len(t, alloc.standaloneAllocs, 0)
require.Equal(t, 3616, alloc.basePages[0].nextOffset)
require.Equal(t, 4096, alloc.basePages[0].size)
require.Equal(t, 480, alloc.basePages[0].remainingSize)
require.Equal(t, 904, alloc.basePages[1].nextOffset)
require.Equal(t, 4096, alloc.basePages[1].size)
require.Equal(t, 3192, alloc.basePages[1].remainingSize)
ReturnAlloc(alloc)
require.Len(t, alloc.basePages, 1)
require.Equal(t, 0, alloc.basePages[0].nextOffset)
require.Equal(t, 4096, alloc.basePages[0].remainingSize)
}
func TestSlightlyTooBigStandalone(t *testing.T) {
alloc := GetAlloc()
require.NotNil(t, alloc)
require.NotNil(t, alloc.Malloc(900))
require.NotNil(t, alloc.Malloc(900))
require.NotNil(t, alloc.Malloc(900))
require.NotNil(t, alloc.Malloc(900))
require.NotNil(t, alloc.Malloc(1200))
require.Len(t, alloc.basePages, 1)
require.Len(t, alloc.standaloneAllocs, 1)
require.Equal(t, 3616, alloc.basePages[0].nextOffset)
require.Equal(t, 4096, alloc.basePages[0].size)
require.Equal(t, 480, alloc.basePages[0].remainingSize)
ReturnAlloc(alloc)
require.Len(t, alloc.basePages, 1)
require.Len(t, alloc.standaloneAllocs, 0)
require.Equal(t, 0, alloc.basePages[0].nextOffset)
require.Equal(t, 4096, alloc.basePages[0].remainingSize)
}
func TestCouldBeStandaloneButFit(t *testing.T) {
alloc := GetAlloc()
require.NotNil(t, alloc)
require.NotNil(t, alloc.Malloc(900))
require.NotNil(t, alloc.Malloc(900))
require.NotNil(t, alloc.Malloc(900))
require.NotNil(t, alloc.Malloc(1200))
require.Len(t, alloc.basePages, 1)
require.Len(t, alloc.standaloneAllocs, 0)
require.Equal(t, 3912, alloc.basePages[0].nextOffset)
require.Equal(t, 4096, alloc.basePages[0].size)
require.Equal(t, 184, alloc.basePages[0].remainingSize)
ReturnAlloc(alloc)
require.Len(t, alloc.basePages, 1)
require.Len(t, alloc.standaloneAllocs, 0)
require.Equal(t, 0, alloc.basePages[0].nextOffset)
require.Equal(t, 4096, alloc.basePages[0].remainingSize)
}
func TestCString(t *testing.T) {
alloc := GetAlloc()
defer ReturnAlloc(alloc)
cStr := alloc.CString("WOW STRING")
goStr := callGoString(cStr)
require.Equal(t, "WOW STRING", goStr)
}
func TestCBytes(t *testing.T) {
alloc := GetAlloc()
defer ReturnAlloc(alloc)
b := []byte("WOW STRING")
cBytes := alloc.CBytes(b)
goBytes := callGoBytes(cBytes, len(b))
require.Equal(t, []byte("WOW STRING"), goBytes)
}