Pat Hickey
5 years ago
6 changed files with 115 additions and 42 deletions
@ -0,0 +1,34 @@ |
|||
use wiggle_runtime::GuestError; |
|||
|
|||
pub struct WasiCtx { |
|||
pub guest_errors: Vec<GuestError>, |
|||
} |
|||
|
|||
impl WasiCtx { |
|||
pub fn new() -> Self { |
|||
Self { |
|||
guest_errors: vec![], |
|||
} |
|||
} |
|||
} |
|||
|
|||
// Errno is used as a first return value in the functions above, therefore
|
|||
// it must implement GuestErrorType with type Context = WasiCtx.
|
|||
// The context type should let you do logging or debugging or whatever you need
|
|||
// with these errors. We just push them to vecs.
|
|||
#[macro_export] |
|||
macro_rules! impl_errno { |
|||
( $errno:ty ) => { |
|||
impl wiggle_runtime::GuestErrorType for $errno { |
|||
type Context = WasiCtx; |
|||
fn success() -> $errno { |
|||
<$errno>::Ok |
|||
} |
|||
fn from_error(e: GuestError, ctx: &mut WasiCtx) -> $errno { |
|||
eprintln!("GUEST ERROR: {:?}", e); |
|||
ctx.guest_errors.push(e); |
|||
types::Errno::InvalidArg |
|||
} |
|||
} |
|||
}; |
|||
} |
@ -0,0 +1,8 @@ |
|||
(typename $errno |
|||
(enum u32 |
|||
$ok |
|||
$invalid_arg |
|||
$dont_want_to |
|||
$physically_unable |
|||
$picket_line)) |
|||
|
@ -0,0 +1,61 @@ |
|||
use proptest::prelude::*; |
|||
use std::convert::TryFrom; |
|||
use wiggle_runtime::{ |
|||
GuestArray, GuestError, GuestErrorType, GuestPtr, GuestPtrMut, GuestRef, GuestRefMut, |
|||
}; |
|||
use wiggle_test::{HostMemory, MemArea}; |
|||
|
|||
mod ctx; |
|||
use ctx::WasiCtx; |
|||
|
|||
wiggle_generate::from_witx!({ |
|||
witx: ["tests/trivial.witx"], |
|||
ctx: WasiCtx, |
|||
}); |
|||
|
|||
impl_errno!(types::Errno); |
|||
|
|||
impl trivial::Trivial for WasiCtx { |
|||
fn int_float_args(&mut self, an_int: u32, an_float: f32) -> Result<(), types::Errno> { |
|||
println!("INT FLOAT ARGS: {} {}", an_int, an_float); |
|||
Ok(()) |
|||
} |
|||
} |
|||
|
|||
// There's nothing meaningful to test here - this just demonstrates the test machinery
|
|||
|
|||
#[derive(Debug)] |
|||
struct IntFloatExercise { |
|||
pub an_int: u32, |
|||
pub an_float: f32, |
|||
} |
|||
|
|||
impl IntFloatExercise { |
|||
pub fn test(&self) { |
|||
let mut ctx = WasiCtx::new(); |
|||
let mut host_memory = HostMemory::new(); |
|||
let mut guest_memory = host_memory.guest_memory(); |
|||
|
|||
let e = trivial::int_float_args( |
|||
&mut ctx, |
|||
&mut guest_memory, |
|||
self.an_int as i32, |
|||
self.an_float, |
|||
); |
|||
|
|||
assert_eq!(e, types::Errno::Ok.into(), "int_float_args error"); |
|||
} |
|||
|
|||
pub fn strat() -> BoxedStrategy<Self> { |
|||
(prop::num::u32::ANY, prop::num::f32::ANY) |
|||
.prop_map(|(an_int, an_float)| IntFloatExercise { an_int, an_float }) |
|||
.boxed() |
|||
} |
|||
} |
|||
|
|||
proptest! { |
|||
#[test] |
|||
fn int_float_exercise(e in IntFloatExercise::strat()) { |
|||
e.test() |
|||
} |
|||
} |
@ -0,0 +1,8 @@ |
|||
(use "errno.witx") |
|||
|
|||
(module $trivial |
|||
(@interface func (export "int_float_args") |
|||
(param $an_int u32) |
|||
(param $an_float f32) |
|||
(result $error $errno)) |
|||
) |
Loading…
Reference in new issue