-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalloc_test.go
57 lines (45 loc) · 1.12 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
package goumem
import (
"github.com/exapsy/goumem/allocator"
"github.com/stretchr/testify/suite"
"testing"
"unsafe"
)
type TestAllocSuite struct {
suite.Suite
}
func (s *TestAllocSuite) SetupSuite() {
}
func (s *TestAllocSuite) TestAlloc() {
type MyStruct struct {
a int
b float64
c string
}
myArr, err := Alloc([10]MyStruct{})
if err != nil {
s.FailNow("Failed to allocate block")
}
s.Equal(uintptr(unsafe.Sizeof([10]MyStruct{})), myArr.Size())
myArr2, err := Alloc([20]MyStruct{})
if err != nil {
s.FailNow("Failed to allocate block")
}
s.Equal(uintptr(unsafe.Sizeof([20]MyStruct{})), myArr2.Size())
*(*[10]MyStruct)(unsafe.Pointer(myArr.Addr())) = [10]MyStruct{{8, 2.0, ""}}
s.Equal(8, (*(*[10]MyStruct)(unsafe.Pointer(myArr.Addr())))[0].a)
err = Free(myArr)
if err != nil {
s.FailNow("Failed to free allocated block")
}
err = Free(myArr)
s.EqualError(err, allocator.ErrAllocatedBlockAlreadyFreed.Error())
s.Equal(myArr.IsFreed(), true)
err = Free(myArr2)
if err != nil {
s.FailNow("Failed to free allocated block")
}
}
func TestAlloc(t *testing.T) {
suite.Run(t, new(TestAllocSuite))
}