Skip to content

Commit

Permalink
Merge pull request bytecodealliance#16 from kubkon/pch/factor_tests
Browse files Browse the repository at this point in the history
Start factoring tests
  • Loading branch information
pchickey committed Feb 21, 2020
2 parents f48474b + f77000a commit 3be9d48
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 148 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ wiggle-generate = { path = "crates/generate" }
wiggle-runtime = { path = "crates/runtime" }

[dev-dependencies]
wiggle-test = { path = "crates/test" }
proptest = "0.9"

[workspace]
members = [
"crates/generate",
"crates/runtime"
"crates/runtime",
"crates/test",
]
exclude = ["crates/WASI"]
2 changes: 2 additions & 0 deletions crates/test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
Cargo.lock
9 changes: 9 additions & 0 deletions crates/test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "wiggle-test"
version = "0.1.0"
authors = ["Pat Hickey <phickey@fastly.com>", "Jakub Konka <kubkon@jakubkonka.com>"]
edition = "2018"

[dependencies]
wiggle-runtime = { path = "../runtime" }
proptest = "0.9"
127 changes: 127 additions & 0 deletions crates/test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use proptest::prelude::*;
use wiggle_runtime::GuestMemory;

#[repr(align(4096))]
pub struct HostMemory {
buffer: [u8; 4096],
}
impl HostMemory {
pub fn new() -> Self {
HostMemory { buffer: [0; 4096] }
}

pub fn guest_memory<'a>(&'a mut self) -> GuestMemory<'a> {
GuestMemory::new(self.buffer.as_mut_ptr(), self.buffer.len() as u32)
}

pub fn mem_area_strat(align: u32) -> BoxedStrategy<MemArea> {
prop::num::u32::ANY
.prop_filter_map("needs to fit in memory", move |p| {
let p_aligned = p - (p % align); // Align according to argument
let ptr = p_aligned % 4096; // Put inside memory
if ptr + align < 4096 {
Some(MemArea { ptr, len: align })
} else {
None
}
})
.boxed()
}
}

#[derive(Debug)]
pub struct MemArea {
pub ptr: u32,
pub len: u32,
}

impl MemArea {
// This code is a whole lot like the Region::overlaps func thats at the core of the code under
// test.
// So, I implemented this one with std::ops::Range so it is less likely I wrote the same bug in two
// places.
pub fn overlapping(&self, b: &Self) -> bool {
// a_range is all elems in A
let a_range = std::ops::Range {
start: self.ptr,
end: self.ptr + self.len, // std::ops::Range is open from the right
};
// b_range is all elems in B
let b_range = std::ops::Range {
start: b.ptr,
end: b.ptr + b.len,
};
// No element in B is contained in A:
for b_elem in b_range.clone() {
if a_range.contains(&b_elem) {
return true;
}
}
// No element in A is contained in B:
for a_elem in a_range {
if b_range.contains(&a_elem) {
return true;
}
}
return false;
}
pub fn non_overlapping_set(areas: &[&Self]) -> bool {
// A is all areas
for (i, a) in areas.iter().enumerate() {
// (A, B) is every pair of areas
for b in areas[i + 1..].iter() {
if a.overlapping(b) {
return false;
}
}
}
return true;
}
}

#[cfg(test)]
mod test {
use super::*;
#[test]
fn hostmemory_is_aligned() {
let mut h = HostMemory::new();
assert_eq!(h.buffer.as_mut_ptr() as usize % 4096, 0);
let mut h = Box::new(HostMemory::new());
assert_eq!(h.buffer.as_mut_ptr() as usize % 4096, 0);
}
}

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
}
}
};
}
8 changes: 8 additions & 0 deletions tests/errno.witx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(typename $errno
(enum u32
$ok
$invalid_arg
$dont_want_to
$physically_unable
$picket_line))

Loading

0 comments on commit 3be9d48

Please sign in to comment.