-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_test.go
47 lines (41 loc) · 905 Bytes
/
memory_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
package flow
import (
"reflect"
"testing"
"unsafe"
)
func TestAlloc(t *testing.T) {
p := alloc(16, nil)
if len(p) != 16 {
t.Errorf("unexpected memory size: %d", len(p))
}
// shrink
q := alloc(8, p)
switch {
case len(q) != 8:
t.Errorf("unexpected memory size: %d", len(q))
case !equalAddress(p, q):
t.Errorf("unexpected memory address: %p", q)
}
// grow
q = alloc(16, q)
switch {
case len(q) != 16:
t.Errorf("unexpected memory size: %d", len(q))
case !equalAddress(p, q):
t.Errorf("unexpected memory address: %p", q)
}
// exceed
q = alloc(32, q)
switch {
case len(q) != 32:
t.Errorf("unexpected memory size: %d", len(q))
case equalAddress(p, q):
t.Errorf("unexpected memory address: %p", q)
}
}
func equalAddress(p, q []byte) bool {
ph := (*reflect.SliceHeader)(unsafe.Pointer(&p))
qh := (*reflect.SliceHeader)(unsafe.Pointer(&q))
return ph.Data == qh.Data
}