-
Notifications
You must be signed in to change notification settings - Fork 0
/
testalloc.go
65 lines (61 loc) · 1.15 KB
/
testalloc.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
package testalloc
import (
"bytes"
"testing"
"github.com/gotranspile/runtimec/libc"
)
func RunTestAlloc(t *testing.T, a libc.Allocator) {
old := libc.SetAllocator(a)
t.Cleanup(func() {
libc.SetAllocator(old)
})
t.Run("zero init", func(t *testing.T) {
const sz = 10
s := libc.Make[byte](sz)
defer libc.FreeS(s)
if !bytes.Equal(s, make([]byte, 10)) {
t.Fatal()
}
})
t.Run("clone slice", func(t *testing.T) {
const sz = 10
s := libc.Make[byte](sz)
defer libc.FreeS(s)
for i := range s {
s[i] = byte(i)
}
s2 := libc.CloneS(s)
defer libc.FreeS(s2)
if !bytes.Equal(s, s2) {
t.Fatal()
}
if &s[0] == &s2[0] {
t.Fatal()
}
})
t.Run("remake zero init", func(t *testing.T) {
const sz = 10
s := libc.Remake[byte](nil, sz)
defer libc.FreeS(s)
if !bytes.Equal(s, make([]byte, 10)) {
t.Fatal()
}
})
t.Run("remake", func(t *testing.T) {
const sz = 10
s := libc.Make[byte](sz)
exp := make([]byte, 2*sz)
for i := range s {
s[i] = byte(i)
exp[i] = byte(i)
}
s2 := libc.Remake(s, 2*sz)
defer libc.FreeS(s2)
if !bytes.Equal(s2, exp) {
t.Fatal(s2)
}
if &s[0] == &s2[0] {
t.Fatal()
}
})
}