forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryImportBindingTests.cs
More file actions
186 lines (149 loc) · 6.25 KB
/
MemoryImportBindingTests.cs
File metadata and controls
186 lines (149 loc) · 6.25 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using FluentAssertions;
using Xunit;
namespace Wasmtime.Tests
{
public class MemoryImportBindingFixture : ModuleFixture
{
protected override string ModuleFileName => "MemoryImportBinding.wat";
}
public class MemoryImportBindingTests : IClassFixture<MemoryImportBindingFixture>
{
class MissingImportsHost : IHost
{
public Instance Instance { get; set; }
}
class MemoryIsStaticHost : IHost
{
public Instance Instance { get; set; }
[Import("mem")]
public static Memory x = new Memory(minimum: 1);
}
class MemoryIsNotReadOnlyHost : IHost
{
public Instance Instance { get; set; }
[Import("mem")]
public Memory x = new Memory(minimum: 1);
}
class NotAMemoryHost : IHost
{
public Instance Instance { get; set; }
[Import("mem")]
public readonly int x = 0;
}
class InvalidMinimumHost : IHost
{
public Instance Instance { get; set; }
[Import("mem")]
public readonly Memory Mem = new Memory(minimum: 2);
}
class InvalidMaximumHost : IHost
{
public Instance Instance { get; set; }
[Import("mem")]
public readonly Memory Mem = new Memory(maximum: 2);
}
class ValidHost : IHost
{
public Instance Instance { get; set; }
[Import("mem")]
public readonly Memory Mem = new Memory(minimum: 1);
}
public MemoryImportBindingTests(MemoryImportBindingFixture fixture)
{
Fixture = fixture;
}
private MemoryImportBindingFixture Fixture { get; set; }
[Fact]
public void ItFailsToInstantiateWithMissingImport()
{
Action action = () => { using var instance = Fixture.Module.Instantiate(new MissingImportsHost()); };
action
.Should()
.Throw<WasmtimeException>()
.WithMessage("Failed to bind memory import 'mem': the host does not contain a memory field with a matching 'Import' attribute.");
}
[Fact]
public void ItFailsToInstantiateWithStaticField()
{
Action action = () => { using var instance = Fixture.Module.Instantiate(new MemoryIsStaticHost()); };
action
.Should()
.Throw<WasmtimeException>()
.WithMessage("Unable to bind 'MemoryIsStaticHost.x' to WebAssembly import 'mem': field cannot be static.");
}
[Fact]
public void ItFailsToInstantiateWithNonReadOnlyField()
{
Action action = () => { using var instance = Fixture.Module.Instantiate(new MemoryIsNotReadOnlyHost()); };
action
.Should()
.Throw<WasmtimeException>()
.WithMessage("Unable to bind 'MemoryIsNotReadOnlyHost.x' to WebAssembly import 'mem': field must be readonly.");
}
[Fact]
public void ItFailsToInstantiateWithInvalidType()
{
Action action = () => { using var instance = Fixture.Module.Instantiate(new NotAMemoryHost()); };
action
.Should()
.Throw<WasmtimeException>()
.WithMessage("Unable to bind 'NotAMemoryHost.x' to WebAssembly import 'mem': field is expected to be of type 'Memory'.");
}
[Fact]
public void ItFailsToInstantiateWhenMemoryHasInvalidMinimum()
{
Action action = () => { using var instance = Fixture.Module.Instantiate(new InvalidMinimumHost()); };
action
.Should()
.Throw<WasmtimeException>()
.WithMessage("Unable to bind 'InvalidMinimumHost.Mem' to WebAssembly import 'mem': Memory does not have the expected minimum of 1 page(s).");
}
[Fact]
public void ItFailsToInstantiateWhenMemoryHasInvalidMaximum()
{
Action action = () => { using var instance = Fixture.Module.Instantiate(new InvalidMaximumHost()); };
action
.Should()
.Throw<WasmtimeException>()
.WithMessage("Unable to bind 'InvalidMaximumHost.Mem' to WebAssembly import 'mem': Memory does not have the expected maximum of 4294967295 page(s).");
}
[Fact]
public void ItBindsTheGlobalsCorrectly()
{
var host = new ValidHost();
using dynamic instance = Fixture.Module.Instantiate(host);
host.Mem.ReadString(0, 11).Should().Be("Hello World");
int written = host.Mem.WriteString(0, "WebAssembly Rocks!");
host.Mem.ReadString(0, written).Should().Be("WebAssembly Rocks!");
host.Mem.ReadByte(20).Should().Be(1);
host.Mem.WriteByte(20, 11);
host.Mem.ReadByte(20).Should().Be(11);
((byte)instance.ReadByte()).Should().Be(11);
host.Mem.ReadInt16(21).Should().Be(2);
host.Mem.WriteInt16(21, 12);
host.Mem.ReadInt16(21).Should().Be(12);
((short)instance.ReadInt16()).Should().Be(12);
host.Mem.ReadInt32(23).Should().Be(3);
host.Mem.WriteInt32(23, 13);
host.Mem.ReadInt32(23).Should().Be(13);
((int)instance.ReadInt32()).Should().Be(13);
host.Mem.ReadInt64(27).Should().Be(4);
host.Mem.WriteInt64(27, 14);
host.Mem.ReadInt64(27).Should().Be(14);
((long)instance.ReadInt64()).Should().Be(14);
host.Mem.ReadSingle(35).Should().Be(5);
host.Mem.WriteSingle(35, 15);
host.Mem.ReadSingle(35).Should().Be(15);
((float)instance.ReadFloat32()).Should().Be(15);
host.Mem.ReadDouble(39).Should().Be(6);
host.Mem.WriteDouble(39, 16);
host.Mem.ReadDouble(39).Should().Be(16);
((double)instance.ReadFloat64()).Should().Be(16);
host.Mem.ReadIntPtr(48).Should().Be((IntPtr)7);
host.Mem.WriteIntPtr(48, (IntPtr)17);
host.Mem.ReadIntPtr(48).Should().Be((IntPtr)17);
((IntPtr)instance.ReadIntPtr()).Should().Be((IntPtr)17);
}
}
}