Files
wasmtime/tests/ints.rs
Pat Hickey bb6995ceaf make wiggle-generate ordinary lib, and wiggle the proc-macro lib
this allows us to reuse the code in wiggle-generate elsewhere, because
a proc-macro=true lib can only export a #[proc_macro] and not ordinary
functions.

In lucet, I will depend on wiggle-generate to define a proc macro that
glues wiggle to the specifics of the runtime.
2020-02-28 11:43:43 -08:00

82 lines
2.1 KiB
Rust

use proptest::prelude::*;
use std::convert::TryFrom;
use wiggle_runtime::GuestError;
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
wiggle::from_witx!({
witx: ["tests/ints.witx"],
ctx: WasiCtx,
});
impl_errno!(types::Errno);
impl ints::Ints for WasiCtx {
fn cookie_cutter(&mut self, init_cookie: types::Cookie) -> Result<types::Bool, types::Errno> {
let res = if init_cookie == types::Cookie::START {
types::Bool::True
} else {
types::Bool::False
};
Ok(res)
}
}
fn cookie_strat() -> impl Strategy<Value = types::Cookie> {
(0..std::u64::MAX)
.prop_map(|x| types::Cookie::try_from(x).expect("within range of cookie"))
.boxed()
}
#[derive(Debug)]
struct CookieCutterExercise {
cookie: types::Cookie,
return_ptr_loc: MemArea,
}
impl CookieCutterExercise {
pub fn strat() -> BoxedStrategy<Self> {
(cookie_strat(), HostMemory::mem_area_strat(4))
.prop_map(|(cookie, return_ptr_loc)| Self {
cookie,
return_ptr_loc,
})
.boxed()
}
pub fn test(&self) {
let mut ctx = WasiCtx::new();
let mut host_memory = HostMemory::new();
let mut guest_memory = host_memory.guest_memory();
let res = ints::cookie_cutter(
&mut ctx,
&mut guest_memory,
self.cookie.into(),
self.return_ptr_loc.ptr as i32,
);
assert_eq!(res, types::Errno::Ok.into(), "cookie cutter errno");
let is_cookie_start = *guest_memory
.ptr::<types::Bool>(self.return_ptr_loc.ptr)
.expect("ptr to returned Bool")
.as_ref()
.expect("deref to Bool value");
assert_eq!(
if is_cookie_start == types::Bool::True {
true
} else {
false
},
self.cookie == types::Cookie::START,
"returned Bool should test if input was Cookie::START",
);
}
}
proptest! {
#[test]
fn cookie_cutter(e in CookieCutterExercise::strat()) {
e.test()
}
}