-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
349 lines (297 loc) · 10.7 KB
/
lib.rs
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#[cfg(test)]
mod test_helpers;
#[cfg(test)]
mod tests {
use crate::test_helpers::TestRunner;
use tarnik::wasm;
use tarnik_ast::WatModule;
#[test]
fn test_ref_equality() -> anyhow::Result<()> {
let module: WatModule = wasm! {
memory!("memory", 1);
struct Foo {}
fn run() {
let foo_1: Foo = Foo {};
let foo_2: Foo = foo_1;
let foo_3: Foo = Foo {};
let foo_4: Foo = foo_3;
assert(foo_1 == foo_2, "the references foo_1 and foo_2 should match");
assert(foo_3 == foo_4, "the references foo_3 and foo_4 should match");
assert(foo_1 != foo_3, "the references foo_1 and foo_3 should not match");
assert(foo_1 != foo_4, "the references foo_1 and foo_4 should not match");
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
#[test]
fn test_data() -> anyhow::Result<()> {
let module: WatModule = wasm! {
memory!("memory", 1);
fn run() {
let offset1: i32 = data!("foo");
let offset2: i32 = data!("bar");
let offset3: i32 = data!("foo");
assert(offset1 == offset3, "data entries should not add a new entry if one exists");
assert(memory[offset2] == 'b', "offset should point at the data");
assert(memory[offset2 + 1] == 'a', "offset should point at the data");
assert(memory[offset2 + 2] == 'r', "offset should point at the data");
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
#[test]
fn test_ref_func() -> anyhow::Result<()> {
let module: WatModule = wasm! {
type FooFunc = fn(i32) -> i32;
fn run() {
let foo_ref: FooFunc = foo;
let y: i32 = foo_ref(33);
assert(y == 44, "it should be possible to call a func ref");
}
fn foo(x: i32) -> i32 {
return x + 11;
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
#[test]
fn test_global_array() -> anyhow::Result<()> {
let module: WatModule = wasm! {
type Arr = [Nullable<anyref>];
static foo: Arr = [null; 10];
fn run() {
let first_element: anyref = foo[0];
let is_null: i32 = ref_test!(first_element, null);
assert(is_null == 1, "array should be initialized and values should be null");
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
#[test]
fn test_global_null() -> anyhow::Result<()> {
let module: WatModule = wasm! {
struct Foo {}
static foo: Nullable<Foo> = null;
fn run() {
let is_null: i32 = ref_test!(foo, null);
assert(is_null == 1, "global should be set to null");
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
#[test]
fn test_global() -> anyhow::Result<()> {
let module: WatModule = wasm! {
static mut foo: i64 = 10;
fn run() {
foo += 34;
assert(foo == 44, "global foo should");
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
#[test]
fn test_try_catch() -> anyhow::Result<()> {
let module: WatModule = wasm! {
type ExceptionType = fn(i32, i64);
tag!(Exception, ExceptionType);
fn run() {
let a: i32 = 0;
let outer_x: i32;
let outer_y: i64;
try {
a += 10;
throw!(Exception, 1, 2 as i64);
}
catch(Exception, x: i32, y: i64) {
a += 100;
outer_x = x;
outer_y = y;
}
catch_all {
a += 1000;
}
assert(outer_x == 1, "first argument to the exception should be equal to 1");
assert(outer_y == 2, "second argument to the exception should be equal to 2");
assert(a == 1110, "a should equal to 1110");
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
#[test]
fn test_nullable() -> anyhow::Result<()> {
let module: WatModule = wasm! {
type I64Array = [i64];
fn run() {
let x: Nullable<I64Array> = null;
// TODO: refactor when negate is implemented
if ref_test!(x, null) {
} else {
assert(0, "Couldn't successfully test for null");
}
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
#[test]
fn test_len() -> anyhow::Result<()> {
let module: WatModule = wasm! {
type I64Array = [i64];
fn run() {
let x: I64Array = [44, 10, 11, 12];
assert(len!(x) == 4, "The $x array should have 4 elements");
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
#[test]
fn test_casting() -> anyhow::Result<()> {
let module: WatModule = wasm! {
type I64Array = [i64];
fn run() {
let x: I64Array = [44];
test_any(x);
}
fn test_any(numbers: anyref) {
if ref_test!(numbers, I64Array) {
let numbers_i64: I64Array = numbers as I64Array;
assert(numbers_i64[0] == 44, "The first element of the numbers array should be 44");
} else {
assert(0, "$numbers should be an I64Array");
}
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
#[test]
fn test_arithmetic_operations() -> anyhow::Result<()> {
let module: WatModule = wasm! {
fn run() {
let x: i32 = 0;
assert(x + 1 == 1, "0 + 1 should equal 1");
assert(x - 1 == -1, "0 - 1 should equal -1");
x = 4;
assert(x * 4 == 16, "4 * 4 should equal 16");
assert(x / 2 == 2, "4 / 1 should equal 2");
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
#[test]
fn test_array_operations() -> anyhow::Result<()> {
let module: WatModule = wasm! {
type Array = [mut i32];
fn run() {
let arr: Array;
arr = [1, 2, 3];
arr[1] = 42;
assert(arr[1] == 42, "Array modification failed");
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
#[test]
fn test_string_operations() -> anyhow::Result<()> {
let module: WatModule = wasm! {
type String = [mut i8];
fn run() {
let str: String;
str = "hello";
str[1] = 'a';
assert(str[1] == 'a', "String modification failed");
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
#[test]
fn test_for_loop() -> anyhow::Result<()> {
let module: WatModule = wasm! {
type Array = [mut i32];
fn run() {
let arr: Array = [1, 2, 3];
let sum: i32 = 0;
for x in arr {
sum = sum + x;
}
assert(sum == 6, "For loop sum incorrect");
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
#[test]
fn test_if_statement() -> anyhow::Result<()> {
let module: WatModule = wasm! {
fn run() {
let x: i32 = 42;
if x == 42 {
assert(1, "If statement worked");
} else {
x = 10;
}
let y: bool = x == 10;
assert(y, "Else branch should be executed");
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
#[test]
fn test_number_addition() -> anyhow::Result<()> {
let module: WatModule = wasm! {
fn run() {
let x: i32 = 40;
let y: i32 = 2;
let z: i32 = x + y;
assert(z == 42, "Addition failed");
}
};
let runner = TestRunner::new()?;
let (code, stdout, stderr) = runner.run_wasm_test(module)?;
assert_eq!(code, 0, "stdout: {}\nstderr: {}", stdout, stderr);
Ok(())
}
}