forked from CosmWasm/wasmvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_test.go
78 lines (69 loc) · 2.29 KB
/
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
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
package api
import (
"testing"
"unsafe"
"github.com/stretchr/testify/require"
)
func TestMakeView(t *testing.T) {
data := []byte{0xaa, 0xbb, 0x64}
dataView := makeView(data)
require.Equal(t, cbool(false), dataView.is_nil)
require.Equal(t, cusize(3), dataView.len)
empty := []byte{}
emptyView := makeView(empty)
require.Equal(t, cbool(false), emptyView.is_nil)
require.Equal(t, cusize(0), emptyView.len)
nilView := makeView(nil)
require.Equal(t, cbool(true), nilView.is_nil)
}
func TestCreateAndDestroyUnmanagedVector(t *testing.T) {
// non-empty
{
original := []byte{0xaa, 0xbb, 0x64}
unmanaged := newUnmanagedVector(original)
require.Equal(t, cbool(false), unmanaged.is_none)
require.Equal(t, 3, int(unmanaged.len))
require.GreaterOrEqual(t, 3, int(unmanaged.cap)) // Rust implementation decides this
copy := copyAndDestroyUnmanagedVector(unmanaged)
require.Equal(t, original, copy)
}
// empty
{
original := []byte{}
unmanaged := newUnmanagedVector(original)
require.Equal(t, cbool(false), unmanaged.is_none)
require.Equal(t, 0, int(unmanaged.len))
require.GreaterOrEqual(t, 0, int(unmanaged.cap)) // Rust implementation decides this
copy := copyAndDestroyUnmanagedVector(unmanaged)
require.Equal(t, original, copy)
}
// none
{
var original []byte
unmanaged := newUnmanagedVector(original)
require.Equal(t, cbool(true), unmanaged.is_none)
// We must not make assumtions on the other fields in this case
copy := copyAndDestroyUnmanagedVector(unmanaged)
require.Nil(t, copy)
}
}
// Like the test above but without `newUnmanagedVector` calls.
// Since only Rust can actually create them, we only test edge cases here.
//
//go:nocheckptr
func TestCopyDestroyUnmanagedVector(t *testing.T) {
{
// ptr, cap and len broken. Do not access those values when is_none is true
invalid_ptr := unsafe.Pointer(uintptr(42))
uv := constructUnmanagedVector(cbool(true), cu8_ptr(invalid_ptr), cusize(0xBB), cusize(0xAA))
copy := copyAndDestroyUnmanagedVector(uv)
require.Nil(t, copy)
}
{
// Capacity is 0, so no allocation happened. Do not access the pointer.
invalid_ptr := unsafe.Pointer(uintptr(42))
uv := constructUnmanagedVector(cbool(false), cu8_ptr(invalid_ptr), cusize(0), cusize(0))
copy := copyAndDestroyUnmanagedVector(uv)
require.Equal(t, []byte{}, copy)
}
}