Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wiggle/tests/atoms.rs
1692 views
1
use proptest::prelude::*;
2
use wiggle::{GuestMemory, GuestPtr};
3
use wiggle_test::{HostMemory, MemArea, WasiCtx, impl_errno};
4
5
wiggle::from_witx!({
6
witx: ["tests/atoms.witx"],
7
});
8
9
impl_errno!(types::Errno);
10
11
impl<'a> atoms::Atoms for WasiCtx<'a> {
12
fn int_float_args(
13
&mut self,
14
_memory: &mut GuestMemory<'_>,
15
an_int: u32,
16
an_float: f32,
17
) -> Result<(), types::Errno> {
18
println!("INT FLOAT ARGS: {an_int} {an_float}");
19
Ok(())
20
}
21
fn double_int_return_float(
22
&mut self,
23
_memory: &mut GuestMemory<'_>,
24
an_int: u32,
25
) -> Result<types::AliasToFloat, types::Errno> {
26
Ok((an_int as f32) * 2.0)
27
}
28
}
29
30
// There's nothing meaningful to test here - this just demonstrates the test machinery
31
32
#[derive(Debug)]
33
struct IntFloatExercise {
34
pub an_int: u32,
35
pub an_float: f32,
36
}
37
38
impl IntFloatExercise {
39
pub fn test(&self) {
40
let mut ctx = WasiCtx::new();
41
let mut host_memory = HostMemory::new();
42
43
let e = atoms::int_float_args(
44
&mut ctx,
45
&mut host_memory.guest_memory(),
46
self.an_int as i32,
47
self.an_float,
48
)
49
.unwrap();
50
51
assert_eq!(e, types::Errno::Ok as i32, "int_float_args error");
52
}
53
54
pub fn strat() -> BoxedStrategy<Self> {
55
(prop::num::u32::ANY, prop::num::f32::ANY)
56
.prop_map(|(an_int, an_float)| IntFloatExercise { an_int, an_float })
57
.boxed()
58
}
59
}
60
61
proptest! {
62
#[test]
63
fn int_float_exercise(e in IntFloatExercise::strat()) {
64
e.test()
65
}
66
}
67
#[derive(Debug)]
68
struct DoubleIntExercise {
69
pub input: u32,
70
pub return_loc: MemArea,
71
}
72
73
impl DoubleIntExercise {
74
pub fn test(&self) {
75
let mut ctx = WasiCtx::new();
76
let mut host_memory = HostMemory::new();
77
let mut memory = host_memory.guest_memory();
78
79
let e = atoms::double_int_return_float(
80
&mut ctx,
81
&mut memory,
82
self.input as i32,
83
self.return_loc.ptr as i32,
84
)
85
.unwrap();
86
87
let return_val = memory
88
.read(GuestPtr::<types::AliasToFloat>::new(self.return_loc.ptr))
89
.expect("failed to read return");
90
assert_eq!(e, types::Errno::Ok as i32, "errno");
91
assert_eq!(return_val, (self.input as f32) * 2.0, "return val");
92
}
93
94
pub fn strat() -> BoxedStrategy<Self> {
95
(prop::num::u32::ANY, HostMemory::mem_area_strat(4))
96
.prop_map(|(input, return_loc)| DoubleIntExercise { input, return_loc })
97
.boxed()
98
}
99
}
100
101
proptest! {
102
#[test]
103
fn double_int_return_float(e in DoubleIntExercise::strat()) {
104
e.test()
105
}
106
}
107
108